read_packed_refs(): make parsing of the header line more robust
[git] / refs / packed-backend.c
1 #include "../cache.h"
2 #include "../config.h"
3 #include "../refs.h"
4 #include "refs-internal.h"
5 #include "ref-cache.h"
6 #include "packed-backend.h"
7 #include "../iterator.h"
8 #include "../lockfile.h"
9
10 struct packed_ref_store;
11
12 struct packed_ref_cache {
13         /*
14          * A back-pointer to the packed_ref_store with which this
15          * cache is associated:
16          */
17         struct packed_ref_store *refs;
18
19         struct ref_cache *cache;
20
21         /*
22          * Count of references to the data structure in this instance,
23          * including the pointer from files_ref_store::packed if any.
24          * The data will not be freed as long as the reference count
25          * is nonzero.
26          */
27         unsigned int referrers;
28
29         /* The metadata from when this packed-refs cache was read */
30         struct stat_validity validity;
31 };
32
33 /*
34  * Increment the reference count of *packed_refs.
35  */
36 static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
37 {
38         packed_refs->referrers++;
39 }
40
41 /*
42  * Decrease the reference count of *packed_refs.  If it goes to zero,
43  * free *packed_refs and return true; otherwise return false.
44  */
45 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
46 {
47         if (!--packed_refs->referrers) {
48                 free_ref_cache(packed_refs->cache);
49                 stat_validity_clear(&packed_refs->validity);
50                 free(packed_refs);
51                 return 1;
52         } else {
53                 return 0;
54         }
55 }
56
57 /*
58  * A container for `packed-refs`-related data. It is not (yet) a
59  * `ref_store`.
60  */
61 struct packed_ref_store {
62         struct ref_store base;
63
64         unsigned int store_flags;
65
66         /* The path of the "packed-refs" file: */
67         char *path;
68
69         /*
70          * A cache of the values read from the `packed-refs` file, if
71          * it might still be current; otherwise, NULL.
72          */
73         struct packed_ref_cache *cache;
74
75         /*
76          * Lock used for the "packed-refs" file. Note that this (and
77          * thus the enclosing `packed_ref_store`) must not be freed.
78          */
79         struct lock_file lock;
80
81         /*
82          * Temporary file used when rewriting new contents to the
83          * "packed-refs" file. Note that this (and thus the enclosing
84          * `packed_ref_store`) must not be freed.
85          */
86         struct tempfile tempfile;
87 };
88
89 struct ref_store *packed_ref_store_create(const char *path,
90                                           unsigned int store_flags)
91 {
92         struct packed_ref_store *refs = xcalloc(1, sizeof(*refs));
93         struct ref_store *ref_store = (struct ref_store *)refs;
94
95         base_ref_store_init(ref_store, &refs_be_packed);
96         refs->store_flags = store_flags;
97
98         refs->path = xstrdup(path);
99         return ref_store;
100 }
101
102 /*
103  * Downcast `ref_store` to `packed_ref_store`. Die if `ref_store` is
104  * not a `packed_ref_store`. Also die if `packed_ref_store` doesn't
105  * support at least the flags specified in `required_flags`. `caller`
106  * is used in any necessary error messages.
107  */
108 static struct packed_ref_store *packed_downcast(struct ref_store *ref_store,
109                                                 unsigned int required_flags,
110                                                 const char *caller)
111 {
112         struct packed_ref_store *refs;
113
114         if (ref_store->be != &refs_be_packed)
115                 die("BUG: ref_store is type \"%s\" not \"packed\" in %s",
116                     ref_store->be->name, caller);
117
118         refs = (struct packed_ref_store *)ref_store;
119
120         if ((refs->store_flags & required_flags) != required_flags)
121                 die("BUG: unallowed operation (%s), requires %x, has %x\n",
122                     caller, required_flags, refs->store_flags);
123
124         return refs;
125 }
126
127 static void clear_packed_ref_cache(struct packed_ref_store *refs)
128 {
129         if (refs->cache) {
130                 struct packed_ref_cache *cache = refs->cache;
131
132                 refs->cache = NULL;
133                 release_packed_ref_cache(cache);
134         }
135 }
136
137 /* The length of a peeled reference line in packed-refs, including EOL: */
138 #define PEELED_LINE_LENGTH 42
139
140 /*
141  * Parse one line from a packed-refs file.  Write the SHA1 to sha1.
142  * Return a pointer to the refname within the line (null-terminated),
143  * or NULL if there was a problem.
144  */
145 static const char *parse_ref_line(struct strbuf *line, struct object_id *oid)
146 {
147         const char *ref;
148
149         if (parse_oid_hex(line->buf, oid, &ref) < 0)
150                 return NULL;
151         if (!isspace(*ref++))
152                 return NULL;
153
154         if (isspace(*ref))
155                 return NULL;
156
157         if (line->buf[line->len - 1] != '\n')
158                 return NULL;
159         line->buf[--line->len] = 0;
160
161         return ref;
162 }
163
164 static NORETURN void die_unterminated_line(const char *path,
165                                            const char *p, size_t len)
166 {
167         if (len < 80)
168                 die("unterminated line in %s: %.*s", path, (int)len, p);
169         else
170                 die("unterminated line in %s: %.75s...", path, p);
171 }
172
173 static NORETURN void die_invalid_line(const char *path,
174                                       const char *p, size_t len)
175 {
176         const char *eol = memchr(p, '\n', len);
177
178         if (!eol)
179                 die_unterminated_line(path, p, len);
180         else if (eol - p < 80)
181                 die("unexpected line in %s: %.*s", path, (int)(eol - p), p);
182         else
183                 die("unexpected line in %s: %.75s...", path, p);
184
185 }
186
187 /*
188  * Read from the `packed-refs` file into a newly-allocated
189  * `packed_ref_cache` and return it. The return value will already
190  * have its reference count incremented.
191  *
192  * A comment line of the form "# pack-refs with: " may contain zero or
193  * more traits. We interpret the traits as follows:
194  *
195  *   No traits:
196  *
197  *      Probably no references are peeled. But if the file contains a
198  *      peeled value for a reference, we will use it.
199  *
200  *   peeled:
201  *
202  *      References under "refs/tags/", if they *can* be peeled, *are*
203  *      peeled in this file. References outside of "refs/tags/" are
204  *      probably not peeled even if they could have been, but if we find
205  *      a peeled value for such a reference we will use it.
206  *
207  *   fully-peeled:
208  *
209  *      All references in the file that can be peeled are peeled.
210  *      Inversely (and this is more important), any references in the
211  *      file for which no peeled value is recorded is not peelable. This
212  *      trait should typically be written alongside "peeled" for
213  *      compatibility with older clients, but we do not require it
214  *      (i.e., "peeled" is a no-op if "fully-peeled" is set).
215  */
216 static struct packed_ref_cache *read_packed_refs(struct packed_ref_store *refs)
217 {
218         struct packed_ref_cache *packed_refs = xcalloc(1, sizeof(*packed_refs));
219         int fd;
220         struct stat st;
221         size_t size;
222         char *buf;
223         const char *pos, *eol, *eof;
224         struct ref_entry *last = NULL;
225         struct strbuf line = STRBUF_INIT;
226         enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
227         struct ref_dir *dir;
228
229         packed_refs->refs = refs;
230         acquire_packed_ref_cache(packed_refs);
231         packed_refs->cache = create_ref_cache(NULL, NULL);
232         packed_refs->cache->root->flag &= ~REF_INCOMPLETE;
233
234         fd = open(refs->path, O_RDONLY);
235         if (fd < 0) {
236                 if (errno == ENOENT) {
237                         /*
238                          * This is OK; it just means that no
239                          * "packed-refs" file has been written yet,
240                          * which is equivalent to it being empty.
241                          */
242                         return packed_refs;
243                 } else {
244                         die_errno("couldn't read %s", refs->path);
245                 }
246         }
247
248         stat_validity_update(&packed_refs->validity, fd);
249
250         if (fstat(fd, &st) < 0)
251                 die_errno("couldn't stat %s", refs->path);
252
253         size = xsize_t(st.st_size);
254         buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
255         pos = buf;
256         eof = buf + size;
257
258         /* If the file has a header line, process it: */
259         if (pos < eof && *pos == '#') {
260                 char *p;
261                 struct string_list traits = STRING_LIST_INIT_NODUP;
262
263                 eol = memchr(pos, '\n', eof - pos);
264                 if (!eol)
265                         die_unterminated_line(refs->path, pos, eof - pos);
266
267                 strbuf_add(&line, pos, eol - pos);
268
269                 if (!skip_prefix(line.buf, "# pack-refs with:", (const char **)&p))
270                         die_invalid_line(refs->path, pos, eof - pos);
271
272                 string_list_split_in_place(&traits, p, ' ', -1);
273
274                 if (unsorted_string_list_has_string(&traits, "fully-peeled"))
275                         peeled = PEELED_FULLY;
276                 else if (unsorted_string_list_has_string(&traits, "peeled"))
277                         peeled = PEELED_TAGS;
278                 /* perhaps other traits later as well */
279
280                 /* The "+ 1" is for the LF character. */
281                 pos = eol + 1;
282
283                 string_list_clear(&traits, 0);
284                 strbuf_reset(&line);
285         }
286
287         dir = get_ref_dir(packed_refs->cache->root);
288         while (pos < eof) {
289                 struct object_id oid;
290                 const char *refname;
291
292                 eol = memchr(pos, '\n', eof - pos);
293                 if (!eol)
294                         die_unterminated_line(refs->path, pos, eof - pos);
295
296                 strbuf_add(&line, pos, eol + 1 - pos);
297
298                 refname = parse_ref_line(&line, &oid);
299                 if (refname) {
300                         int flag = REF_ISPACKED;
301
302                         if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
303                                 if (!refname_is_safe(refname))
304                                         die("packed refname is dangerous: %s", refname);
305                                 oidclr(&oid);
306                                 flag |= REF_BAD_NAME | REF_ISBROKEN;
307                         }
308                         last = create_ref_entry(refname, &oid, flag);
309                         if (peeled == PEELED_FULLY ||
310                             (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
311                                 last->flag |= REF_KNOWS_PEELED;
312                         add_ref_entry(dir, last);
313                 } else if (last &&
314                     line.buf[0] == '^' &&
315                     line.len == PEELED_LINE_LENGTH &&
316                     line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
317                     !get_oid_hex(line.buf + 1, &oid)) {
318                         oidcpy(&last->u.value.peeled, &oid);
319                         /*
320                          * Regardless of what the file header said,
321                          * we definitely know the value of *this*
322                          * reference:
323                          */
324                         last->flag |= REF_KNOWS_PEELED;
325                 } else {
326                         die_invalid_line(refs->path, line.buf, line.len);
327                 }
328
329                 /* The "+ 1" is for the LF character. */
330                 pos = eol + 1;
331                 strbuf_reset(&line);
332         }
333
334         if (munmap(buf, size))
335                 die_errno("error ummapping packed-refs file");
336         close(fd);
337
338         strbuf_release(&line);
339         return packed_refs;
340 }
341
342 /*
343  * Check that the packed refs cache (if any) still reflects the
344  * contents of the file. If not, clear the cache.
345  */
346 static void validate_packed_ref_cache(struct packed_ref_store *refs)
347 {
348         if (refs->cache &&
349             !stat_validity_check(&refs->cache->validity, refs->path))
350                 clear_packed_ref_cache(refs);
351 }
352
353 /*
354  * Get the packed_ref_cache for the specified packed_ref_store,
355  * creating and populating it if it hasn't been read before or if the
356  * file has been changed (according to its `validity` field) since it
357  * was last read. On the other hand, if we hold the lock, then assume
358  * that the file hasn't been changed out from under us, so skip the
359  * extra `stat()` call in `stat_validity_check()`.
360  */
361 static struct packed_ref_cache *get_packed_ref_cache(struct packed_ref_store *refs)
362 {
363         if (!is_lock_file_locked(&refs->lock))
364                 validate_packed_ref_cache(refs);
365
366         if (!refs->cache)
367                 refs->cache = read_packed_refs(refs);
368
369         return refs->cache;
370 }
371
372 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
373 {
374         return get_ref_dir(packed_ref_cache->cache->root);
375 }
376
377 static struct ref_dir *get_packed_refs(struct packed_ref_store *refs)
378 {
379         return get_packed_ref_dir(get_packed_ref_cache(refs));
380 }
381
382 /*
383  * Return the ref_entry for the given refname from the packed
384  * references.  If it does not exist, return NULL.
385  */
386 static struct ref_entry *get_packed_ref(struct packed_ref_store *refs,
387                                         const char *refname)
388 {
389         return find_ref_entry(get_packed_refs(refs), refname);
390 }
391
392 static int packed_read_raw_ref(struct ref_store *ref_store,
393                                const char *refname, unsigned char *sha1,
394                                struct strbuf *referent, unsigned int *type)
395 {
396         struct packed_ref_store *refs =
397                 packed_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
398
399         struct ref_entry *entry;
400
401         *type = 0;
402
403         entry = get_packed_ref(refs, refname);
404         if (!entry) {
405                 errno = ENOENT;
406                 return -1;
407         }
408
409         hashcpy(sha1, entry->u.value.oid.hash);
410         *type = REF_ISPACKED;
411         return 0;
412 }
413
414 static int packed_peel_ref(struct ref_store *ref_store,
415                            const char *refname, unsigned char *sha1)
416 {
417         struct packed_ref_store *refs =
418                 packed_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
419                                 "peel_ref");
420         struct ref_entry *r = get_packed_ref(refs, refname);
421
422         if (!r || peel_entry(r, 0))
423                 return -1;
424
425         hashcpy(sha1, r->u.value.peeled.hash);
426         return 0;
427 }
428
429 struct packed_ref_iterator {
430         struct ref_iterator base;
431
432         struct packed_ref_cache *cache;
433         struct ref_iterator *iter0;
434         unsigned int flags;
435 };
436
437 static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
438 {
439         struct packed_ref_iterator *iter =
440                 (struct packed_ref_iterator *)ref_iterator;
441         int ok;
442
443         while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
444                 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
445                     ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
446                         continue;
447
448                 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
449                     !ref_resolves_to_object(iter->iter0->refname,
450                                             iter->iter0->oid,
451                                             iter->iter0->flags))
452                         continue;
453
454                 iter->base.refname = iter->iter0->refname;
455                 iter->base.oid = iter->iter0->oid;
456                 iter->base.flags = iter->iter0->flags;
457                 return ITER_OK;
458         }
459
460         iter->iter0 = NULL;
461         if (ref_iterator_abort(ref_iterator) != ITER_DONE)
462                 ok = ITER_ERROR;
463
464         return ok;
465 }
466
467 static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator,
468                                    struct object_id *peeled)
469 {
470         struct packed_ref_iterator *iter =
471                 (struct packed_ref_iterator *)ref_iterator;
472
473         return ref_iterator_peel(iter->iter0, peeled);
474 }
475
476 static int packed_ref_iterator_abort(struct ref_iterator *ref_iterator)
477 {
478         struct packed_ref_iterator *iter =
479                 (struct packed_ref_iterator *)ref_iterator;
480         int ok = ITER_DONE;
481
482         if (iter->iter0)
483                 ok = ref_iterator_abort(iter->iter0);
484
485         release_packed_ref_cache(iter->cache);
486         base_ref_iterator_free(ref_iterator);
487         return ok;
488 }
489
490 static struct ref_iterator_vtable packed_ref_iterator_vtable = {
491         packed_ref_iterator_advance,
492         packed_ref_iterator_peel,
493         packed_ref_iterator_abort
494 };
495
496 static struct ref_iterator *packed_ref_iterator_begin(
497                 struct ref_store *ref_store,
498                 const char *prefix, unsigned int flags)
499 {
500         struct packed_ref_store *refs;
501         struct packed_ref_iterator *iter;
502         struct ref_iterator *ref_iterator;
503         unsigned int required_flags = REF_STORE_READ;
504
505         if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN))
506                 required_flags |= REF_STORE_ODB;
507         refs = packed_downcast(ref_store, required_flags, "ref_iterator_begin");
508
509         iter = xcalloc(1, sizeof(*iter));
510         ref_iterator = &iter->base;
511         base_ref_iterator_init(ref_iterator, &packed_ref_iterator_vtable, 1);
512
513         /*
514          * Note that get_packed_ref_cache() internally checks whether
515          * the packed-ref cache is up to date with what is on disk,
516          * and re-reads it if not.
517          */
518
519         iter->cache = get_packed_ref_cache(refs);
520         acquire_packed_ref_cache(iter->cache);
521         iter->iter0 = cache_ref_iterator_begin(iter->cache->cache, prefix, 0);
522
523         iter->flags = flags;
524
525         return ref_iterator;
526 }
527
528 /*
529  * Write an entry to the packed-refs file for the specified refname.
530  * If peeled is non-NULL, write it as the entry's peeled value. On
531  * error, return a nonzero value and leave errno set at the value left
532  * by the failing call to `fprintf()`.
533  */
534 static int write_packed_entry(FILE *fh, const char *refname,
535                               const unsigned char *sha1,
536                               const unsigned char *peeled)
537 {
538         if (fprintf(fh, "%s %s\n", sha1_to_hex(sha1), refname) < 0 ||
539             (peeled && fprintf(fh, "^%s\n", sha1_to_hex(peeled)) < 0))
540                 return -1;
541
542         return 0;
543 }
544
545 int packed_refs_lock(struct ref_store *ref_store, int flags, struct strbuf *err)
546 {
547         struct packed_ref_store *refs =
548                 packed_downcast(ref_store, REF_STORE_WRITE | REF_STORE_MAIN,
549                                 "packed_refs_lock");
550         static int timeout_configured = 0;
551         static int timeout_value = 1000;
552
553         if (!timeout_configured) {
554                 git_config_get_int("core.packedrefstimeout", &timeout_value);
555                 timeout_configured = 1;
556         }
557
558         /*
559          * Note that we close the lockfile immediately because we
560          * don't write new content to it, but rather to a separate
561          * tempfile.
562          */
563         if (hold_lock_file_for_update_timeout(
564                             &refs->lock,
565                             refs->path,
566                             flags, timeout_value) < 0) {
567                 unable_to_lock_message(refs->path, errno, err);
568                 return -1;
569         }
570
571         if (close_lock_file(&refs->lock)) {
572                 strbuf_addf(err, "unable to close %s: %s", refs->path, strerror(errno));
573                 return -1;
574         }
575
576         /*
577          * Now that we hold the `packed-refs` lock, make sure that our
578          * cache matches the current version of the file. Normally
579          * `get_packed_ref_cache()` does that for us, but that
580          * function assumes that when the file is locked, any existing
581          * cache is still valid. We've just locked the file, but it
582          * might have changed the moment *before* we locked it.
583          */
584         validate_packed_ref_cache(refs);
585
586         /*
587          * Now make sure that the packed-refs file as it exists in the
588          * locked state is loaded into the cache:
589          */
590         get_packed_ref_cache(refs);
591         return 0;
592 }
593
594 void packed_refs_unlock(struct ref_store *ref_store)
595 {
596         struct packed_ref_store *refs = packed_downcast(
597                         ref_store,
598                         REF_STORE_READ | REF_STORE_WRITE,
599                         "packed_refs_unlock");
600
601         if (!is_lock_file_locked(&refs->lock))
602                 die("BUG: packed_refs_unlock() called when not locked");
603         rollback_lock_file(&refs->lock);
604 }
605
606 int packed_refs_is_locked(struct ref_store *ref_store)
607 {
608         struct packed_ref_store *refs = packed_downcast(
609                         ref_store,
610                         REF_STORE_READ | REF_STORE_WRITE,
611                         "packed_refs_is_locked");
612
613         return is_lock_file_locked(&refs->lock);
614 }
615
616 /*
617  * The packed-refs header line that we write out.  Perhaps other
618  * traits will be added later.
619  *
620  * Note that earlier versions of Git used to parse these traits by
621  * looking for " trait " in the line. For this reason, the space after
622  * the colon and the trailing space are required.
623  */
624 static const char PACKED_REFS_HEADER[] =
625         "# pack-refs with: peeled fully-peeled \n";
626
627 static int packed_init_db(struct ref_store *ref_store, struct strbuf *err)
628 {
629         /* Nothing to do. */
630         return 0;
631 }
632
633 /*
634  * Write the packed-refs from the cache to the packed-refs tempfile,
635  * incorporating any changes from `updates`. `updates` must be a
636  * sorted string list whose keys are the refnames and whose util
637  * values are `struct ref_update *`. On error, rollback the tempfile,
638  * write an error message to `err`, and return a nonzero value.
639  *
640  * The packfile must be locked before calling this function and will
641  * remain locked when it is done.
642  */
643 static int write_with_updates(struct packed_ref_store *refs,
644                               struct string_list *updates,
645                               struct strbuf *err)
646 {
647         struct ref_iterator *iter = NULL;
648         size_t i;
649         int ok;
650         FILE *out;
651         struct strbuf sb = STRBUF_INIT;
652         char *packed_refs_path;
653
654         if (!is_lock_file_locked(&refs->lock))
655                 die("BUG: write_with_updates() called while unlocked");
656
657         /*
658          * If packed-refs is a symlink, we want to overwrite the
659          * symlinked-to file, not the symlink itself. Also, put the
660          * staging file next to it:
661          */
662         packed_refs_path = get_locked_file_path(&refs->lock);
663         strbuf_addf(&sb, "%s.new", packed_refs_path);
664         free(packed_refs_path);
665         if (create_tempfile(&refs->tempfile, sb.buf) < 0) {
666                 strbuf_addf(err, "unable to create file %s: %s",
667                             sb.buf, strerror(errno));
668                 strbuf_release(&sb);
669                 return -1;
670         }
671         strbuf_release(&sb);
672
673         out = fdopen_tempfile(&refs->tempfile, "w");
674         if (!out) {
675                 strbuf_addf(err, "unable to fdopen packed-refs tempfile: %s",
676                             strerror(errno));
677                 goto error;
678         }
679
680         if (fprintf(out, "%s", PACKED_REFS_HEADER) < 0)
681                 goto write_error;
682
683         /*
684          * We iterate in parallel through the current list of refs and
685          * the list of updates, processing an entry from at least one
686          * of the lists each time through the loop. When the current
687          * list of refs is exhausted, set iter to NULL. When the list
688          * of updates is exhausted, leave i set to updates->nr.
689          */
690         iter = packed_ref_iterator_begin(&refs->base, "",
691                                          DO_FOR_EACH_INCLUDE_BROKEN);
692         if ((ok = ref_iterator_advance(iter)) != ITER_OK)
693                 iter = NULL;
694
695         i = 0;
696
697         while (iter || i < updates->nr) {
698                 struct ref_update *update = NULL;
699                 int cmp;
700
701                 if (i >= updates->nr) {
702                         cmp = -1;
703                 } else {
704                         update = updates->items[i].util;
705
706                         if (!iter)
707                                 cmp = +1;
708                         else
709                                 cmp = strcmp(iter->refname, update->refname);
710                 }
711
712                 if (!cmp) {
713                         /*
714                          * There is both an old value and an update
715                          * for this reference. Check the old value if
716                          * necessary:
717                          */
718                         if ((update->flags & REF_HAVE_OLD)) {
719                                 if (is_null_oid(&update->old_oid)) {
720                                         strbuf_addf(err, "cannot update ref '%s': "
721                                                     "reference already exists",
722                                                     update->refname);
723                                         goto error;
724                                 } else if (oidcmp(&update->old_oid, iter->oid)) {
725                                         strbuf_addf(err, "cannot update ref '%s': "
726                                                     "is at %s but expected %s",
727                                                     update->refname,
728                                                     oid_to_hex(iter->oid),
729                                                     oid_to_hex(&update->old_oid));
730                                         goto error;
731                                 }
732                         }
733
734                         /* Now figure out what to use for the new value: */
735                         if ((update->flags & REF_HAVE_NEW)) {
736                                 /*
737                                  * The update takes precedence. Skip
738                                  * the iterator over the unneeded
739                                  * value.
740                                  */
741                                 if ((ok = ref_iterator_advance(iter)) != ITER_OK)
742                                         iter = NULL;
743                                 cmp = +1;
744                         } else {
745                                 /*
746                                  * The update doesn't actually want to
747                                  * change anything. We're done with it.
748                                  */
749                                 i++;
750                                 cmp = -1;
751                         }
752                 } else if (cmp > 0) {
753                         /*
754                          * There is no old value but there is an
755                          * update for this reference. Make sure that
756                          * the update didn't expect an existing value:
757                          */
758                         if ((update->flags & REF_HAVE_OLD) &&
759                             !is_null_oid(&update->old_oid)) {
760                                 strbuf_addf(err, "cannot update ref '%s': "
761                                             "reference is missing but expected %s",
762                                             update->refname,
763                                             oid_to_hex(&update->old_oid));
764                                 goto error;
765                         }
766                 }
767
768                 if (cmp < 0) {
769                         /* Pass the old reference through. */
770
771                         struct object_id peeled;
772                         int peel_error = ref_iterator_peel(iter, &peeled);
773
774                         if (write_packed_entry(out, iter->refname,
775                                                iter->oid->hash,
776                                                peel_error ? NULL : peeled.hash))
777                                 goto write_error;
778
779                         if ((ok = ref_iterator_advance(iter)) != ITER_OK)
780                                 iter = NULL;
781                 } else if (is_null_oid(&update->new_oid)) {
782                         /*
783                          * The update wants to delete the reference,
784                          * and the reference either didn't exist or we
785                          * have already skipped it. So we're done with
786                          * the update (and don't have to write
787                          * anything).
788                          */
789                         i++;
790                 } else {
791                         struct object_id peeled;
792                         int peel_error = peel_object(update->new_oid.hash,
793                                                      peeled.hash);
794
795                         if (write_packed_entry(out, update->refname,
796                                                update->new_oid.hash,
797                                                peel_error ? NULL : peeled.hash))
798                                 goto write_error;
799
800                         i++;
801                 }
802         }
803
804         if (ok != ITER_DONE) {
805                 strbuf_addf(err, "unable to write packed-refs file: "
806                             "error iterating over old contents");
807                 goto error;
808         }
809
810         if (close_tempfile(&refs->tempfile)) {
811                 strbuf_addf(err, "error closing file %s: %s",
812                             get_tempfile_path(&refs->tempfile),
813                             strerror(errno));
814                 strbuf_release(&sb);
815                 return -1;
816         }
817
818         return 0;
819
820 write_error:
821         strbuf_addf(err, "error writing to %s: %s",
822                     get_tempfile_path(&refs->tempfile), strerror(errno));
823
824 error:
825         if (iter)
826                 ref_iterator_abort(iter);
827
828         delete_tempfile(&refs->tempfile);
829         return -1;
830 }
831
832 struct packed_transaction_backend_data {
833         /* True iff the transaction owns the packed-refs lock. */
834         int own_lock;
835
836         struct string_list updates;
837 };
838
839 static void packed_transaction_cleanup(struct packed_ref_store *refs,
840                                        struct ref_transaction *transaction)
841 {
842         struct packed_transaction_backend_data *data = transaction->backend_data;
843
844         if (data) {
845                 string_list_clear(&data->updates, 0);
846
847                 if (is_tempfile_active(&refs->tempfile))
848                         delete_tempfile(&refs->tempfile);
849
850                 if (data->own_lock && is_lock_file_locked(&refs->lock)) {
851                         packed_refs_unlock(&refs->base);
852                         data->own_lock = 0;
853                 }
854
855                 free(data);
856                 transaction->backend_data = NULL;
857         }
858
859         transaction->state = REF_TRANSACTION_CLOSED;
860 }
861
862 static int packed_transaction_prepare(struct ref_store *ref_store,
863                                       struct ref_transaction *transaction,
864                                       struct strbuf *err)
865 {
866         struct packed_ref_store *refs = packed_downcast(
867                         ref_store,
868                         REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
869                         "ref_transaction_prepare");
870         struct packed_transaction_backend_data *data;
871         size_t i;
872         int ret = TRANSACTION_GENERIC_ERROR;
873
874         /*
875          * Note that we *don't* skip transactions with zero updates,
876          * because such a transaction might be executed for the side
877          * effect of ensuring that all of the references are peeled.
878          * If the caller wants to optimize away empty transactions, it
879          * should do so itself.
880          */
881
882         data = xcalloc(1, sizeof(*data));
883         string_list_init(&data->updates, 0);
884
885         transaction->backend_data = data;
886
887         /*
888          * Stick the updates in a string list by refname so that we
889          * can sort them:
890          */
891         for (i = 0; i < transaction->nr; i++) {
892                 struct ref_update *update = transaction->updates[i];
893                 struct string_list_item *item =
894                         string_list_append(&data->updates, update->refname);
895
896                 /* Store a pointer to update in item->util: */
897                 item->util = update;
898         }
899         string_list_sort(&data->updates);
900
901         if (ref_update_reject_duplicates(&data->updates, err))
902                 goto failure;
903
904         if (!is_lock_file_locked(&refs->lock)) {
905                 if (packed_refs_lock(ref_store, 0, err))
906                         goto failure;
907                 data->own_lock = 1;
908         }
909
910         if (write_with_updates(refs, &data->updates, err))
911                 goto failure;
912
913         transaction->state = REF_TRANSACTION_PREPARED;
914         return 0;
915
916 failure:
917         packed_transaction_cleanup(refs, transaction);
918         return ret;
919 }
920
921 static int packed_transaction_abort(struct ref_store *ref_store,
922                                     struct ref_transaction *transaction,
923                                     struct strbuf *err)
924 {
925         struct packed_ref_store *refs = packed_downcast(
926                         ref_store,
927                         REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
928                         "ref_transaction_abort");
929
930         packed_transaction_cleanup(refs, transaction);
931         return 0;
932 }
933
934 static int packed_transaction_finish(struct ref_store *ref_store,
935                                      struct ref_transaction *transaction,
936                                      struct strbuf *err)
937 {
938         struct packed_ref_store *refs = packed_downcast(
939                         ref_store,
940                         REF_STORE_READ | REF_STORE_WRITE | REF_STORE_ODB,
941                         "ref_transaction_finish");
942         int ret = TRANSACTION_GENERIC_ERROR;
943         char *packed_refs_path;
944
945         packed_refs_path = get_locked_file_path(&refs->lock);
946         if (rename_tempfile(&refs->tempfile, packed_refs_path)) {
947                 strbuf_addf(err, "error replacing %s: %s",
948                             refs->path, strerror(errno));
949                 goto cleanup;
950         }
951
952         clear_packed_ref_cache(refs);
953         ret = 0;
954
955 cleanup:
956         free(packed_refs_path);
957         packed_transaction_cleanup(refs, transaction);
958         return ret;
959 }
960
961 static int packed_initial_transaction_commit(struct ref_store *ref_store,
962                                             struct ref_transaction *transaction,
963                                             struct strbuf *err)
964 {
965         return ref_transaction_commit(transaction, err);
966 }
967
968 static int packed_delete_refs(struct ref_store *ref_store, const char *msg,
969                              struct string_list *refnames, unsigned int flags)
970 {
971         struct packed_ref_store *refs =
972                 packed_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
973         struct strbuf err = STRBUF_INIT;
974         struct ref_transaction *transaction;
975         struct string_list_item *item;
976         int ret;
977
978         (void)refs; /* We need the check above, but don't use the variable */
979
980         if (!refnames->nr)
981                 return 0;
982
983         /*
984          * Since we don't check the references' old_oids, the
985          * individual updates can't fail, so we can pack all of the
986          * updates into a single transaction.
987          */
988
989         transaction = ref_store_transaction_begin(ref_store, &err);
990         if (!transaction)
991                 return -1;
992
993         for_each_string_list_item(item, refnames) {
994                 if (ref_transaction_delete(transaction, item->string, NULL,
995                                            flags, msg, &err)) {
996                         warning(_("could not delete reference %s: %s"),
997                                 item->string, err.buf);
998                         strbuf_reset(&err);
999                 }
1000         }
1001
1002         ret = ref_transaction_commit(transaction, &err);
1003
1004         if (ret) {
1005                 if (refnames->nr == 1)
1006                         error(_("could not delete reference %s: %s"),
1007                               refnames->items[0].string, err.buf);
1008                 else
1009                         error(_("could not delete references: %s"), err.buf);
1010         }
1011
1012         ref_transaction_free(transaction);
1013         strbuf_release(&err);
1014         return ret;
1015 }
1016
1017 static int packed_pack_refs(struct ref_store *ref_store, unsigned int flags)
1018 {
1019         /*
1020          * Packed refs are already packed. It might be that loose refs
1021          * are packed *into* a packed refs store, but that is done by
1022          * updating the packed references via a transaction.
1023          */
1024         return 0;
1025 }
1026
1027 static int packed_create_symref(struct ref_store *ref_store,
1028                                const char *refname, const char *target,
1029                                const char *logmsg)
1030 {
1031         die("BUG: packed reference store does not support symrefs");
1032 }
1033
1034 static int packed_rename_ref(struct ref_store *ref_store,
1035                             const char *oldrefname, const char *newrefname,
1036                             const char *logmsg)
1037 {
1038         die("BUG: packed reference store does not support renaming references");
1039 }
1040
1041 static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_store)
1042 {
1043         return empty_ref_iterator_begin();
1044 }
1045
1046 static int packed_for_each_reflog_ent(struct ref_store *ref_store,
1047                                       const char *refname,
1048                                       each_reflog_ent_fn fn, void *cb_data)
1049 {
1050         return 0;
1051 }
1052
1053 static int packed_for_each_reflog_ent_reverse(struct ref_store *ref_store,
1054                                               const char *refname,
1055                                               each_reflog_ent_fn fn,
1056                                               void *cb_data)
1057 {
1058         return 0;
1059 }
1060
1061 static int packed_reflog_exists(struct ref_store *ref_store,
1062                                const char *refname)
1063 {
1064         return 0;
1065 }
1066
1067 static int packed_create_reflog(struct ref_store *ref_store,
1068                                const char *refname, int force_create,
1069                                struct strbuf *err)
1070 {
1071         die("BUG: packed reference store does not support reflogs");
1072 }
1073
1074 static int packed_delete_reflog(struct ref_store *ref_store,
1075                                const char *refname)
1076 {
1077         return 0;
1078 }
1079
1080 static int packed_reflog_expire(struct ref_store *ref_store,
1081                                 const char *refname, const unsigned char *sha1,
1082                                 unsigned int flags,
1083                                 reflog_expiry_prepare_fn prepare_fn,
1084                                 reflog_expiry_should_prune_fn should_prune_fn,
1085                                 reflog_expiry_cleanup_fn cleanup_fn,
1086                                 void *policy_cb_data)
1087 {
1088         return 0;
1089 }
1090
1091 struct ref_storage_be refs_be_packed = {
1092         NULL,
1093         "packed",
1094         packed_ref_store_create,
1095         packed_init_db,
1096         packed_transaction_prepare,
1097         packed_transaction_finish,
1098         packed_transaction_abort,
1099         packed_initial_transaction_commit,
1100
1101         packed_pack_refs,
1102         packed_peel_ref,
1103         packed_create_symref,
1104         packed_delete_refs,
1105         packed_rename_ref,
1106
1107         packed_ref_iterator_begin,
1108         packed_read_raw_ref,
1109
1110         packed_reflog_iterator_begin,
1111         packed_for_each_reflog_ent,
1112         packed_for_each_reflog_ent_reverse,
1113         packed_reflog_exists,
1114         packed_create_reflog,
1115         packed_delete_reflog,
1116         packed_reflog_expire
1117 };