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 <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Oct 12, 2009 at 00:33 UTC
1ba447b8dc2ec4e6c8ebbbdaa449f38edc29ad3f
1 file changed
+23
-2
builtin-check-ref-format.c
+23
-2
@@ -7,6 +7,28 @@
7
#include "builtin.h"
8
#include "strbuf.h"
9
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
+
32
int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
33
{
34
if (argc == 3 && !strcmp(argv[1], "--branch")) {
@@ -22,8 +44,7 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
44
45
if (check_ref_format(argv[2]))
46
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]);
48
printf("%s\n", refname);
49
exit(0);
50
}