Skip to content

Commit b77e3bd

Browse files
committed
symbolic-ref: teach "--[no-]recurse" option
Suppose you are managing many maintenance tracks in your project, and some of the more recent ones are maint-2.36 and maint-2.37. Further imagine that your project recently tagged the official 2.38 release, which means you would need to start maint-2.38 track soon, by doing: $ git checkout -b maint-2.38 v2.38.0^0 $ git branch --list 'maint-2.3[6-9]' * maint-2.38 maint-2.36 maint-2.37 So far, so good. But it also is reasonable to want not to have to worry about which maintenance track is the latest, by pointing a more generic-sounding 'maint' branch at it, by doing: $ git symbolic-ref refs/heads/maint refs/heads/maint-2.38 which would allow you to say "whichever it is, check out the latest maintenance track", by doing: $ git checkout maint $ git branch --show-current maint-2.38 It is arguably better to say that we are on 'maint-2.38' rather than on 'maint', and "git merge/pull" would record "into maint-2.38" and not "into maint", so I think what we have is a good behaviour. One thing that is slightly irritating, however, is that I do not think there is a good way (other than "cat .git/HEAD") to learn that you checked out 'maint' to get into that state. Just like the output of "git branch --show-current" shows above, "git symbolic-ref HEAD" would report 'refs/heads/maint-2.38', bypassing the intermediate symbolic ref at 'refs/heads/maint' that is pointed at by HEAD. The internal resolve_ref() API already has the necessary support for stopping after resolving a single level of a symbolic-ref, and we can expose it by adding a "--[no-]recurse" option to the command. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd59c5b commit b77e3bd

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
@@ -163,4 +163,18 @@ test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
163163
test_cmp expect actual
164164
'
165165

166+
test_expect_success 'symbolic-ref pointing at another' '
167+
git update-ref refs/heads/maint-2.37 HEAD &&
168+
git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
169+
git checkout maint &&
170+
171+
git symbolic-ref HEAD >actual &&
172+
echo refs/heads/maint-2.37 >expect &&
173+
test_cmp expect actual &&
174+
175+
git symbolic-ref --no-recurse HEAD >actual &&
176+
echo refs/heads/maint >expect &&
177+
test_cmp expect actual
178+
'
179+
166180
test_done

0 commit comments

Comments
 (0)