diff options
author | Junio C Hamano <gitster@pobox.com> | 2010-01-21 19:05:19 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2010-01-22 15:34:56 -0800 |
commit | 288123f01cb1b835edbf6e2e188159c2ff858aca (patch) | |
tree | c10efa0ec8474dc08928427134ad7116d3bee046 /path.c | |
parent | 19c6a4f8369f37c0df1fb57008a891eb6a6dc4bb (diff) | |
download | git-288123f01cb1b835edbf6e2e188159c2ff858aca.tar.gz |
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>
Diffstat (limited to 'path.c')
-rw-r--r-- | path.c | 39 |
1 files changed, 30 insertions, 9 deletions
@@ -394,17 +394,38 @@ int set_shared_perm(const char *path, int mode) const char *make_relative_path(const char *abs, const char *base) { static char buf[PATH_MAX + 1]; - int baselen; - if (!base) - return abs; - baselen = strlen(base); - if (prefixcmp(abs, base)) + int i = 0, j = 0; + + if (!base || !base[0]) return abs; - if (abs[baselen] == '/') - baselen++; - else if (base[baselen - 1] != '/') + while (base[i]) { + 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; - strcpy(buf, abs + baselen); + while (is_dir_sep(abs[j])) + j++; + if (!abs[j]) + strcpy(buf, "."); + else + strcpy(buf, abs + j); return buf; } |