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