]> rtime.felk.cvut.cz Git - git.git/commitdiff
git-checkout: be careful about untracked symlinks
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 30 Jul 2009 03:22:25 +0000 (20:22 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Jul 2009 03:24:28 +0000 (20:24 -0700)
This fixes the case where an untracked symlink that points at a directory
with tracked paths confuses the checkout logic, demostrated in t6035.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
entry.c
t/t6035-merge-dir-to-symlink.sh

diff --git a/cache.h b/cache.h
index e6c7f3307d09e592aa5bcc63fe6d372aa54a5da3..9222774e6cd8a0d6edeedbe62e95136c198a87f6 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -468,6 +468,9 @@ extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_obje
 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
 
+/* "careful lstat()" */
+extern int check_path(const char *path, int len, struct stat *st);
+
 #define REFRESH_REALLY         0x0001  /* ignore_valid */
 #define REFRESH_UNMERGED       0x0002  /* allow unmerged */
 #define REFRESH_QUIET          0x0004  /* be quiet about it */
diff --git a/entry.c b/entry.c
index d3e86c722a6663a65f69b73621f57aa6331a5e5b..f276cf3b88ea40cb4e6d03c623ce27cb0204c63e 100644 (file)
--- a/entry.c
+++ b/entry.c
@@ -175,6 +175,19 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
        return 0;
 }
 
+/*
+ * This is like 'lstat()', except it refuses to follow symlinks
+ * in the path.
+ */
+int check_path(const char *path, int len, struct stat *st)
+{
+       if (has_symlink_leading_path(path, len)) {
+               errno = ENOENT;
+               return -1;
+       }
+       return lstat(path, st);
+}
+
 int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
 {
        static char path[PATH_MAX + 1];
@@ -188,7 +201,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
        strcpy(path + len, ce->name);
        len += ce_namelen(ce);
 
-       if (!lstat(path, &st)) {
+       if (!check_path(path, len, &st)) {
                unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
                if (!changed)
                        return 0;
index ba90fc53e0447e632aa77702f20e793d765da540..a0ddf1e0f4013d1adba97561b5b692da7010383f 100755 (executable)
@@ -17,7 +17,7 @@ test_expect_success 'create a commit where dir a/b changed to symlink' '
        git commit -m "dir to symlink"
 '
 
-test_expect_failure 'keep a/b-2/c/d across checkout' '
+test_expect_success 'keep a/b-2/c/d across checkout' '
        git checkout HEAD^0 &&
        git reset --hard master &&
        git rm --cached a/b &&