| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2005 Johannes Schindelin |
| 4 | # |
| 5 | |
| 6 | test_description='Test diff of symlinks. |
| 7 | |
| 8 | ' |
| 9 | |
| 10 | . ./test-lib.sh |
| 11 | . "$TEST_DIRECTORY"/lib-diff.sh |
| 12 | |
| 13 | # Print the short OID of a symlink with the given name. |
| 14 | symlink_oid () { |
| 15 | local oid="$(printf "%s" "$1" | git hash-object --stdin)" && |
| 16 | git rev-parse --short "$oid" |
| 17 | } |
| 18 | |
| 19 | # Print the short OID of the given file. |
| 20 | short_oid () { |
| 21 | local oid="$(git hash-object "$1")" && |
| 22 | git rev-parse --short "$oid" |
| 23 | } |
| 24 | |
| 25 | test_expect_success 'diff new symlink and file' ' |
| 26 | symlink=$(symlink_oid xyzzy) && |
| 27 | cat >expected <<-EOF && |
| 28 | diff --git a/frotz b/frotz |
| 29 | new file mode 120000 |
| 30 | index 0000000..$symlink |
| 31 | --- /dev/null |
| 32 | +++ b/frotz |
| 33 | @@ -0,0 +1 @@ |
| 34 | +xyzzy |
| 35 | \ No newline at end of file |
| 36 | diff --git a/nitfol b/nitfol |
| 37 | new file mode 100644 |
| 38 | index 0000000..$symlink |
| 39 | --- /dev/null |
| 40 | +++ b/nitfol |
| 41 | @@ -0,0 +1 @@ |
| 42 | +xyzzy |
| 43 | EOF |
| 44 | |
| 45 | # the empty tree |
| 46 | git update-index && |
| 47 | tree=$(git write-tree) && |
| 48 | |
| 49 | test_ln_s_add xyzzy frotz && |
| 50 | echo xyzzy >nitfol && |
| 51 | git update-index --add nitfol && |
| 52 | GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current && |
| 53 | compare_diff_patch expected current |
| 54 | ' |
| 55 | |
| 56 | test_expect_success 'diff unchanged symlink and file' ' |
| 57 | tree=$(git write-tree) && |
| 58 | git update-index frotz nitfol && |
| 59 | test -z "$(git diff-index --name-only $tree)" |
| 60 | ' |
| 61 | |
| 62 | test_expect_success 'diff removed symlink and file' ' |
| 63 | cat >expected <<-EOF && |
| 64 | diff --git a/frotz b/frotz |
| 65 | deleted file mode 120000 |
| 66 | index $symlink..0000000 |
| 67 | --- a/frotz |
| 68 | +++ /dev/null |
| 69 | @@ -1 +0,0 @@ |
| 70 | -xyzzy |
| 71 | \ No newline at end of file |
| 72 | diff --git a/nitfol b/nitfol |
| 73 | deleted file mode 100644 |
| 74 | index $symlink..0000000 |
| 75 | --- a/nitfol |
| 76 | +++ /dev/null |
| 77 | @@ -1 +0,0 @@ |
| 78 | -xyzzy |
| 79 | EOF |
| 80 | mv frotz frotz2 && |
| 81 | mv nitfol nitfol2 && |
| 82 | git diff-index -M -p $tree >current && |
| 83 | compare_diff_patch expected current |
| 84 | ' |
| 85 | |
| 86 | test_expect_success 'diff identical, but newly created symlink and file' ' |
| 87 | >expected && |
| 88 | rm -f frotz nitfol && |
| 89 | echo xyzzy >nitfol && |
| 90 | test-tool chmtime +10 nitfol && |
| 91 | if test_have_prereq SYMLINKS |
| 92 | then |
| 93 | ln -s xyzzy frotz |
| 94 | else |
| 95 | printf xyzzy >frotz |
| 96 | # the symlink property propagates from the index |
| 97 | fi && |
| 98 | git diff-index -M -p $tree >current && |
| 99 | compare_diff_patch expected current && |
| 100 | |
| 101 | >expected && |
| 102 | git diff-index -M -p -w $tree >current && |
| 103 | compare_diff_patch expected current |
| 104 | ' |
| 105 | |
| 106 | test_expect_success 'diff different symlink and file' ' |
| 107 | new=$(symlink_oid yxyyz) && |
| 108 | cat >expected <<-EOF && |
| 109 | diff --git a/frotz b/frotz |
| 110 | index $symlink..$new 120000 |
| 111 | --- a/frotz |
| 112 | +++ b/frotz |
| 113 | @@ -1 +1 @@ |
| 114 | -xyzzy |
| 115 | \ No newline at end of file |
| 116 | +yxyyz |
| 117 | \ No newline at end of file |
| 118 | diff --git a/nitfol b/nitfol |
| 119 | index $symlink..$new 100644 |
| 120 | --- a/nitfol |
| 121 | +++ b/nitfol |
| 122 | @@ -1 +1 @@ |
| 123 | -xyzzy |
| 124 | +yxyyz |
| 125 | EOF |
| 126 | rm -f frotz && |
| 127 | if test_have_prereq SYMLINKS |
| 128 | then |
| 129 | ln -s yxyyz frotz |
| 130 | else |
| 131 | printf yxyyz >frotz |
| 132 | # the symlink property propagates from the index |
| 133 | fi && |
| 134 | echo yxyyz >nitfol && |
| 135 | git diff-index -M -p $tree >current && |
| 136 | compare_diff_patch expected current |
| 137 | ' |
| 138 | |
| 139 | test_expect_success SYMLINKS 'diff symlinks with non-existing targets' ' |
| 140 | ln -s narf pinky && |
| 141 | ln -s take\ over brain && |
| 142 | test_must_fail git diff --no-index pinky brain >output 2>output.err && |
| 143 | grep narf output && |
| 144 | test_must_be_empty output.err |
| 145 | ' |
| 146 | |
| 147 | test_expect_success SYMLINKS 'setup symlinks with attributes' ' |
| 148 | echo "*.bin diff=bin" >>.gitattributes && |
| 149 | echo content >file.bin && |
| 150 | ln -s file.bin link.bin && |
| 151 | git add -N file.bin link.bin |
| 152 | ' |
| 153 | |
| 154 | test_expect_success SYMLINKS 'symlinks do not respect userdiff config by path' ' |
| 155 | file=$(short_oid file.bin) && |
| 156 | link=$(symlink_oid file.bin) && |
| 157 | cat >expect <<-EOF && |
| 158 | diff --git a/file.bin b/file.bin |
| 159 | new file mode 100644 |
| 160 | index 0000000..$file |
| 161 | Binary files /dev/null and b/file.bin differ |
| 162 | diff --git a/link.bin b/link.bin |
| 163 | new file mode 120000 |
| 164 | index 0000000..$link |
| 165 | --- /dev/null |
| 166 | +++ b/link.bin |
| 167 | @@ -0,0 +1 @@ |
| 168 | +file.bin |
| 169 | \ No newline at end of file |
| 170 | EOF |
| 171 | git config diff.bin.binary true && |
| 172 | git diff file.bin link.bin >actual && |
| 173 | test_cmp expect actual |
| 174 | ' |
| 175 | |
| 176 | test_done |