Skip to content

Commit 1ba447b

Browse files
jrngitster
authored andcommitted
check-ref-format: simplify --print implementation
normalize_path_copy() is a complicated function, but most of its functionality will never apply to a ref name that has been checked with check_ref_format(). Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 38eedc6 commit 1ba447b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

builtin-check-ref-format.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@
77
#include "builtin.h"
88
#include "strbuf.h"
99

10+
/*
11+
* Replace each run of adjacent slashes in src with a single slash,
12+
* and write the result to dst.
13+
*
14+
* This function is similar to normalize_path_copy(), but stripped down
15+
* to meet check_ref_format's simpler needs.
16+
*/
17+
static void collapse_slashes(char *dst, const char *src)
18+
{
19+
char ch;
20+
char prev = '\0';
21+
22+
while ((ch = *src++) != '\0') {
23+
if (prev == '/' && ch == prev)
24+
continue;
25+
26+
*dst++ = ch;
27+
prev = ch;
28+
}
29+
*dst = '\0';
30+
}
31+
1032
int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
1133
{
1234
if (argc == 3 && !strcmp(argv[1], "--branch")) {
@@ -22,8 +44,7 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
2244

2345
if (check_ref_format(argv[2]))
2446
exit(1);
25-
if (normalize_path_copy(refname, argv[2]))
26-
die("Could not normalize ref name '%s'", argv[2]);
47+
collapse_slashes(refname, argv[2]);
2748
printf("%s\n", refname);
2849
exit(0);
2950
}

0 commit comments

Comments
 (0)