Skip to content

Commit 919cc4d

Browse files
committed
Merge branch 'mm/apply-double-slash'
* mm/apply-double-slash: apply: handle filenames with double slashes better
2 parents 1af4731 + 33eb4dd commit 919cc4d

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)