Merge branch 'ab/checkout-branch-info-leakfix'
[git/debian.git] / builtin / reflog.c
bloba4b1dd27e13c93c670489f11c7c20b980952ba2a
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"
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>");
27 static timestamp_t default_reflog_expire;
28 static timestamp_t default_reflog_expire_unreachable;
30 struct cmd_reflog_expire_cb {
31 int stalefix;
32 timestamp_t expire_total;
33 timestamp_t expire_unreachable;
34 int recno;
37 struct expire_reflog_policy_cb {
38 enum {
39 UE_NORMAL,
40 UE_ALWAYS,
41 UE_HEAD
42 } unreachable_expire_kind;
43 struct commit_list *mark_list;
44 unsigned long mark_limit;
45 struct cmd_reflog_expire_cb cmd;
46 struct commit *tip_commit;
47 struct commit_list *tips;
48 unsigned int dry_run:1;
51 struct worktree_reflogs {
52 struct worktree *worktree;
53 struct string_list reflogs;
56 /* Remember to update object flag allocation in object.h */
57 #define INCOMPLETE (1u<<10)
58 #define STUDYING (1u<<11)
59 #define REACHABLE (1u<<12)
61 static int tree_is_complete(const struct object_id *oid)
63 struct tree_desc desc;
64 struct name_entry entry;
65 int complete;
66 struct tree *tree;
68 tree = lookup_tree(the_repository, oid);
69 if (!tree)
70 return 0;
71 if (tree->object.flags & SEEN)
72 return 1;
73 if (tree->object.flags & INCOMPLETE)
74 return 0;
76 if (!tree->buffer) {
77 enum object_type type;
78 unsigned long size;
79 void *data = read_object_file(oid, &type, &size);
80 if (!data) {
81 tree->object.flags |= INCOMPLETE;
82 return 0;
84 tree->buffer = data;
85 tree->size = size;
87 init_tree_desc(&desc, tree->buffer, tree->size);
88 complete = 1;
89 while (tree_entry(&desc, &entry)) {
90 if (!has_object_file(&entry.oid) ||
91 (S_ISDIR(entry.mode) && !tree_is_complete(&entry.oid))) {
92 tree->object.flags |= INCOMPLETE;
93 complete = 0;
96 free_tree_buffer(tree);
98 if (complete)
99 tree->object.flags |= SEEN;
100 return complete;
103 static int commit_is_complete(struct commit *commit)
105 struct object_array study;
106 struct object_array found;
107 int is_incomplete = 0;
108 int i;
110 /* early return */
111 if (commit->object.flags & SEEN)
112 return 1;
113 if (commit->object.flags & INCOMPLETE)
114 return 0;
116 * Find all commits that are reachable and are not marked as
117 * SEEN. Then make sure the trees and blobs contained are
118 * complete. After that, mark these commits also as SEEN.
119 * If some of the objects that are needed to complete this
120 * commit are missing, mark this commit as INCOMPLETE.
122 memset(&study, 0, sizeof(study));
123 memset(&found, 0, sizeof(found));
124 add_object_array(&commit->object, NULL, &study);
125 add_object_array(&commit->object, NULL, &found);
126 commit->object.flags |= STUDYING;
127 while (study.nr) {
128 struct commit *c;
129 struct commit_list *parent;
131 c = (struct commit *)object_array_pop(&study);
132 if (!c->object.parsed && !parse_object(the_repository, &c->object.oid))
133 c->object.flags |= INCOMPLETE;
135 if (c->object.flags & INCOMPLETE) {
136 is_incomplete = 1;
137 break;
139 else if (c->object.flags & SEEN)
140 continue;
141 for (parent = c->parents; parent; parent = parent->next) {
142 struct commit *p = parent->item;
143 if (p->object.flags & STUDYING)
144 continue;
145 p->object.flags |= STUDYING;
146 add_object_array(&p->object, NULL, &study);
147 add_object_array(&p->object, NULL, &found);
150 if (!is_incomplete) {
152 * make sure all commits in "found" array have all the
153 * necessary objects.
155 for (i = 0; i < found.nr; i++) {
156 struct commit *c =
157 (struct commit *)found.objects[i].item;
158 if (!tree_is_complete(get_commit_tree_oid(c))) {
159 is_incomplete = 1;
160 c->object.flags |= INCOMPLETE;
163 if (!is_incomplete) {
164 /* mark all found commits as complete, iow SEEN */
165 for (i = 0; i < found.nr; i++)
166 found.objects[i].item->flags |= SEEN;
169 /* clear flags from the objects we traversed */
170 for (i = 0; i < found.nr; i++)
171 found.objects[i].item->flags &= ~STUDYING;
172 if (is_incomplete)
173 commit->object.flags |= INCOMPLETE;
174 else {
176 * If we come here, we have (1) traversed the ancestry chain
177 * from the "commit" until we reach SEEN commits (which are
178 * known to be complete), and (2) made sure that the commits
179 * encountered during the above traversal refer to trees that
180 * are complete. Which means that we know *all* the commits
181 * we have seen during this process are complete.
183 for (i = 0; i < found.nr; i++)
184 found.objects[i].item->flags |= SEEN;
186 /* free object arrays */
187 object_array_clear(&study);
188 object_array_clear(&found);
189 return !is_incomplete;
192 static int keep_entry(struct commit **it, struct object_id *oid)
194 struct commit *commit;
196 if (is_null_oid(oid))
197 return 1;
198 commit = lookup_commit_reference_gently(the_repository, oid, 1);
199 if (!commit)
200 return 0;
203 * Make sure everything in this commit exists.
205 * We have walked all the objects reachable from the refs
206 * and cache earlier. The commits reachable by this commit
207 * must meet SEEN commits -- and then we should mark them as
208 * SEEN as well.
210 if (!commit_is_complete(commit))
211 return 0;
212 *it = commit;
213 return 1;
217 * Starting from commits in the cb->mark_list, mark commits that are
218 * reachable from them. Stop the traversal at commits older than
219 * the expire_limit and queue them back, so that the caller can call
220 * us again to restart the traversal with longer expire_limit.
222 static void mark_reachable(struct expire_reflog_policy_cb *cb)
224 struct commit_list *pending;
225 timestamp_t expire_limit = cb->mark_limit;
226 struct commit_list *leftover = NULL;
228 for (pending = cb->mark_list; pending; pending = pending->next)
229 pending->item->object.flags &= ~REACHABLE;
231 pending = cb->mark_list;
232 while (pending) {
233 struct commit_list *parent;
234 struct commit *commit = pop_commit(&pending);
235 if (commit->object.flags & REACHABLE)
236 continue;
237 if (parse_commit(commit))
238 continue;
239 commit->object.flags |= REACHABLE;
240 if (commit->date < expire_limit) {
241 commit_list_insert(commit, &leftover);
242 continue;
244 commit->object.flags |= REACHABLE;
245 parent = commit->parents;
246 while (parent) {
247 commit = parent->item;
248 parent = parent->next;
249 if (commit->object.flags & REACHABLE)
250 continue;
251 commit_list_insert(commit, &pending);
254 cb->mark_list = leftover;
257 static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid)
260 * We may or may not have the commit yet - if not, look it
261 * up using the supplied sha1.
263 if (!commit) {
264 if (is_null_oid(oid))
265 return 0;
267 commit = lookup_commit_reference_gently(the_repository, oid,
270 /* Not a commit -- keep it */
271 if (!commit)
272 return 0;
275 /* Reachable from the current ref? Don't prune. */
276 if (commit->object.flags & REACHABLE)
277 return 0;
279 if (cb->mark_list && cb->mark_limit) {
280 cb->mark_limit = 0; /* dig down to the root */
281 mark_reachable(cb);
284 return !(commit->object.flags & REACHABLE);
288 * Return true iff the specified reflog entry should be expired.
290 static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid,
291 const char *email, timestamp_t timestamp, int tz,
292 const char *message, void *cb_data)
294 struct expire_reflog_policy_cb *cb = cb_data;
295 struct commit *old_commit, *new_commit;
297 if (timestamp < cb->cmd.expire_total)
298 return 1;
300 old_commit = new_commit = NULL;
301 if (cb->cmd.stalefix &&
302 (!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid)))
303 return 1;
305 if (timestamp < cb->cmd.expire_unreachable) {
306 switch (cb->unreachable_expire_kind) {
307 case UE_ALWAYS:
308 return 1;
309 case UE_NORMAL:
310 case UE_HEAD:
311 if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid))
312 return 1;
313 break;
317 if (cb->cmd.recno && --(cb->cmd.recno) == 0)
318 return 1;
320 return 0;
323 static int should_expire_reflog_ent_verbose(struct object_id *ooid,
324 struct object_id *noid,
325 const char *email,
326 timestamp_t timestamp, int tz,
327 const char *message, void *cb_data)
329 struct expire_reflog_policy_cb *cb = cb_data;
330 int expire;
332 expire = should_expire_reflog_ent(ooid, noid, email, timestamp, tz,
333 message, cb);
335 if (!expire)
336 printf("keep %s", message);
337 else if (cb->dry_run)
338 printf("would prune %s", message);
339 else
340 printf("prune %s", message);
342 return expire;
345 static int push_tip_to_list(const char *refname, const struct object_id *oid,
346 int flags, void *cb_data)
348 struct commit_list **list = cb_data;
349 struct commit *tip_commit;
350 if (flags & REF_ISSYMREF)
351 return 0;
352 tip_commit = lookup_commit_reference_gently(the_repository, oid, 1);
353 if (!tip_commit)
354 return 0;
355 commit_list_insert(tip_commit, list);
356 return 0;
359 static int is_head(const char *refname)
361 switch (ref_type(refname)) {
362 case REF_TYPE_OTHER_PSEUDOREF:
363 case REF_TYPE_MAIN_PSEUDOREF:
364 if (parse_worktree_ref(refname, NULL, NULL, &refname))
365 BUG("not a worktree ref: %s", refname);
366 break;
367 default:
368 break;
370 return !strcmp(refname, "HEAD");
373 static void reflog_expiry_prepare(const char *refname,
374 const struct object_id *oid,
375 void *cb_data)
377 struct expire_reflog_policy_cb *cb = cb_data;
378 struct commit_list *elem;
379 struct commit *commit = NULL;
381 if (!cb->cmd.expire_unreachable || is_head(refname)) {
382 cb->unreachable_expire_kind = UE_HEAD;
383 } else {
384 commit = lookup_commit(the_repository, oid);
385 cb->unreachable_expire_kind = commit ? UE_NORMAL : UE_ALWAYS;
388 if (cb->cmd.expire_unreachable <= cb->cmd.expire_total)
389 cb->unreachable_expire_kind = UE_ALWAYS;
391 switch (cb->unreachable_expire_kind) {
392 case UE_ALWAYS:
393 return;
394 case UE_HEAD:
395 for_each_ref(push_tip_to_list, &cb->tips);
396 for (elem = cb->tips; elem; elem = elem->next)
397 commit_list_insert(elem->item, &cb->mark_list);
398 break;
399 case UE_NORMAL:
400 commit_list_insert(commit, &cb->mark_list);
401 /* For reflog_expiry_cleanup() below */
402 cb->tip_commit = commit;
404 cb->mark_limit = cb->cmd.expire_total;
405 mark_reachable(cb);
408 static void reflog_expiry_cleanup(void *cb_data)
410 struct expire_reflog_policy_cb *cb = cb_data;
411 struct commit_list *elem;
413 switch (cb->unreachable_expire_kind) {
414 case UE_ALWAYS:
415 return;
416 case UE_HEAD:
417 for (elem = cb->tips; elem; elem = elem->next)
418 clear_commit_marks(elem->item, REACHABLE);
419 free_commit_list(cb->tips);
420 break;
421 case UE_NORMAL:
422 clear_commit_marks(cb->tip_commit, REACHABLE);
423 break;
427 static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data)
429 struct worktree_reflogs *cb = cb_data;
430 struct worktree *worktree = cb->worktree;
431 struct strbuf newref = STRBUF_INIT;
434 * Avoid collecting the same shared ref multiple times because
435 * they are available via all worktrees.
437 if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
438 return 0;
440 strbuf_worktree_ref(worktree, &newref, ref);
441 string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL));
443 return 0;
446 static struct reflog_expire_cfg {
447 struct reflog_expire_cfg *next;
448 timestamp_t expire_total;
449 timestamp_t expire_unreachable;
450 char pattern[FLEX_ARRAY];
451 } *reflog_expire_cfg, **reflog_expire_cfg_tail;
453 static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
455 struct reflog_expire_cfg *ent;
457 if (!reflog_expire_cfg_tail)
458 reflog_expire_cfg_tail = &reflog_expire_cfg;
460 for (ent = reflog_expire_cfg; ent; ent = ent->next)
461 if (!strncmp(ent->pattern, pattern, len) &&
462 ent->pattern[len] == '\0')
463 return ent;
465 FLEX_ALLOC_MEM(ent, pattern, pattern, len);
466 *reflog_expire_cfg_tail = ent;
467 reflog_expire_cfg_tail = &(ent->next);
468 return ent;
471 /* expiry timer slot */
472 #define EXPIRE_TOTAL 01
473 #define EXPIRE_UNREACH 02
475 static int reflog_expire_config(const char *var, const char *value, void *cb)
477 const char *pattern, *key;
478 size_t pattern_len;
479 timestamp_t expire;
480 int slot;
481 struct reflog_expire_cfg *ent;
483 if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0)
484 return git_default_config(var, value, cb);
486 if (!strcmp(key, "reflogexpire")) {
487 slot = EXPIRE_TOTAL;
488 if (git_config_expiry_date(&expire, var, value))
489 return -1;
490 } else if (!strcmp(key, "reflogexpireunreachable")) {
491 slot = EXPIRE_UNREACH;
492 if (git_config_expiry_date(&expire, var, value))
493 return -1;
494 } else
495 return git_default_config(var, value, cb);
497 if (!pattern) {
498 switch (slot) {
499 case EXPIRE_TOTAL:
500 default_reflog_expire = expire;
501 break;
502 case EXPIRE_UNREACH:
503 default_reflog_expire_unreachable = expire;
504 break;
506 return 0;
509 ent = find_cfg_ent(pattern, pattern_len);
510 if (!ent)
511 return -1;
512 switch (slot) {
513 case EXPIRE_TOTAL:
514 ent->expire_total = expire;
515 break;
516 case EXPIRE_UNREACH:
517 ent->expire_unreachable = expire;
518 break;
520 return 0;
523 static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref)
525 struct reflog_expire_cfg *ent;
527 if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH))
528 return; /* both given explicitly -- nothing to tweak */
530 for (ent = reflog_expire_cfg; ent; ent = ent->next) {
531 if (!wildmatch(ent->pattern, ref, 0)) {
532 if (!(slot & EXPIRE_TOTAL))
533 cb->expire_total = ent->expire_total;
534 if (!(slot & EXPIRE_UNREACH))
535 cb->expire_unreachable = ent->expire_unreachable;
536 return;
541 * If unconfigured, make stash never expire
543 if (!strcmp(ref, "refs/stash")) {
544 if (!(slot & EXPIRE_TOTAL))
545 cb->expire_total = 0;
546 if (!(slot & EXPIRE_UNREACH))
547 cb->expire_unreachable = 0;
548 return;
551 /* Nothing matched -- use the default value */
552 if (!(slot & EXPIRE_TOTAL))
553 cb->expire_total = default_reflog_expire;
554 if (!(slot & EXPIRE_UNREACH))
555 cb->expire_unreachable = default_reflog_expire_unreachable;
558 static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
560 struct cmd_reflog_expire_cb cmd = { 0 };
561 timestamp_t now = time(NULL);
562 int i, status, do_all, all_worktrees = 1;
563 int explicit_expiry = 0;
564 unsigned int flags = 0;
565 int verbose = 0;
566 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
568 default_reflog_expire_unreachable = now - 30 * 24 * 3600;
569 default_reflog_expire = now - 90 * 24 * 3600;
570 git_config(reflog_expire_config, NULL);
572 save_commit_buffer = 0;
573 do_all = status = 0;
575 cmd.expire_total = default_reflog_expire;
576 cmd.expire_unreachable = default_reflog_expire_unreachable;
578 for (i = 1; i < argc; i++) {
579 const char *arg = argv[i];
581 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
582 flags |= EXPIRE_REFLOGS_DRY_RUN;
583 else if (skip_prefix(arg, "--expire=", &arg)) {
584 if (parse_expiry_date(arg, &cmd.expire_total))
585 die(_("'%s' is not a valid timestamp"), arg);
586 explicit_expiry |= EXPIRE_TOTAL;
588 else if (skip_prefix(arg, "--expire-unreachable=", &arg)) {
589 if (parse_expiry_date(arg, &cmd.expire_unreachable))
590 die(_("'%s' is not a valid timestamp"), arg);
591 explicit_expiry |= EXPIRE_UNREACH;
593 else if (!strcmp(arg, "--stale-fix"))
594 cmd.stalefix = 1;
595 else if (!strcmp(arg, "--rewrite"))
596 flags |= EXPIRE_REFLOGS_REWRITE;
597 else if (!strcmp(arg, "--updateref"))
598 flags |= EXPIRE_REFLOGS_UPDATE_REF;
599 else if (!strcmp(arg, "--all"))
600 do_all = 1;
601 else if (!strcmp(arg, "--single-worktree"))
602 all_worktrees = 0;
603 else if (!strcmp(arg, "--verbose"))
604 verbose = 1;
605 else if (!strcmp(arg, "--")) {
606 i++;
607 break;
609 else if (arg[0] == '-')
610 usage(_(reflog_expire_usage));
611 else
612 break;
615 if (verbose)
616 should_prune_fn = should_expire_reflog_ent_verbose;
619 * We can trust the commits and objects reachable from refs
620 * even in older repository. We cannot trust what's reachable
621 * from reflog if the repository was pruned with older git.
623 if (cmd.stalefix) {
624 struct rev_info revs;
626 repo_init_revisions(the_repository, &revs, prefix);
627 revs.do_not_die_on_missing_tree = 1;
628 revs.ignore_missing = 1;
629 revs.ignore_missing_links = 1;
630 if (verbose)
631 printf(_("Marking reachable objects..."));
632 mark_reachable_objects(&revs, 0, 0, NULL);
633 if (verbose)
634 putchar('\n');
637 if (do_all) {
638 struct worktree_reflogs collected = {
639 .reflogs = STRING_LIST_INIT_DUP,
641 struct string_list_item *item;
642 struct worktree **worktrees, **p;
644 worktrees = get_worktrees();
645 for (p = worktrees; *p; p++) {
646 if (!all_worktrees && !(*p)->is_current)
647 continue;
648 collected.worktree = *p;
649 refs_for_each_reflog(get_worktree_ref_store(*p),
650 collect_reflog, &collected);
652 free_worktrees(worktrees);
654 for_each_string_list_item(item, &collected.reflogs) {
655 struct expire_reflog_policy_cb cb = {
656 .cmd = cmd,
657 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
660 set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string);
661 status |= reflog_expire(item->string, flags,
662 reflog_expiry_prepare,
663 should_prune_fn,
664 reflog_expiry_cleanup,
665 &cb);
667 string_list_clear(&collected.reflogs, 0);
670 for (; i < argc; i++) {
671 char *ref;
672 struct expire_reflog_policy_cb cb = { .cmd = cmd };
674 if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) {
675 status |= error(_("%s points nowhere!"), argv[i]);
676 continue;
678 set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref);
679 status |= reflog_expire(ref, flags,
680 reflog_expiry_prepare,
681 should_prune_fn,
682 reflog_expiry_cleanup,
683 &cb);
684 free(ref);
686 return status;
689 static int count_reflog_ent(struct object_id *ooid, struct object_id *noid,
690 const char *email, timestamp_t timestamp, int tz,
691 const char *message, void *cb_data)
693 struct cmd_reflog_expire_cb *cb = cb_data;
694 if (!cb->expire_total || timestamp < cb->expire_total)
695 cb->recno++;
696 return 0;
699 static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
701 struct cmd_reflog_expire_cb cmd = { 0 };
702 int i, status = 0;
703 unsigned int flags = 0;
704 int verbose = 0;
705 reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent;
707 for (i = 1; i < argc; i++) {
708 const char *arg = argv[i];
709 if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n"))
710 flags |= EXPIRE_REFLOGS_DRY_RUN;
711 else if (!strcmp(arg, "--rewrite"))
712 flags |= EXPIRE_REFLOGS_REWRITE;
713 else if (!strcmp(arg, "--updateref"))
714 flags |= EXPIRE_REFLOGS_UPDATE_REF;
715 else if (!strcmp(arg, "--verbose"))
716 verbose = 1;
717 else if (!strcmp(arg, "--")) {
718 i++;
719 break;
721 else if (arg[0] == '-')
722 usage(_(reflog_delete_usage));
723 else
724 break;
727 if (verbose)
728 should_prune_fn = should_expire_reflog_ent_verbose;
730 if (argc - i < 1)
731 return error(_("no reflog specified to delete"));
733 for ( ; i < argc; i++) {
734 const char *spec = strstr(argv[i], "@{");
735 char *ep, *ref;
736 int recno;
737 struct expire_reflog_policy_cb cb = {
738 .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN),
741 if (!spec) {
742 status |= error(_("not a reflog: %s"), argv[i]);
743 continue;
746 if (!dwim_log(argv[i], spec - argv[i], NULL, &ref)) {
747 status |= error(_("no reflog for '%s'"), argv[i]);
748 continue;
751 recno = strtoul(spec + 2, &ep, 10);
752 if (*ep == '}') {
753 cmd.recno = -recno;
754 for_each_reflog_ent(ref, count_reflog_ent, &cmd);
755 } else {
756 cmd.expire_total = approxidate(spec + 2);
757 for_each_reflog_ent(ref, count_reflog_ent, &cmd);
758 cmd.expire_total = 0;
761 cb.cmd = cmd;
762 status |= reflog_expire(ref, flags,
763 reflog_expiry_prepare,
764 should_prune_fn,
765 reflog_expiry_cleanup,
766 &cb);
767 free(ref);
769 return status;
772 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
774 int i, start = 0;
776 for (i = 1; i < argc; i++) {
777 const char *arg = argv[i];
778 if (!strcmp(arg, "--")) {
779 i++;
780 break;
782 else if (arg[0] == '-')
783 usage(_(reflog_exists_usage));
784 else
785 break;
788 start = i;
790 if (argc - start != 1)
791 usage(_(reflog_exists_usage));
793 if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
794 die(_("invalid ref format: %s"), argv[start]);
795 return !reflog_exists(argv[start]);
799 * main "reflog"
802 static const char reflog_usage[] =
803 N_("git reflog [ show | expire | delete | exists ]");
805 int cmd_reflog(int argc, const char **argv, const char *prefix)
807 if (argc > 1 && !strcmp(argv[1], "-h"))
808 usage(_(reflog_usage));
810 /* With no command, we default to showing it. */
811 if (argc < 2 || *argv[1] == '-')
812 return cmd_log_reflog(argc, argv, prefix);
814 if (!strcmp(argv[1], "show"))
815 return cmd_log_reflog(argc - 1, argv + 1, prefix);
817 if (!strcmp(argv[1], "expire"))
818 return cmd_reflog_expire(argc - 1, argv + 1, prefix);
820 if (!strcmp(argv[1], "delete"))
821 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
823 if (!strcmp(argv[1], "exists"))
824 return cmd_reflog_exists(argc - 1, argv + 1, prefix);
826 return cmd_log_reflog(argc, argv, prefix);