Skip to content

Commit 6dfa213

Browse files
committed
Merge branch 'jp/symlink-dirs' into maint
* jp/symlink-dirs: t6035-merge-dir-to-symlink depends on SYMLINKS prerequisite git-checkout: be careful about untracked symlinks lstat_cache: guard against full match of length of 'name' parameter Demonstrate bugs when a directory is replaced with a symlink
2 parents bb0c806 + b6b0737 commit 6dfa213

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
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;

symlinks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ static int lstat_cache(struct cache_def *cache, const char *name, int len,
9191
longest_path_match(name, len, cache->path, cache->len,
9292
&previous_slash);
9393
match_flags = cache->flags & track_flags & (FL_NOENT|FL_SYMLINK);
94+
95+
if (!(track_flags & FL_FULLPATH) && match_len == len)
96+
match_len = last_slash = previous_slash;
97+
9498
if (match_flags && match_len == cache->len)
9599
return match_flags;
96100
/*

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/bin/sh
2+
3+
test_description='merging when a directory was replaced with a symlink'
4+
. ./test-lib.sh
5+
6+
if ! test_have_prereq SYMLINKS
7+
then
8+
say 'Symbolic links not supported, skipping tests.'
9+
test_done
10+
fi
11+
12+
test_expect_success 'create a commit where dir a/b changed to symlink' '
13+
mkdir -p a/b/c a/b-2/c &&
14+
> a/b/c/d &&
15+
> a/b-2/c/d &&
16+
> a/x &&
17+
git add -A &&
18+
git commit -m base &&
19+
git tag start &&
20+
rm -rf a/b &&
21+
ln -s b-2 a/b &&
22+
git add -A &&
23+
git commit -m "dir to symlink"
24+
'
25+
26+
test_expect_success 'keep a/b-2/c/d across checkout' '
27+
git checkout HEAD^0 &&
28+
git reset --hard master &&
29+
git rm --cached a/b &&
30+
git commit -m "untracked symlink remains" &&
31+
git checkout start^0 &&
32+
test -f a/b-2/c/d
33+
'
34+
35+
test_expect_success 'checkout should not have deleted a/b-2/c/d' '
36+
git checkout HEAD^0 &&
37+
git reset --hard master &&
38+
git checkout start^0 &&
39+
test -f a/b-2/c/d
40+
'
41+
42+
test_expect_success 'setup for merge test' '
43+
git reset --hard &&
44+
test -f a/b-2/c/d &&
45+
echo x > a/x &&
46+
git add a/x &&
47+
git commit -m x &&
48+
git tag baseline
49+
'
50+
51+
test_expect_success 'do not lose a/b-2/c/d in merge (resolve)' '
52+
git reset --hard &&
53+
git checkout baseline^0 &&
54+
git merge -s resolve master &&
55+
test -h a/b &&
56+
test -f a/b-2/c/d
57+
'
58+
59+
test_expect_failure 'do not lose a/b-2/c/d in merge (recursive)' '
60+
git reset --hard &&
61+
git checkout baseline^0 &&
62+
git merge -s recursive master &&
63+
test -h a/b &&
64+
test -f a/b-2/c/d
65+
'
66+
67+
test_expect_success 'setup a merge where dir a/b-2 changed to symlink' '
68+
git reset --hard &&
69+
git checkout start^0 &&
70+
rm -rf a/b-2 &&
71+
ln -s b a/b-2 &&
72+
git add -A &&
73+
git commit -m "dir a/b-2 to symlink" &&
74+
git tag test2
75+
'
76+
77+
test_expect_failure 'merge should not have conflicts (resolve)' '
78+
git reset --hard &&
79+
git checkout baseline^0 &&
80+
git merge -s resolve test2 &&
81+
test -h a/b-2 &&
82+
test -f a/b/c/d
83+
'
84+
85+
test_expect_failure 'merge should not have conflicts (recursive)' '
86+
git reset --hard &&
87+
git checkout baseline^0 &&
88+
git merge -s recursive test2 &&
89+
test -h a/b-2 &&
90+
test -f a/b/c/d
91+
'
92+
93+
test_done

0 commit comments

Comments
 (0)