1#!/bin/sh
23
test_description='git add -u
45
This test creates a working tree state with three files:
67
top (previously committed, modified)
8dir/sub (previously committed, modified)
9dir/other (untracked)
1011
and issues a git add -u with path limiting on "dir" to add
12only the updates to dir/sub.
1314
Also tested are "git add -u" without limiting, and "git add -u"
15without contents changes, and other conditions'
1617
. ./test-lib.sh
1819
test_expect_success setup '
20echo initial >check &&
21echo initial >top &&
22echo initial >foo &&
23mkdir dir1 dir2 &&
24echo initial >dir1/sub1 &&
25echo initial >dir1/sub2 &&
26echo initial >dir2/sub3 &&
27git add check dir1 dir2 top foo &&
28test_tick
29git commit -m initial &&
3031
echo changed >check &&
32echo changed >top &&
33echo changed >dir2/sub3 &&
34rm -f dir1/sub1 &&
35echo other >dir2/other
36'
3738
test_expect_success update '
39git add -u dir1 dir2
40'
4142
test_expect_success 'update noticed a removal' '
43test "$(git ls-files dir1/sub1)" = ""
44'
4546
test_expect_success 'update touched correct path' '
47test "$(git diff-files --name-status dir2/sub3)" = ""
48'
4950
test_expect_success 'update did not touch other tracked files' '
51test "$(git diff-files --name-status check)" = "M check" &&
52test "$(git diff-files --name-status top)" = "M top"
53'
5455
test_expect_success 'update did not touch untracked files' '
56test "$(git ls-files dir2/other)" = ""
57'
5859
test_expect_success 'cache tree has not been corrupted' '
6061
git ls-files -s |
62sed -e "s/ 0 / /" >expect &&
63git ls-tree -r $(git write-tree) |
64sed -e "s/ blob / /" >current &&
65test_cmp expect current
6667
'
6869
test_expect_success 'update from a subdirectory' '
70(
71cd dir1 &&
72echo more >sub2 &&
73git add -u sub2
74)
75'
7677
test_expect_success 'change gets noticed' '
7879
test "$(git diff-files --name-status dir1)" = ""
8081
'
8283
test_expect_success SYMLINKS 'replace a file with a symlink' '
8485
rm foo &&
86ln -s top foo &&
87git add -u -- foo
8889
'
9091
test_expect_success 'add everything changed' '
9293
git add -u &&
94test -z "$(git diff-files)"
9596
'
9798
test_expect_success 'touch and then add -u' '
99100
touch check &&
101git add -u &&
102test -z "$(git diff-files)"
103104
'
105106
test_expect_success 'touch and then add explicitly' '
107108
touch check &&
109git add check &&
110test -z "$(git diff-files)"
111112
'
113114
test_expect_success 'add -n -u should not add but just report' '
115116
(
117echo "add '\''check'\''" &&
118echo "remove '\''top'\''"
119) >expect &&
120before=$(git ls-files -s check top) &&
121echo changed >>check &&
122rm -f top &&
123git add -n -u >actual &&
124after=$(git ls-files -s check top) &&
125126
test "$before" = "$after" &&
127test_cmp expect actual
128129
'
130131
test_expect_success 'add -u resolves unmerged paths' '
132git reset --hard &&
133one=$(echo 1 | git hash-object -w --stdin) &&
134two=$(echo 2 | git hash-object -w --stdin) &&
135three=$(echo 3 | git hash-object -w --stdin) &&
136{
137for path in path1 path2
138do
139echo "100644 $one 1 $path"
140echo "100644 $two 2 $path"
141echo "100644 $three 3 $path"
142done
143echo "100644 $one 1 path3"
144echo "100644 $one 1 path4"
145echo "100644 $one 3 path5"
146echo "100644 $one 3 path6"
147} |
148git update-index --index-info &&
149echo 3 >path1 &&
150echo 2 >path3 &&
151echo 2 >path5 &&
152git add -u &&
153git ls-files -s path1 path2 path3 path4 path5 path6 >actual &&
154{
155echo "100644 $three 0 path1"
156echo "100644 $one 1 path3"
157echo "100644 $one 1 path4"
158echo "100644 $one 3 path5"
159echo "100644 $one 3 path6"
160} >expect &&
161test_cmp expect actual &&
162163
# Bonus tests. Explicit resolving
164git add path3 path5 &&
165test_must_fail git add path4 &&
166test_must_fail git add path6 &&
167git rm path4 &&
168git rm path6 &&
169170
git ls-files -s "path?" >actual &&
171{
172echo "100644 $three 0 path1"
173echo "100644 $two 0 path3"
174echo "100644 $two 0 path5"
175} >expect
176177
'
178179
test_expect_success '"add -u non-existent" should fail' '
180test_must_fail git add -u non-existent &&
181! (git ls-files | grep "non-existent")
182'
183184
test_done