summaryrefslogtreecommitdiff
path: root/test-path-utils.c
diff options
context:
space:
mode:
authorJiang Xin <worldhello.net@gmail.com>2013-06-25 23:53:43 +0800
committerJunio C Hamano <gitster@pobox.com>2013-06-26 09:59:00 -0700
commite02ca72f70ed8f0268a81f72cb3230c72e538e77 (patch)
tree922b62a3d30e9d5a604acaab92d0c83aba4114f8 /test-path-utils.c
parent203439b2840c4c384060df2fa192994e4b6740ed (diff)
downloadgit-e02ca72f70ed8f0268a81f72cb3230c72e538e77.tar.gz
path.c: refactor relative_path(), not only strip prefix
Original design of relative_path() is simple, just strip the prefix (*base) from the absolute path (*abs). In most cases, we need a real relative path, such as: ../foo, ../../bar. That's why there is another reimplementation (path_relative()) in quote.c. Borrow some codes from path_relative() in quote.c to refactor relative_path() in path.c, so that it could return real relative path, and user can reuse this function without reimplementing his/her own. The function path_relative() in quote.c will be substituted, and I would use the new relative_path() function when implementing the interactive git-clean later. Different results for relative_path() before and after this refactor: abs path base path relative (original) relative (refactor) ======== ========= =================== =================== /a/b /a/b . ./ /a/b/ /a/b . ./ /a /a/b/ /a ../ / /a/b/ / ../../ /a/c /a/b/ /a/c ../c /x/y /a/b/ /x/y ../../x/y a/b/ a/b/ . ./ a/b/ a/b . ./ a a/b a ../ x/y a/b/ x/y ../../x/y a/c a/b a/c ../c (empty) (null) (empty) ./ (empty) (empty) (empty) ./ (empty) /a/b (empty) ./ (null) (null) (null) ./ (null) (empty) (null) ./ (null) /a/b (segfault) ./ You may notice that return value "." has been changed to "./". It is because: * Function quote_path_relative() in quote.c will show the relative path as "./" if abs(in) and base(prefix) are the same. * Function relative_path() is called only once (in setup.c), and it will be OK for the return value as "./" instead of ".". Signed-off-by: Jiang Xin <worldhello.net@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'test-path-utils.c')
-rw-r--r--test-path-utils.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/test-path-utils.c b/test-path-utils.c
index 8a6d22404e..1bf4730619 100644
--- a/test-path-utils.c
+++ b/test-path-utils.c
@@ -117,14 +117,16 @@ int main(int argc, char **argv)
}
if (argc == 4 && !strcmp(argv[1], "relative_path")) {
+ struct strbuf sb = STRBUF_INIT;
const char *in, *prefix, *rel;
normalize_argv_string(&in, argv[2]);
normalize_argv_string(&prefix, argv[3]);
- rel = relative_path(in, prefix);
+ rel = relative_path(in, prefix, &sb);
if (!rel)
puts("(null)");
else
puts(strlen(rel) > 0 ? rel : "(empty)");
+ strbuf_release(&sb);
return 0;
}