]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'jc/symbolic-ref-no-recurse'
authorJunio C Hamano <gitster@pobox.com>
Fri, 21 Oct 2022 18:37:28 +0000 (11:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Oct 2022 18:37:28 +0000 (11:37 -0700)
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

1  2 
builtin/symbolic-ref.c
t/t1401-symbolic-ref.sh

diff --combined builtin/symbolic-ref.c
index 1b0f10225f0c2630fab0f67534e7135b30571c66,d5a70e7bd83484af4e784181ff577a5042c8121a..590ed17dd3f52934ff5cd3bcdd4070ac3d015105
@@@ -6,14 -6,17 +6,17 @@@
  
  static const char * const git_symbolic_ref_usage[] = {
        N_("git symbolic-ref [<options>] <name> [<ref>]"),
-       N_("git symbolic-ref -d [-q] <name>"),
+       N_("git symbolic-ref -d [-q] [--no-recurse] <name>"),
        NULL
  };
  
- static int check_symref(const char *HEAD, int quiet, int shorten, int print)
+ static int check_symref(const char *HEAD, int quiet, int shorten, int recurse, int print)
  {
-       int flag;
-       const char *refname = resolve_ref_unsafe(HEAD, 0, NULL, &flag);
+       int resolve_flags, flag;
+       const char *refname;
+       resolve_flags = (recurse ? 0 : RESOLVE_REF_NO_RECURSE);
+       refname = resolve_ref_unsafe(HEAD, resolve_flags, NULL, &flag);
  
        if (!refname)
                die("No such ref: %s", HEAD);
  
  int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
  {
-       int quiet = 0, delete = 0, shorten = 0, ret = 0;
+       int quiet = 0, delete = 0, shorten = 0, recurse = 1, ret = 0;
        const char *msg = NULL;
        struct option options[] = {
                OPT__QUIET(&quiet,
                        N_("suppress error message for non-symbolic (detached) refs")),
                OPT_BOOL('d', "delete", &delete, N_("delete symbolic ref")),
                OPT_BOOL(0, "short", &shorten, N_("shorten ref output")),
+               OPT_BOOL(0, "recurse", &recurse, N_("recursively dereference (default)")),
                OPT_STRING('m', NULL, &msg, N_("reason"), N_("reason of the update")),
                OPT_END(),
        };
@@@ -55,7 -59,7 +59,7 @@@
        if (delete) {
                if (argc != 1)
                        usage_with_options(git_symbolic_ref_usage, options);
-               ret = check_symref(argv[0], 1, 0, 0);
+               ret = check_symref(argv[0], 1, 0, 0, 0);
                if (ret)
                        die("Cannot delete %s, not a symbolic ref", argv[0]);
                if (!strcmp(argv[0], "HEAD"))
  
        switch (argc) {
        case 1:
-               ret = check_symref(argv[0], quiet, shorten, 1);
+               ret = check_symref(argv[0], quiet, shorten, recurse, 1);
                break;
        case 2:
                if (!strcmp(argv[0], "HEAD") &&
                    !starts_with(argv[1], "refs/"))
                        die("Refusing to point HEAD outside of refs/");
 +              if (check_refname_format(argv[1], REFNAME_ALLOW_ONELEVEL) < 0)
 +                      die("Refusing to set '%s' to invalid ref '%s'", argv[0], argv[1]);
                ret = !!create_symref(argv[0], argv[1], msg);
                break;
        default:
diff --combined t/t1401-symbolic-ref.sh
index 0c204089b83595bc516e9c26416cd67191d3c083,773a6e8e01677a5ff289c5cafbf78b73a1910b70..d708acdb819536083cc0f8bc5e41d2196d90aee3
@@@ -1,8 -1,6 +1,8 @@@
  #!/bin/sh
  
  test_description='basic symbolic-ref tests'
 +
 +TEST_PASSES_SANITIZE_LEAK=true
  . ./test-lib.sh
  
  # If the tests munging HEAD fail, they can break detection of
@@@ -165,14 -163,18 +165,28 @@@ test_expect_success 'symbolic-ref can r
        test_cmp expect actual
  '
  
 +test_expect_success 'symbolic-ref refuses invalid target for non-HEAD' '
 +      test_must_fail git symbolic-ref refs/heads/invalid foo..bar
 +'
 +
 +test_expect_success 'symbolic-ref allows top-level target for non-HEAD' '
 +      git symbolic-ref refs/heads/top-level FETCH_HEAD &&
 +      git update-ref FETCH_HEAD HEAD &&
 +      test_cmp_rev top-level HEAD
 +'
 +
+ test_expect_success 'symbolic-ref pointing at another' '
+       git update-ref refs/heads/maint-2.37 HEAD &&
+       git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
+       git checkout maint &&
+       git symbolic-ref HEAD >actual &&
+       echo refs/heads/maint-2.37 >expect &&
+       test_cmp expect actual &&
+       git symbolic-ref --no-recurse HEAD >actual &&
+       echo refs/heads/maint >expect &&
+       test_cmp expect actual
+ '
  test_done