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