cache_ref_iterator_begin(): make function smarter
[git] / refs / files-backend.c
1 #include "../cache.h"
2 #include "../refs.h"
3 #include "refs-internal.h"
4 #include "ref-cache.h"
5 #include "../iterator.h"
6 #include "../dir-iterator.h"
7 #include "../lockfile.h"
8 #include "../object.h"
9 #include "../dir.h"
10
11 struct ref_lock {
12         char *ref_name;
13         struct lock_file *lk;
14         struct object_id old_oid;
15 };
16
17 /*
18  * Return true if refname, which has the specified oid and flags, can
19  * be resolved to an object in the database. If the referred-to object
20  * does not exist, emit a warning and return false.
21  */
22 static int ref_resolves_to_object(const char *refname,
23                                   const struct object_id *oid,
24                                   unsigned int flags)
25 {
26         if (flags & REF_ISBROKEN)
27                 return 0;
28         if (!has_sha1_file(oid->hash)) {
29                 error("%s does not point to a valid object!", refname);
30                 return 0;
31         }
32         return 1;
33 }
34
35 /*
36  * Return true if the reference described by entry can be resolved to
37  * an object in the database; otherwise, emit a warning and return
38  * false.
39  */
40 static int entry_resolves_to_object(struct ref_entry *entry)
41 {
42         return ref_resolves_to_object(entry->name,
43                                       &entry->u.value.oid, entry->flag);
44 }
45
46 struct packed_ref_cache {
47         struct ref_cache *cache;
48
49         /*
50          * Count of references to the data structure in this instance,
51          * including the pointer from files_ref_store::packed if any.
52          * The data will not be freed as long as the reference count
53          * is nonzero.
54          */
55         unsigned int referrers;
56
57         /*
58          * Iff the packed-refs file associated with this instance is
59          * currently locked for writing, this points at the associated
60          * lock (which is owned by somebody else).  The referrer count
61          * is also incremented when the file is locked and decremented
62          * when it is unlocked.
63          */
64         struct lock_file *lock;
65
66         /* The metadata from when this packed-refs cache was read */
67         struct stat_validity validity;
68 };
69
70 /*
71  * Future: need to be in "struct repository"
72  * when doing a full libification.
73  */
74 struct files_ref_store {
75         struct ref_store base;
76         unsigned int store_flags;
77
78         char *gitdir;
79         char *gitcommondir;
80         char *packed_refs_path;
81
82         struct ref_cache *loose;
83         struct packed_ref_cache *packed;
84 };
85
86 /* Lock used for the main packed-refs file: */
87 static struct lock_file packlock;
88
89 /*
90  * Increment the reference count of *packed_refs.
91  */
92 static void acquire_packed_ref_cache(struct packed_ref_cache *packed_refs)
93 {
94         packed_refs->referrers++;
95 }
96
97 /*
98  * Decrease the reference count of *packed_refs.  If it goes to zero,
99  * free *packed_refs and return true; otherwise return false.
100  */
101 static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
102 {
103         if (!--packed_refs->referrers) {
104                 free_ref_cache(packed_refs->cache);
105                 stat_validity_clear(&packed_refs->validity);
106                 free(packed_refs);
107                 return 1;
108         } else {
109                 return 0;
110         }
111 }
112
113 static void clear_packed_ref_cache(struct files_ref_store *refs)
114 {
115         if (refs->packed) {
116                 struct packed_ref_cache *packed_refs = refs->packed;
117
118                 if (packed_refs->lock)
119                         die("internal error: packed-ref cache cleared while locked");
120                 refs->packed = NULL;
121                 release_packed_ref_cache(packed_refs);
122         }
123 }
124
125 static void clear_loose_ref_cache(struct files_ref_store *refs)
126 {
127         if (refs->loose) {
128                 free_ref_cache(refs->loose);
129                 refs->loose = NULL;
130         }
131 }
132
133 /*
134  * Create a new submodule ref cache and add it to the internal
135  * set of caches.
136  */
137 static struct ref_store *files_ref_store_create(const char *gitdir,
138                                                 unsigned int flags)
139 {
140         struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
141         struct ref_store *ref_store = (struct ref_store *)refs;
142         struct strbuf sb = STRBUF_INIT;
143
144         base_ref_store_init(ref_store, &refs_be_files);
145         refs->store_flags = flags;
146
147         refs->gitdir = xstrdup(gitdir);
148         get_common_dir_noenv(&sb, gitdir);
149         refs->gitcommondir = strbuf_detach(&sb, NULL);
150         strbuf_addf(&sb, "%s/packed-refs", refs->gitcommondir);
151         refs->packed_refs_path = strbuf_detach(&sb, NULL);
152
153         return ref_store;
154 }
155
156 /*
157  * Die if refs is not the main ref store. caller is used in any
158  * necessary error messages.
159  */
160 static void files_assert_main_repository(struct files_ref_store *refs,
161                                          const char *caller)
162 {
163         if (refs->store_flags & REF_STORE_MAIN)
164                 return;
165
166         die("BUG: operation %s only allowed for main ref store", caller);
167 }
168
169 /*
170  * Downcast ref_store to files_ref_store. Die if ref_store is not a
171  * files_ref_store. required_flags is compared with ref_store's
172  * store_flags to ensure the ref_store has all required capabilities.
173  * "caller" is used in any necessary error messages.
174  */
175 static struct files_ref_store *files_downcast(struct ref_store *ref_store,
176                                               unsigned int required_flags,
177                                               const char *caller)
178 {
179         struct files_ref_store *refs;
180
181         if (ref_store->be != &refs_be_files)
182                 die("BUG: ref_store is type \"%s\" not \"files\" in %s",
183                     ref_store->be->name, caller);
184
185         refs = (struct files_ref_store *)ref_store;
186
187         if ((refs->store_flags & required_flags) != required_flags)
188                 die("BUG: operation %s requires abilities 0x%x, but only have 0x%x",
189                     caller, required_flags, refs->store_flags);
190
191         return refs;
192 }
193
194 /* The length of a peeled reference line in packed-refs, including EOL: */
195 #define PEELED_LINE_LENGTH 42
196
197 /*
198  * The packed-refs header line that we write out.  Perhaps other
199  * traits will be added later.  The trailing space is required.
200  */
201 static const char PACKED_REFS_HEADER[] =
202         "# pack-refs with: peeled fully-peeled \n";
203
204 /*
205  * Parse one line from a packed-refs file.  Write the SHA1 to sha1.
206  * Return a pointer to the refname within the line (null-terminated),
207  * or NULL if there was a problem.
208  */
209 static const char *parse_ref_line(struct strbuf *line, unsigned char *sha1)
210 {
211         const char *ref;
212
213         /*
214          * 42: the answer to everything.
215          *
216          * In this case, it happens to be the answer to
217          *  40 (length of sha1 hex representation)
218          *  +1 (space in between hex and name)
219          *  +1 (newline at the end of the line)
220          */
221         if (line->len <= 42)
222                 return NULL;
223
224         if (get_sha1_hex(line->buf, sha1) < 0)
225                 return NULL;
226         if (!isspace(line->buf[40]))
227                 return NULL;
228
229         ref = line->buf + 41;
230         if (isspace(*ref))
231                 return NULL;
232
233         if (line->buf[line->len - 1] != '\n')
234                 return NULL;
235         line->buf[--line->len] = 0;
236
237         return ref;
238 }
239
240 /*
241  * Read f, which is a packed-refs file, into dir.
242  *
243  * A comment line of the form "# pack-refs with: " may contain zero or
244  * more traits. We interpret the traits as follows:
245  *
246  *   No traits:
247  *
248  *      Probably no references are peeled. But if the file contains a
249  *      peeled value for a reference, we will use it.
250  *
251  *   peeled:
252  *
253  *      References under "refs/tags/", if they *can* be peeled, *are*
254  *      peeled in this file. References outside of "refs/tags/" are
255  *      probably not peeled even if they could have been, but if we find
256  *      a peeled value for such a reference we will use it.
257  *
258  *   fully-peeled:
259  *
260  *      All references in the file that can be peeled are peeled.
261  *      Inversely (and this is more important), any references in the
262  *      file for which no peeled value is recorded is not peelable. This
263  *      trait should typically be written alongside "peeled" for
264  *      compatibility with older clients, but we do not require it
265  *      (i.e., "peeled" is a no-op if "fully-peeled" is set).
266  */
267 static void read_packed_refs(FILE *f, struct ref_dir *dir)
268 {
269         struct ref_entry *last = NULL;
270         struct strbuf line = STRBUF_INIT;
271         enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
272
273         while (strbuf_getwholeline(&line, f, '\n') != EOF) {
274                 unsigned char sha1[20];
275                 const char *refname;
276                 const char *traits;
277
278                 if (skip_prefix(line.buf, "# pack-refs with:", &traits)) {
279                         if (strstr(traits, " fully-peeled "))
280                                 peeled = PEELED_FULLY;
281                         else if (strstr(traits, " peeled "))
282                                 peeled = PEELED_TAGS;
283                         /* perhaps other traits later as well */
284                         continue;
285                 }
286
287                 refname = parse_ref_line(&line, sha1);
288                 if (refname) {
289                         int flag = REF_ISPACKED;
290
291                         if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
292                                 if (!refname_is_safe(refname))
293                                         die("packed refname is dangerous: %s", refname);
294                                 hashclr(sha1);
295                                 flag |= REF_BAD_NAME | REF_ISBROKEN;
296                         }
297                         last = create_ref_entry(refname, sha1, flag, 0);
298                         if (peeled == PEELED_FULLY ||
299                             (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
300                                 last->flag |= REF_KNOWS_PEELED;
301                         add_ref_entry(dir, last);
302                         continue;
303                 }
304                 if (last &&
305                     line.buf[0] == '^' &&
306                     line.len == PEELED_LINE_LENGTH &&
307                     line.buf[PEELED_LINE_LENGTH - 1] == '\n' &&
308                     !get_sha1_hex(line.buf + 1, sha1)) {
309                         hashcpy(last->u.value.peeled.hash, sha1);
310                         /*
311                          * Regardless of what the file header said,
312                          * we definitely know the value of *this*
313                          * reference:
314                          */
315                         last->flag |= REF_KNOWS_PEELED;
316                 }
317         }
318
319         strbuf_release(&line);
320 }
321
322 static const char *files_packed_refs_path(struct files_ref_store *refs)
323 {
324         return refs->packed_refs_path;
325 }
326
327 static void files_reflog_path(struct files_ref_store *refs,
328                               struct strbuf *sb,
329                               const char *refname)
330 {
331         if (!refname) {
332                 /*
333                  * FIXME: of course this is wrong in multi worktree
334                  * setting. To be fixed real soon.
335                  */
336                 strbuf_addf(sb, "%s/logs", refs->gitcommondir);
337                 return;
338         }
339
340         switch (ref_type(refname)) {
341         case REF_TYPE_PER_WORKTREE:
342         case REF_TYPE_PSEUDOREF:
343                 strbuf_addf(sb, "%s/logs/%s", refs->gitdir, refname);
344                 break;
345         case REF_TYPE_NORMAL:
346                 strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
347                 break;
348         default:
349                 die("BUG: unknown ref type %d of ref %s",
350                     ref_type(refname), refname);
351         }
352 }
353
354 static void files_ref_path(struct files_ref_store *refs,
355                            struct strbuf *sb,
356                            const char *refname)
357 {
358         switch (ref_type(refname)) {
359         case REF_TYPE_PER_WORKTREE:
360         case REF_TYPE_PSEUDOREF:
361                 strbuf_addf(sb, "%s/%s", refs->gitdir, refname);
362                 break;
363         case REF_TYPE_NORMAL:
364                 strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
365                 break;
366         default:
367                 die("BUG: unknown ref type %d of ref %s",
368                     ref_type(refname), refname);
369         }
370 }
371
372 /*
373  * Get the packed_ref_cache for the specified files_ref_store,
374  * creating it if necessary.
375  */
376 static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
377 {
378         const char *packed_refs_file = files_packed_refs_path(refs);
379
380         if (refs->packed &&
381             !stat_validity_check(&refs->packed->validity, packed_refs_file))
382                 clear_packed_ref_cache(refs);
383
384         if (!refs->packed) {
385                 FILE *f;
386
387                 refs->packed = xcalloc(1, sizeof(*refs->packed));
388                 acquire_packed_ref_cache(refs->packed);
389                 refs->packed->cache = create_ref_cache(&refs->base, NULL);
390                 refs->packed->cache->root->flag &= ~REF_INCOMPLETE;
391                 f = fopen(packed_refs_file, "r");
392                 if (f) {
393                         stat_validity_update(&refs->packed->validity, fileno(f));
394                         read_packed_refs(f, get_ref_dir(refs->packed->cache->root));
395                         fclose(f);
396                 }
397         }
398         return refs->packed;
399 }
400
401 static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_cache)
402 {
403         return get_ref_dir(packed_ref_cache->cache->root);
404 }
405
406 static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
407 {
408         return get_packed_ref_dir(get_packed_ref_cache(refs));
409 }
410
411 /*
412  * Add a reference to the in-memory packed reference cache.  This may
413  * only be called while the packed-refs file is locked (see
414  * lock_packed_refs()).  To actually write the packed-refs file, call
415  * commit_packed_refs().
416  */
417 static void add_packed_ref(struct files_ref_store *refs,
418                            const char *refname, const unsigned char *sha1)
419 {
420         struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
421
422         if (!packed_ref_cache->lock)
423                 die("internal error: packed refs not locked");
424         add_ref_entry(get_packed_ref_dir(packed_ref_cache),
425                       create_ref_entry(refname, sha1, REF_ISPACKED, 1));
426 }
427
428 /*
429  * Read the loose references from the namespace dirname into dir
430  * (without recursing).  dirname must end with '/'.  dir must be the
431  * directory entry corresponding to dirname.
432  */
433 static void loose_fill_ref_dir(struct ref_store *ref_store,
434                                struct ref_dir *dir, const char *dirname)
435 {
436         struct files_ref_store *refs =
437                 files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
438         DIR *d;
439         struct dirent *de;
440         int dirnamelen = strlen(dirname);
441         struct strbuf refname;
442         struct strbuf path = STRBUF_INIT;
443         size_t path_baselen;
444
445         files_ref_path(refs, &path, dirname);
446         path_baselen = path.len;
447
448         d = opendir(path.buf);
449         if (!d) {
450                 strbuf_release(&path);
451                 return;
452         }
453
454         strbuf_init(&refname, dirnamelen + 257);
455         strbuf_add(&refname, dirname, dirnamelen);
456
457         while ((de = readdir(d)) != NULL) {
458                 unsigned char sha1[20];
459                 struct stat st;
460                 int flag;
461
462                 if (de->d_name[0] == '.')
463                         continue;
464                 if (ends_with(de->d_name, ".lock"))
465                         continue;
466                 strbuf_addstr(&refname, de->d_name);
467                 strbuf_addstr(&path, de->d_name);
468                 if (stat(path.buf, &st) < 0) {
469                         ; /* silently ignore */
470                 } else if (S_ISDIR(st.st_mode)) {
471                         strbuf_addch(&refname, '/');
472                         add_entry_to_dir(dir,
473                                          create_dir_entry(dir->cache, refname.buf,
474                                                           refname.len, 1));
475                 } else {
476                         if (!refs_resolve_ref_unsafe(&refs->base,
477                                                      refname.buf,
478                                                      RESOLVE_REF_READING,
479                                                      sha1, &flag)) {
480                                 hashclr(sha1);
481                                 flag |= REF_ISBROKEN;
482                         } else if (is_null_sha1(sha1)) {
483                                 /*
484                                  * It is so astronomically unlikely
485                                  * that NULL_SHA1 is the SHA-1 of an
486                                  * actual object that we consider its
487                                  * appearance in a loose reference
488                                  * file to be repo corruption
489                                  * (probably due to a software bug).
490                                  */
491                                 flag |= REF_ISBROKEN;
492                         }
493
494                         if (check_refname_format(refname.buf,
495                                                  REFNAME_ALLOW_ONELEVEL)) {
496                                 if (!refname_is_safe(refname.buf))
497                                         die("loose refname is dangerous: %s", refname.buf);
498                                 hashclr(sha1);
499                                 flag |= REF_BAD_NAME | REF_ISBROKEN;
500                         }
501                         add_entry_to_dir(dir,
502                                          create_ref_entry(refname.buf, sha1, flag, 0));
503                 }
504                 strbuf_setlen(&refname, dirnamelen);
505                 strbuf_setlen(&path, path_baselen);
506         }
507         strbuf_release(&refname);
508         strbuf_release(&path);
509         closedir(d);
510
511         /*
512          * Manually add refs/bisect, which, being per-worktree, might
513          * not appear in the directory listing for refs/ in the main
514          * repo.
515          */
516         if (!strcmp(dirname, "refs/")) {
517                 int pos = search_ref_dir(dir, "refs/bisect/", 12);
518
519                 if (pos < 0) {
520                         struct ref_entry *child_entry = create_dir_entry(
521                                         dir->cache, "refs/bisect/", 12, 1);
522                         add_entry_to_dir(dir, child_entry);
523                 }
524         }
525 }
526
527 static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
528 {
529         if (!refs->loose) {
530                 /*
531                  * Mark the top-level directory complete because we
532                  * are about to read the only subdirectory that can
533                  * hold references:
534                  */
535                 refs->loose = create_ref_cache(&refs->base, loose_fill_ref_dir);
536
537                 /* We're going to fill the top level ourselves: */
538                 refs->loose->root->flag &= ~REF_INCOMPLETE;
539
540                 /*
541                  * Add an incomplete entry for "refs/" (to be filled
542                  * lazily):
543                  */
544                 add_entry_to_dir(get_ref_dir(refs->loose->root),
545                                  create_dir_entry(refs->loose, "refs/", 5, 1));
546         }
547         return refs->loose;
548 }
549
550 static struct ref_dir *get_loose_ref_dir(struct files_ref_store *refs)
551 {
552         return get_ref_dir(get_loose_ref_cache(refs)->root);
553 }
554
555 /*
556  * Return the ref_entry for the given refname from the packed
557  * references.  If it does not exist, return NULL.
558  */
559 static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
560                                         const char *refname)
561 {
562         return find_ref_entry(get_packed_refs(refs), refname);
563 }
564
565 /*
566  * A loose ref file doesn't exist; check for a packed ref.
567  */
568 static int resolve_packed_ref(struct files_ref_store *refs,
569                               const char *refname,
570                               unsigned char *sha1, unsigned int *flags)
571 {
572         struct ref_entry *entry;
573
574         /*
575          * The loose reference file does not exist; check for a packed
576          * reference.
577          */
578         entry = get_packed_ref(refs, refname);
579         if (entry) {
580                 hashcpy(sha1, entry->u.value.oid.hash);
581                 *flags |= REF_ISPACKED;
582                 return 0;
583         }
584         /* refname is not a packed reference. */
585         return -1;
586 }
587
588 static int files_read_raw_ref(struct ref_store *ref_store,
589                               const char *refname, unsigned char *sha1,
590                               struct strbuf *referent, unsigned int *type)
591 {
592         struct files_ref_store *refs =
593                 files_downcast(ref_store, REF_STORE_READ, "read_raw_ref");
594         struct strbuf sb_contents = STRBUF_INIT;
595         struct strbuf sb_path = STRBUF_INIT;
596         const char *path;
597         const char *buf;
598         struct stat st;
599         int fd;
600         int ret = -1;
601         int save_errno;
602         int remaining_retries = 3;
603
604         *type = 0;
605         strbuf_reset(&sb_path);
606
607         files_ref_path(refs, &sb_path, refname);
608
609         path = sb_path.buf;
610
611 stat_ref:
612         /*
613          * We might have to loop back here to avoid a race
614          * condition: first we lstat() the file, then we try
615          * to read it as a link or as a file.  But if somebody
616          * changes the type of the file (file <-> directory
617          * <-> symlink) between the lstat() and reading, then
618          * we don't want to report that as an error but rather
619          * try again starting with the lstat().
620          *
621          * We'll keep a count of the retries, though, just to avoid
622          * any confusing situation sending us into an infinite loop.
623          */
624
625         if (remaining_retries-- <= 0)
626                 goto out;
627
628         if (lstat(path, &st) < 0) {
629                 if (errno != ENOENT)
630                         goto out;
631                 if (resolve_packed_ref(refs, refname, sha1, type)) {
632                         errno = ENOENT;
633                         goto out;
634                 }
635                 ret = 0;
636                 goto out;
637         }
638
639         /* Follow "normalized" - ie "refs/.." symlinks by hand */
640         if (S_ISLNK(st.st_mode)) {
641                 strbuf_reset(&sb_contents);
642                 if (strbuf_readlink(&sb_contents, path, 0) < 0) {
643                         if (errno == ENOENT || errno == EINVAL)
644                                 /* inconsistent with lstat; retry */
645                                 goto stat_ref;
646                         else
647                                 goto out;
648                 }
649                 if (starts_with(sb_contents.buf, "refs/") &&
650                     !check_refname_format(sb_contents.buf, 0)) {
651                         strbuf_swap(&sb_contents, referent);
652                         *type |= REF_ISSYMREF;
653                         ret = 0;
654                         goto out;
655                 }
656                 /*
657                  * It doesn't look like a refname; fall through to just
658                  * treating it like a non-symlink, and reading whatever it
659                  * points to.
660                  */
661         }
662
663         /* Is it a directory? */
664         if (S_ISDIR(st.st_mode)) {
665                 /*
666                  * Even though there is a directory where the loose
667                  * ref is supposed to be, there could still be a
668                  * packed ref:
669                  */
670                 if (resolve_packed_ref(refs, refname, sha1, type)) {
671                         errno = EISDIR;
672                         goto out;
673                 }
674                 ret = 0;
675                 goto out;
676         }
677
678         /*
679          * Anything else, just open it and try to use it as
680          * a ref
681          */
682         fd = open(path, O_RDONLY);
683         if (fd < 0) {
684                 if (errno == ENOENT && !S_ISLNK(st.st_mode))
685                         /* inconsistent with lstat; retry */
686                         goto stat_ref;
687                 else
688                         goto out;
689         }
690         strbuf_reset(&sb_contents);
691         if (strbuf_read(&sb_contents, fd, 256) < 0) {
692                 int save_errno = errno;
693                 close(fd);
694                 errno = save_errno;
695                 goto out;
696         }
697         close(fd);
698         strbuf_rtrim(&sb_contents);
699         buf = sb_contents.buf;
700         if (starts_with(buf, "ref:")) {
701                 buf += 4;
702                 while (isspace(*buf))
703                         buf++;
704
705                 strbuf_reset(referent);
706                 strbuf_addstr(referent, buf);
707                 *type |= REF_ISSYMREF;
708                 ret = 0;
709                 goto out;
710         }
711
712         /*
713          * Please note that FETCH_HEAD has additional
714          * data after the sha.
715          */
716         if (get_sha1_hex(buf, sha1) ||
717             (buf[40] != '\0' && !isspace(buf[40]))) {
718                 *type |= REF_ISBROKEN;
719                 errno = EINVAL;
720                 goto out;
721         }
722
723         ret = 0;
724
725 out:
726         save_errno = errno;
727         strbuf_release(&sb_path);
728         strbuf_release(&sb_contents);
729         errno = save_errno;
730         return ret;
731 }
732
733 static void unlock_ref(struct ref_lock *lock)
734 {
735         /* Do not free lock->lk -- atexit() still looks at them */
736         if (lock->lk)
737                 rollback_lock_file(lock->lk);
738         free(lock->ref_name);
739         free(lock);
740 }
741
742 /*
743  * Lock refname, without following symrefs, and set *lock_p to point
744  * at a newly-allocated lock object. Fill in lock->old_oid, referent,
745  * and type similarly to read_raw_ref().
746  *
747  * The caller must verify that refname is a "safe" reference name (in
748  * the sense of refname_is_safe()) before calling this function.
749  *
750  * If the reference doesn't already exist, verify that refname doesn't
751  * have a D/F conflict with any existing references. extras and skip
752  * are passed to refs_verify_refname_available() for this check.
753  *
754  * If mustexist is not set and the reference is not found or is
755  * broken, lock the reference anyway but clear sha1.
756  *
757  * Return 0 on success. On failure, write an error message to err and
758  * return TRANSACTION_NAME_CONFLICT or TRANSACTION_GENERIC_ERROR.
759  *
760  * Implementation note: This function is basically
761  *
762  *     lock reference
763  *     read_raw_ref()
764  *
765  * but it includes a lot more code to
766  * - Deal with possible races with other processes
767  * - Avoid calling refs_verify_refname_available() when it can be
768  *   avoided, namely if we were successfully able to read the ref
769  * - Generate informative error messages in the case of failure
770  */
771 static int lock_raw_ref(struct files_ref_store *refs,
772                         const char *refname, int mustexist,
773                         const struct string_list *extras,
774                         const struct string_list *skip,
775                         struct ref_lock **lock_p,
776                         struct strbuf *referent,
777                         unsigned int *type,
778                         struct strbuf *err)
779 {
780         struct ref_lock *lock;
781         struct strbuf ref_file = STRBUF_INIT;
782         int attempts_remaining = 3;
783         int ret = TRANSACTION_GENERIC_ERROR;
784
785         assert(err);
786         files_assert_main_repository(refs, "lock_raw_ref");
787
788         *type = 0;
789
790         /* First lock the file so it can't change out from under us. */
791
792         *lock_p = lock = xcalloc(1, sizeof(*lock));
793
794         lock->ref_name = xstrdup(refname);
795         files_ref_path(refs, &ref_file, refname);
796
797 retry:
798         switch (safe_create_leading_directories(ref_file.buf)) {
799         case SCLD_OK:
800                 break; /* success */
801         case SCLD_EXISTS:
802                 /*
803                  * Suppose refname is "refs/foo/bar". We just failed
804                  * to create the containing directory, "refs/foo",
805                  * because there was a non-directory in the way. This
806                  * indicates a D/F conflict, probably because of
807                  * another reference such as "refs/foo". There is no
808                  * reason to expect this error to be transitory.
809                  */
810                 if (refs_verify_refname_available(&refs->base, refname,
811                                                   extras, skip, err)) {
812                         if (mustexist) {
813                                 /*
814                                  * To the user the relevant error is
815                                  * that the "mustexist" reference is
816                                  * missing:
817                                  */
818                                 strbuf_reset(err);
819                                 strbuf_addf(err, "unable to resolve reference '%s'",
820                                             refname);
821                         } else {
822                                 /*
823                                  * The error message set by
824                                  * refs_verify_refname_available() is
825                                  * OK.
826                                  */
827                                 ret = TRANSACTION_NAME_CONFLICT;
828                         }
829                 } else {
830                         /*
831                          * The file that is in the way isn't a loose
832                          * reference. Report it as a low-level
833                          * failure.
834                          */
835                         strbuf_addf(err, "unable to create lock file %s.lock; "
836                                     "non-directory in the way",
837                                     ref_file.buf);
838                 }
839                 goto error_return;
840         case SCLD_VANISHED:
841                 /* Maybe another process was tidying up. Try again. */
842                 if (--attempts_remaining > 0)
843                         goto retry;
844                 /* fall through */
845         default:
846                 strbuf_addf(err, "unable to create directory for %s",
847                             ref_file.buf);
848                 goto error_return;
849         }
850
851         if (!lock->lk)
852                 lock->lk = xcalloc(1, sizeof(struct lock_file));
853
854         if (hold_lock_file_for_update(lock->lk, ref_file.buf, LOCK_NO_DEREF) < 0) {
855                 if (errno == ENOENT && --attempts_remaining > 0) {
856                         /*
857                          * Maybe somebody just deleted one of the
858                          * directories leading to ref_file.  Try
859                          * again:
860                          */
861                         goto retry;
862                 } else {
863                         unable_to_lock_message(ref_file.buf, errno, err);
864                         goto error_return;
865                 }
866         }
867
868         /*
869          * Now we hold the lock and can read the reference without
870          * fear that its value will change.
871          */
872
873         if (files_read_raw_ref(&refs->base, refname,
874                                lock->old_oid.hash, referent, type)) {
875                 if (errno == ENOENT) {
876                         if (mustexist) {
877                                 /* Garden variety missing reference. */
878                                 strbuf_addf(err, "unable to resolve reference '%s'",
879                                             refname);
880                                 goto error_return;
881                         } else {
882                                 /*
883                                  * Reference is missing, but that's OK. We
884                                  * know that there is not a conflict with
885                                  * another loose reference because
886                                  * (supposing that we are trying to lock
887                                  * reference "refs/foo/bar"):
888                                  *
889                                  * - We were successfully able to create
890                                  *   the lockfile refs/foo/bar.lock, so we
891                                  *   know there cannot be a loose reference
892                                  *   named "refs/foo".
893                                  *
894                                  * - We got ENOENT and not EISDIR, so we
895                                  *   know that there cannot be a loose
896                                  *   reference named "refs/foo/bar/baz".
897                                  */
898                         }
899                 } else if (errno == EISDIR) {
900                         /*
901                          * There is a directory in the way. It might have
902                          * contained references that have been deleted. If
903                          * we don't require that the reference already
904                          * exists, try to remove the directory so that it
905                          * doesn't cause trouble when we want to rename the
906                          * lockfile into place later.
907                          */
908                         if (mustexist) {
909                                 /* Garden variety missing reference. */
910                                 strbuf_addf(err, "unable to resolve reference '%s'",
911                                             refname);
912                                 goto error_return;
913                         } else if (remove_dir_recursively(&ref_file,
914                                                           REMOVE_DIR_EMPTY_ONLY)) {
915                                 if (refs_verify_refname_available(
916                                                     &refs->base, refname,
917                                                     extras, skip, err)) {
918                                         /*
919                                          * The error message set by
920                                          * verify_refname_available() is OK.
921                                          */
922                                         ret = TRANSACTION_NAME_CONFLICT;
923                                         goto error_return;
924                                 } else {
925                                         /*
926                                          * We can't delete the directory,
927                                          * but we also don't know of any
928                                          * references that it should
929                                          * contain.
930                                          */
931                                         strbuf_addf(err, "there is a non-empty directory '%s' "
932                                                     "blocking reference '%s'",
933                                                     ref_file.buf, refname);
934                                         goto error_return;
935                                 }
936                         }
937                 } else if (errno == EINVAL && (*type & REF_ISBROKEN)) {
938                         strbuf_addf(err, "unable to resolve reference '%s': "
939                                     "reference broken", refname);
940                         goto error_return;
941                 } else {
942                         strbuf_addf(err, "unable to resolve reference '%s': %s",
943                                     refname, strerror(errno));
944                         goto error_return;
945                 }
946
947                 /*
948                  * If the ref did not exist and we are creating it,
949                  * make sure there is no existing ref that conflicts
950                  * with refname:
951                  */
952                 if (refs_verify_refname_available(
953                                     &refs->base, refname,
954                                     extras, skip, err))
955                         goto error_return;
956         }
957
958         ret = 0;
959         goto out;
960
961 error_return:
962         unlock_ref(lock);
963         *lock_p = NULL;
964
965 out:
966         strbuf_release(&ref_file);
967         return ret;
968 }
969
970 static int files_peel_ref(struct ref_store *ref_store,
971                           const char *refname, unsigned char *sha1)
972 {
973         struct files_ref_store *refs =
974                 files_downcast(ref_store, REF_STORE_READ | REF_STORE_ODB,
975                                "peel_ref");
976         int flag;
977         unsigned char base[20];
978
979         if (current_ref_iter && current_ref_iter->refname == refname) {
980                 struct object_id peeled;
981
982                 if (ref_iterator_peel(current_ref_iter, &peeled))
983                         return -1;
984                 hashcpy(sha1, peeled.hash);
985                 return 0;
986         }
987
988         if (refs_read_ref_full(ref_store, refname,
989                                RESOLVE_REF_READING, base, &flag))
990                 return -1;
991
992         /*
993          * If the reference is packed, read its ref_entry from the
994          * cache in the hope that we already know its peeled value.
995          * We only try this optimization on packed references because
996          * (a) forcing the filling of the loose reference cache could
997          * be expensive and (b) loose references anyway usually do not
998          * have REF_KNOWS_PEELED.
999          */
1000         if (flag & REF_ISPACKED) {
1001                 struct ref_entry *r = get_packed_ref(refs, refname);
1002                 if (r) {
1003                         if (peel_entry(r, 0))
1004                                 return -1;
1005                         hashcpy(sha1, r->u.value.peeled.hash);
1006                         return 0;
1007                 }
1008         }
1009
1010         return peel_object(base, sha1);
1011 }
1012
1013 struct files_ref_iterator {
1014         struct ref_iterator base;
1015
1016         struct packed_ref_cache *packed_ref_cache;
1017         struct ref_iterator *iter0;
1018         unsigned int flags;
1019 };
1020
1021 static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
1022 {
1023         struct files_ref_iterator *iter =
1024                 (struct files_ref_iterator *)ref_iterator;
1025         int ok;
1026
1027         while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
1028                 if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
1029                     ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
1030                         continue;
1031
1032                 if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
1033                     !ref_resolves_to_object(iter->iter0->refname,
1034                                             iter->iter0->oid,
1035                                             iter->iter0->flags))
1036                         continue;
1037
1038                 iter->base.refname = iter->iter0->refname;
1039                 iter->base.oid = iter->iter0->oid;
1040                 iter->base.flags = iter->iter0->flags;
1041                 return ITER_OK;
1042         }
1043
1044         iter->iter0 = NULL;
1045         if (ref_iterator_abort(ref_iterator) != ITER_DONE)
1046                 ok = ITER_ERROR;
1047
1048         return ok;
1049 }
1050
1051 static int files_ref_iterator_peel(struct ref_iterator *ref_iterator,
1052                                    struct object_id *peeled)
1053 {
1054         struct files_ref_iterator *iter =
1055                 (struct files_ref_iterator *)ref_iterator;
1056
1057         return ref_iterator_peel(iter->iter0, peeled);
1058 }
1059
1060 static int files_ref_iterator_abort(struct ref_iterator *ref_iterator)
1061 {
1062         struct files_ref_iterator *iter =
1063                 (struct files_ref_iterator *)ref_iterator;
1064         int ok = ITER_DONE;
1065
1066         if (iter->iter0)
1067                 ok = ref_iterator_abort(iter->iter0);
1068
1069         release_packed_ref_cache(iter->packed_ref_cache);
1070         base_ref_iterator_free(ref_iterator);
1071         return ok;
1072 }
1073
1074 static struct ref_iterator_vtable files_ref_iterator_vtable = {
1075         files_ref_iterator_advance,
1076         files_ref_iterator_peel,
1077         files_ref_iterator_abort
1078 };
1079
1080 static struct ref_iterator *files_ref_iterator_begin(
1081                 struct ref_store *ref_store,
1082                 const char *prefix, unsigned int flags)
1083 {
1084         struct files_ref_store *refs;
1085         struct ref_iterator *loose_iter, *packed_iter;
1086         struct files_ref_iterator *iter;
1087         struct ref_iterator *ref_iterator;
1088
1089         if (ref_paranoia < 0)
1090                 ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
1091         if (ref_paranoia)
1092                 flags |= DO_FOR_EACH_INCLUDE_BROKEN;
1093
1094         refs = files_downcast(ref_store,
1095                               REF_STORE_READ | (ref_paranoia ? 0 : REF_STORE_ODB),
1096                               "ref_iterator_begin");
1097
1098         iter = xcalloc(1, sizeof(*iter));
1099         ref_iterator = &iter->base;
1100         base_ref_iterator_init(ref_iterator, &files_ref_iterator_vtable);
1101
1102         /*
1103          * We must make sure that all loose refs are read before
1104          * accessing the packed-refs file; this avoids a race
1105          * condition if loose refs are migrated to the packed-refs
1106          * file by a simultaneous process, but our in-memory view is
1107          * from before the migration. We ensure this as follows:
1108          * First, we call start the loose refs iteration with its
1109          * `prime_ref` argument set to true. This causes the loose
1110          * references in the subtree to be pre-read into the cache.
1111          * (If they've already been read, that's OK; we only need to
1112          * guarantee that they're read before the packed refs, not
1113          * *how much* before.) After that, we call
1114          * get_packed_ref_cache(), which internally checks whether the
1115          * packed-ref cache is up to date with what is on disk, and
1116          * re-reads it if not.
1117          */
1118
1119         loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
1120                                               prefix, 1);
1121
1122         iter->packed_ref_cache = get_packed_ref_cache(refs);
1123         acquire_packed_ref_cache(iter->packed_ref_cache);
1124         packed_iter = cache_ref_iterator_begin(iter->packed_ref_cache->cache,
1125                                                prefix, 0);
1126
1127         iter->iter0 = overlay_ref_iterator_begin(loose_iter, packed_iter);
1128         iter->flags = flags;
1129
1130         return ref_iterator;
1131 }
1132
1133 /*
1134  * Verify that the reference locked by lock has the value old_sha1.
1135  * Fail if the reference doesn't exist and mustexist is set. Return 0
1136  * on success. On error, write an error message to err, set errno, and
1137  * return a negative value.
1138  */
1139 static int verify_lock(struct ref_store *ref_store, struct ref_lock *lock,
1140                        const unsigned char *old_sha1, int mustexist,
1141                        struct strbuf *err)
1142 {
1143         assert(err);
1144
1145         if (refs_read_ref_full(ref_store, lock->ref_name,
1146                                mustexist ? RESOLVE_REF_READING : 0,
1147                                lock->old_oid.hash, NULL)) {
1148                 if (old_sha1) {
1149                         int save_errno = errno;
1150                         strbuf_addf(err, "can't verify ref '%s'", lock->ref_name);
1151                         errno = save_errno;
1152                         return -1;
1153                 } else {
1154                         oidclr(&lock->old_oid);
1155                         return 0;
1156                 }
1157         }
1158         if (old_sha1 && hashcmp(lock->old_oid.hash, old_sha1)) {
1159                 strbuf_addf(err, "ref '%s' is at %s but expected %s",
1160                             lock->ref_name,
1161                             oid_to_hex(&lock->old_oid),
1162                             sha1_to_hex(old_sha1));
1163                 errno = EBUSY;
1164                 return -1;
1165         }
1166         return 0;
1167 }
1168
1169 static int remove_empty_directories(struct strbuf *path)
1170 {
1171         /*
1172          * we want to create a file but there is a directory there;
1173          * if that is an empty directory (or a directory that contains
1174          * only empty directories), remove them.
1175          */
1176         return remove_dir_recursively(path, REMOVE_DIR_EMPTY_ONLY);
1177 }
1178
1179 static int create_reflock(const char *path, void *cb)
1180 {
1181         struct lock_file *lk = cb;
1182
1183         return hold_lock_file_for_update(lk, path, LOCK_NO_DEREF) < 0 ? -1 : 0;
1184 }
1185
1186 /*
1187  * Locks a ref returning the lock on success and NULL on failure.
1188  * On failure errno is set to something meaningful.
1189  */
1190 static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
1191                                             const char *refname,
1192                                             const unsigned char *old_sha1,
1193                                             const struct string_list *extras,
1194                                             const struct string_list *skip,
1195                                             unsigned int flags, int *type,
1196                                             struct strbuf *err)
1197 {
1198         struct strbuf ref_file = STRBUF_INIT;
1199         struct ref_lock *lock;
1200         int last_errno = 0;
1201         int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
1202         int resolve_flags = RESOLVE_REF_NO_RECURSE;
1203         int resolved;
1204
1205         files_assert_main_repository(refs, "lock_ref_sha1_basic");
1206         assert(err);
1207
1208         lock = xcalloc(1, sizeof(struct ref_lock));
1209
1210         if (mustexist)
1211                 resolve_flags |= RESOLVE_REF_READING;
1212         if (flags & REF_DELETING)
1213                 resolve_flags |= RESOLVE_REF_ALLOW_BAD_NAME;
1214
1215         files_ref_path(refs, &ref_file, refname);
1216         resolved = !!refs_resolve_ref_unsafe(&refs->base,
1217                                              refname, resolve_flags,
1218                                              lock->old_oid.hash, type);
1219         if (!resolved && errno == EISDIR) {
1220                 /*
1221                  * we are trying to lock foo but we used to
1222                  * have foo/bar which now does not exist;
1223                  * it is normal for the empty directory 'foo'
1224                  * to remain.
1225                  */
1226                 if (remove_empty_directories(&ref_file)) {
1227                         last_errno = errno;
1228                         if (!refs_verify_refname_available(
1229                                             &refs->base,
1230                                             refname, extras, skip, err))
1231                                 strbuf_addf(err, "there are still refs under '%s'",
1232                                             refname);
1233                         goto error_return;
1234                 }
1235                 resolved = !!refs_resolve_ref_unsafe(&refs->base,
1236                                                      refname, resolve_flags,
1237                                                      lock->old_oid.hash, type);
1238         }
1239         if (!resolved) {
1240                 last_errno = errno;
1241                 if (last_errno != ENOTDIR ||
1242                     !refs_verify_refname_available(&refs->base, refname,
1243                                                    extras, skip, err))
1244                         strbuf_addf(err, "unable to resolve reference '%s': %s",
1245                                     refname, strerror(last_errno));
1246
1247                 goto error_return;
1248         }
1249
1250         /*
1251          * If the ref did not exist and we are creating it, make sure
1252          * there is no existing packed ref whose name begins with our
1253          * refname, nor a packed ref whose name is a proper prefix of
1254          * our refname.
1255          */
1256         if (is_null_oid(&lock->old_oid) &&
1257             refs_verify_refname_available(&refs->base, refname,
1258                                           extras, skip, err)) {
1259                 last_errno = ENOTDIR;
1260                 goto error_return;
1261         }
1262
1263         lock->lk = xcalloc(1, sizeof(struct lock_file));
1264
1265         lock->ref_name = xstrdup(refname);
1266
1267         if (raceproof_create_file(ref_file.buf, create_reflock, lock->lk)) {
1268                 last_errno = errno;
1269                 unable_to_lock_message(ref_file.buf, errno, err);
1270                 goto error_return;
1271         }
1272
1273         if (verify_lock(&refs->base, lock, old_sha1, mustexist, err)) {
1274                 last_errno = errno;
1275                 goto error_return;
1276         }
1277         goto out;
1278
1279  error_return:
1280         unlock_ref(lock);
1281         lock = NULL;
1282
1283  out:
1284         strbuf_release(&ref_file);
1285         errno = last_errno;
1286         return lock;
1287 }
1288
1289 /*
1290  * Write an entry to the packed-refs file for the specified refname.
1291  * If peeled is non-NULL, write it as the entry's peeled value.
1292  */
1293 static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
1294                                unsigned char *peeled)
1295 {
1296         fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
1297         if (peeled)
1298                 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
1299 }
1300
1301 /*
1302  * An each_ref_entry_fn that writes the entry to a packed-refs file.
1303  */
1304 static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
1305 {
1306         enum peel_status peel_status = peel_entry(entry, 0);
1307
1308         if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1309                 error("internal error: %s is not a valid packed reference!",
1310                       entry->name);
1311         write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
1312                            peel_status == PEEL_PEELED ?
1313                            entry->u.value.peeled.hash : NULL);
1314         return 0;
1315 }
1316
1317 /*
1318  * Lock the packed-refs file for writing. Flags is passed to
1319  * hold_lock_file_for_update(). Return 0 on success. On errors, set
1320  * errno appropriately and return a nonzero value.
1321  */
1322 static int lock_packed_refs(struct files_ref_store *refs, int flags)
1323 {
1324         static int timeout_configured = 0;
1325         static int timeout_value = 1000;
1326         struct packed_ref_cache *packed_ref_cache;
1327
1328         files_assert_main_repository(refs, "lock_packed_refs");
1329
1330         if (!timeout_configured) {
1331                 git_config_get_int("core.packedrefstimeout", &timeout_value);
1332                 timeout_configured = 1;
1333         }
1334
1335         if (hold_lock_file_for_update_timeout(
1336                             &packlock, files_packed_refs_path(refs),
1337                             flags, timeout_value) < 0)
1338                 return -1;
1339         /*
1340          * Get the current packed-refs while holding the lock.  If the
1341          * packed-refs file has been modified since we last read it,
1342          * this will automatically invalidate the cache and re-read
1343          * the packed-refs file.
1344          */
1345         packed_ref_cache = get_packed_ref_cache(refs);
1346         packed_ref_cache->lock = &packlock;
1347         /* Increment the reference count to prevent it from being freed: */
1348         acquire_packed_ref_cache(packed_ref_cache);
1349         return 0;
1350 }
1351
1352 /*
1353  * Write the current version of the packed refs cache from memory to
1354  * disk. The packed-refs file must already be locked for writing (see
1355  * lock_packed_refs()). Return zero on success. On errors, set errno
1356  * and return a nonzero value
1357  */
1358 static int commit_packed_refs(struct files_ref_store *refs)
1359 {
1360         struct packed_ref_cache *packed_ref_cache =
1361                 get_packed_ref_cache(refs);
1362         int error = 0;
1363         int save_errno = 0;
1364         FILE *out;
1365
1366         files_assert_main_repository(refs, "commit_packed_refs");
1367
1368         if (!packed_ref_cache->lock)
1369                 die("internal error: packed-refs not locked");
1370
1371         out = fdopen_lock_file(packed_ref_cache->lock, "w");
1372         if (!out)
1373                 die_errno("unable to fdopen packed-refs descriptor");
1374
1375         fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
1376         do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
1377                                  write_packed_entry_fn, out);
1378
1379         if (commit_lock_file(packed_ref_cache->lock)) {
1380                 save_errno = errno;
1381                 error = -1;
1382         }
1383         packed_ref_cache->lock = NULL;
1384         release_packed_ref_cache(packed_ref_cache);
1385         errno = save_errno;
1386         return error;
1387 }
1388
1389 /*
1390  * Rollback the lockfile for the packed-refs file, and discard the
1391  * in-memory packed reference cache.  (The packed-refs file will be
1392  * read anew if it is needed again after this function is called.)
1393  */
1394 static void rollback_packed_refs(struct files_ref_store *refs)
1395 {
1396         struct packed_ref_cache *packed_ref_cache =
1397                 get_packed_ref_cache(refs);
1398
1399         files_assert_main_repository(refs, "rollback_packed_refs");
1400
1401         if (!packed_ref_cache->lock)
1402                 die("internal error: packed-refs not locked");
1403         rollback_lock_file(packed_ref_cache->lock);
1404         packed_ref_cache->lock = NULL;
1405         release_packed_ref_cache(packed_ref_cache);
1406         clear_packed_ref_cache(refs);
1407 }
1408
1409 struct ref_to_prune {
1410         struct ref_to_prune *next;
1411         unsigned char sha1[20];
1412         char name[FLEX_ARRAY];
1413 };
1414
1415 struct pack_refs_cb_data {
1416         unsigned int flags;
1417         struct ref_dir *packed_refs;
1418         struct ref_to_prune *ref_to_prune;
1419 };
1420
1421 /*
1422  * An each_ref_entry_fn that is run over loose references only.  If
1423  * the loose reference can be packed, add an entry in the packed ref
1424  * cache.  If the reference should be pruned, also add it to
1425  * ref_to_prune in the pack_refs_cb_data.
1426  */
1427 static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
1428 {
1429         struct pack_refs_cb_data *cb = cb_data;
1430         enum peel_status peel_status;
1431         struct ref_entry *packed_entry;
1432         int is_tag_ref = starts_with(entry->name, "refs/tags/");
1433
1434         /* Do not pack per-worktree refs: */
1435         if (ref_type(entry->name) != REF_TYPE_NORMAL)
1436                 return 0;
1437
1438         /* ALWAYS pack tags */
1439         if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
1440                 return 0;
1441
1442         /* Do not pack symbolic or broken refs: */
1443         if ((entry->flag & REF_ISSYMREF) || !entry_resolves_to_object(entry))
1444                 return 0;
1445
1446         /* Add a packed ref cache entry equivalent to the loose entry. */
1447         peel_status = peel_entry(entry, 1);
1448         if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1449                 die("internal error peeling reference %s (%s)",
1450                     entry->name, oid_to_hex(&entry->u.value.oid));
1451         packed_entry = find_ref_entry(cb->packed_refs, entry->name);
1452         if (packed_entry) {
1453                 /* Overwrite existing packed entry with info from loose entry */
1454                 packed_entry->flag = REF_ISPACKED | REF_KNOWS_PEELED;
1455                 oidcpy(&packed_entry->u.value.oid, &entry->u.value.oid);
1456         } else {
1457                 packed_entry = create_ref_entry(entry->name, entry->u.value.oid.hash,
1458                                                 REF_ISPACKED | REF_KNOWS_PEELED, 0);
1459                 add_ref_entry(cb->packed_refs, packed_entry);
1460         }
1461         oidcpy(&packed_entry->u.value.peeled, &entry->u.value.peeled);
1462
1463         /* Schedule the loose reference for pruning if requested. */
1464         if ((cb->flags & PACK_REFS_PRUNE)) {
1465                 struct ref_to_prune *n;
1466                 FLEX_ALLOC_STR(n, name, entry->name);
1467                 hashcpy(n->sha1, entry->u.value.oid.hash);
1468                 n->next = cb->ref_to_prune;
1469                 cb->ref_to_prune = n;
1470         }
1471         return 0;
1472 }
1473
1474 enum {
1475         REMOVE_EMPTY_PARENTS_REF = 0x01,
1476         REMOVE_EMPTY_PARENTS_REFLOG = 0x02
1477 };
1478
1479 /*
1480  * Remove empty parent directories associated with the specified
1481  * reference and/or its reflog, but spare [logs/]refs/ and immediate
1482  * subdirs. flags is a combination of REMOVE_EMPTY_PARENTS_REF and/or
1483  * REMOVE_EMPTY_PARENTS_REFLOG.
1484  */
1485 static void try_remove_empty_parents(struct files_ref_store *refs,
1486                                      const char *refname,
1487                                      unsigned int flags)
1488 {
1489         struct strbuf buf = STRBUF_INIT;
1490         struct strbuf sb = STRBUF_INIT;
1491         char *p, *q;
1492         int i;
1493
1494         strbuf_addstr(&buf, refname);
1495         p = buf.buf;
1496         for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
1497                 while (*p && *p != '/')
1498                         p++;
1499                 /* tolerate duplicate slashes; see check_refname_format() */
1500                 while (*p == '/')
1501                         p++;
1502         }
1503         q = buf.buf + buf.len;
1504         while (flags & (REMOVE_EMPTY_PARENTS_REF | REMOVE_EMPTY_PARENTS_REFLOG)) {
1505                 while (q > p && *q != '/')
1506                         q--;
1507                 while (q > p && *(q-1) == '/')
1508                         q--;
1509                 if (q == p)
1510                         break;
1511                 strbuf_setlen(&buf, q - buf.buf);
1512
1513                 strbuf_reset(&sb);
1514                 files_ref_path(refs, &sb, buf.buf);
1515                 if ((flags & REMOVE_EMPTY_PARENTS_REF) && rmdir(sb.buf))
1516                         flags &= ~REMOVE_EMPTY_PARENTS_REF;
1517
1518                 strbuf_reset(&sb);
1519                 files_reflog_path(refs, &sb, buf.buf);
1520                 if ((flags & REMOVE_EMPTY_PARENTS_REFLOG) && rmdir(sb.buf))
1521                         flags &= ~REMOVE_EMPTY_PARENTS_REFLOG;
1522         }
1523         strbuf_release(&buf);
1524         strbuf_release(&sb);
1525 }
1526
1527 /* make sure nobody touched the ref, and unlink */
1528 static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r)
1529 {
1530         struct ref_transaction *transaction;
1531         struct strbuf err = STRBUF_INIT;
1532
1533         if (check_refname_format(r->name, 0))
1534                 return;
1535
1536         transaction = ref_store_transaction_begin(&refs->base, &err);
1537         if (!transaction ||
1538             ref_transaction_delete(transaction, r->name, r->sha1,
1539                                    REF_ISPRUNING | REF_NODEREF, NULL, &err) ||
1540             ref_transaction_commit(transaction, &err)) {
1541                 ref_transaction_free(transaction);
1542                 error("%s", err.buf);
1543                 strbuf_release(&err);
1544                 return;
1545         }
1546         ref_transaction_free(transaction);
1547         strbuf_release(&err);
1548 }
1549
1550 static void prune_refs(struct files_ref_store *refs, struct ref_to_prune *r)
1551 {
1552         while (r) {
1553                 prune_ref(refs, r);
1554                 r = r->next;
1555         }
1556 }
1557
1558 static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
1559 {
1560         struct files_ref_store *refs =
1561                 files_downcast(ref_store, REF_STORE_WRITE | REF_STORE_ODB,
1562                                "pack_refs");
1563         struct pack_refs_cb_data cbdata;
1564
1565         memset(&cbdata, 0, sizeof(cbdata));
1566         cbdata.flags = flags;
1567
1568         lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
1569         cbdata.packed_refs = get_packed_refs(refs);
1570
1571         do_for_each_entry_in_dir(get_loose_ref_dir(refs),
1572                                  pack_if_possible_fn, &cbdata);
1573
1574         if (commit_packed_refs(refs))
1575                 die_errno("unable to overwrite old ref-pack file");
1576
1577         prune_refs(refs, cbdata.ref_to_prune);
1578         return 0;
1579 }
1580
1581 /*
1582  * Rewrite the packed-refs file, omitting any refs listed in
1583  * 'refnames'. On error, leave packed-refs unchanged, write an error
1584  * message to 'err', and return a nonzero value.
1585  *
1586  * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
1587  */
1588 static int repack_without_refs(struct files_ref_store *refs,
1589                                struct string_list *refnames, struct strbuf *err)
1590 {
1591         struct ref_dir *packed;
1592         struct string_list_item *refname;
1593         int ret, needs_repacking = 0, removed = 0;
1594
1595         files_assert_main_repository(refs, "repack_without_refs");
1596         assert(err);
1597
1598         /* Look for a packed ref */
1599         for_each_string_list_item(refname, refnames) {
1600                 if (get_packed_ref(refs, refname->string)) {
1601                         needs_repacking = 1;
1602                         break;
1603                 }
1604         }
1605
1606         /* Avoid locking if we have nothing to do */
1607         if (!needs_repacking)
1608                 return 0; /* no refname exists in packed refs */
1609
1610         if (lock_packed_refs(refs, 0)) {
1611                 unable_to_lock_message(files_packed_refs_path(refs), errno, err);
1612                 return -1;
1613         }
1614         packed = get_packed_refs(refs);
1615
1616         /* Remove refnames from the cache */
1617         for_each_string_list_item(refname, refnames)
1618                 if (remove_entry_from_dir(packed, refname->string) != -1)
1619                         removed = 1;
1620         if (!removed) {
1621                 /*
1622                  * All packed entries disappeared while we were
1623                  * acquiring the lock.
1624                  */
1625                 rollback_packed_refs(refs);
1626                 return 0;
1627         }
1628
1629         /* Write what remains */
1630         ret = commit_packed_refs(refs);
1631         if (ret)
1632                 strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
1633                             strerror(errno));
1634         return ret;
1635 }
1636
1637 static int files_delete_refs(struct ref_store *ref_store,
1638                              struct string_list *refnames, unsigned int flags)
1639 {
1640         struct files_ref_store *refs =
1641                 files_downcast(ref_store, REF_STORE_WRITE, "delete_refs");
1642         struct strbuf err = STRBUF_INIT;
1643         int i, result = 0;
1644
1645         if (!refnames->nr)
1646                 return 0;
1647
1648         result = repack_without_refs(refs, refnames, &err);
1649         if (result) {
1650                 /*
1651                  * If we failed to rewrite the packed-refs file, then
1652                  * it is unsafe to try to remove loose refs, because
1653                  * doing so might expose an obsolete packed value for
1654                  * a reference that might even point at an object that
1655                  * has been garbage collected.
1656                  */
1657                 if (refnames->nr == 1)
1658                         error(_("could not delete reference %s: %s"),
1659                               refnames->items[0].string, err.buf);
1660                 else
1661                         error(_("could not delete references: %s"), err.buf);
1662
1663                 goto out;
1664         }
1665
1666         for (i = 0; i < refnames->nr; i++) {
1667                 const char *refname = refnames->items[i].string;
1668
1669                 if (refs_delete_ref(&refs->base, NULL, refname, NULL, flags))
1670                         result |= error(_("could not remove reference %s"), refname);
1671         }
1672
1673 out:
1674         strbuf_release(&err);
1675         return result;
1676 }
1677
1678 /*
1679  * People using contrib's git-new-workdir have .git/logs/refs ->
1680  * /some/other/path/.git/logs/refs, and that may live on another device.
1681  *
1682  * IOW, to avoid cross device rename errors, the temporary renamed log must
1683  * live into logs/refs.
1684  */
1685 #define TMP_RENAMED_LOG  "refs/.tmp-renamed-log"
1686
1687 struct rename_cb {
1688         const char *tmp_renamed_log;
1689         int true_errno;
1690 };
1691
1692 static int rename_tmp_log_callback(const char *path, void *cb_data)
1693 {
1694         struct rename_cb *cb = cb_data;
1695
1696         if (rename(cb->tmp_renamed_log, path)) {
1697                 /*
1698                  * rename(a, b) when b is an existing directory ought
1699                  * to result in ISDIR, but Solaris 5.8 gives ENOTDIR.
1700                  * Sheesh. Record the true errno for error reporting,
1701                  * but report EISDIR to raceproof_create_file() so
1702                  * that it knows to retry.
1703                  */
1704                 cb->true_errno = errno;
1705                 if (errno == ENOTDIR)
1706                         errno = EISDIR;
1707                 return -1;
1708         } else {
1709                 return 0;
1710         }
1711 }
1712
1713 static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname)
1714 {
1715         struct strbuf path = STRBUF_INIT;
1716         struct strbuf tmp = STRBUF_INIT;
1717         struct rename_cb cb;
1718         int ret;
1719
1720         files_reflog_path(refs, &path, newrefname);
1721         files_reflog_path(refs, &tmp, TMP_RENAMED_LOG);
1722         cb.tmp_renamed_log = tmp.buf;
1723         ret = raceproof_create_file(path.buf, rename_tmp_log_callback, &cb);
1724         if (ret) {
1725                 if (errno == EISDIR)
1726                         error("directory not empty: %s", path.buf);
1727                 else
1728                         error("unable to move logfile %s to %s: %s",
1729                               tmp.buf, path.buf,
1730                               strerror(cb.true_errno));
1731         }
1732
1733         strbuf_release(&path);
1734         strbuf_release(&tmp);
1735         return ret;
1736 }
1737
1738 static int write_ref_to_lockfile(struct ref_lock *lock,
1739                                  const unsigned char *sha1, struct strbuf *err);
1740 static int commit_ref_update(struct files_ref_store *refs,
1741                              struct ref_lock *lock,
1742                              const unsigned char *sha1, const char *logmsg,
1743                              struct strbuf *err);
1744
1745 static int files_rename_ref(struct ref_store *ref_store,
1746                             const char *oldrefname, const char *newrefname,
1747                             const char *logmsg)
1748 {
1749         struct files_ref_store *refs =
1750                 files_downcast(ref_store, REF_STORE_WRITE, "rename_ref");
1751         unsigned char sha1[20], orig_sha1[20];
1752         int flag = 0, logmoved = 0;
1753         struct ref_lock *lock;
1754         struct stat loginfo;
1755         struct strbuf sb_oldref = STRBUF_INIT;
1756         struct strbuf sb_newref = STRBUF_INIT;
1757         struct strbuf tmp_renamed_log = STRBUF_INIT;
1758         int log, ret;
1759         struct strbuf err = STRBUF_INIT;
1760
1761         files_reflog_path(refs, &sb_oldref, oldrefname);
1762         files_reflog_path(refs, &sb_newref, newrefname);
1763         files_reflog_path(refs, &tmp_renamed_log, TMP_RENAMED_LOG);
1764
1765         log = !lstat(sb_oldref.buf, &loginfo);
1766         if (log && S_ISLNK(loginfo.st_mode)) {
1767                 ret = error("reflog for %s is a symlink", oldrefname);
1768                 goto out;
1769         }
1770
1771         if (!refs_resolve_ref_unsafe(&refs->base, oldrefname,
1772                                      RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1773                                 orig_sha1, &flag)) {
1774                 ret = error("refname %s not found", oldrefname);
1775                 goto out;
1776         }
1777
1778         if (flag & REF_ISSYMREF) {
1779                 ret = error("refname %s is a symbolic ref, renaming it is not supported",
1780                             oldrefname);
1781                 goto out;
1782         }
1783         if (!refs_rename_ref_available(&refs->base, oldrefname, newrefname)) {
1784                 ret = 1;
1785                 goto out;
1786         }
1787
1788         if (log && rename(sb_oldref.buf, tmp_renamed_log.buf)) {
1789                 ret = error("unable to move logfile logs/%s to logs/"TMP_RENAMED_LOG": %s",
1790                             oldrefname, strerror(errno));
1791                 goto out;
1792         }
1793
1794         if (refs_delete_ref(&refs->base, logmsg, oldrefname,
1795                             orig_sha1, REF_NODEREF)) {
1796                 error("unable to delete old %s", oldrefname);
1797                 goto rollback;
1798         }
1799
1800         /*
1801          * Since we are doing a shallow lookup, sha1 is not the
1802          * correct value to pass to delete_ref as old_sha1. But that
1803          * doesn't matter, because an old_sha1 check wouldn't add to
1804          * the safety anyway; we want to delete the reference whatever
1805          * its current value.
1806          */
1807         if (!refs_read_ref_full(&refs->base, newrefname,
1808                                 RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1809                                 sha1, NULL) &&
1810             refs_delete_ref(&refs->base, NULL, newrefname,
1811                             NULL, REF_NODEREF)) {
1812                 if (errno == EISDIR) {
1813                         struct strbuf path = STRBUF_INIT;
1814                         int result;
1815
1816                         files_ref_path(refs, &path, newrefname);
1817                         result = remove_empty_directories(&path);
1818                         strbuf_release(&path);
1819
1820                         if (result) {
1821                                 error("Directory not empty: %s", newrefname);
1822                                 goto rollback;
1823                         }
1824                 } else {
1825                         error("unable to delete existing %s", newrefname);
1826                         goto rollback;
1827                 }
1828         }
1829
1830         if (log && rename_tmp_log(refs, newrefname))
1831                 goto rollback;
1832
1833         logmoved = log;
1834
1835         lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
1836                                    REF_NODEREF, NULL, &err);
1837         if (!lock) {
1838                 error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
1839                 strbuf_release(&err);
1840                 goto rollback;
1841         }
1842         hashcpy(lock->old_oid.hash, orig_sha1);
1843
1844         if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1845             commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
1846                 error("unable to write current sha1 into %s: %s", newrefname, err.buf);
1847                 strbuf_release(&err);
1848                 goto rollback;
1849         }
1850
1851         ret = 0;
1852         goto out;
1853
1854  rollback:
1855         lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
1856                                    REF_NODEREF, NULL, &err);
1857         if (!lock) {
1858                 error("unable to lock %s for rollback: %s", oldrefname, err.buf);
1859                 strbuf_release(&err);
1860                 goto rollbacklog;
1861         }
1862
1863         flag = log_all_ref_updates;
1864         log_all_ref_updates = LOG_REFS_NONE;
1865         if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
1866             commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
1867                 error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
1868                 strbuf_release(&err);
1869         }
1870         log_all_ref_updates = flag;
1871
1872  rollbacklog:
1873         if (logmoved && rename(sb_newref.buf, sb_oldref.buf))
1874                 error("unable to restore logfile %s from %s: %s",
1875                         oldrefname, newrefname, strerror(errno));
1876         if (!logmoved && log &&
1877             rename(tmp_renamed_log.buf, sb_oldref.buf))
1878                 error("unable to restore logfile %s from logs/"TMP_RENAMED_LOG": %s",
1879                         oldrefname, strerror(errno));
1880         ret = 1;
1881  out:
1882         strbuf_release(&sb_newref);
1883         strbuf_release(&sb_oldref);
1884         strbuf_release(&tmp_renamed_log);
1885
1886         return ret;
1887 }
1888
1889 static int close_ref(struct ref_lock *lock)
1890 {
1891         if (close_lock_file(lock->lk))
1892                 return -1;
1893         return 0;
1894 }
1895
1896 static int commit_ref(struct ref_lock *lock)
1897 {
1898         char *path = get_locked_file_path(lock->lk);
1899         struct stat st;
1900
1901         if (!lstat(path, &st) && S_ISDIR(st.st_mode)) {
1902                 /*
1903                  * There is a directory at the path we want to rename
1904                  * the lockfile to. Hopefully it is empty; try to
1905                  * delete it.
1906                  */
1907                 size_t len = strlen(path);
1908                 struct strbuf sb_path = STRBUF_INIT;
1909
1910                 strbuf_attach(&sb_path, path, len, len);
1911
1912                 /*
1913                  * If this fails, commit_lock_file() will also fail
1914                  * and will report the problem.
1915                  */
1916                 remove_empty_directories(&sb_path);
1917                 strbuf_release(&sb_path);
1918         } else {
1919                 free(path);
1920         }
1921
1922         if (commit_lock_file(lock->lk))
1923                 return -1;
1924         return 0;
1925 }
1926
1927 static int open_or_create_logfile(const char *path, void *cb)
1928 {
1929         int *fd = cb;
1930
1931         *fd = open(path, O_APPEND | O_WRONLY | O_CREAT, 0666);
1932         return (*fd < 0) ? -1 : 0;
1933 }
1934
1935 /*
1936  * Create a reflog for a ref. If force_create = 0, only create the
1937  * reflog for certain refs (those for which should_autocreate_reflog
1938  * returns non-zero). Otherwise, create it regardless of the reference
1939  * name. If the logfile already existed or was created, return 0 and
1940  * set *logfd to the file descriptor opened for appending to the file.
1941  * If no logfile exists and we decided not to create one, return 0 and
1942  * set *logfd to -1. On failure, fill in *err, set *logfd to -1, and
1943  * return -1.
1944  */
1945 static int log_ref_setup(struct files_ref_store *refs,
1946                          const char *refname, int force_create,
1947                          int *logfd, struct strbuf *err)
1948 {
1949         struct strbuf logfile_sb = STRBUF_INIT;
1950         char *logfile;
1951
1952         files_reflog_path(refs, &logfile_sb, refname);
1953         logfile = strbuf_detach(&logfile_sb, NULL);
1954
1955         if (force_create || should_autocreate_reflog(refname)) {
1956                 if (raceproof_create_file(logfile, open_or_create_logfile, logfd)) {
1957                         if (errno == ENOENT)
1958                                 strbuf_addf(err, "unable to create directory for '%s': "
1959                                             "%s", logfile, strerror(errno));
1960                         else if (errno == EISDIR)
1961                                 strbuf_addf(err, "there are still logs under '%s'",
1962                                             logfile);
1963                         else
1964                                 strbuf_addf(err, "unable to append to '%s': %s",
1965                                             logfile, strerror(errno));
1966
1967                         goto error;
1968                 }
1969         } else {
1970                 *logfd = open(logfile, O_APPEND | O_WRONLY, 0666);
1971                 if (*logfd < 0) {
1972                         if (errno == ENOENT || errno == EISDIR) {
1973                                 /*
1974                                  * The logfile doesn't already exist,
1975                                  * but that is not an error; it only
1976                                  * means that we won't write log
1977                                  * entries to it.
1978                                  */
1979                                 ;
1980                         } else {
1981                                 strbuf_addf(err, "unable to append to '%s': %s",
1982                                             logfile, strerror(errno));
1983                                 goto error;
1984                         }
1985                 }
1986         }
1987
1988         if (*logfd >= 0)
1989                 adjust_shared_perm(logfile);
1990
1991         free(logfile);
1992         return 0;
1993
1994 error:
1995         free(logfile);
1996         return -1;
1997 }
1998
1999 static int files_create_reflog(struct ref_store *ref_store,
2000                                const char *refname, int force_create,
2001                                struct strbuf *err)
2002 {
2003         struct files_ref_store *refs =
2004                 files_downcast(ref_store, REF_STORE_WRITE, "create_reflog");
2005         int fd;
2006
2007         if (log_ref_setup(refs, refname, force_create, &fd, err))
2008                 return -1;
2009
2010         if (fd >= 0)
2011                 close(fd);
2012
2013         return 0;
2014 }
2015
2016 static int log_ref_write_fd(int fd, const unsigned char *old_sha1,
2017                             const unsigned char *new_sha1,
2018                             const char *committer, const char *msg)
2019 {
2020         int msglen, written;
2021         unsigned maxlen, len;
2022         char *logrec;
2023
2024         msglen = msg ? strlen(msg) : 0;
2025         maxlen = strlen(committer) + msglen + 100;
2026         logrec = xmalloc(maxlen);
2027         len = xsnprintf(logrec, maxlen, "%s %s %s\n",
2028                         sha1_to_hex(old_sha1),
2029                         sha1_to_hex(new_sha1),
2030                         committer);
2031         if (msglen)
2032                 len += copy_reflog_msg(logrec + len - 1, msg) - 1;
2033
2034         written = len <= maxlen ? write_in_full(fd, logrec, len) : -1;
2035         free(logrec);
2036         if (written != len)
2037                 return -1;
2038
2039         return 0;
2040 }
2041
2042 static int files_log_ref_write(struct files_ref_store *refs,
2043                                const char *refname, const unsigned char *old_sha1,
2044                                const unsigned char *new_sha1, const char *msg,
2045                                int flags, struct strbuf *err)
2046 {
2047         int logfd, result;
2048
2049         if (log_all_ref_updates == LOG_REFS_UNSET)
2050                 log_all_ref_updates = is_bare_repository() ? LOG_REFS_NONE : LOG_REFS_NORMAL;
2051
2052         result = log_ref_setup(refs, refname,
2053                                flags & REF_FORCE_CREATE_REFLOG,
2054                                &logfd, err);
2055
2056         if (result)
2057                 return result;
2058
2059         if (logfd < 0)
2060                 return 0;
2061         result = log_ref_write_fd(logfd, old_sha1, new_sha1,
2062                                   git_committer_info(0), msg);
2063         if (result) {
2064                 struct strbuf sb = STRBUF_INIT;
2065                 int save_errno = errno;
2066
2067                 files_reflog_path(refs, &sb, refname);
2068                 strbuf_addf(err, "unable to append to '%s': %s",
2069                             sb.buf, strerror(save_errno));
2070                 strbuf_release(&sb);
2071                 close(logfd);
2072                 return -1;
2073         }
2074         if (close(logfd)) {
2075                 struct strbuf sb = STRBUF_INIT;
2076                 int save_errno = errno;
2077
2078                 files_reflog_path(refs, &sb, refname);
2079                 strbuf_addf(err, "unable to append to '%s': %s",
2080                             sb.buf, strerror(save_errno));
2081                 strbuf_release(&sb);
2082                 return -1;
2083         }
2084         return 0;
2085 }
2086
2087 /*
2088  * Write sha1 into the open lockfile, then close the lockfile. On
2089  * errors, rollback the lockfile, fill in *err and
2090  * return -1.
2091  */
2092 static int write_ref_to_lockfile(struct ref_lock *lock,
2093                                  const unsigned char *sha1, struct strbuf *err)
2094 {
2095         static char term = '\n';
2096         struct object *o;
2097         int fd;
2098
2099         o = parse_object(sha1);
2100         if (!o) {
2101                 strbuf_addf(err,
2102                             "trying to write ref '%s' with nonexistent object %s",
2103                             lock->ref_name, sha1_to_hex(sha1));
2104                 unlock_ref(lock);
2105                 return -1;
2106         }
2107         if (o->type != OBJ_COMMIT && is_branch(lock->ref_name)) {
2108                 strbuf_addf(err,
2109                             "trying to write non-commit object %s to branch '%s'",
2110                             sha1_to_hex(sha1), lock->ref_name);
2111                 unlock_ref(lock);
2112                 return -1;
2113         }
2114         fd = get_lock_file_fd(lock->lk);
2115         if (write_in_full(fd, sha1_to_hex(sha1), 40) != 40 ||
2116             write_in_full(fd, &term, 1) != 1 ||
2117             close_ref(lock) < 0) {
2118                 strbuf_addf(err,
2119                             "couldn't write '%s'", get_lock_file_path(lock->lk));
2120                 unlock_ref(lock);
2121                 return -1;
2122         }
2123         return 0;
2124 }
2125
2126 /*
2127  * Commit a change to a loose reference that has already been written
2128  * to the loose reference lockfile. Also update the reflogs if
2129  * necessary, using the specified lockmsg (which can be NULL).
2130  */
2131 static int commit_ref_update(struct files_ref_store *refs,
2132                              struct ref_lock *lock,
2133                              const unsigned char *sha1, const char *logmsg,
2134                              struct strbuf *err)
2135 {
2136         files_assert_main_repository(refs, "commit_ref_update");
2137
2138         clear_loose_ref_cache(refs);
2139         if (files_log_ref_write(refs, lock->ref_name,
2140                                 lock->old_oid.hash, sha1,
2141                                 logmsg, 0, err)) {
2142                 char *old_msg = strbuf_detach(err, NULL);
2143                 strbuf_addf(err, "cannot update the ref '%s': %s",
2144                             lock->ref_name, old_msg);
2145                 free(old_msg);
2146                 unlock_ref(lock);
2147                 return -1;
2148         }
2149
2150         if (strcmp(lock->ref_name, "HEAD") != 0) {
2151                 /*
2152                  * Special hack: If a branch is updated directly and HEAD
2153                  * points to it (may happen on the remote side of a push
2154                  * for example) then logically the HEAD reflog should be
2155                  * updated too.
2156                  * A generic solution implies reverse symref information,
2157                  * but finding all symrefs pointing to the given branch
2158                  * would be rather costly for this rare event (the direct
2159                  * update of a branch) to be worth it.  So let's cheat and
2160                  * check with HEAD only which should cover 99% of all usage
2161                  * scenarios (even 100% of the default ones).
2162                  */
2163                 unsigned char head_sha1[20];
2164                 int head_flag;
2165                 const char *head_ref;
2166
2167                 head_ref = refs_resolve_ref_unsafe(&refs->base, "HEAD",
2168                                                    RESOLVE_REF_READING,
2169                                                    head_sha1, &head_flag);
2170                 if (head_ref && (head_flag & REF_ISSYMREF) &&
2171                     !strcmp(head_ref, lock->ref_name)) {
2172                         struct strbuf log_err = STRBUF_INIT;
2173                         if (files_log_ref_write(refs, "HEAD",
2174                                                 lock->old_oid.hash, sha1,
2175                                                 logmsg, 0, &log_err)) {
2176                                 error("%s", log_err.buf);
2177                                 strbuf_release(&log_err);
2178                         }
2179                 }
2180         }
2181
2182         if (commit_ref(lock)) {
2183                 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
2184                 unlock_ref(lock);
2185                 return -1;
2186         }
2187
2188         unlock_ref(lock);
2189         return 0;
2190 }
2191
2192 static int create_ref_symlink(struct ref_lock *lock, const char *target)
2193 {
2194         int ret = -1;
2195 #ifndef NO_SYMLINK_HEAD
2196         char *ref_path = get_locked_file_path(lock->lk);
2197         unlink(ref_path);
2198         ret = symlink(target, ref_path);
2199         free(ref_path);
2200
2201         if (ret)
2202                 fprintf(stderr, "no symlink - falling back to symbolic ref\n");
2203 #endif
2204         return ret;
2205 }
2206
2207 static void update_symref_reflog(struct files_ref_store *refs,
2208                                  struct ref_lock *lock, const char *refname,
2209                                  const char *target, const char *logmsg)
2210 {
2211         struct strbuf err = STRBUF_INIT;
2212         unsigned char new_sha1[20];
2213         if (logmsg &&
2214             !refs_read_ref_full(&refs->base, target,
2215                                 RESOLVE_REF_READING, new_sha1, NULL) &&
2216             files_log_ref_write(refs, refname, lock->old_oid.hash,
2217                                 new_sha1, logmsg, 0, &err)) {
2218                 error("%s", err.buf);
2219                 strbuf_release(&err);
2220         }
2221 }
2222
2223 static int create_symref_locked(struct files_ref_store *refs,
2224                                 struct ref_lock *lock, const char *refname,
2225                                 const char *target, const char *logmsg)
2226 {
2227         if (prefer_symlink_refs && !create_ref_symlink(lock, target)) {
2228                 update_symref_reflog(refs, lock, refname, target, logmsg);
2229                 return 0;
2230         }
2231
2232         if (!fdopen_lock_file(lock->lk, "w"))
2233                 return error("unable to fdopen %s: %s",
2234                              lock->lk->tempfile.filename.buf, strerror(errno));
2235
2236         update_symref_reflog(refs, lock, refname, target, logmsg);
2237
2238         /* no error check; commit_ref will check ferror */
2239         fprintf(lock->lk->tempfile.fp, "ref: %s\n", target);
2240         if (commit_ref(lock) < 0)
2241                 return error("unable to write symref for %s: %s", refname,
2242                              strerror(errno));
2243         return 0;
2244 }
2245
2246 static int files_create_symref(struct ref_store *ref_store,
2247                                const char *refname, const char *target,
2248                                const char *logmsg)
2249 {
2250         struct files_ref_store *refs =
2251                 files_downcast(ref_store, REF_STORE_WRITE, "create_symref");
2252         struct strbuf err = STRBUF_INIT;
2253         struct ref_lock *lock;
2254         int ret;
2255
2256         lock = lock_ref_sha1_basic(refs, refname, NULL,
2257                                    NULL, NULL, REF_NODEREF, NULL,
2258                                    &err);
2259         if (!lock) {
2260                 error("%s", err.buf);
2261                 strbuf_release(&err);
2262                 return -1;
2263         }
2264
2265         ret = create_symref_locked(refs, lock, refname, target, logmsg);
2266         unlock_ref(lock);
2267         return ret;
2268 }
2269
2270 int set_worktree_head_symref(const char *gitdir, const char *target, const char *logmsg)
2271 {
2272         /*
2273          * FIXME: this obviously will not work well for future refs
2274          * backends. This function needs to die.
2275          */
2276         struct files_ref_store *refs =
2277                 files_downcast(get_main_ref_store(),
2278                                REF_STORE_WRITE,
2279                                "set_head_symref");
2280
2281         static struct lock_file head_lock;
2282         struct ref_lock *lock;
2283         struct strbuf head_path = STRBUF_INIT;
2284         const char *head_rel;
2285         int ret;
2286
2287         strbuf_addf(&head_path, "%s/HEAD", absolute_path(gitdir));
2288         if (hold_lock_file_for_update(&head_lock, head_path.buf,
2289                                       LOCK_NO_DEREF) < 0) {
2290                 struct strbuf err = STRBUF_INIT;
2291                 unable_to_lock_message(head_path.buf, errno, &err);
2292                 error("%s", err.buf);
2293                 strbuf_release(&err);
2294                 strbuf_release(&head_path);
2295                 return -1;
2296         }
2297
2298         /* head_rel will be "HEAD" for the main tree, "worktrees/wt/HEAD" for
2299            linked trees */
2300         head_rel = remove_leading_path(head_path.buf,
2301                                        absolute_path(get_git_common_dir()));
2302         /* to make use of create_symref_locked(), initialize ref_lock */
2303         lock = xcalloc(1, sizeof(struct ref_lock));
2304         lock->lk = &head_lock;
2305         lock->ref_name = xstrdup(head_rel);
2306
2307         ret = create_symref_locked(refs, lock, head_rel, target, logmsg);
2308
2309         unlock_ref(lock); /* will free lock */
2310         strbuf_release(&head_path);
2311         return ret;
2312 }
2313
2314 static int files_reflog_exists(struct ref_store *ref_store,
2315                                const char *refname)
2316 {
2317         struct files_ref_store *refs =
2318                 files_downcast(ref_store, REF_STORE_READ, "reflog_exists");
2319         struct strbuf sb = STRBUF_INIT;
2320         struct stat st;
2321         int ret;
2322
2323         files_reflog_path(refs, &sb, refname);
2324         ret = !lstat(sb.buf, &st) && S_ISREG(st.st_mode);
2325         strbuf_release(&sb);
2326         return ret;
2327 }
2328
2329 static int files_delete_reflog(struct ref_store *ref_store,
2330                                const char *refname)
2331 {
2332         struct files_ref_store *refs =
2333                 files_downcast(ref_store, REF_STORE_WRITE, "delete_reflog");
2334         struct strbuf sb = STRBUF_INIT;
2335         int ret;
2336
2337         files_reflog_path(refs, &sb, refname);
2338         ret = remove_path(sb.buf);
2339         strbuf_release(&sb);
2340         return ret;
2341 }
2342
2343 static int show_one_reflog_ent(struct strbuf *sb, each_reflog_ent_fn fn, void *cb_data)
2344 {
2345         struct object_id ooid, noid;
2346         char *email_end, *message;
2347         unsigned long timestamp;
2348         int tz;
2349         const char *p = sb->buf;
2350
2351         /* old SP new SP name <email> SP time TAB msg LF */
2352         if (!sb->len || sb->buf[sb->len - 1] != '\n' ||
2353             parse_oid_hex(p, &ooid, &p) || *p++ != ' ' ||
2354             parse_oid_hex(p, &noid, &p) || *p++ != ' ' ||
2355             !(email_end = strchr(p, '>')) ||
2356             email_end[1] != ' ' ||
2357             !(timestamp = strtoul(email_end + 2, &message, 10)) ||
2358             !message || message[0] != ' ' ||
2359             (message[1] != '+' && message[1] != '-') ||
2360             !isdigit(message[2]) || !isdigit(message[3]) ||
2361             !isdigit(message[4]) || !isdigit(message[5]))
2362                 return 0; /* corrupt? */
2363         email_end[1] = '\0';
2364         tz = strtol(message + 1, NULL, 10);
2365         if (message[6] != '\t')
2366                 message += 6;
2367         else
2368                 message += 7;
2369         return fn(&ooid, &noid, p, timestamp, tz, message, cb_data);
2370 }
2371
2372 static char *find_beginning_of_line(char *bob, char *scan)
2373 {
2374         while (bob < scan && *(--scan) != '\n')
2375                 ; /* keep scanning backwards */
2376         /*
2377          * Return either beginning of the buffer, or LF at the end of
2378          * the previous line.
2379          */
2380         return scan;
2381 }
2382
2383 static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
2384                                              const char *refname,
2385                                              each_reflog_ent_fn fn,
2386                                              void *cb_data)
2387 {
2388         struct files_ref_store *refs =
2389                 files_downcast(ref_store, REF_STORE_READ,
2390                                "for_each_reflog_ent_reverse");
2391         struct strbuf sb = STRBUF_INIT;
2392         FILE *logfp;
2393         long pos;
2394         int ret = 0, at_tail = 1;
2395
2396         files_reflog_path(refs, &sb, refname);
2397         logfp = fopen(sb.buf, "r");
2398         strbuf_release(&sb);
2399         if (!logfp)
2400                 return -1;
2401
2402         /* Jump to the end */
2403         if (fseek(logfp, 0, SEEK_END) < 0)
2404                 return error("cannot seek back reflog for %s: %s",
2405                              refname, strerror(errno));
2406         pos = ftell(logfp);
2407         while (!ret && 0 < pos) {
2408                 int cnt;
2409                 size_t nread;
2410                 char buf[BUFSIZ];
2411                 char *endp, *scanp;
2412
2413                 /* Fill next block from the end */
2414                 cnt = (sizeof(buf) < pos) ? sizeof(buf) : pos;
2415                 if (fseek(logfp, pos - cnt, SEEK_SET))
2416                         return error("cannot seek back reflog for %s: %s",
2417                                      refname, strerror(errno));
2418                 nread = fread(buf, cnt, 1, logfp);
2419                 if (nread != 1)
2420                         return error("cannot read %d bytes from reflog for %s: %s",
2421                                      cnt, refname, strerror(errno));
2422                 pos -= cnt;
2423
2424                 scanp = endp = buf + cnt;
2425                 if (at_tail && scanp[-1] == '\n')
2426                         /* Looking at the final LF at the end of the file */
2427                         scanp--;
2428                 at_tail = 0;
2429
2430                 while (buf < scanp) {
2431                         /*
2432                          * terminating LF of the previous line, or the beginning
2433                          * of the buffer.
2434                          */
2435                         char *bp;
2436
2437                         bp = find_beginning_of_line(buf, scanp);
2438
2439                         if (*bp == '\n') {
2440                                 /*
2441                                  * The newline is the end of the previous line,
2442                                  * so we know we have complete line starting
2443                                  * at (bp + 1). Prefix it onto any prior data
2444                                  * we collected for the line and process it.
2445                                  */
2446                                 strbuf_splice(&sb, 0, 0, bp + 1, endp - (bp + 1));
2447                                 scanp = bp;
2448                                 endp = bp + 1;
2449                                 ret = show_one_reflog_ent(&sb, fn, cb_data);
2450                                 strbuf_reset(&sb);
2451                                 if (ret)
2452                                         break;
2453                         } else if (!pos) {
2454                                 /*
2455                                  * We are at the start of the buffer, and the
2456                                  * start of the file; there is no previous
2457                                  * line, and we have everything for this one.
2458                                  * Process it, and we can end the loop.
2459                                  */
2460                                 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2461                                 ret = show_one_reflog_ent(&sb, fn, cb_data);
2462                                 strbuf_reset(&sb);
2463                                 break;
2464                         }
2465
2466                         if (bp == buf) {
2467                                 /*
2468                                  * We are at the start of the buffer, and there
2469                                  * is more file to read backwards. Which means
2470                                  * we are in the middle of a line. Note that we
2471                                  * may get here even if *bp was a newline; that
2472                                  * just means we are at the exact end of the
2473                                  * previous line, rather than some spot in the
2474                                  * middle.
2475                                  *
2476                                  * Save away what we have to be combined with
2477                                  * the data from the next read.
2478                                  */
2479                                 strbuf_splice(&sb, 0, 0, buf, endp - buf);
2480                                 break;
2481                         }
2482                 }
2483
2484         }
2485         if (!ret && sb.len)
2486                 die("BUG: reverse reflog parser had leftover data");
2487
2488         fclose(logfp);
2489         strbuf_release(&sb);
2490         return ret;
2491 }
2492
2493 static int files_for_each_reflog_ent(struct ref_store *ref_store,
2494                                      const char *refname,
2495                                      each_reflog_ent_fn fn, void *cb_data)
2496 {
2497         struct files_ref_store *refs =
2498                 files_downcast(ref_store, REF_STORE_READ,
2499                                "for_each_reflog_ent");
2500         FILE *logfp;
2501         struct strbuf sb = STRBUF_INIT;
2502         int ret = 0;
2503
2504         files_reflog_path(refs, &sb, refname);
2505         logfp = fopen(sb.buf, "r");
2506         strbuf_release(&sb);
2507         if (!logfp)
2508                 return -1;
2509
2510         while (!ret && !strbuf_getwholeline(&sb, logfp, '\n'))
2511                 ret = show_one_reflog_ent(&sb, fn, cb_data);
2512         fclose(logfp);
2513         strbuf_release(&sb);
2514         return ret;
2515 }
2516
2517 struct files_reflog_iterator {
2518         struct ref_iterator base;
2519
2520         struct ref_store *ref_store;
2521         struct dir_iterator *dir_iterator;
2522         struct object_id oid;
2523 };
2524
2525 static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
2526 {
2527         struct files_reflog_iterator *iter =
2528                 (struct files_reflog_iterator *)ref_iterator;
2529         struct dir_iterator *diter = iter->dir_iterator;
2530         int ok;
2531
2532         while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
2533                 int flags;
2534
2535                 if (!S_ISREG(diter->st.st_mode))
2536                         continue;
2537                 if (diter->basename[0] == '.')
2538                         continue;
2539                 if (ends_with(diter->basename, ".lock"))
2540                         continue;
2541
2542                 if (refs_read_ref_full(iter->ref_store,
2543                                        diter->relative_path, 0,
2544                                        iter->oid.hash, &flags)) {
2545                         error("bad ref for %s", diter->path.buf);
2546                         continue;
2547                 }
2548
2549                 iter->base.refname = diter->relative_path;
2550                 iter->base.oid = &iter->oid;
2551                 iter->base.flags = flags;
2552                 return ITER_OK;
2553         }
2554
2555         iter->dir_iterator = NULL;
2556         if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
2557                 ok = ITER_ERROR;
2558         return ok;
2559 }
2560
2561 static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
2562                                    struct object_id *peeled)
2563 {
2564         die("BUG: ref_iterator_peel() called for reflog_iterator");
2565 }
2566
2567 static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
2568 {
2569         struct files_reflog_iterator *iter =
2570                 (struct files_reflog_iterator *)ref_iterator;
2571         int ok = ITER_DONE;
2572
2573         if (iter->dir_iterator)
2574                 ok = dir_iterator_abort(iter->dir_iterator);
2575
2576         base_ref_iterator_free(ref_iterator);
2577         return ok;
2578 }
2579
2580 static struct ref_iterator_vtable files_reflog_iterator_vtable = {
2581         files_reflog_iterator_advance,
2582         files_reflog_iterator_peel,
2583         files_reflog_iterator_abort
2584 };
2585
2586 static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
2587 {
2588         struct files_ref_store *refs =
2589                 files_downcast(ref_store, REF_STORE_READ,
2590                                "reflog_iterator_begin");
2591         struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2592         struct ref_iterator *ref_iterator = &iter->base;
2593         struct strbuf sb = STRBUF_INIT;
2594
2595         base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
2596         files_reflog_path(refs, &sb, NULL);
2597         iter->dir_iterator = dir_iterator_begin(sb.buf);
2598         iter->ref_store = ref_store;
2599         strbuf_release(&sb);
2600         return ref_iterator;
2601 }
2602
2603 static int ref_update_reject_duplicates(struct string_list *refnames,
2604                                         struct strbuf *err)
2605 {
2606         int i, n = refnames->nr;
2607
2608         assert(err);
2609
2610         for (i = 1; i < n; i++)
2611                 if (!strcmp(refnames->items[i - 1].string, refnames->items[i].string)) {
2612                         strbuf_addf(err,
2613                                     "multiple updates for ref '%s' not allowed.",
2614                                     refnames->items[i].string);
2615                         return 1;
2616                 }
2617         return 0;
2618 }
2619
2620 /*
2621  * If update is a direct update of head_ref (the reference pointed to
2622  * by HEAD), then add an extra REF_LOG_ONLY update for HEAD.
2623  */
2624 static int split_head_update(struct ref_update *update,
2625                              struct ref_transaction *transaction,
2626                              const char *head_ref,
2627                              struct string_list *affected_refnames,
2628                              struct strbuf *err)
2629 {
2630         struct string_list_item *item;
2631         struct ref_update *new_update;
2632
2633         if ((update->flags & REF_LOG_ONLY) ||
2634             (update->flags & REF_ISPRUNING) ||
2635             (update->flags & REF_UPDATE_VIA_HEAD))
2636                 return 0;
2637
2638         if (strcmp(update->refname, head_ref))
2639                 return 0;
2640
2641         /*
2642          * First make sure that HEAD is not already in the
2643          * transaction. This insertion is O(N) in the transaction
2644          * size, but it happens at most once per transaction.
2645          */
2646         item = string_list_insert(affected_refnames, "HEAD");
2647         if (item->util) {
2648                 /* An entry already existed */
2649                 strbuf_addf(err,
2650                             "multiple updates for 'HEAD' (including one "
2651                             "via its referent '%s') are not allowed",
2652                             update->refname);
2653                 return TRANSACTION_NAME_CONFLICT;
2654         }
2655
2656         new_update = ref_transaction_add_update(
2657                         transaction, "HEAD",
2658                         update->flags | REF_LOG_ONLY | REF_NODEREF,
2659                         update->new_sha1, update->old_sha1,
2660                         update->msg);
2661
2662         item->util = new_update;
2663
2664         return 0;
2665 }
2666
2667 /*
2668  * update is for a symref that points at referent and doesn't have
2669  * REF_NODEREF set. Split it into two updates:
2670  * - The original update, but with REF_LOG_ONLY and REF_NODEREF set
2671  * - A new, separate update for the referent reference
2672  * Note that the new update will itself be subject to splitting when
2673  * the iteration gets to it.
2674  */
2675 static int split_symref_update(struct files_ref_store *refs,
2676                                struct ref_update *update,
2677                                const char *referent,
2678                                struct ref_transaction *transaction,
2679                                struct string_list *affected_refnames,
2680                                struct strbuf *err)
2681 {
2682         struct string_list_item *item;
2683         struct ref_update *new_update;
2684         unsigned int new_flags;
2685
2686         /*
2687          * First make sure that referent is not already in the
2688          * transaction. This insertion is O(N) in the transaction
2689          * size, but it happens at most once per symref in a
2690          * transaction.
2691          */
2692         item = string_list_insert(affected_refnames, referent);
2693         if (item->util) {
2694                 /* An entry already existed */
2695                 strbuf_addf(err,
2696                             "multiple updates for '%s' (including one "
2697                             "via symref '%s') are not allowed",
2698                             referent, update->refname);
2699                 return TRANSACTION_NAME_CONFLICT;
2700         }
2701
2702         new_flags = update->flags;
2703         if (!strcmp(update->refname, "HEAD")) {
2704                 /*
2705                  * Record that the new update came via HEAD, so that
2706                  * when we process it, split_head_update() doesn't try
2707                  * to add another reflog update for HEAD. Note that
2708                  * this bit will be propagated if the new_update
2709                  * itself needs to be split.
2710                  */
2711                 new_flags |= REF_UPDATE_VIA_HEAD;
2712         }
2713
2714         new_update = ref_transaction_add_update(
2715                         transaction, referent, new_flags,
2716                         update->new_sha1, update->old_sha1,
2717                         update->msg);
2718
2719         new_update->parent_update = update;
2720
2721         /*
2722          * Change the symbolic ref update to log only. Also, it
2723          * doesn't need to check its old SHA-1 value, as that will be
2724          * done when new_update is processed.
2725          */
2726         update->flags |= REF_LOG_ONLY | REF_NODEREF;
2727         update->flags &= ~REF_HAVE_OLD;
2728
2729         item->util = new_update;
2730
2731         return 0;
2732 }
2733
2734 /*
2735  * Return the refname under which update was originally requested.
2736  */
2737 static const char *original_update_refname(struct ref_update *update)
2738 {
2739         while (update->parent_update)
2740                 update = update->parent_update;
2741
2742         return update->refname;
2743 }
2744
2745 /*
2746  * Check whether the REF_HAVE_OLD and old_oid values stored in update
2747  * are consistent with oid, which is the reference's current value. If
2748  * everything is OK, return 0; otherwise, write an error message to
2749  * err and return -1.
2750  */
2751 static int check_old_oid(struct ref_update *update, struct object_id *oid,
2752                          struct strbuf *err)
2753 {
2754         if (!(update->flags & REF_HAVE_OLD) ||
2755                    !hashcmp(oid->hash, update->old_sha1))
2756                 return 0;
2757
2758         if (is_null_sha1(update->old_sha1))
2759                 strbuf_addf(err, "cannot lock ref '%s': "
2760                             "reference already exists",
2761                             original_update_refname(update));
2762         else if (is_null_oid(oid))
2763                 strbuf_addf(err, "cannot lock ref '%s': "
2764                             "reference is missing but expected %s",
2765                             original_update_refname(update),
2766                             sha1_to_hex(update->old_sha1));
2767         else
2768                 strbuf_addf(err, "cannot lock ref '%s': "
2769                             "is at %s but expected %s",
2770                             original_update_refname(update),
2771                             oid_to_hex(oid),
2772                             sha1_to_hex(update->old_sha1));
2773
2774         return -1;
2775 }
2776
2777 /*
2778  * Prepare for carrying out update:
2779  * - Lock the reference referred to by update.
2780  * - Read the reference under lock.
2781  * - Check that its old SHA-1 value (if specified) is correct, and in
2782  *   any case record it in update->lock->old_oid for later use when
2783  *   writing the reflog.
2784  * - If it is a symref update without REF_NODEREF, split it up into a
2785  *   REF_LOG_ONLY update of the symref and add a separate update for
2786  *   the referent to transaction.
2787  * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
2788  *   update of HEAD.
2789  */
2790 static int lock_ref_for_update(struct files_ref_store *refs,
2791                                struct ref_update *update,
2792                                struct ref_transaction *transaction,
2793                                const char *head_ref,
2794                                struct string_list *affected_refnames,
2795                                struct strbuf *err)
2796 {
2797         struct strbuf referent = STRBUF_INIT;
2798         int mustexist = (update->flags & REF_HAVE_OLD) &&
2799                 !is_null_sha1(update->old_sha1);
2800         int ret;
2801         struct ref_lock *lock;
2802
2803         files_assert_main_repository(refs, "lock_ref_for_update");
2804
2805         if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
2806                 update->flags |= REF_DELETING;
2807
2808         if (head_ref) {
2809                 ret = split_head_update(update, transaction, head_ref,
2810                                         affected_refnames, err);
2811                 if (ret)
2812                         return ret;
2813         }
2814
2815         ret = lock_raw_ref(refs, update->refname, mustexist,
2816                            affected_refnames, NULL,
2817                            &lock, &referent,
2818                            &update->type, err);
2819         if (ret) {
2820                 char *reason;
2821
2822                 reason = strbuf_detach(err, NULL);
2823                 strbuf_addf(err, "cannot lock ref '%s': %s",
2824                             original_update_refname(update), reason);
2825                 free(reason);
2826                 return ret;
2827         }
2828
2829         update->backend_data = lock;
2830
2831         if (update->type & REF_ISSYMREF) {
2832                 if (update->flags & REF_NODEREF) {
2833                         /*
2834                          * We won't be reading the referent as part of
2835                          * the transaction, so we have to read it here
2836                          * to record and possibly check old_sha1:
2837                          */
2838                         if (refs_read_ref_full(&refs->base,
2839                                                referent.buf, 0,
2840                                                lock->old_oid.hash, NULL)) {
2841                                 if (update->flags & REF_HAVE_OLD) {
2842                                         strbuf_addf(err, "cannot lock ref '%s': "
2843                                                     "error reading reference",
2844                                                     original_update_refname(update));
2845                                         return -1;
2846                                 }
2847                         } else if (check_old_oid(update, &lock->old_oid, err)) {
2848                                 return TRANSACTION_GENERIC_ERROR;
2849                         }
2850                 } else {
2851                         /*
2852                          * Create a new update for the reference this
2853                          * symref is pointing at. Also, we will record
2854                          * and verify old_sha1 for this update as part
2855                          * of processing the split-off update, so we
2856                          * don't have to do it here.
2857                          */
2858                         ret = split_symref_update(refs, update,
2859                                                   referent.buf, transaction,
2860                                                   affected_refnames, err);
2861                         if (ret)
2862                                 return ret;
2863                 }
2864         } else {
2865                 struct ref_update *parent_update;
2866
2867                 if (check_old_oid(update, &lock->old_oid, err))
2868                         return TRANSACTION_GENERIC_ERROR;
2869
2870                 /*
2871                  * If this update is happening indirectly because of a
2872                  * symref update, record the old SHA-1 in the parent
2873                  * update:
2874                  */
2875                 for (parent_update = update->parent_update;
2876                      parent_update;
2877                      parent_update = parent_update->parent_update) {
2878                         struct ref_lock *parent_lock = parent_update->backend_data;
2879                         oidcpy(&parent_lock->old_oid, &lock->old_oid);
2880                 }
2881         }
2882
2883         if ((update->flags & REF_HAVE_NEW) &&
2884             !(update->flags & REF_DELETING) &&
2885             !(update->flags & REF_LOG_ONLY)) {
2886                 if (!(update->type & REF_ISSYMREF) &&
2887                     !hashcmp(lock->old_oid.hash, update->new_sha1)) {
2888                         /*
2889                          * The reference already has the desired
2890                          * value, so we don't need to write it.
2891                          */
2892                 } else if (write_ref_to_lockfile(lock, update->new_sha1,
2893                                                  err)) {
2894                         char *write_err = strbuf_detach(err, NULL);
2895
2896                         /*
2897                          * The lock was freed upon failure of
2898                          * write_ref_to_lockfile():
2899                          */
2900                         update->backend_data = NULL;
2901                         strbuf_addf(err,
2902                                     "cannot update ref '%s': %s",
2903                                     update->refname, write_err);
2904                         free(write_err);
2905                         return TRANSACTION_GENERIC_ERROR;
2906                 } else {
2907                         update->flags |= REF_NEEDS_COMMIT;
2908                 }
2909         }
2910         if (!(update->flags & REF_NEEDS_COMMIT)) {
2911                 /*
2912                  * We didn't call write_ref_to_lockfile(), so
2913                  * the lockfile is still open. Close it to
2914                  * free up the file descriptor:
2915                  */
2916                 if (close_ref(lock)) {
2917                         strbuf_addf(err, "couldn't close '%s.lock'",
2918                                     update->refname);
2919                         return TRANSACTION_GENERIC_ERROR;
2920                 }
2921         }
2922         return 0;
2923 }
2924
2925 static int files_transaction_commit(struct ref_store *ref_store,
2926                                     struct ref_transaction *transaction,
2927                                     struct strbuf *err)
2928 {
2929         struct files_ref_store *refs =
2930                 files_downcast(ref_store, REF_STORE_WRITE,
2931                                "ref_transaction_commit");
2932         int ret = 0, i;
2933         struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
2934         struct string_list_item *ref_to_delete;
2935         struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
2936         char *head_ref = NULL;
2937         int head_type;
2938         struct object_id head_oid;
2939         struct strbuf sb = STRBUF_INIT;
2940
2941         assert(err);
2942
2943         if (transaction->state != REF_TRANSACTION_OPEN)
2944                 die("BUG: commit called for transaction that is not open");
2945
2946         if (!transaction->nr) {
2947                 transaction->state = REF_TRANSACTION_CLOSED;
2948                 return 0;
2949         }
2950
2951         /*
2952          * Fail if a refname appears more than once in the
2953          * transaction. (If we end up splitting up any updates using
2954          * split_symref_update() or split_head_update(), those
2955          * functions will check that the new updates don't have the
2956          * same refname as any existing ones.)
2957          */
2958         for (i = 0; i < transaction->nr; i++) {
2959                 struct ref_update *update = transaction->updates[i];
2960                 struct string_list_item *item =
2961                         string_list_append(&affected_refnames, update->refname);
2962
2963                 /*
2964                  * We store a pointer to update in item->util, but at
2965                  * the moment we never use the value of this field
2966                  * except to check whether it is non-NULL.
2967                  */
2968                 item->util = update;
2969         }
2970         string_list_sort(&affected_refnames);
2971         if (ref_update_reject_duplicates(&affected_refnames, err)) {
2972                 ret = TRANSACTION_GENERIC_ERROR;
2973                 goto cleanup;
2974         }
2975
2976         /*
2977          * Special hack: If a branch is updated directly and HEAD
2978          * points to it (may happen on the remote side of a push
2979          * for example) then logically the HEAD reflog should be
2980          * updated too.
2981          *
2982          * A generic solution would require reverse symref lookups,
2983          * but finding all symrefs pointing to a given branch would be
2984          * rather costly for this rare event (the direct update of a
2985          * branch) to be worth it. So let's cheat and check with HEAD
2986          * only, which should cover 99% of all usage scenarios (even
2987          * 100% of the default ones).
2988          *
2989          * So if HEAD is a symbolic reference, then record the name of
2990          * the reference that it points to. If we see an update of
2991          * head_ref within the transaction, then split_head_update()
2992          * arranges for the reflog of HEAD to be updated, too.
2993          */
2994         head_ref = refs_resolve_refdup(ref_store, "HEAD",
2995                                        RESOLVE_REF_NO_RECURSE,
2996                                        head_oid.hash, &head_type);
2997
2998         if (head_ref && !(head_type & REF_ISSYMREF)) {
2999                 free(head_ref);
3000                 head_ref = NULL;
3001         }
3002
3003         /*
3004          * Acquire all locks, verify old values if provided, check
3005          * that new values are valid, and write new values to the
3006          * lockfiles, ready to be activated. Only keep one lockfile
3007          * open at a time to avoid running out of file descriptors.
3008          */
3009         for (i = 0; i < transaction->nr; i++) {
3010                 struct ref_update *update = transaction->updates[i];
3011
3012                 ret = lock_ref_for_update(refs, update, transaction,
3013                                           head_ref, &affected_refnames, err);
3014                 if (ret)
3015                         goto cleanup;
3016         }
3017
3018         /* Perform updates first so live commits remain referenced */
3019         for (i = 0; i < transaction->nr; i++) {
3020                 struct ref_update *update = transaction->updates[i];
3021                 struct ref_lock *lock = update->backend_data;
3022
3023                 if (update->flags & REF_NEEDS_COMMIT ||
3024                     update->flags & REF_LOG_ONLY) {
3025                         if (files_log_ref_write(refs,
3026                                                 lock->ref_name,
3027                                                 lock->old_oid.hash,
3028                                                 update->new_sha1,
3029                                                 update->msg, update->flags,
3030                                                 err)) {
3031                                 char *old_msg = strbuf_detach(err, NULL);
3032
3033                                 strbuf_addf(err, "cannot update the ref '%s': %s",
3034                                             lock->ref_name, old_msg);
3035                                 free(old_msg);
3036                                 unlock_ref(lock);
3037                                 update->backend_data = NULL;
3038                                 ret = TRANSACTION_GENERIC_ERROR;
3039                                 goto cleanup;
3040                         }
3041                 }
3042                 if (update->flags & REF_NEEDS_COMMIT) {
3043                         clear_loose_ref_cache(refs);
3044                         if (commit_ref(lock)) {
3045                                 strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
3046                                 unlock_ref(lock);
3047                                 update->backend_data = NULL;
3048                                 ret = TRANSACTION_GENERIC_ERROR;
3049                                 goto cleanup;
3050                         }
3051                 }
3052         }
3053         /* Perform deletes now that updates are safely completed */
3054         for (i = 0; i < transaction->nr; i++) {
3055                 struct ref_update *update = transaction->updates[i];
3056                 struct ref_lock *lock = update->backend_data;
3057
3058                 if (update->flags & REF_DELETING &&
3059                     !(update->flags & REF_LOG_ONLY)) {
3060                         if (!(update->type & REF_ISPACKED) ||
3061                             update->type & REF_ISSYMREF) {
3062                                 /* It is a loose reference. */
3063                                 strbuf_reset(&sb);
3064                                 files_ref_path(refs, &sb, lock->ref_name);
3065                                 if (unlink_or_msg(sb.buf, err)) {
3066                                         ret = TRANSACTION_GENERIC_ERROR;
3067                                         goto cleanup;
3068                                 }
3069                                 update->flags |= REF_DELETED_LOOSE;
3070                         }
3071
3072                         if (!(update->flags & REF_ISPRUNING))
3073                                 string_list_append(&refs_to_delete,
3074                                                    lock->ref_name);
3075                 }
3076         }
3077
3078         if (repack_without_refs(refs, &refs_to_delete, err)) {
3079                 ret = TRANSACTION_GENERIC_ERROR;
3080                 goto cleanup;
3081         }
3082
3083         /* Delete the reflogs of any references that were deleted: */
3084         for_each_string_list_item(ref_to_delete, &refs_to_delete) {
3085                 strbuf_reset(&sb);
3086                 files_reflog_path(refs, &sb, ref_to_delete->string);
3087                 if (!unlink_or_warn(sb.buf))
3088                         try_remove_empty_parents(refs, ref_to_delete->string,
3089                                                  REMOVE_EMPTY_PARENTS_REFLOG);
3090         }
3091
3092         clear_loose_ref_cache(refs);
3093
3094 cleanup:
3095         strbuf_release(&sb);
3096         transaction->state = REF_TRANSACTION_CLOSED;
3097
3098         for (i = 0; i < transaction->nr; i++) {
3099                 struct ref_update *update = transaction->updates[i];
3100                 struct ref_lock *lock = update->backend_data;
3101
3102                 if (lock)
3103                         unlock_ref(lock);
3104
3105                 if (update->flags & REF_DELETED_LOOSE) {
3106                         /*
3107                          * The loose reference was deleted. Delete any
3108                          * empty parent directories. (Note that this
3109                          * can only work because we have already
3110                          * removed the lockfile.)
3111                          */
3112                         try_remove_empty_parents(refs, update->refname,
3113                                                  REMOVE_EMPTY_PARENTS_REF);
3114                 }
3115         }
3116
3117         string_list_clear(&refs_to_delete, 0);
3118         free(head_ref);
3119         string_list_clear(&affected_refnames, 0);
3120
3121         return ret;
3122 }
3123
3124 static int ref_present(const char *refname,
3125                        const struct object_id *oid, int flags, void *cb_data)
3126 {
3127         struct string_list *affected_refnames = cb_data;
3128
3129         return string_list_has_string(affected_refnames, refname);
3130 }
3131
3132 static int files_initial_transaction_commit(struct ref_store *ref_store,
3133                                             struct ref_transaction *transaction,
3134                                             struct strbuf *err)
3135 {
3136         struct files_ref_store *refs =
3137                 files_downcast(ref_store, REF_STORE_WRITE,
3138                                "initial_ref_transaction_commit");
3139         int ret = 0, i;
3140         struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
3141
3142         assert(err);
3143
3144         if (transaction->state != REF_TRANSACTION_OPEN)
3145                 die("BUG: commit called for transaction that is not open");
3146
3147         /* Fail if a refname appears more than once in the transaction: */
3148         for (i = 0; i < transaction->nr; i++)
3149                 string_list_append(&affected_refnames,
3150                                    transaction->updates[i]->refname);
3151         string_list_sort(&affected_refnames);
3152         if (ref_update_reject_duplicates(&affected_refnames, err)) {
3153                 ret = TRANSACTION_GENERIC_ERROR;
3154                 goto cleanup;
3155         }
3156
3157         /*
3158          * It's really undefined to call this function in an active
3159          * repository or when there are existing references: we are
3160          * only locking and changing packed-refs, so (1) any
3161          * simultaneous processes might try to change a reference at
3162          * the same time we do, and (2) any existing loose versions of
3163          * the references that we are setting would have precedence
3164          * over our values. But some remote helpers create the remote
3165          * "HEAD" and "master" branches before calling this function,
3166          * so here we really only check that none of the references
3167          * that we are creating already exists.
3168          */
3169         if (refs_for_each_rawref(&refs->base, ref_present,
3170                                  &affected_refnames))
3171                 die("BUG: initial ref transaction called with existing refs");
3172
3173         for (i = 0; i < transaction->nr; i++) {
3174                 struct ref_update *update = transaction->updates[i];
3175
3176                 if ((update->flags & REF_HAVE_OLD) &&
3177                     !is_null_sha1(update->old_sha1))
3178                         die("BUG: initial ref transaction with old_sha1 set");
3179                 if (refs_verify_refname_available(&refs->base, update->refname,
3180                                                   &affected_refnames, NULL,
3181                                                   err)) {
3182                         ret = TRANSACTION_NAME_CONFLICT;
3183                         goto cleanup;
3184                 }
3185         }
3186
3187         if (lock_packed_refs(refs, 0)) {
3188                 strbuf_addf(err, "unable to lock packed-refs file: %s",
3189                             strerror(errno));
3190                 ret = TRANSACTION_GENERIC_ERROR;
3191                 goto cleanup;
3192         }
3193
3194         for (i = 0; i < transaction->nr; i++) {
3195                 struct ref_update *update = transaction->updates[i];
3196
3197                 if ((update->flags & REF_HAVE_NEW) &&
3198                     !is_null_sha1(update->new_sha1))
3199                         add_packed_ref(refs, update->refname, update->new_sha1);
3200         }
3201
3202         if (commit_packed_refs(refs)) {
3203                 strbuf_addf(err, "unable to commit packed-refs file: %s",
3204                             strerror(errno));
3205                 ret = TRANSACTION_GENERIC_ERROR;
3206                 goto cleanup;
3207         }
3208
3209 cleanup:
3210         transaction->state = REF_TRANSACTION_CLOSED;
3211         string_list_clear(&affected_refnames, 0);
3212         return ret;
3213 }
3214
3215 struct expire_reflog_cb {
3216         unsigned int flags;
3217         reflog_expiry_should_prune_fn *should_prune_fn;
3218         void *policy_cb;
3219         FILE *newlog;
3220         struct object_id last_kept_oid;
3221 };
3222
3223 static int expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
3224                              const char *email, unsigned long timestamp, int tz,
3225                              const char *message, void *cb_data)
3226 {
3227         struct expire_reflog_cb *cb = cb_data;
3228         struct expire_reflog_policy_cb *policy_cb = cb->policy_cb;
3229
3230         if (cb->flags & EXPIRE_REFLOGS_REWRITE)
3231                 ooid = &cb->last_kept_oid;
3232
3233         if ((*cb->should_prune_fn)(ooid->hash, noid->hash, email, timestamp, tz,
3234                                    message, policy_cb)) {
3235                 if (!cb->newlog)
3236                         printf("would prune %s", message);
3237                 else if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3238                         printf("prune %s", message);
3239         } else {
3240                 if (cb->newlog) {
3241                         fprintf(cb->newlog, "%s %s %s %lu %+05d\t%s",
3242                                 oid_to_hex(ooid), oid_to_hex(noid),
3243                                 email, timestamp, tz, message);
3244                         oidcpy(&cb->last_kept_oid, noid);
3245                 }
3246                 if (cb->flags & EXPIRE_REFLOGS_VERBOSE)
3247                         printf("keep %s", message);
3248         }
3249         return 0;
3250 }
3251
3252 static int files_reflog_expire(struct ref_store *ref_store,
3253                                const char *refname, const unsigned char *sha1,
3254                                unsigned int flags,
3255                                reflog_expiry_prepare_fn prepare_fn,
3256                                reflog_expiry_should_prune_fn should_prune_fn,
3257                                reflog_expiry_cleanup_fn cleanup_fn,
3258                                void *policy_cb_data)
3259 {
3260         struct files_ref_store *refs =
3261                 files_downcast(ref_store, REF_STORE_WRITE, "reflog_expire");
3262         static struct lock_file reflog_lock;
3263         struct expire_reflog_cb cb;
3264         struct ref_lock *lock;
3265         struct strbuf log_file_sb = STRBUF_INIT;
3266         char *log_file;
3267         int status = 0;
3268         int type;
3269         struct strbuf err = STRBUF_INIT;
3270
3271         memset(&cb, 0, sizeof(cb));
3272         cb.flags = flags;
3273         cb.policy_cb = policy_cb_data;
3274         cb.should_prune_fn = should_prune_fn;
3275
3276         /*
3277          * The reflog file is locked by holding the lock on the
3278          * reference itself, plus we might need to update the
3279          * reference if --updateref was specified:
3280          */
3281         lock = lock_ref_sha1_basic(refs, refname, sha1,
3282                                    NULL, NULL, REF_NODEREF,
3283                                    &type, &err);
3284         if (!lock) {
3285                 error("cannot lock ref '%s': %s", refname, err.buf);
3286                 strbuf_release(&err);
3287                 return -1;
3288         }
3289         if (!refs_reflog_exists(ref_store, refname)) {
3290                 unlock_ref(lock);
3291                 return 0;
3292         }
3293
3294         files_reflog_path(refs, &log_file_sb, refname);
3295         log_file = strbuf_detach(&log_file_sb, NULL);
3296         if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3297                 /*
3298                  * Even though holding $GIT_DIR/logs/$reflog.lock has
3299                  * no locking implications, we use the lock_file
3300                  * machinery here anyway because it does a lot of the
3301                  * work we need, including cleaning up if the program
3302                  * exits unexpectedly.
3303                  */
3304                 if (hold_lock_file_for_update(&reflog_lock, log_file, 0) < 0) {
3305                         struct strbuf err = STRBUF_INIT;
3306                         unable_to_lock_message(log_file, errno, &err);
3307                         error("%s", err.buf);
3308                         strbuf_release(&err);
3309                         goto failure;
3310                 }
3311                 cb.newlog = fdopen_lock_file(&reflog_lock, "w");
3312                 if (!cb.newlog) {
3313                         error("cannot fdopen %s (%s)",
3314                               get_lock_file_path(&reflog_lock), strerror(errno));
3315                         goto failure;
3316                 }
3317         }
3318
3319         (*prepare_fn)(refname, sha1, cb.policy_cb);
3320         refs_for_each_reflog_ent(ref_store, refname, expire_reflog_ent, &cb);
3321         (*cleanup_fn)(cb.policy_cb);
3322
3323         if (!(flags & EXPIRE_REFLOGS_DRY_RUN)) {
3324                 /*
3325                  * It doesn't make sense to adjust a reference pointed
3326                  * to by a symbolic ref based on expiring entries in
3327                  * the symbolic reference's reflog. Nor can we update
3328                  * a reference if there are no remaining reflog
3329                  * entries.
3330                  */
3331                 int update = (flags & EXPIRE_REFLOGS_UPDATE_REF) &&
3332                         !(type & REF_ISSYMREF) &&
3333                         !is_null_oid(&cb.last_kept_oid);
3334
3335                 if (close_lock_file(&reflog_lock)) {
3336                         status |= error("couldn't write %s: %s", log_file,
3337                                         strerror(errno));
3338                 } else if (update &&
3339                            (write_in_full(get_lock_file_fd(lock->lk),
3340                                 oid_to_hex(&cb.last_kept_oid), GIT_SHA1_HEXSZ) != GIT_SHA1_HEXSZ ||
3341                             write_str_in_full(get_lock_file_fd(lock->lk), "\n") != 1 ||
3342                             close_ref(lock) < 0)) {
3343                         status |= error("couldn't write %s",
3344                                         get_lock_file_path(lock->lk));
3345                         rollback_lock_file(&reflog_lock);
3346                 } else if (commit_lock_file(&reflog_lock)) {
3347                         status |= error("unable to write reflog '%s' (%s)",
3348                                         log_file, strerror(errno));
3349                 } else if (update && commit_ref(lock)) {
3350                         status |= error("couldn't set %s", lock->ref_name);
3351                 }
3352         }
3353         free(log_file);
3354         unlock_ref(lock);
3355         return status;
3356
3357  failure:
3358         rollback_lock_file(&reflog_lock);
3359         free(log_file);
3360         unlock_ref(lock);
3361         return -1;
3362 }
3363
3364 static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
3365 {
3366         struct files_ref_store *refs =
3367                 files_downcast(ref_store, REF_STORE_WRITE, "init_db");
3368         struct strbuf sb = STRBUF_INIT;
3369
3370         /*
3371          * Create .git/refs/{heads,tags}
3372          */
3373         files_ref_path(refs, &sb, "refs/heads");
3374         safe_create_dir(sb.buf, 1);
3375
3376         strbuf_reset(&sb);
3377         files_ref_path(refs, &sb, "refs/tags");
3378         safe_create_dir(sb.buf, 1);
3379
3380         strbuf_release(&sb);
3381         return 0;
3382 }
3383
3384 struct ref_storage_be refs_be_files = {
3385         NULL,
3386         "files",
3387         files_ref_store_create,
3388         files_init_db,
3389         files_transaction_commit,
3390         files_initial_transaction_commit,
3391
3392         files_pack_refs,
3393         files_peel_ref,
3394         files_create_symref,
3395         files_delete_refs,
3396         files_rename_ref,
3397
3398         files_ref_iterator_begin,
3399         files_read_raw_ref,
3400
3401         files_reflog_iterator_begin,
3402         files_for_each_reflog_ent,
3403         files_for_each_reflog_ent_reverse,
3404         files_reflog_exists,
3405         files_create_reflog,
3406         files_delete_reflog,
3407         files_reflog_expire
3408 };