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 <mmarek@suse.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michal Marek committed
May 21, 2009 at 14:25 UTC
33eb4dd9fce3b46b6962b46213d6f222bcd0cb3a
1 file changed
+20
-6
builtin-apply.c
+20
-6
@@ -320,6 +320,20 @@ static int name_terminate(const char *name, int namelen, int c, int terminate)
320
return 1;
321
}
322
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
+
337
static char *find_name(const char *line, char *def, int p_value, int terminate)
338
{
339
int len;
@@ -349,7 +363,7 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
363
free(def);
364
if (root)
365
strbuf_insert(&name, 0, root, root_len);
352
- return strbuf_detach(&name, NULL);
366
+ return squash_slash(strbuf_detach(&name, NULL));
367
}
368
}
369
strbuf_release(&name);
@@ -369,10 +383,10 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
383
start = line;
384
}
385
if (!start)
372
- return def;
386
+ return squash_slash(def);
387
len = line - start;
388
if (!len)
375
- return def;
389
+ return squash_slash(def);
390
391
/*
392
* 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)
397
if (def) {
398
int deflen = strlen(def);
399
if (deflen < len && !strncmp(start, def, deflen))
386
- return def;
400
+ return squash_slash(def);
401
free(def);
402
}
403
@@ -392,10 +406,10 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
406
strcpy(ret, root);
407
memcpy(ret + root_len, start, len);
408
ret[root_len + len] = '\0';
395
- return ret;
409
+ return squash_slash(ret);
410
}
411
398
- return xmemdupz(start, len);
412
+ return squash_slash(xmemdupz(start, len));
413
}
414
415
static int count_slashes(const char *cp)