Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description="Test whether cache-tree is properly updated |
| 4 | |
| 5 | Tests whether various commands properly update and/or rewrite the |
| 6 | cache-tree extension. |
| 7 | " |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | cmp_cache_tree () { |
Nguyễn Thái Ngọc Duy | 06ccb29 | 2018-03-24 08:44:39 +0100 | [diff] [blame] | 11 | test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual && |
brian m. carlson | 2ece6ad | 2018-05-13 02:24:15 +0000 | [diff] [blame] | 12 | sed "s/$OID_REGEX/SHA/" <actual >filtered && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 13 | test_cmp "$1" filtered |
| 14 | } |
| 15 | |
| 16 | # We don't bother with actually checking the SHA1: |
Nguyễn Thái Ngọc Duy | 06ccb29 | 2018-03-24 08:44:39 +0100 | [diff] [blame] | 17 | # test-tool dump-cache-tree already verifies that all existing data is |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 18 | # correct. |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 19 | generate_expected_cache_tree_rec () { |
| 20 | dir="$1${1:+/}" && |
| 21 | parent="$2" && |
| 22 | # ls-files might have foo/bar, foo/bar/baz, and foo/bar/quux |
| 23 | # We want to count only foo because it's the only direct child |
| 24 | subtrees=$(git ls-files|grep /|cut -d / -f 1|uniq) && |
Ben Walton | d69360c | 2014-12-22 15:25:44 -0800 | [diff] [blame] | 25 | subtree_count=$(echo "$subtrees"|awk -v c=0 '$1 != "" {++c} END {print c}') && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 26 | entries=$(git ls-files|wc -l) && |
| 27 | printf "SHA $dir (%d entries, %d subtrees)\n" "$entries" "$subtree_count" && |
| 28 | for subtree in $subtrees |
| 29 | do |
| 30 | cd "$subtree" |
| 31 | generate_expected_cache_tree_rec "$dir$subtree" "$dir" || return 1 |
| 32 | cd .. |
| 33 | done && |
| 34 | dir=$parent |
| 35 | } |
| 36 | |
| 37 | generate_expected_cache_tree () { |
| 38 | ( |
| 39 | generate_expected_cache_tree_rec |
| 40 | ) |
| 41 | } |
| 42 | |
| 43 | test_cache_tree () { |
| 44 | generate_expected_cache_tree >expect && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 45 | cmp_cache_tree expect |
| 46 | } |
| 47 | |
| 48 | test_invalid_cache_tree () { |
David Turner | 59a8adb | 2014-07-10 17:31:25 -0700 | [diff] [blame] | 49 | printf "invalid %s ()\n" "" "$@" >expect && |
Nguyễn Thái Ngọc Duy | 06ccb29 | 2018-03-24 08:44:39 +0100 | [diff] [blame] | 50 | test-tool dump-cache-tree | |
David Turner | 59a8adb | 2014-07-10 17:31:25 -0700 | [diff] [blame] | 51 | sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual && |
| 52 | test_cmp expect actual |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | test_no_cache_tree () { |
| 56 | : >expect && |
| 57 | cmp_cache_tree expect |
| 58 | } |
| 59 | |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 60 | test_expect_success 'initial commit has cache-tree' ' |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 61 | test_commit foo && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 62 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 63 | ' |
| 64 | |
| 65 | test_expect_success 'read-tree HEAD establishes cache-tree' ' |
| 66 | git read-tree HEAD && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 67 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 68 | ' |
| 69 | |
| 70 | test_expect_success 'git-add invalidates cache-tree' ' |
| 71 | test_when_finished "git reset --hard; git read-tree HEAD" && |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 72 | echo "I changed this file" >foo && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 73 | git add foo && |
| 74 | test_invalid_cache_tree |
| 75 | ' |
| 76 | |
David Turner | 59a8adb | 2014-07-10 17:31:25 -0700 | [diff] [blame] | 77 | test_expect_success 'git-add in subdir invalidates cache-tree' ' |
| 78 | test_when_finished "git reset --hard; git read-tree HEAD" && |
| 79 | mkdir dirx && |
| 80 | echo "I changed this file" >dirx/foo && |
| 81 | git add dirx/foo && |
| 82 | test_invalid_cache_tree |
| 83 | ' |
| 84 | |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 85 | cat >before <<\EOF |
| 86 | SHA (3 entries, 2 subtrees) |
| 87 | SHA dir1/ (1 entries, 0 subtrees) |
| 88 | SHA dir2/ (1 entries, 0 subtrees) |
| 89 | EOF |
| 90 | |
| 91 | cat >expect <<\EOF |
| 92 | invalid (2 subtrees) |
| 93 | invalid dir1/ (0 subtrees) |
| 94 | SHA dir2/ (1 entries, 0 subtrees) |
| 95 | EOF |
| 96 | |
David Turner | 59a8adb | 2014-07-10 17:31:25 -0700 | [diff] [blame] | 97 | test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' ' |
| 98 | git tag no-children && |
| 99 | test_when_finished "git reset --hard no-children; git read-tree HEAD" && |
| 100 | mkdir dir1 dir2 && |
| 101 | test_commit dir1/a && |
| 102 | test_commit dir2/b && |
| 103 | echo "I changed this file" >dir1/a && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 104 | cmp_cache_tree before && |
| 105 | echo "I changed this file" >dir1/a && |
David Turner | 59a8adb | 2014-07-10 17:31:25 -0700 | [diff] [blame] | 106 | git add dir1/a && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 107 | cmp_cache_tree expect |
David Turner | 59a8adb | 2014-07-10 17:31:25 -0700 | [diff] [blame] | 108 | ' |
| 109 | |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 110 | test_expect_success 'update-index invalidates cache-tree' ' |
| 111 | test_when_finished "git reset --hard; git read-tree HEAD" && |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 112 | echo "I changed this file" >foo && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 113 | git update-index --add foo && |
| 114 | test_invalid_cache_tree |
| 115 | ' |
| 116 | |
| 117 | test_expect_success 'write-tree establishes cache-tree' ' |
Nguyễn Thái Ngọc Duy | ff5fb8b | 2018-03-24 08:44:56 +0100 | [diff] [blame] | 118 | test-tool scrap-cache-tree && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 119 | git write-tree && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 120 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 121 | ' |
| 122 | |
Nguyễn Thái Ngọc Duy | ff5fb8b | 2018-03-24 08:44:56 +0100 | [diff] [blame] | 123 | test_expect_success 'test-tool scrap-cache-tree works' ' |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 124 | git read-tree HEAD && |
Nguyễn Thái Ngọc Duy | ff5fb8b | 2018-03-24 08:44:56 +0100 | [diff] [blame] | 125 | test-tool scrap-cache-tree && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 126 | test_no_cache_tree |
| 127 | ' |
| 128 | |
Thomas Rast | 11c8a74 | 2011-12-06 18:43:38 +0100 | [diff] [blame] | 129 | test_expect_success 'second commit has cache-tree' ' |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 130 | test_commit bar && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 131 | test_cache_tree |
| 132 | ' |
| 133 | |
Jeff King | 5a97639 | 2014-11-18 12:22:31 -0500 | [diff] [blame] | 134 | test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' ' |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 135 | cat <<-\EOT >foo.c && |
| 136 | int foo() |
| 137 | { |
| 138 | return 42; |
| 139 | } |
| 140 | int bar() |
| 141 | { |
| 142 | return 42; |
| 143 | } |
| 144 | EOT |
| 145 | git add foo.c && |
| 146 | test_invalid_cache_tree && |
| 147 | git commit -m "add a file" && |
| 148 | test_cache_tree && |
| 149 | cat <<-\EOT >foo.c && |
| 150 | int foo() |
| 151 | { |
| 152 | return 43; |
| 153 | } |
| 154 | int bar() |
| 155 | { |
| 156 | return 44; |
| 157 | } |
| 158 | EOT |
| 159 | (echo p; echo 1; echo; echo s; echo n; echo y; echo q) | |
| 160 | git commit --interactive -m foo && |
| 161 | test_cache_tree |
| 162 | ' |
| 163 | |
| 164 | test_expect_success 'commit in child dir has cache-tree' ' |
| 165 | mkdir dir && |
| 166 | >dir/child.t && |
| 167 | git add dir/child.t && |
| 168 | git commit -m dir/child.t && |
| 169 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 170 | ' |
| 171 | |
Thomas Rast | 6c52ec8 | 2011-12-06 18:43:39 +0100 | [diff] [blame] | 172 | test_expect_success 'reset --hard gives cache-tree' ' |
Nguyễn Thái Ngọc Duy | ff5fb8b | 2018-03-24 08:44:56 +0100 | [diff] [blame] | 173 | test-tool scrap-cache-tree && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 174 | git reset --hard && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 175 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 176 | ' |
| 177 | |
Thomas Rast | 6c52ec8 | 2011-12-06 18:43:39 +0100 | [diff] [blame] | 178 | test_expect_success 'reset --hard without index gives cache-tree' ' |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 179 | rm -f .git/index && |
| 180 | git reset --hard && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 181 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 182 | ' |
| 183 | |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 184 | test_expect_success 'checkout gives cache-tree' ' |
| 185 | git tag current && |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 186 | git checkout HEAD^ && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 187 | test_cache_tree |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 188 | ' |
| 189 | |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 190 | test_expect_success 'checkout -b gives cache-tree' ' |
| 191 | git checkout current && |
| 192 | git checkout -b prev HEAD^ && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 193 | test_cache_tree |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 194 | ' |
| 195 | |
| 196 | test_expect_success 'checkout -B gives cache-tree' ' |
| 197 | git checkout current && |
| 198 | git checkout -B prev HEAD^ && |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 199 | test_cache_tree |
| 200 | ' |
| 201 | |
Brian Degenhardt | 52fca21 | 2015-07-28 15:30:40 -0400 | [diff] [blame] | 202 | test_expect_success 'merge --ff-only maintains cache-tree' ' |
| 203 | git checkout current && |
| 204 | git checkout -b changes && |
| 205 | test_commit llamas && |
| 206 | test_commit pachyderm && |
| 207 | test_cache_tree && |
| 208 | git checkout current && |
| 209 | test_cache_tree && |
| 210 | git merge --ff-only changes && |
| 211 | test_cache_tree |
| 212 | ' |
| 213 | |
| 214 | test_expect_success 'merge maintains cache-tree' ' |
| 215 | git checkout current && |
| 216 | git checkout -b changes2 && |
| 217 | test_commit alpacas && |
| 218 | test_cache_tree && |
| 219 | git checkout current && |
| 220 | test_commit struthio && |
| 221 | test_cache_tree && |
| 222 | git merge changes2 && |
| 223 | test_cache_tree |
| 224 | ' |
| 225 | |
David Turner | 9c4d6c0 | 2014-07-13 13:28:19 -0700 | [diff] [blame] | 226 | test_expect_success 'partial commit gives cache-tree' ' |
| 227 | git checkout -b partial no-children && |
| 228 | test_commit one && |
| 229 | test_commit two && |
| 230 | echo "some change" >one.t && |
| 231 | git add one.t && |
| 232 | echo "some other change" >two.t && |
| 233 | git commit two.t -m partial && |
| 234 | test_cache_tree |
David Turner | aecf567 | 2014-07-05 21:06:56 -0700 | [diff] [blame] | 235 | ' |
| 236 | |
Junio C Hamano | 4ed115e | 2014-09-02 14:16:20 -0700 | [diff] [blame] | 237 | test_expect_success 'no phantom error when switching trees' ' |
| 238 | mkdir newdir && |
| 239 | >newdir/one && |
| 240 | git add newdir/one && |
| 241 | git checkout 2>errors && |
| 242 | ! test -s errors |
| 243 | ' |
| 244 | |
David Turner | 475a344 | 2015-08-27 13:07:54 -0400 | [diff] [blame] | 245 | test_expect_success 'switching trees does not invalidate shared index' ' |
| 246 | git update-index --split-index && |
| 247 | >split && |
| 248 | git add split && |
Nguyễn Thái Ngọc Duy | 8133061 | 2018-03-24 08:44:40 +0100 | [diff] [blame] | 249 | test-tool dump-split-index .git/index | grep -v ^own >before && |
David Turner | 475a344 | 2015-08-27 13:07:54 -0400 | [diff] [blame] | 250 | git commit -m "as-is" && |
Nguyễn Thái Ngọc Duy | 8133061 | 2018-03-24 08:44:40 +0100 | [diff] [blame] | 251 | test-tool dump-split-index .git/index | grep -v ^own >after && |
David Turner | 475a344 | 2015-08-27 13:07:54 -0400 | [diff] [blame] | 252 | test_cmp before after |
| 253 | ' |
| 254 | |
Thomas Rast | 4eb0346 | 2011-12-06 18:43:36 +0100 | [diff] [blame] | 255 | test_done |