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 <gitster@pobox.com>

Junio C Hamano committed Oct 7, 2022 at 15:00 UTC b77e3bdd978bda6415e819972f962b3b16d22a71
3 files changed +35 -8
Documentation/git-symbolic-ref.txt
+10 -1
@@ -9,7 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git symbolic-ref' [-m <reason>] <name> <ref>
12 -'git symbolic-ref' [-q] [--short] <name>
12 +'git symbolic-ref' [-q] [--short] [--no-recurse] <name>
13 'git symbolic-ref' --delete [-q] <name>
14
15 DESCRIPTION
@@ -46,6 +46,15 @@ OPTIONS
46 When showing the value of <name> as a symbolic ref, try to shorten the
47 value, e.g. from `refs/heads/master` to `master`.
48
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 +
58 -m::
59 Update the reflog for <name> with <reason>. This is valid only
60 when creating or updating a symbolic ref.
builtin/symbolic-ref.c
+11 -7
@@ -6,14 +6,17 @@
6
7 static const char * const git_symbolic_ref_usage[] = {
8 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>"),
10 NULL
11 };
12
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)
14 {
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);
20
21 if (!refname)
22 die("No such ref: %s", HEAD);
@@ -35,13 +38,14 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int print)
38
39 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
40 {
38 - int quiet = 0, delete = 0, shorten = 0, ret = 0;
41 + int quiet = 0, delete = 0, shorten = 0, recurse = 1, ret = 0;
42 const char *msg = NULL;
43 struct option options[] = {
44 OPT__QUIET(&quiet,
45 N_("suppress error message for non-symbolic (detached) refs")),
46 OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
47 OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
48 + OPT_BOOL(0, "recurse", &recurse, N_("recursively dereference (default)")),
49 OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
50 OPT_END(),
51 };
@@ -55,7 +59,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
59 if (delete) {
60 if (argc != 1)
61 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);
63 if (ret)
64 die("Cannot delete %s, not a symbolic ref", argv[0]);
65 if (!strcmp(argv[0], "HEAD"))
@@ -65,7 +69,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
69
70 switch (argc) {
71 case 1:
68 - ret = check_symref(argv[0], quiet, shorten, 1);
72 + ret = check_symref(argv[0], quiet, shorten, recurse, 1);
73 break;
74 case 2:
75 if (!strcmp(argv[0], "HEAD") &&
t/t1401-symbolic-ref.sh
+14
@@ -163,4 +163,18 @@ test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
163 test_cmp expect actual
164 '
165
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 +
180 test_done