summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2011-03-26 09:04:24 (GMT)
committerJunio C Hamano <gitster@pobox.com>2011-03-29 00:01:15 (GMT)
commit9b125da490990ae4d08dd1517dc38b188b874fa8 (patch)
tree1da0d8359926faf97b67946095f5e5e9937186cc /dir.c
parent78bc46675353e4833a59e0f4dfa47d57a9c1a46b (diff)
downloadgit-9b125da490990ae4d08dd1517dc38b188b874fa8.zip
git-9b125da490990ae4d08dd1517dc38b188b874fa8.tar.gz
git-9b125da490990ae4d08dd1517dc38b188b874fa8.tar.bz2
setup: return correct prefix if worktree is '/'
The same old problem reappears after setup code is reworked. We tend to assume there is at least one path component in a path and forget that path can be simply '/'. Reported-by: Matthijs Kooijman <matthijs@stdin.nl> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index 570b651..b0100f5 100644
--- a/dir.c
+++ b/dir.c
@@ -1048,6 +1048,37 @@ char *get_relative_cwd(char *buffer, int size, const char *dir)
}
}
+ * Given two normalized paths (a trailing slash is ok), if subdir is
+ * outside dir, return -1. Otherwise return the offset in subdir that
+ * can be used as relative path to dir.
+ */
+int dir_inside_of(const char *subdir, const char *dir)
+{
+ int offset = 0;
+
+ assert(dir && subdir && *dir && *subdir);
+
+ while (*dir && *subdir && *dir == *subdir) {
+ dir++;
+ subdir++;
+ offset++;
+ }
+
+ /* hel[p]/me vs hel[l]/yeah */
+ if (*dir && *subdir)
+ return -1;
+
+ if (!*subdir)
+ return !*dir ? offset : -1; /* same dir */
+
+ /* foo/[b]ar vs foo/[] */
+ if (is_dir_sep(dir[-1]))
+ return is_dir_sep(subdir[-1]) ? offset : -1;
+
+ /* foo[/]bar vs foo[] */
+ return is_dir_sep(*subdir) ? offset + 1 : -1;
+}
+
int is_inside_dir(const char *dir)
{
char buffer[PATH_MAX];