Skip to content

Commit 975b31d

Browse files
dschoJunio C Hamano
authored andcommitted
Handle symlinks graciously
This patch converts a stat() to an lstat() call, thereby fixing the case when the date of a symlink was not the same as the one recorded in the index. The included test case demonstrates this. This is for the case that the symlink points to a non-existing file. If the file exists, worse things than just an error message happen. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent c97451c commit 975b31d

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

diff.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ static void diff_fill_sha1_info(struct diff_filespec *one)
650650
if (DIFF_FILE_VALID(one)) {
651651
if (!one->sha1_valid) {
652652
struct stat st;
653-
if (stat(one->path, &st) < 0)
653+
if (lstat(one->path, &st) < 0)
654654
die("stat %s", one->path);
655655
if (index_path(one->sha1, one->path, &st, 0))
656656
die("cannot hash %s\n", one->path);

t/t4011-diff-symlink.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2005 Johannes Schindelin
4+
#
5+
6+
test_description='Test diff of symlinks.
7+
8+
'
9+
. ./test-lib.sh
10+
. ../diff-lib.sh
11+
12+
cat > expected << EOF
13+
diff --git a/frotz b/frotz
14+
new file mode 120000
15+
index 0000000..7c465af
16+
--- /dev/null
17+
+++ b/frotz
18+
@@ -0,0 +1 @@
19+
+xyzzy
20+
\ No newline at end of file
21+
EOF
22+
23+
test_expect_success \
24+
'diff new symlink' \
25+
'ln -s xyzzy frotz &&
26+
git-update-index &&
27+
tree=$(git-write-tree) &&
28+
git-update-index --add frotz &&
29+
GIT_DIFF_OPTS=--unified=0 git-diff-index -M -p $tree > current &&
30+
compare_diff_patch current expected'
31+
32+
test_expect_success \
33+
'diff unchanged symlink' \
34+
'tree=$(git-write-tree) &&
35+
git-update-index frotz &&
36+
test -z "$(git-diff-index --name-only $tree)"'
37+
38+
cat > expected << EOF
39+
diff --git a/frotz b/frotz
40+
deleted file mode 120000
41+
index 7c465af..0000000
42+
--- a/frotz
43+
+++ /dev/null
44+
@@ -1 +0,0 @@
45+
-xyzzy
46+
\ No newline at end of file
47+
EOF
48+
49+
test_expect_success \
50+
'diff removed symlink' \
51+
'rm frotz &&
52+
git-diff-index -M -p $tree > current &&
53+
compare_diff_patch current expected'
54+
55+
cat > expected << EOF
56+
diff --git a/frotz b/frotz
57+
EOF
58+
59+
test_expect_success \
60+
'diff identical, but newly created symlink' \
61+
'sleep 1 &&
62+
ln -s xyzzy frotz &&
63+
git-diff-index -M -p $tree > current &&
64+
compare_diff_patch current expected'
65+
66+
cat > expected << EOF
67+
diff --git a/frotz b/frotz
68+
index 7c465af..df1db54 120000
69+
--- a/frotz
70+
+++ b/frotz
71+
@@ -1 +1 @@
72+
-xyzzy
73+
\ No newline at end of file
74+
+yxyyz
75+
\ No newline at end of file
76+
EOF
77+
78+
test_expect_success \
79+
'diff different symlink' \
80+
'rm frotz &&
81+
ln -s yxyyz frotz &&
82+
git-diff-index -M -p $tree > current &&
83+
compare_diff_patch current expected'
84+
85+
test_done

0 commit comments

Comments
 (0)