Skip to content

Commit b6986d8

Browse files
torvaldsgitster
authored andcommitted
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 <[email protected]>
1 parent 7771675 commit b6986d8

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_obje
468468
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
469469
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
470470

471+
/* "careful lstat()" */
472+
extern int check_path(const char *path, int len, struct stat *st);
473+
471474
#define REFRESH_REALLY 0x0001 /* ignore_valid */
472475
#define REFRESH_UNMERGED 0x0002 /* allow unmerged */
473476
#define REFRESH_QUIET 0x0004 /* be quiet about it */

entry.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
175175
return 0;
176176
}
177177

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+
178191
int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *topath)
179192
{
180193
static char path[PATH_MAX + 1];
@@ -188,7 +201,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
188201
strcpy(path + len, ce->name);
189202
len += ce_namelen(ce);
190203

191-
if (!lstat(path, &st)) {
204+
if (!check_path(path, len, &st)) {
192205
unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
193206
if (!changed)
194207
return 0;

t/t6035-merge-dir-to-symlink.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test_expect_success 'create a commit where dir a/b changed to symlink' '
1717
git commit -m "dir to symlink"
1818
'
1919

20-
test_expect_failure 'keep a/b-2/c/d across checkout' '
20+
test_expect_success 'keep a/b-2/c/d across checkout' '
2121
git checkout HEAD^0 &&
2222
git reset --hard master &&
2323
git rm --cached a/b &&

0 commit comments

Comments
 (0)