1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-10-28 21:07:52 +01:00

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 <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2010-01-21 19:05:19 -08:00
parent 19c6a4f836
commit 288123f01c
2 changed files with 36 additions and 9 deletions

39
path.c
View file

@ -394,17 +394,38 @@ int set_shared_perm(const char *path, int mode)
const char *make_relative_path(const char *abs, const char *base) const char *make_relative_path(const char *abs, const char *base)
{ {
static char buf[PATH_MAX + 1]; static char buf[PATH_MAX + 1];
int baselen; int i = 0, j = 0;
if (!base)
if (!base || !base[0])
return abs; return abs;
baselen = strlen(base); while (base[i]) {
if (prefixcmp(abs, base)) if (is_dir_sep(base[i])) {
if (!is_dir_sep(abs[j]))
return abs;
while (is_dir_sep(base[i]))
i++;
while (is_dir_sep(abs[j]))
j++;
continue;
} else if (abs[j] != base[i]) {
return abs;
}
i++;
j++;
}
if (
/* "/foo" is a prefix of "/foo" */
abs[j] &&
/* "/foo" is not a prefix of "/foobar" */
!is_dir_sep(base[i-1]) && !is_dir_sep(abs[j])
)
return abs; return abs;
if (abs[baselen] == '/') while (is_dir_sep(abs[j]))
baselen++; j++;
else if (base[baselen - 1] != '/') if (!abs[j])
return abs; strcpy(buf, ".");
strcpy(buf, abs + baselen); else
strcpy(buf, abs + j);
return buf; return buf;
} }

View file

@ -189,4 +189,10 @@ test_expect_success 'absolute pathspec should fail gracefully' '
) )
' '
test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
: > dummy_file
echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
'
test_done test_done