git-reflog: add exists command

This is necessary because alternate ref backends might store reflogs somewhere other than .git/logs. Code that now directly manipulates .git/logs should instead go through git-reflog. Signed-off-by: David Turner <dturner@twopensource.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Jul 21, 2015 at 17:04 UTC afcb2e7a3b855887e79d1ee6d70ec80ab6456a64
3 files changed +41 -1
Documentation/git-reflog.txt
+4
@@ -23,6 +23,7 @@ depending on the subcommand:
23 [--dry-run] [--verbose] [--all | <refs>...]
24 'git reflog delete' [--rewrite] [--updateref]
25 [--dry-run] [--verbose] ref@\{specifier\}...
26 +'git reflog exists' <ref>
27
28 Reference logs, or "reflogs", record when the tips of branches and
29 other references were updated in the local repository. Reflogs are
@@ -52,6 +53,9 @@ argument must be an _exact_ entry (e.g. "`git reflog delete
53 master@{2}`"). This subcommand is also typically not used directly by
54 end users.
55
56 +The "exists" subcommand checks whether a ref has a reflog. It exits
57 +with zero status if the reflog exists, and non-zero status if it does
58 +not.
59
60 OPTIONS
61 -------
builtin/reflog.c
+32 -1
@@ -13,6 +13,8 @@ static const char reflog_expire_usage[] =
13 "git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
14 static const char reflog_delete_usage[] =
15 "git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
16 +static const char reflog_exists_usage[] =
17 +"git reflog exists <ref>";
18
19 static unsigned long default_reflog_expire;
20 static unsigned long default_reflog_expire_unreachable;
@@ -699,12 +701,38 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
701 return status;
702 }
703
704 +static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
705 +{
706 + int i, start = 0;
707 +
708 + for (i = 1; i < argc; i++) {
709 + const char *arg = argv[i];
710 + if (!strcmp(arg, "--")) {
711 + i++;
712 + break;
713 + }
714 + else if (arg[0] == '-')
715 + usage(reflog_exists_usage);
716 + else
717 + break;
718 + }
719 +
720 + start = i;
721 +
722 + if (argc - start != 1)
723 + usage(reflog_exists_usage);
724 +
725 + if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
726 + die("invalid ref format: %s", argv[start]);
727 + return !reflog_exists(argv[start]);
728 +}
729 +
730 /*
731 * main "reflog"
732 */
733
734 static const char reflog_usage[] =
707 -"git reflog [ show | expire | delete ]";
735 +"git reflog [ show | expire | delete | exists ]";
736
737 int cmd_reflog(int argc, const char **argv, const char *prefix)
738 {
@@ -724,5 +752,8 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
752 if (!strcmp(argv[1], "delete"))
753 return cmd_reflog_delete(argc - 1, argv + 1, prefix);
754
755 + if (!strcmp(argv[1], "exists"))
756 + return cmd_reflog_exists(argc - 1, argv + 1, prefix);
757 +
758 return cmd_log_reflog(argc, argv, prefix);
759 }
t/t1411-reflog-show.sh
+5
@@ -166,4 +166,9 @@ test_expect_success 'git log -g -p shows diffs vs. parents' '
166 test_cmp expect actual
167 '
168
169 +test_expect_success 'reflog exists works' '
170 + git reflog exists refs/heads/master &&
171 + ! git reflog exists refs/heads/nonexistent
172 +'
173 +
174 test_done