Skip to content

Commit afcb2e7

Browse files
dturner-twgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent abd0cd3 commit afcb2e7

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Documentation/git-reflog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ depending on the subcommand:
2323
[--dry-run] [--verbose] [--all | <refs>...]
2424
'git reflog delete' [--rewrite] [--updateref]
2525
[--dry-run] [--verbose] ref@\{specifier\}...
26+
'git reflog exists' <ref>
2627

2728
Reference logs, or "reflogs", record when the tips of branches and
2829
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
5253
master@{2}`"). This subcommand is also typically not used directly by
5354
end users.
5455

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.
5559

5660
OPTIONS
5761
-------

builtin/reflog.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ static const char reflog_expire_usage[] =
1313
"git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all] <refs>...";
1414
static const char reflog_delete_usage[] =
1515
"git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] <refs>...";
16+
static const char reflog_exists_usage[] =
17+
"git reflog exists <ref>";
1618

1719
static unsigned long default_reflog_expire;
1820
static unsigned long default_reflog_expire_unreachable;
@@ -699,12 +701,38 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
699701
return status;
700702
}
701703

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+
702730
/*
703731
* main "reflog"
704732
*/
705733

706734
static const char reflog_usage[] =
707-
"git reflog [ show | expire | delete ]";
735+
"git reflog [ show | expire | delete | exists ]";
708736

709737
int cmd_reflog(int argc, const char **argv, const char *prefix)
710738
{
@@ -724,5 +752,8 @@ int cmd_reflog(int argc, const char **argv, const char *prefix)
724752
if (!strcmp(argv[1], "delete"))
725753
return cmd_reflog_delete(argc - 1, argv + 1, prefix);
726754

755+
if (!strcmp(argv[1], "exists"))
756+
return cmd_reflog_exists(argc - 1, argv + 1, prefix);
757+
727758
return cmd_log_reflog(argc, argv, prefix);
728759
}

t/t1411-reflog-show.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ test_expect_success 'git log -g -p shows diffs vs. parents' '
166166
test_cmp expect actual
167167
'
168168

169+
test_expect_success 'reflog exists works' '
170+
git reflog exists refs/heads/master &&
171+
! git reflog exists refs/heads/nonexistent
172+
'
173+
169174
test_done

0 commit comments

Comments
 (0)