X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=path.c;h=79aa104712364a8c18964feecd4c8079449a78cf;hb=9ee6bcd398057cb0cf3c52b9dc93b094d3d37235;hp=2ec950b27f1c3e4919ce7d1696360c5a49abb724;hpb=684d0d8dcfa2288744a553fc7294c5f9f5c12026;p=git.git diff --git a/path.c b/path.c index 2ec950b27..79aa10471 100644 --- a/path.c +++ b/path.c @@ -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; }