Skip to content

Commit 2f633f4

Browse files
mhaggergitster
authored andcommitted
check-ref-format --print: Normalize refnames that start with slashes
When asked if "refs///heads/master" is valid, check-ref-format says "Yes, it is well formed", and when asked to print canonical form, it shows "refs/heads/master". This is so that it can be tucked after "$GIT_DIR/" to form a valid pathname for a loose ref, and we normalize a pathname like "$GIT_DIR/refs///heads/master" to de-dup the slashes in it. Similarly, when asked if "/refs/heads/master" is valid, check-ref-format says "Yes, it is Ok", but the leading slash is not removed when printing, leading to "$GIT_DIR//refs/heads/master". Fix it to make sure such leading slashes are removed. Add tests that such refnames are accepted and normalized correctly. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cdb791f commit 2f633f4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

builtin/check-ref-format.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ static const char builtin_check_ref_format_usage[] =
1212
" or: git check-ref-format --branch <branchname-shorthand>";
1313

1414
/*
15-
* Replace each run of adjacent slashes in src with a single slash,
16-
* and write the result to dst.
15+
* Remove leading slashes and replace each run of adjacent slashes in
16+
* src with a single slash, and write the result to dst.
1717
*
1818
* This function is similar to normalize_path_copy(), but stripped down
1919
* to meet check_ref_format's simpler needs.
2020
*/
2121
static void collapse_slashes(char *dst, const char *src)
2222
{
2323
char ch;
24-
char prev = '\0';
24+
char prev = '/';
2525

2626
while ((ch = *src++) != '\0') {
2727
if (prev == '/' && ch == prev)

t/t1402-check-ref-format.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ invalid_ref 'foo'
1818
valid_ref 'foo/bar/baz'
1919
valid_ref 'refs///heads/foo'
2020
invalid_ref 'heads/foo/'
21+
valid_ref '/heads/foo'
22+
valid_ref '///heads/foo'
23+
invalid_ref '/foo'
2124
invalid_ref './foo'
2225
invalid_ref '.refs/foo'
2326
invalid_ref 'heads/foo..bar'
@@ -70,7 +73,10 @@ invalid_ref_normalized() {
7073

7174
valid_ref_normalized 'heads/foo' 'heads/foo'
7275
valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
76+
valid_ref_normalized '/heads/foo' 'heads/foo'
77+
valid_ref_normalized '///heads/foo' 'heads/foo'
7378
invalid_ref_normalized 'foo'
79+
invalid_ref_normalized '/foo'
7480
invalid_ref_normalized 'heads/foo/../bar'
7581
invalid_ref_normalized 'heads/./foo'
7682
invalid_ref_normalized 'heads\foo'

0 commit comments

Comments
 (0)