git-checkout: be careful about untracked symlinks
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>
Linus Torvalds committed
Jul 29, 2009 at 20:22 UTC
b6986d8a75812a003a1623e0f0dff93c4a026b44
3 files changed
+18
-2
cache.h
+3
@@ -468,6 +468,9 @@ extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_obje
468
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
469
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
470
471
+/* "careful lstat()" */
472
+extern int check_path(const char *path, int len, struct stat *st);
473
+
474
#define REFRESH_REALLY 0x0001 /* ignore_valid */
475
#define REFRESH_UNMERGED 0x0002 /* allow unmerged */
476
#define REFRESH_QUIET 0x0004 /* be quiet about it */
entry.c
+14
-1
@@ -175,6 +175,19 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
175
return 0;
176
}
177
178
+/*
179
+ * This is like 'lstat()', except it refuses to follow symlinks
180
+ * in the path.
181
+ */
182
+int check_path(const char *path, int len, struct stat *st)
183
+{
184
+ if (has_symlink_leading_path(path, len)) {
185
+ errno = ENOENT;
186
+ return -1;
187
+ }
188
+ return lstat(path, st);
189
+}
190
+
191
int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
192
{
193
static char path[PATH_MAX + 1];
@@ -188,7 +201,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
201
strcpy(path + len, ce->name);
202
len += ce_namelen(ce);
203
191
- if (!lstat(path, &st)) {
204
+ if (!check_path(path, len, &st)) {
205
unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
206
if (!changed)
207
return 0;
t/t6035-merge-dir-to-symlink.sh
+1
-1
@@ -17,7 +17,7 @@ test_expect_success 'create a commit where dir a/b changed to symlink' '
17
git commit -m "dir to symlink"
18
'
19
20
-test_expect_failure 'keep a/b-2/c/d across checkout' '
20
+test_expect_success 'keep a/b-2/c/d across checkout' '
21
git checkout HEAD^0 &&
22
git reset --hard master &&
23
git rm --cached a/b &&