Make collapse_slashes() allocate memory for its result

This will make upcoming changes a tiny bit easier. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Sep 15, 2011 at 23:10 UTC 7f748c7cb2317800dcdb311c3e75a086ae0d1600
1 file changed +10 -9
builtin/check-ref-format.c
+10 -9
@@ -12,25 +12,28 @@ static const char builtin_check_ref_format_usage[] =
12 " or: git check-ref-format --branch <branchname-shorthand>";
13
14 /*
15 - * Remove leading slashes and replace each run of adjacent slashes in
16 - * src with a single slash, and write the result to dst.
15 + * Return a copy of refname but with leading slashes removed and runs
16 + * of adjacent slashes replaced with single slashes.
17 *
18 * This function is similar to normalize_path_copy(), but stripped down
19 * to meet check_ref_format's simpler needs.
20 */
21 -static void collapse_slashes(char *dst, const char *src)
21 +static char *collapse_slashes(const char *refname)
22 {
23 + char *ret = xmalloc(strlen(refname) + 1);
24 char ch;
25 char prev = '/';
26 + char *cp = ret;
27
26 - while ((ch = *src++) != '\0') {
28 + while ((ch = *refname++) != '\0') {
29 if (prev == '/' && ch == prev)
30 continue;
31
30 - *dst++ = ch;
32 + *cp++ = ch;
33 prev = ch;
34 }
33 - *dst = '\0';
35 + *cp = '\0';
36 + return ret;
37 }
38
39 static int check_ref_format_branch(const char *arg)
@@ -47,9 +50,7 @@ static int check_ref_format_branch(const char *arg)
50
51 static void refname_format_print(const char *arg)
52 {
50 - char *refname = xmalloc(strlen(arg) + 1);
51 -
52 - collapse_slashes(refname, arg);
53 + char *refname = collapse_slashes(arg);
54 printf("%s\n", refname);
55 }
56