Skip to content

Commit 288123f

Browse files
committed
ignore duplicated slashes in make_relative_path()
The function takes two paths, an early part of abs is supposed to match base; otherwise abs is not a path under base and the function returns the full path of abs. The caller can easily confuse the implementation by giving duplicated and needless slashes in these path arguments. Credit for test script, motivation and initial patch goes to Thomas Rast. A follow-up fix (squashed) is by Hannes. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 19c6a4f commit 288123f

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

path.c

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,38 @@ int set_shared_perm(const char *path, int mode)
394394
const char *make_relative_path(const char *abs, const char *base)
395395
{
396396
static char buf[PATH_MAX + 1];
397-
int baselen;
398-
if (!base)
399-
return abs;
400-
baselen = strlen(base);
401-
if (prefixcmp(abs, base))
397+
int i = 0, j = 0;
398+
399+
if (!base || !base[0])
402400
return abs;
403-
if (abs[baselen] == '/')
404-
baselen++;
405-
else if (base[baselen - 1] != '/')
401+
while (base[i]) {
402+
if (is_dir_sep(base[i])) {
403+
if (!is_dir_sep(abs[j]))
404+
return abs;
405+
while (is_dir_sep(base[i]))
406+
i++;
407+
while (is_dir_sep(abs[j]))
408+
j++;
409+
continue;
410+
} else if (abs[j] != base[i]) {
411+
return abs;
412+
}
413+
i++;
414+
j++;
415+
}
416+
if (
417+
/* "/foo" is a prefix of "/foo" */
418+
abs[j] &&
419+
/* "/foo" is not a prefix of "/foobar" */
420+
!is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])
421+
)
406422
return abs;
407-
strcpy(buf, abs + baselen);
423+
while (is_dir_sep(abs[j]))
424+
j++;
425+
if (!abs[j])
426+
strcpy(buf, ".");
427+
else
428+
strcpy(buf, abs + j);
408429
return buf;
409430
}
410431

t/t1501-worktree.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,10 @@ test_expect_success 'absolute pathspec should fail gracefully' '
189189
)
190190
'
191191

192+
test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
193+
: > dummy_file
194+
echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
195+
git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
196+
'
197+
192198
test_done

0 commit comments

Comments
 (0)