@matheusbsbs / matheus-bagliano / commits / 38eedc634b
Fork 0

git check-ref-format --print

Tolerating empty path components in ref names means each ref does not have a unique name. This creates difficulty for porcelains that want to see if two branches are equal. Add a helper associating to each ref a canonical name. If a user asks a porcelain to create a ref "refs/heads//master", the porcelain can run "git check-ref-format --print refs/heads//master" and only deal with "refs/heads/master" from then on. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 12, 2009 at 16:39 UTC 38eedc634bc5d30e8a7a2356d9eb3ae95d9b1d75
3 files changed +46 -6
Documentation/git-check-ref-format.txt
+19 -6
@@ -9,6 +9,7 @@ SYNOPSIS
9 --------
10 [verse]
11 'git check-ref-format' <refname>
12 +'git check-ref-format' --print <refname>
13 'git check-ref-format' [--branch] <branchname-shorthand>
14
15 DESCRIPTION
@@ -63,16 +64,28 @@ reference name expressions (see linkgit:git-rev-parse[1]):
64
65 . at-open-brace `@{` is used as a notation to access a reflog entry.
66
67 +With the `--print` option, if 'refname' is acceptable, it prints the
68 +canonicalized name of a hypothetical reference with that name. That is,
69 +it prints 'refname' with any extra `/` characters removed.
70 +
71 With the `--branch` option, it expands a branch name shorthand and
72 prints the name of the branch the shorthand refers to.
73
69 -EXAMPLE
70 --------
71 -
72 -git check-ref-format --branch @{-1}::
73 -
74 -Print the name of the previous branch.
74 +EXAMPLES
75 +--------
76
77 +* Print the name of the previous branch:
78 ++
79 +------------
80 +$ git check-ref-format --branch @{-1}
81 +------------
82 +
83 +* Determine the reference name to use for a new branch:
84 ++
85 +------------
86 +$ ref=$(git check-ref-format --print "refs/heads/$newbranch") ||
87 +die "we do not like '$newbranch' as a branch name."
88 +------------
89
90 GIT
91 ---
builtin-check-ref-format.c
+10
@@ -17,6 +17,16 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
17 printf("%s\n", sb.buf + 11);
18 exit(0);
19 }
20 + if (argc == 3 && !strcmp(argv[1], "--print")) {
21 + char *refname = xmalloc(strlen(argv[2]) + 1);
22 +
23 + if (check_ref_format(argv[2]))
24 + exit(1);
25 + if (normalize_path_copy(refname, argv[2]))
26 + die("Could not normalize ref name '%s'", argv[2]);
27 + printf("%s\n", refname);
28 + exit(0);
29 + }
30 if (argc != 2)
31 usage("git check-ref-format refname");
32 return !!check_ref_format(argv[1]);
t/t1402-check-ref-format.sh
+17
@@ -41,4 +41,21 @@ test_expect_success "check-ref-format --branch @{-1}" '
41 refname2=$(git check-ref-format --branch @{-2}) &&
42 test "$refname2" = master'
43
44 +valid_ref_normalized() {
45 + test_expect_success "ref name '$1' simplifies to '$2'" "
46 + refname=\$(git check-ref-format --print '$1') &&
47 + test \"\$refname\" = '$2'"
48 +}
49 +invalid_ref_normalized() {
50 + test_expect_success "check-ref-format --print rejects '$1'" "
51 + test_must_fail git check-ref-format --print '$1'"
52 +}
53 +
54 +valid_ref_normalized 'heads/foo' 'heads/foo'
55 +valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
56 +invalid_ref_normalized 'foo'
57 +invalid_ref_normalized 'heads/foo/../bar'
58 +invalid_ref_normalized 'heads/./foo'
59 +invalid_ref_normalized 'heads\foo'
60 +
61 test_done