Skip to content

Commit 33eb4dd

Browse files
michal42gitster
authored andcommitted
apply: handle filenames with double slashes better
When there are duplicated slashes in pathnames, like this: --- a/perl//Git.pm +++ b/perl//Git.pm @@ -1358,3 +1358,4 @@ 1; # Famous last words +# test the paths gleaned from the patch header won't be found in the index and cause "apply --index" and "apply --cached" to fail. Fix this by squashing the duplicated slashes upon input. Signed-off-by: Michal Marek <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a7eb51 commit 33eb4dd

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

builtin-apply.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@ static int name_terminate(const char *name, int namelen, int c, int terminate)
320320
return 1;
321321
}
322322

323+
/* remove double slashes to make --index work with such filenames */
324+
static char *squash_slash(char *name)
325+
{
326+
int i = 0, j = 0;
327+
328+
while (name[i]) {
329+
if ((name[j++] = name[i++]) == '/')
330+
while (name[i] == '/')
331+
i++;
332+
}
333+
name[j] = '\0';
334+
return name;
335+
}
336+
323337
static char *find_name(const char *line, char *def, int p_value, int terminate)
324338
{
325339
int len;
@@ -349,7 +363,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
349363
free(def);
350364
if (root)
351365
strbuf_insert(&name, 0, root, root_len);
352-
return strbuf_detach(&name, NULL);
366+
return squash_slash(strbuf_detach(&name, NULL));
353367
}
354368
}
355369
strbuf_release(&name);
@@ -369,10 +383,10 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
369383
start = line;
370384
}
371385
if (!start)
372-
return def;
386+
return squash_slash(def);
373387
len = line - start;
374388
if (!len)
375-
return def;
389+
return squash_slash(def);
376390

377391
/*
378392
* Generally we prefer the shorter name, especially
@@ -383,7 +397,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
383397
if (def) {
384398
int deflen = strlen(def);
385399
if (deflen < len && !strncmp(start, def, deflen))
386-
return def;
400+
return squash_slash(def);
387401
free(def);
388402
}
389403

@@ -392,10 +406,10 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
392406
strcpy(ret, root);
393407
memcpy(ret + root_len, start, len);
394408
ret[root_len + len] = '\0';
395-
return ret;
409+
return squash_slash(ret);
396410
}
397411

398-
return xmemdupz(start, len);
412+
return squash_slash(xmemdupz(start, len));
399413
}
400414

401415
static int count_slashes(const char *cp)

0 commit comments

Comments
 (0)