Skip to content

Commit 7f748c7

Browse files
mhaggergitster
authored andcommitted
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>
1 parent 7e9d2fe commit 7f748c7

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

builtin/check-ref-format.c

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

1414
/*
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.
1717
*
1818
* This function is similar to normalize_path_copy(), but stripped down
1919
* to meet check_ref_format's simpler needs.
2020
*/
21-
static void collapse_slashes(char *dst, const char *src)
21+
static char *collapse_slashes(const char *refname)
2222
{
23+
char *ret = xmalloc(strlen(refname) + 1);
2324
char ch;
2425
char prev = '/';
26+
char *cp = ret;
2527

26-
while ((ch = *src++) != '\0') {
28+
while ((ch = *refname++) != '\0') {
2729
if (prev == '/' && ch == prev)
2830
continue;
2931

30-
*dst++ = ch;
32+
*cp++ = ch;
3133
prev = ch;
3234
}
33-
*dst = '\0';
35+
*cp = '\0';
36+
return ret;
3437
}
3538

3639
static int check_ref_format_branch(const char *arg)
@@ -47,9 +50,7 @@ static int check_ref_format_branch(const char *arg)
4750

4851
static void refname_format_print(const char *arg)
4952
{
50-
char *refname = xmalloc(strlen(arg) + 1);
51-
52-
collapse_slashes(refname, arg);
53+
char *refname = collapse_slashes(arg);
5354
printf("%s\n", refname);
5455
}
5556

0 commit comments

Comments
 (0)