Skip to content

Commit

Permalink
Make collapse_slashes() allocate memory for its result
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Michael Haggerty authored and Junio C Hamano committed Oct 5, 2011
1 parent 7e9d2fe commit 7f748c7
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions builtin/check-ref-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@ static const char builtin_check_ref_format_usage[] =
" or: git check-ref-format --branch <branchname-shorthand>";

/*
* Remove leading slashes and replace each run of adjacent slashes in
* src with a single slash, and write the result to dst.
* Return a copy of refname but with leading slashes removed and runs
* of adjacent slashes replaced with single slashes.
*
* This function is similar to normalize_path_copy(), but stripped down
* to meet check_ref_format's simpler needs.
*/
static void collapse_slashes(char *dst, const char *src)
static char *collapse_slashes(const char *refname)
{
char *ret = xmalloc(strlen(refname) + 1);
char ch;
char prev = '/';
char *cp = ret;

while ((ch = *src++) != '\0') {
while ((ch = *refname++) != '\0') {
if (prev == '/' && ch == prev)
continue;

*dst++ = ch;
*cp++ = ch;
prev = ch;
}
*dst = '\0';
*cp = '\0';
return ret;
}

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

static void refname_format_print(const char *arg)
{
char *refname = xmalloc(strlen(arg) + 1);

collapse_slashes(refname, arg);
char *refname = collapse_slashes(arg);
printf("%s\n", refname);
}

Expand Down

0 comments on commit 7f748c7

Please sign in to comment.