# [PATCH] Handle symlinks graciously

3 messages from 2005-12-26 to 2005-12-27. Participants: Johannes Schindelin, Junio C Hamano.
Thread: https://gitlist.dev/t/2932

## Johannes Schindelin, 2005-12-26 21:31

Subject: [PATCH] Handle symlinks graciously
Message-ID: <Pine.LNX.4.63.0512262231350.21076@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0512262231350.21076%40wbgn013.biozentrum.uni-wuerzburg.de

```

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>

---

	A few weeks ago I hunted a bug in t4004 on OS X, just to discover
	that it only occurred rarely. The problem is when the atime or mtime
	differ: in this case, diff-index would try to stat() the symlink,
	not lstat() it.

 diff.c                  |    2 +
 t/t4011-diff-symlink.sh |   84 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 1 deletions(-)
 create mode 100755 t/t4011-diff-symlink.sh

bd43cfdf69d99c3552fab5a375d9a72657d3bcad
diff --git a/diff.c b/diff.c
index c815918..6f064df 100644
--- a/diff.c
+++ b/diff.c
@@ -650,7 +650,7 @@ static void diff_fill_sha1_info(struct d
 	if (DIFF_FILE_VALID(one)) {
 		if (!one->sha1_valid) {
 			struct stat st;
-			if (stat(one->path, &st) < 0)
+			if (lstat(one->path, &st) < 0)
 				die("stat %s", one->path);
 			if (index_path(one->sha1, one->path, &st, 0))
 				die("cannot hash %s\n", one->path);
diff --git a/t/t4011-diff-symlink.sh b/t/t4011-diff-symlink.sh
new file mode 100755
index 0000000..9e70063
--- /dev/null
+++ b/t/t4011-diff-symlink.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Johannes Schindelin
+#
+
+test_description='Test diff of symlinks.
+
+'
+. ./test-lib.sh
+. ../diff-lib.sh
+
+cat > expected << EOF
+diff --git a/frotz b/frotz
+new file mode 120000
+index 0000000..7c465af
+--- /dev/null
++++ b/frotz
+@@ -0,0 +1 @@
++xyzzy
+\ No newline at end of file
+EOF
+
+test_expect_success \
+    'diff new symlink' \
+    'ln -s xyzzy frotz &&
+    git-update-index &&
+    tree=$(git-write-tree) &&
+    git-update-index --add frotz &&
+    GIT_DIFF_OPTS=--unified=0 git-diff-index -M -p $tree > current &&
+    compare_diff_patch current expected'
+
+test_expect_success \
+    'diff unchanged symlink' \
+    'tree=$(git-write-tree) &&
+    test -z "$(git-diff-index --name-only $tree)"'
+
+cat > expected << EOF
+diff --git a/frotz b/frotz
+deleted file mode 120000
+index 7c465af..0000000
+--- a/frotz
++++ /dev/null
+@@ -1 +0,0 @@
+-xyzzy
+\ No newline at end of file
+EOF
+
+test_expect_success \
+    'diff removed symlink' \
+    'rm frotz &&
+    git-diff-index -M -p $tree > current &&
+    compare_diff_patch current expected'
+
+cat > expected << EOF
+diff --git a/frotz b/frotz
+EOF
+
+test_expect_success \
+    'diff identical, but newly created symlink' \
+    'sleep 1 &&
+    ln -s xyzzy frotz &&
+    git-diff-index -M -p $tree > current &&
+    compare_diff_patch current expected'
+
+cat > expected << EOF
+diff --git a/frotz b/frotz
+index 7c465af..df1db54 120000
+--- a/frotz
++++ b/frotz
+@@ -1 +1 @@
+-xyzzy
+\ No newline at end of file
++yxyyz
+\ No newline at end of file
+EOF
+
+test_expect_success \
+    'diff different symlink' \
+    'rm frotz &&
+    ln -s yxyyz frotz &&
+    git-diff-index -M -p $tree > current &&
+    compare_diff_patch current expected'
+
+test_done
-- 
1.0.GIT

```

## Johannes Schindelin, 2005-12-26 23:00

Subject: Re: [PATCH] Handle symlinks graciously
Message-ID: <Pine.LNX.4.63.0512270000070.347@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0512270000070.347%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <Pine.LNX.4.63.0512262231350.21076@wbgn013.biozentrum.uni-wuerzburg.de>

```
Hi,

to make the test t4011 pass all the time, this little patch is needed on 
top:

diff --git a/t/t4011-diff-symlink.sh b/t/t4011-diff-symlink.sh
index 9e70063..e3ebf38 100755
--- a/t/t4011-diff-symlink.sh
+++ b/t/t4011-diff-symlink.sh
@@ -32,6 +32,7 @@ test_expect_success \
 test_expect_success \
     'diff unchanged symlink' \
     'tree=$(git-write-tree) &&
+    git-update-index frotz &&
     test -z "$(git-diff-index --name-only $tree)"'
 
 cat > expected << EOF

Ciao,
Dscho

```

## Junio C Hamano, 2005-12-27 02:33

Subject: Re: [PATCH] Handle symlinks graciously
Message-ID: <7v8xu7feum.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v8xu7feum.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.63.0512262231350.21076@wbgn013.biozentrum.uni-wuerzburg.de>

```
Good catch; thanks.

```
