Merge branch 'dl/merge-autostash'
[git] / builtin / reflog.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "lockfile.h"
4 #include "object-store.h"
5 #include "repository.h"
6 #include "commit.h"
7 #include "refs.h"
8 #include "dir.h"
9 #include "tree-walk.h"
10 #include "diff.h"
11 #include "revision.h"
12 #include "reachable.h"
13 #include "worktree.h"
14
15 /* NEEDSWORK: switch to using parse_options */
16 static const char reflog_expire_usage[] =
17 N_("git reflog expire [--expire=<time>] "
18    "[--expire-unreachable=<time>] "
19    "[--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] "
20    "[--verbose] [--all] <refs>...");
21 static const char reflog_delete_usage[] =
22 N_("git reflog delete [--rewrite] [--updateref] "
23    "[--dry-run | -n] [--verbose] <refs>...");
24 static const char reflog_exists_usage[] =
25 N_("git reflog exists <ref>");
26
27 static timestamp_t default_reflog_expire;
28 static timestamp_t default_reflog_expire_unreachable;
29
30 struct cmd_reflog_expire_cb {
31         struct rev_info revs;
32         int stalefix;
33         timestamp_t expire_total;
34         timestamp_t expire_unreachable;
35         int recno;
36 };
37
38 struct expire_reflog_policy_cb {
39         enum {
40                 UE_NORMAL,
41                 UE_ALWAYS,
42                 UE_HEAD
43         } unreachable_expire_kind;
44         struct commit_list *mark_list;
45         unsigned long mark_limit;
46         struct cmd_reflog_expire_cb cmd;
47         struct commit *tip_commit;
48         struct commit_list *tips;
49 };
50
51 struct collected_reflog {
52         struct object_id oid;
53         char reflog[FLEX_ARRAY];
54 };
55
56 struct collect_reflog_cb {
57         struct collected_reflog **e;
58         int alloc;
59         int nr;
60         struct worktree *wt;
61 };
62
63 /* Remember to update object flag allocation in object.h */
64 #define INCOMPLETE      (1u<<10)
65 #define STUDYING        (1u<<11)
66 #define REACHABLE       (1u<<12)
67
68 static int tree_is_complete(const struct object_id *oid)
69 {
70         struct tree_desc desc;
71         struct name_entry entry;
72         int complete;
73         struct tree *tree;
74
75         tree = lookup_tree(the_repository, oid);
76         if (!tree)
77                 return 0;
78         if (tree->object.flags & SEEN)
79                 return 1;
80         if (tree->object.flags & INCOMPLETE)
81                 return 0;
82
83         if (!tree->buffer) {
84                 enum object_type type;
85                 unsigned long size;
86                 void *data = read_object_file(oid, &type, &size);
87                 if (!data) {
88                         tree->object.flags |= INCOMPLETE;
89                         return 0;
90                 }
91                 tree->buffer = data;
92                 tree->size = size;
93         }
94         init_tree_desc(&desc, tree->buffer, tree->size);
95         complete = 1;
96         while (tree_entry(&desc, &entry)) {
97                 if (!has_object_file(&entry.oid) ||
98                     (S_ISDIR(entry.mode) && !tree_is_complete(&entry.oid))) {
99                         tree->object.flags |= INCOMPLETE;
100                         complete = 0;
101                 }
102         }
103         free_tree_buffer(tree);
104
105         if (complete)
106                 tree->object.flags |= SEEN;
107         return complete;
108 }
109
110 static int commit_is_complete(struct commit *commit)
111 {
112         struct object_array study;
113         struct object_array found;
114         int is_incomplete = 0;
115         int i;
116
117         /* early return */
118         if (commit->object.flags & SEEN)
119                 return 1;
120         if (commit->object.flags & INCOMPLETE)
121                 return 0;
122         /*
123          * Find all commits that are reachable and are not marked as
124          * SEEN.  Then make sure the trees and blobs contained are
125          * complete.  After that, mark these commits also as SEEN.
126          * If some of the objects that are needed to complete this
127          * commit are missing, mark this commit as INCOMPLETE.
128          */
129         memset(&study, 0, sizeof(study));
130         memset(&found, 0, sizeof(found));
131         add_object_array(&commit->object, NULL, &study);
132         add_object_array(&commit->object, NULL, &found);
133         commit->object.flags |= STUDYING;
134         while (study.nr) {
135                 struct commit *c;
136                 struct commit_list *parent;
137
138                 c = (struct commit *)object_array_pop(&study);
139                 if (!c->object.parsed && !parse_object(the_repository, &c->object.oid))
140                         c->object.flags |= INCOMPLETE;
141
142                 if (c->object.flags & INCOMPLETE) {
143                         is_incomplete = 1;
144                         break;
145                 }
146                 else if (c->object.flags & SEEN)
147                         continue;
148                 for (parent = c->parents; parent; parent = parent->next) {
149                         struct commit *p = parent->item;
150                         if (p->object.flags & STUDYING)
151                                 continue;
152                         p->object.flags |= STUDYING;
153                         add_object_array(&p->object, NULL, &study);
154                         add_object_array(&p->object, NULL, &found);
155                 }
156         }
157         if (!is_incomplete) {
158                 /*
159                  * make sure all commits in "found" array have all the
160                  * necessary objects.
161                  */
162                 for (i = 0; i < found.nr; i++) {
163                         struct commit *c =
164                                 (struct commit *)found.objects[i].item;
165                         if (!tree_is_complete(get_commit_tree_oid(c))) {
166                                 is_incomplete = 1;
167                                 c->object.flags |= INCOMPLETE;
168                         }
169                 }
170                 if (!is_incomplete) {
171                         /* mark all found commits as complete, iow SEEN */
172                         for (i = 0; i < found.nr; i++)
173                                 found.objects[i].item->flags |= SEEN;
174                 }
175         }
176         /* clear flags from the objects we traversed */
177         for (i = 0; i < found.nr; i++)
178                 found.objects[i].item->flags &= ~STUDYING;
179         if (is_incomplete)
180                 commit->object.flags |= INCOMPLETE;
181         else {
182                 /*
183                  * If we come here, we have (1) traversed the ancestry chain
184                  * from the "commit" until we reach SEEN commits (which are
185                  * known to be complete), and (2) made sure that the commits
186                  * encountered during the above traversal refer to trees that
187                  * are complete.  Which means that we know *all* the commits
188                  * we have seen during this process are complete.
189                  */
190                 for (i = 0; i < found.nr; i++)
191                         found.objects[i].item->flags |= SEEN;
192         }
193         /* free object arrays */
194         object_array_clear(&study);
195         object_array_clear(&found);
196         return !is_incomplete;
197 }
198
199 static int keep_entry(struct commit **it, struct object_id *oid)
200 {
201         struct commit *commit;
202
203         if (is_null_oid(oid))
204                 return 1;
205         commit = lookup_commit_reference_gently(the_repository, oid, 1);
206         if (!commit)
207                 return 0;
208
209         /*
210          * Make sure everything in this commit exists.
211          *
212          * We have walked all the objects reachable from the refs
213          * and cache earlier.  The commits reachable by this commit
214          * must meet SEEN commits -- and then we should mark them as
215          * SEEN as well.
216          */
217         if (!commit_is_complete(commit))
218                 return 0;
219         *it = commit;
220         return 1;
221 }
222
223 /*
224  * Starting from commits in the cb->mark_list, mark commits that are
225  * reachable from them.  Stop the traversal at commits older than
226  * the expire_limit and queue them back, so that the caller can call
227  * us again to restart the traversal with longer expire_limit.
228  */
229 static void mark_reachable(struct expire_reflog_policy_cb *cb)
230 {
231         struct commit_list *pending;
232         timestamp_t expire_limit = cb->mark_limit;
233         struct commit_list *leftover = NULL;
234
235         for (pending = cb->mark_list; pending; pending = pending->next)
236                 pending->item->object.flags &= ~REACHABLE;
237
238         pending = cb->mark_list;
239         while (pending) {
240                 struct commit_list *parent;
241                 struct commit *commit = pop_commit(&pending);
242                 if (commit->object.flags & REACHABLE)
243                         continue;
244                 if (parse_commit(commit))
245                         continue;
246                 commit->object.flags |= REACHABLE;
247                 if (commit->date < expire_limit) {
248                         commit_list_insert(commit, &leftover);
249                         continue;
250                 }
251                 commit->object.flags |= REACHABLE;
252                 parent = commit->parents;
253                 while (parent) {
254                         commit = parent->item;
255                         parent = parent->next;
256                         if (commit->object.flags & REACHABLE)
257                                 continue;
258                         commit_list_insert(commit, &pending);
259                 }
260         }
261         cb->mark_list = leftover;
262 }
263
264 static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid)
265 {
266         /*
267          * We may or may not have the commit yet - if not, look it
268          * up using the supplied sha1.
269          */
270         if (!commit) {
271                 if (is_null_oid(oid))
272                         return 0;
273
274                 commit = lookup_commit_reference_gently(the_repository, oid,
275                                                         1);
276
277                 /* Not a commit -- keep it */
278                 if (!commit)
279                         return 0;
280         }
281
282         /* Reachable from the current ref?  Don't prune. */
283         if (commit->object.flags & REACHABLE)
284                 return 0;
285
286         if (cb->mark_list && cb->mark_limit) {
287                 cb->mark_limit = 0; /* dig down to the root */
288                 mark_reachable(cb);
289         }
290
291         return !(commit->object.flags & REACHABLE);
292 }
293
294 /*
295  * Return true iff the specified reflog entry should be expired.
296  */
297 static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
298                                     const char *email, timestamp_t timestamp, int tz,
299                                     const char *message, void *cb_data)
300 {
301         struct expire_reflog_policy_cb *cb = cb_data;
302         struct commit *old_commit, *new_commit;
303
304         if (timestamp < cb->cmd.expire_total)
305                 return 1;
306
307         old_commit = new_commit = NULL;
308         if (cb->cmd.stalefix &&
309             (!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid)))
310                 return 1;
311
312         if (timestamp < cb->cmd.expire_unreachable) {
313                 if (cb->unreachable_expire_kind == UE_ALWAYS)
314                         return 1;
315                 if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
316                         return 1;
317         }
318
319         if (cb->cmd.recno && --(cb->cmd.recno) == 0)
320                 return 1;
321
322         return 0;
323 }
324
325 static int push_tip_to_list(const char *refname, const struct object_id *oid,
326                             int flags, void *cb_data)
327 {
328         struct commit_list **list = cb_data;
329         struct commit *tip_commit;
330         if (flags & REF_ISSYMREF)
331                 return 0;
332         tip_commit = lookup_commit_reference_gently(the_repository, oid, 1);
333         if (!tip_commit)
334                 return 0;
335         commit_list_insert(tip_commit, list);
336         return 0;
337 }
338
339 static int is_head(const char *refname)
340 {
341         switch (ref_type(refname)) {
342         case REF_TYPE_OTHER_PSEUDOREF:
343         case REF_TYPE_MAIN_PSEUDOREF:
344                 if (parse_worktree_ref(refname, NULL, NULL, &refname))
345                         BUG("not a worktree ref: %s", refname);
346                 break;
347         default:
348                 break;
349         }
350         return !strcmp(refname, "HEAD");
351 }
352
353 static void reflog_expiry_prepare(const char *refname,
354                                   const struct object_id *oid,
355                                   void *cb_data)
356 {
357         struct expire_reflog_policy_cb *cb = cb_data;
358
359         if (!cb->cmd.expire_unreachable || is_head(refname)) {
360                 cb->tip_commit = NULL;
361                 cb->unreachable_expire_kind = UE_HEAD;
362         } else {
363                 cb->tip_commit = lookup_commit_reference_gently(the_repository,
364                                                                 oid, 1);
365                 if (!cb->tip_commit)
366                         cb->unreachable_expire_kind = UE_ALWAYS;
367                 else
368                         cb->unreachable_expire_kind = UE_NORMAL;
369         }
370
371         if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
372                 cb->unreachable_expire_kind = UE_ALWAYS;
373
374         cb->mark_list = NULL;
375         cb->tips = NULL;
376         if (cb->unreachable_expire_kind != UE_ALWAYS) {
377                 if (cb->unreachable_expire_kind == UE_HEAD) {
378                         struct commit_list *elem;
379
380                         for_each_ref(push_tip_to_list, &cb->tips);
381                         for (elem = cb->tips; elem; elem = elem->next)
382                                 commit_list_insert(elem->item, &cb->mark_list);
383                 } else {
384                         commit_list_insert(cb->tip_commit, &cb->mark_list);
385                 }
386                 cb->mark_limit = cb->cmd.expire_total;
387                 mark_reachable(cb);
388         }
389 }
390
391 static void reflog_expiry_cleanup(void *cb_data)
392 {
393         struct expire_reflog_policy_cb *cb = cb_data;
394
395         if (cb->unreachable_expire_kind != UE_ALWAYS) {
396                 if (cb->unreachable_expire_kind == UE_HEAD) {
397                         struct commit_list *elem;
398                         for (elem = cb->tips; elem; elem = elem->next)
399                                 clear_commit_marks(elem->item, REACHABLE);
400                         free_commit_list(cb->tips);
401                 } else {
402                         clear_commit_marks(cb->tip_commit, REACHABLE);
403                 }
404         }
405 }
406
407 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
408 {
409         struct collected_reflog *e;
410         struct collect_reflog_cb *cb = cb_data;
411         struct strbuf newref = STRBUF_INIT;
412
413         /*
414          * Avoid collecting the same shared ref multiple times because
415          * they are available via all worktrees.
416          */
417         if (!cb->wt->is_current && ref_type(ref) == REF_TYPE_NORMAL)
418                 return 0;
419
420         strbuf_worktree_ref(cb->wt, &newref, ref);
421         FLEX_ALLOC_STR(e, reflog, newref.buf);
422         strbuf_release(&newref);
423
424         oidcpy(&e->oid, oid);
425         ALLOC_GROW(cb->e, cb->nr + 1, cb->alloc);
426         cb->e[cb->nr++] = e;
427         return 0;
428 }
429
430 static struct reflog_expire_cfg {
431         struct reflog_expire_cfg *next;
432         timestamp_t expire_total;
433         timestamp_t expire_unreachable;
434         char pattern[FLEX_ARRAY];
435 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
436
437 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
438 {
439         struct reflog_expire_cfg *ent;
440
441         if (!reflog_expire_cfg_tail)
442                 reflog_expire_cfg_tail = &reflog_expire_cfg;
443
444         for (ent = reflog_expire_cfg; ent; ent = ent->next)
445                 if (!strncmp(ent->pattern, pattern, len) &&
446                     ent->pattern[len] == '\0')
447                         return ent;
448
449         FLEX_ALLOC_MEM(ent, pattern, pattern, len);
450         *reflog_expire_cfg_tail = ent;
451         reflog_expire_cfg_tail = &(ent->next);
452         return ent;
453 }
454
455 /* expiry timer slot */
456 #define EXPIRE_TOTAL   01
457 #define EXPIRE_UNREACH 02
458
459 static int reflog_expire_config(const char *var, const char *value, void *cb)
460 {
461         const char *pattern, *key;
462         size_t pattern_len;
463         timestamp_t expire;
464         int slot;
465         struct reflog_expire_cfg *ent;
466
467         if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
468                 return git_default_config(var, value, cb);
469
470         if (!strcmp(key, "reflogexpire")) {
471                 slot = EXPIRE_TOTAL;
472                 if (git_config_expiry_date(&expire, var, value))
473                         return -1;
474         } else if (!strcmp(key, "reflogexpireunreachable")) {
475                 slot = EXPIRE_UNREACH;
476                 if (git_config_expiry_date(&expire, var, value))
477                         return -1;
478         } else
479                 return git_default_config(var, value, cb);
480
481         if (!pattern) {
482                 switch (slot) {
483                 case EXPIRE_TOTAL:
484                         default_reflog_expire = expire;
485                         break;
486                 case EXPIRE_UNREACH:
487                         default_reflog_expire_unreachable = expire;
488                         break;
489                 }
490                 return 0;
491         }
492
493         ent = find_cfg_ent(pattern, pattern_len);
494         if (!ent)
495                 return -1;
496         switch (slot) {
497         case EXPIRE_TOTAL:
498                 ent->expire_total = expire;
499                 break;
500         case EXPIRE_UNREACH:
501                 ent->expire_unreachable = expire;
502                 break;
503         }
504         return 0;
505 }
506
507 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
508 {
509         struct reflog_expire_cfg *ent;
510
511         if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
512                 return; /* both given explicitly -- nothing to tweak */
513
514         for (ent = reflog_expire_cfg; ent; ent = ent->next) {
515                 if (!wildmatch(ent->pattern, ref, 0)) {
516                         if (!(slot & EXPIRE_TOTAL))
517                                 cb->expire_total = ent->expire_total;
518                         if (!(slot & EXPIRE_UNREACH))
519                                 cb->expire_unreachable = ent->expire_unreachable;
520                         return;
521                 }
522         }
523
524         /*
525          * If unconfigured, make stash never expire
526          */
527         if (!strcmp(ref, "refs/stash")) {
528                 if (!(slot & EXPIRE_TOTAL))
529                         cb->expire_total = 0;
530                 if (!(slot & EXPIRE_UNREACH))
531                         cb->expire_unreachable = 0;
532                 return;
533         }
534
535         /* Nothing matched -- use the default value */
536         if (!(slot & EXPIRE_TOTAL))
537                 cb->expire_total = default_reflog_expire;
538         if (!(slot & EXPIRE_UNREACH))
539                 cb->expire_unreachable = default_reflog_expire_unreachable;
540 }
541
542 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
543 {
544         struct expire_reflog_policy_cb cb;
545         timestamp_t now = time(NULL);
546         int i, status, do_all, all_worktrees = 1;
547         int explicit_expiry = 0;
548         unsigned int flags = 0;
549
550         default_reflog_expire_unreachable = now - 30 * 24 * 3600;
551         default_reflog_expire = now - 90 * 24 * 3600;
552         git_config(reflog_expire_config, NULL);
553
554         save_commit_buffer = 0;
555         do_all = status = 0;
556         memset(&cb, 0, sizeof(cb));
557
558         cb.cmd.expire_total = default_reflog_expire;
559         cb.cmd.expire_unreachable = default_reflog_expire_unreachable;
560
561         for (i = 1; i < argc; i++) {
562                 const char *arg = argv[i];
563
564                 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
565                         flags |= EXPIRE_REFLOGS_DRY_RUN;
566                 else if (skip_prefix(arg, "--expire=", &arg)) {
567                         if (parse_expiry_date(arg, &cb.cmd.expire_total))
568                                 die(_("'%s' is not a valid timestamp"), arg);
569                         explicit_expiry |= EXPIRE_TOTAL;
570                 }
571                 else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
572                         if (parse_expiry_date(arg, &cb.cmd.expire_unreachable))
573                                 die(_("'%s' is not a valid timestamp"), arg);
574                         explicit_expiry |= EXPIRE_UNREACH;
575                 }
576                 else if (!strcmp(arg, "--stale-fix"))
577                         cb.cmd.stalefix = 1;
578                 else if (!strcmp(arg, "--rewrite"))
579                         flags |= EXPIRE_REFLOGS_REWRITE;
580                 else if (!strcmp(arg, "--updateref"))
581                         flags |= EXPIRE_REFLOGS_UPDATE_REF;
582                 else if (!strcmp(arg, "--all"))
583                         do_all = 1;
584                 else if (!strcmp(arg, "--single-worktree"))
585                         all_worktrees = 0;
586                 else if (!strcmp(arg, "--verbose"))
587                         flags |= EXPIRE_REFLOGS_VERBOSE;
588                 else if (!strcmp(arg, "--")) {
589                         i++;
590                         break;
591                 }
592                 else if (arg[0] == '-')
593                         usage(_(reflog_expire_usage));
594                 else
595                         break;
596         }
597
598         /*
599          * We can trust the commits and objects reachable from refs
600          * even in older repository.  We cannot trust what's reachable
601          * from reflog if the repository was pruned with older git.
602          */
603         if (cb.cmd.stalefix) {
604                 repo_init_revisions(the_repository, &cb.cmd.revs, prefix);
605                 if (flags & EXPIRE_REFLOGS_VERBOSE)
606                         printf(_("Marking reachable objects..."));
607                 mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
608                 if (flags & EXPIRE_REFLOGS_VERBOSE)
609                         putchar('\n');
610         }
611
612         if (do_all) {
613                 struct collect_reflog_cb collected;
614                 struct worktree **worktrees, **p;
615                 int i;
616
617                 memset(&collected, 0, sizeof(collected));
618                 worktrees = get_worktrees(0);
619                 for (p = worktrees; *p; p++) {
620                         if (!all_worktrees && !(*p)->is_current)
621                                 continue;
622                         collected.wt = *p;
623                         refs_for_each_reflog(get_worktree_ref_store(*p),
624                                              collect_reflog, &collected);
625                 }
626                 free_worktrees(worktrees);
627                 for (i = 0; i < collected.nr; i++) {
628                         struct collected_reflog *e = collected.e[i];
629                         set_reflog_expiry_param(&cb.cmd, explicit_expiry, e->reflog);
630                         status |= reflog_expire(e->reflog, &e->oid, flags,
631                                                 reflog_expiry_prepare,
632                                                 should_expire_reflog_ent,
633                                                 reflog_expiry_cleanup,
634                                                 &cb);
635                         free(e);
636                 }
637                 free(collected.e);
638         }
639
640         for (; i < argc; i++) {
641                 char *ref;
642                 struct object_id oid;
643                 if (!dwim_log(argv[i], strlen(argv[i]), &oid, &ref)) {
644                         status |= error(_("%s points nowhere!"), argv[i]);
645                         continue;
646                 }
647                 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
648                 status |= reflog_expire(ref, &oid, flags,
649                                         reflog_expiry_prepare,
650                                         should_expire_reflog_ent,
651                                         reflog_expiry_cleanup,
652                                         &cb);
653         }
654         return status;
655 }
656
657 static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
658                 const char *email, timestamp_t timestamp, int tz,
659                 const char *message, void *cb_data)
660 {
661         struct expire_reflog_policy_cb *cb = cb_data;
662         if (!cb->cmd.expire_total || timestamp < cb->cmd.expire_total)
663                 cb->cmd.recno++;
664         return 0;
665 }
666
667 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
668 {
669         struct expire_reflog_policy_cb cb;
670         int i, status = 0;
671         unsigned int flags = 0;
672
673         memset(&cb, 0, sizeof(cb));
674
675         for (i = 1; i < argc; i++) {
676                 const char *arg = argv[i];
677                 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
678                         flags |= EXPIRE_REFLOGS_DRY_RUN;
679                 else if (!strcmp(arg, "--rewrite"))
680                         flags |= EXPIRE_REFLOGS_REWRITE;
681                 else if (!strcmp(arg, "--updateref"))
682                         flags |= EXPIRE_REFLOGS_UPDATE_REF;
683                 else if (!strcmp(arg, "--verbose"))
684                         flags |= EXPIRE_REFLOGS_VERBOSE;
685                 else if (!strcmp(arg, "--")) {
686                         i++;
687                         break;
688                 }
689                 else if (arg[0] == '-')
690                         usage(_(reflog_delete_usage));
691                 else
692                         break;
693         }
694
695         if (argc - i < 1)
696                 return error(_("no reflog specified to delete"));
697
698         for ( ; i < argc; i++) {
699                 const char *spec = strstr(argv[i], "@{");
700                 struct object_id oid;
701                 char *ep, *ref;
702                 int recno;
703
704                 if (!spec) {
705                         status |= error(_("not a reflog: %s"), argv[i]);
706                         continue;
707                 }
708
709                 if (!dwim_log(argv[i], spec - argv[i], &oid, &ref)) {
710                         status |= error(_("no reflog for '%s'"), argv[i]);
711                         continue;
712                 }
713
714                 recno = strtoul(spec + 2, &ep, 10);
715                 if (*ep == '}') {
716                         cb.cmd.recno = -recno;
717                         for_each_reflog_ent(ref, count_reflog_ent, &cb);
718                 } else {
719                         cb.cmd.expire_total = approxidate(spec + 2);
720                         for_each_reflog_ent(ref, count_reflog_ent, &cb);
721                         cb.cmd.expire_total = 0;
722                 }
723
724                 status |= reflog_expire(ref, &oid, flags,
725                                         reflog_expiry_prepare,
726                                         should_expire_reflog_ent,
727                                         reflog_expiry_cleanup,
728                                         &cb);
729                 free(ref);
730         }
731         return status;
732 }
733
734 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
735 {
736         int i, start = 0;
737
738         for (i = 1; i < argc; i++) {
739                 const char *arg = argv[i];
740                 if (!strcmp(arg, "--")) {
741                         i++;
742                         break;
743                 }
744                 else if (arg[0] == '-')
745                         usage(_(reflog_exists_usage));
746                 else
747                         break;
748         }
749
750         start = i;
751
752         if (argc - start != 1)
753                 usage(_(reflog_exists_usage));
754
755         if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
756                 die(_("invalid ref format: %s"), argv[start]);
757         return !reflog_exists(argv[start]);
758 }
759
760 /*
761  * main "reflog"
762  */
763
764 static const char reflog_usage[] =
765 N_("git reflog [ show | expire | delete | exists ]");
766
767 int cmd_reflog(int argc, const char **argv, const char *prefix)
768 {
769         if (argc > 1 && !strcmp(argv[1], "-h"))
770                 usage(_(reflog_usage));
771
772         /* With no command, we default to showing it. */
773         if (argc < 2 || *argv[1] == '-')
774                 return cmd_log_reflog(argc, argv, prefix);
775
776         if (!strcmp(argv[1], "show"))
777                 return cmd_log_reflog(argc - 1, argv + 1, prefix);
778
779         if (!strcmp(argv[1], "expire"))
780                 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
781
782         if (!strcmp(argv[1], "delete"))
783                 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
784
785         if (!strcmp(argv[1], "exists"))
786                 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
787
788         return cmd_log_reflog(argc, argv, prefix);
789 }