Skip to content

Commit 4a48c7d

Browse files
committed
Merge branch 'jc/symbolic-ref-no-recurse'
After checking out a "branch" that is a symbolic-ref that points at another branch, "git symbolic-ref HEAD" reports the underlying branch, not the symbolic-ref the user gave checkout as argument. The command learned the "--no-recurse" option to stop after dereferencing a symbolic-ref only once. * jc/symbolic-ref-no-recurse: symbolic-ref: teach "--[no-]recurse" option
2 parents 6269c46 + b77e3bd commit 4a48c7d

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

Documentation/git-symbolic-ref.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git symbolic-ref' [-m <reason>] <name> <ref>
12-
'git symbolic-ref' [-q] [--short] <name>
12+
'git symbolic-ref' [-q] [--short] [--no-recurse] <name>
1313
'git symbolic-ref' --delete [-q] <name>
1414

1515
DESCRIPTION
@@ -46,6 +46,15 @@ OPTIONS
4646
When showing the value of <name> as a symbolic ref, try to shorten the
4747
value, e.g. from `refs/heads/master` to `master`.
4848

49+
--recurse::
50+
--no-recurse::
51+
When showing the value of <name> as a symbolic ref, if
52+
<name> refers to another symbolic ref, follow such a chain
53+
of symbolic refs until the result no longer points at a
54+
symbolic ref (`--recurse`, which is the default).
55+
`--no-recurse` stops after dereferencing only a single level
56+
of symbolic ref.
57+
4958
-m::
5059
Update the reflog for <name> with <reason>. This is valid only
5160
when creating or updating a symbolic ref.

builtin/symbolic-ref.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
static const char * const git_symbolic_ref_usage[] = {
88
N_("git symbolic-ref [<options>] <name> [<ref>]"),
9-
N_("git symbolic-ref -d [-q] <name>"),
9+
N_("git symbolic-ref -d [-q] [--no-recurse] <name>"),
1010
NULL
1111
};
1212

13-
static int check_symref(const char *HEAD, int quiet, int shorten, int print)
13+
static int check_symref(const char *HEAD, int quiet, int shorten, int recurse, int print)
1414
{
15-
int flag;
16-
const char *refname = resolve_ref_unsafe(HEAD, 0, NULL, &flag);
15+
int resolve_flags, flag;
16+
const char *refname;
17+
18+
resolve_flags = (recurse ? 0 : RESOLVE_REF_NO_RECURSE);
19+
refname = resolve_ref_unsafe(HEAD, resolve_flags, NULL, &flag);
1720

1821
if (!refname)
1922
die("No such ref: %s", HEAD);
@@ -35,13 +38,14 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int print)
3538

3639
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
3740
{
38-
int quiet = 0, delete = 0, shorten = 0, ret = 0;
41+
int quiet = 0, delete = 0, shorten = 0, recurse = 1, ret = 0;
3942
const char *msg = NULL;
4043
struct option options[] = {
4144
OPT__QUIET(&quiet,
4245
N_("suppress error message for non-symbolic (detached) refs")),
4346
OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
4447
OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
48+
OPT_BOOL(0, "recurse", &recurse, N_("recursively dereference (default)")),
4549
OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
4650
OPT_END(),
4751
};
@@ -55,7 +59,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
5559
if (delete) {
5660
if (argc != 1)
5761
usage_with_options(git_symbolic_ref_usage, options);
58-
ret = check_symref(argv[0], 1, 0, 0);
62+
ret = check_symref(argv[0], 1, 0, 0, 0);
5963
if (ret)
6064
die("Cannot delete %s, not a symbolic ref", argv[0]);
6165
if (!strcmp(argv[0], "HEAD"))
@@ -65,7 +69,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
6569

6670
switch (argc) {
6771
case 1:
68-
ret = check_symref(argv[0], quiet, shorten, 1);
72+
ret = check_symref(argv[0], quiet, shorten, recurse, 1);
6973
break;
7074
case 2:
7175
if (!strcmp(argv[0], "HEAD") &&

t/t1401-symbolic-ref.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,18 @@ test_expect_success 'symbolic-ref allows top-level target for non-HEAD' '
175175
test_cmp_rev top-level HEAD
176176
'
177177

178+
test_expect_success 'symbolic-ref pointing at another' '
179+
git update-ref refs/heads/maint-2.37 HEAD &&
180+
git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
181+
git checkout maint &&
182+
183+
git symbolic-ref HEAD >actual &&
184+
echo refs/heads/maint-2.37 >expect &&
185+
test_cmp expect actual &&
186+
187+
git symbolic-ref --no-recurse HEAD >actual &&
188+
echo refs/heads/maint >expect &&
189+
test_cmp expect actual
190+
'
191+
178192
test_done

0 commit comments

Comments
 (0)