| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='Intent to add' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | test_expect_success 'intent to add' ' |
| 8 | test_commit 1 && |
| 9 | git rm 1.t && |
| 10 | echo hello >1.t && |
| 11 | echo hello >file && |
| 12 | echo hello >elif && |
| 13 | git add -N file && |
| 14 | git add elif && |
| 15 | git add -N 1.t |
| 16 | ' |
| 17 | |
| 18 | test_expect_success 'git status' ' |
| 19 | git status --porcelain >actual.raw && |
| 20 | grep -v actual actual.raw >actual && |
| 21 | cat >expect <<-\EOF && |
| 22 | DA 1.t |
| 23 | A elif |
| 24 | A file |
| 25 | EOF |
| 26 | test_cmp expect actual |
| 27 | ' |
| 28 | |
| 29 | test_expect_success 'git status with porcelain v2' ' |
| 30 | git status --porcelain=v2 >actual.raw && |
| 31 | grep -v "^?" actual.raw >actual && |
| 32 | nam1=$(echo 1 | git hash-object --stdin) && |
| 33 | nam2=$(git hash-object elif) && |
| 34 | cat >expect <<-EOF && |
| 35 | 1 DA N... 100644 000000 100644 $nam1 $ZERO_OID 1.t |
| 36 | 1 A. N... 000000 100644 100644 $ZERO_OID $nam2 elif |
| 37 | 1 .A N... 000000 000000 100644 $ZERO_OID $ZERO_OID file |
| 38 | EOF |
| 39 | test_cmp expect actual |
| 40 | ' |
| 41 | |
| 42 | test_expect_success 'check result of "add -N"' ' |
| 43 | git ls-files -s file >actual && |
| 44 | empty=$(git hash-object --stdin </dev/null) && |
| 45 | echo "100644 $empty 0 file" >expect && |
| 46 | test_cmp expect actual |
| 47 | ' |
| 48 | |
| 49 | test_expect_success 'intent to add is just an ordinary empty blob' ' |
| 50 | git add -u && |
| 51 | git ls-files -s file >actual && |
| 52 | git ls-files -s elif | sed -e "s/elif/file/" >expect && |
| 53 | test_cmp expect actual |
| 54 | ' |
| 55 | |
| 56 | test_expect_success 'intent to add does not clobber existing paths' ' |
| 57 | git add -N file elif && |
| 58 | empty=$(git hash-object --stdin </dev/null) && |
| 59 | git ls-files -s >actual && |
| 60 | test_grep ! "$empty" actual |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'i-t-a entry is simply ignored' ' |
| 64 | test_tick && |
| 65 | git commit -a -m initial && |
| 66 | git reset --hard && |
| 67 | |
| 68 | echo xyzzy >rezrov && |
| 69 | echo frotz >nitfol && |
| 70 | git add rezrov && |
| 71 | git add -N nitfol && |
| 72 | git commit -m second && |
| 73 | test $(git ls-tree HEAD -- nitfol | wc -l) = 0 && |
| 74 | test $(git diff --name-only HEAD -- nitfol | wc -l) = 1 && |
| 75 | test $(git diff --name-only -- nitfol | wc -l) = 1 |
| 76 | ' |
| 77 | |
| 78 | test_expect_success 'can commit with an unrelated i-t-a entry in index' ' |
| 79 | git reset --hard && |
| 80 | echo bozbar >rezrov && |
| 81 | echo frotz >nitfol && |
| 82 | git add rezrov && |
| 83 | git add -N nitfol && |
| 84 | git commit -m partial rezrov |
| 85 | ' |
| 86 | |
| 87 | test_expect_success 'can "commit -a" with an i-t-a entry' ' |
| 88 | git reset --hard && |
| 89 | : >nitfol && |
| 90 | git add -N nitfol && |
| 91 | git commit -a -m all |
| 92 | ' |
| 93 | |
| 94 | test_expect_success 'cache-tree invalidates i-t-a paths' ' |
| 95 | git reset --hard && |
| 96 | mkdir dir && |
| 97 | : >dir/foo && |
| 98 | git add dir/foo && |
| 99 | git commit -m foo && |
| 100 | |
| 101 | : >dir/bar && |
| 102 | git add -N dir/bar && |
| 103 | git diff --name-only >actual && |
| 104 | echo dir/bar >expect && |
| 105 | test_cmp expect actual && |
| 106 | |
| 107 | git write-tree >/dev/null && |
| 108 | |
| 109 | git diff --name-only >actual && |
| 110 | echo dir/bar >expect && |
| 111 | test_cmp expect actual |
| 112 | ' |
| 113 | |
| 114 | test_expect_success 'cache-tree does not ignore dir that has i-t-a entries' ' |
| 115 | git init ita-in-dir && |
| 116 | ( |
| 117 | cd ita-in-dir && |
| 118 | mkdir 2 && |
| 119 | for f in 1 2/1 2/2 3 |
| 120 | do |
| 121 | echo "$f" >"$f" || return 1 |
| 122 | done && |
| 123 | git add 1 2/2 3 && |
| 124 | git add -N 2/1 && |
| 125 | git commit -m committed && |
| 126 | git ls-tree -r HEAD >actual && |
| 127 | test_grep 2/2 actual |
| 128 | ) |
| 129 | ' |
| 130 | |
| 131 | test_expect_success 'cache-tree does skip dir that becomes empty' ' |
| 132 | rm -fr ita-in-dir && |
| 133 | git init ita-in-dir && |
| 134 | ( |
| 135 | cd ita-in-dir && |
| 136 | mkdir -p 1/2/3 && |
| 137 | echo 4 >1/2/3/4 && |
| 138 | git add -N 1/2/3/4 && |
| 139 | git write-tree >actual && |
| 140 | echo $EMPTY_TREE >expected && |
| 141 | test_cmp expected actual |
| 142 | ) |
| 143 | ' |
| 144 | |
| 145 | test_expect_success 'commit: ita entries ignored in empty initial commit check' ' |
| 146 | git init empty-initial-commit && |
| 147 | ( |
| 148 | cd empty-initial-commit && |
| 149 | : >one && |
| 150 | git add -N one && |
| 151 | test_must_fail git commit -m nothing-new-here |
| 152 | ) |
| 153 | ' |
| 154 | |
| 155 | test_expect_success 'commit: ita entries ignored in empty commit check' ' |
| 156 | git init empty-subsequent-commit && |
| 157 | ( |
| 158 | cd empty-subsequent-commit && |
| 159 | test_commit one && |
| 160 | : >two && |
| 161 | git add -N two && |
| 162 | test_must_fail git commit -m nothing-new-here |
| 163 | ) |
| 164 | ' |
| 165 | |
| 166 | test_expect_success 'rename detection finds the right names' ' |
| 167 | git init rename-detection && |
| 168 | ( |
| 169 | cd rename-detection && |
| 170 | echo contents >first && |
| 171 | git add first && |
| 172 | git commit -m first && |
| 173 | mv first third && |
| 174 | git add -N third && |
| 175 | |
| 176 | git status >actual.raw.1 && |
| 177 | grep -v "^?" actual.raw.1 >actual.1 && |
| 178 | test_grep "renamed: *first -> third" actual.1 && |
| 179 | |
| 180 | git status --porcelain >actual.raw.2 && |
| 181 | grep -v "^?" actual.raw.2 >actual.2 && |
| 182 | cat >expected.2 <<-\EOF && |
| 183 | R first -> third |
| 184 | EOF |
| 185 | test_cmp expected.2 actual.2 && |
| 186 | |
| 187 | hash=$(git hash-object third) && |
| 188 | git status --porcelain=v2 >actual.raw.3 && |
| 189 | grep -v "^?" actual.raw.3 >actual.3 && |
| 190 | cat >expected.3 <<-EOF && |
| 191 | 2 .R N... 100644 100644 100644 $hash $hash R100 third first |
| 192 | EOF |
| 193 | test_cmp expected.3 actual.3 && |
| 194 | |
| 195 | git diff --stat >actual.4 && |
| 196 | cat >expected.4 <<-EOF && |
| 197 | first => third | 0 |
| 198 | 1 file changed, 0 insertions(+), 0 deletions(-) |
| 199 | EOF |
| 200 | test_cmp expected.4 actual.4 && |
| 201 | |
| 202 | git diff --cached --stat >actual.5 && |
| 203 | test_must_be_empty actual.5 |
| 204 | |
| 205 | ) |
| 206 | ' |
| 207 | |
| 208 | test_expect_success 'double rename detection in status' ' |
| 209 | git init rename-detection-2 && |
| 210 | ( |
| 211 | cd rename-detection-2 && |
| 212 | echo contents >first && |
| 213 | git add first && |
| 214 | git commit -m first && |
| 215 | git mv first second && |
| 216 | mv second third && |
| 217 | git add -N third && |
| 218 | |
| 219 | git status >actual.raw.1 && |
| 220 | grep -v "^?" actual.raw.1 >actual.1 && |
| 221 | test_grep "renamed: *first -> second" actual.1 && |
| 222 | test_grep "renamed: *second -> third" actual.1 && |
| 223 | |
| 224 | git status --porcelain >actual.raw.2 && |
| 225 | grep -v "^?" actual.raw.2 >actual.2 && |
| 226 | cat >expected.2 <<-\EOF && |
| 227 | R first -> second |
| 228 | R second -> third |
| 229 | EOF |
| 230 | test_cmp expected.2 actual.2 && |
| 231 | |
| 232 | hash=$(git hash-object third) && |
| 233 | git status --porcelain=v2 >actual.raw.3 && |
| 234 | grep -v "^?" actual.raw.3 >actual.3 && |
| 235 | cat >expected.3 <<-EOF && |
| 236 | 2 R. N... 100644 100644 100644 $hash $hash R100 second first |
| 237 | 2 .R N... 100644 100644 100644 $hash $hash R100 third second |
| 238 | EOF |
| 239 | test_cmp expected.3 actual.3 |
| 240 | ) |
| 241 | ' |
| 242 | |
| 243 | test_expect_success 'i-t-a files shown as new for "diff", "diff-files"; not-new for "diff --cached"' ' |
| 244 | git reset --hard && |
| 245 | : >empty && |
| 246 | content="foo" && |
| 247 | echo "$content" >not-empty && |
| 248 | |
| 249 | hash_e=$(git hash-object empty) && |
| 250 | hash_n=$(git hash-object not-empty) && |
| 251 | |
| 252 | cat >expect.diff_p <<-EOF && |
| 253 | diff --git a/empty b/empty |
| 254 | new file mode 100644 |
| 255 | index 0000000..$(git rev-parse --short $hash_e) |
| 256 | diff --git a/not-empty b/not-empty |
| 257 | new file mode 100644 |
| 258 | index 0000000..$(git rev-parse --short $hash_n) |
| 259 | --- /dev/null |
| 260 | +++ b/not-empty |
| 261 | @@ -0,0 +1 @@ |
| 262 | +$content |
| 263 | EOF |
| 264 | cat >expect.diff_s <<-EOF && |
| 265 | create mode 100644 empty |
| 266 | create mode 100644 not-empty |
| 267 | EOF |
| 268 | cat >expect.diff_a <<-EOF && |
| 269 | :000000 100644 0000000 0000000 A$(printf "\t")empty |
| 270 | :000000 100644 0000000 0000000 A$(printf "\t")not-empty |
| 271 | EOF |
| 272 | |
| 273 | git add -N empty not-empty && |
| 274 | |
| 275 | git diff >actual && |
| 276 | test_cmp expect.diff_p actual && |
| 277 | |
| 278 | git diff --summary >actual && |
| 279 | test_cmp expect.diff_s actual && |
| 280 | |
| 281 | git diff-files -p >actual && |
| 282 | test_cmp expect.diff_p actual && |
| 283 | |
| 284 | git diff-files --abbrev >actual && |
| 285 | test_cmp expect.diff_a actual && |
| 286 | |
| 287 | git diff --cached >actual && |
| 288 | test_must_be_empty actual |
| 289 | ' |
| 290 | |
| 291 | test_expect_success '"diff HEAD" includes ita as new files' ' |
| 292 | git reset --hard && |
| 293 | echo new >new-ita && |
| 294 | oid=$(git hash-object new-ita) && |
| 295 | oid=$(git rev-parse --short $oid) && |
| 296 | git add -N new-ita && |
| 297 | git diff HEAD >actual && |
| 298 | cat >expected <<-EOF && |
| 299 | diff --git a/new-ita b/new-ita |
| 300 | new file mode 100644 |
| 301 | index 0000000..$oid |
| 302 | --- /dev/null |
| 303 | +++ b/new-ita |
| 304 | @@ -0,0 +1 @@ |
| 305 | +new |
| 306 | EOF |
| 307 | test_cmp expected actual |
| 308 | ' |
| 309 | |
| 310 | test_expect_success 'apply --intent-to-add' ' |
| 311 | git reset --hard && |
| 312 | echo new >new-ita && |
| 313 | git add -N new-ita && |
| 314 | git diff >expected && |
| 315 | test_grep "new file" expected && |
| 316 | git reset --hard && |
| 317 | git apply --intent-to-add expected && |
| 318 | git diff >actual && |
| 319 | test_cmp expected actual |
| 320 | ' |
| 321 | |
| 322 | test_done |