Skip to content

Commit 38eedc6

Browse files
committed
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>
1 parent 58a05c7 commit 38eedc6

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

Documentation/git-check-ref-format.txt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git check-ref-format' <refname>
12+
'git check-ref-format' --print <refname>
1213
'git check-ref-format' [--branch] <branchname-shorthand>
1314

1415
DESCRIPTION
@@ -63,16 +64,28 @@ reference name expressions (see linkgit:git-rev-parse[1]):
6364

6465
. at-open-brace `@{` is used as a notation to access a reflog entry.
6566

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+
6671
With the `--branch` option, it expands a branch name shorthand and
6772
prints the name of the branch the shorthand refers to.
6873

69-
EXAMPLE
70-
-------
71-
72-
git check-ref-format --branch @{-1}::
73-
74-
Print the name of the previous branch.
74+
EXAMPLES
75+
--------
7576

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+
------------
7689

7790
GIT
7891
---

builtin-check-ref-format.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
1717
printf("%s\n", sb.buf + 11);
1818
exit(0);
1919
}
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+
}
2030
if (argc != 2)
2131
usage("git check-ref-format refname");
2232
return !!check_ref_format(argv[1]);

t/t1402-check-ref-format.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ test_expect_success "check-ref-format --branch @{-1}" '
4141
refname2=$(git check-ref-format --branch @{-2}) &&
4242
test "$refname2" = master'
4343

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+
4461
test_done

0 commit comments

Comments
 (0)