blob: 9bf2bdffd24b773a2eec636efdd534269f45e74b [file] [log] [blame]
Jeff King93c44d42007-05-12 02:42:00 -04001#!/bin/sh
2
Junio C Hamano25487bd2007-11-11 18:44:16 -08003test_description='git add -u
Jeff King93c44d42007-05-12 02:42:00 -04004
5This test creates a working tree state with three files:
6
7 top (previously committed, modified)
8 dir/sub (previously committed, modified)
9 dir/other (untracked)
10
Junio C Hamano5be60072007-07-02 22:52:14 -070011and issues a git add -u with path limiting on "dir" to add
Junio C Hamano25487bd2007-11-11 18:44:16 -080012only the updates to dir/sub.
13
14Also tested are "git add -u" without limiting, and "git add -u"
Junio C Hamano4cc8d6c2009-01-28 14:24:53 -080015without contents changes, and other conditions'
Jeff King93c44d42007-05-12 02:42:00 -040016
17. ./test-lib.sh
18
Junio C Hamanoa4882c22007-08-15 14:12:14 -070019test_expect_success setup '
20 echo initial >check &&
21 echo initial >top &&
Benoit Sigoure43b98ac2007-09-14 10:29:04 +020022 echo initial >foo &&
Junio C Hamanoa4882c22007-08-15 14:12:14 -070023 mkdir dir1 dir2 &&
24 echo initial >dir1/sub1 &&
25 echo initial >dir1/sub2 &&
26 echo initial >dir2/sub3 &&
Benoit Sigoure43b98ac2007-09-14 10:29:04 +020027 git add check dir1 dir2 top foo &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -050028 test_tick &&
Nanako Shiraishi0cb0e142008-09-03 17:59:27 +090029 git commit -m initial &&
Junio C Hamanoa4882c22007-08-15 14:12:14 -070030
31 echo changed >check &&
32 echo changed >top &&
33 echo changed >dir2/sub3 &&
34 rm -f dir1/sub1 &&
35 echo other >dir2/other
Jeff King93c44d42007-05-12 02:42:00 -040036'
37
Junio C Hamanoa4882c22007-08-15 14:12:14 -070038test_expect_success update '
39 git add -u dir1 dir2
40'
Jeff King93c44d42007-05-12 02:42:00 -040041
Junio C Hamanoa4882c22007-08-15 14:12:14 -070042test_expect_success 'update noticed a removal' '
Nanako Shiraishi0cb0e142008-09-03 17:59:27 +090043 test "$(git ls-files dir1/sub1)" = ""
Junio C Hamanoa4882c22007-08-15 14:12:14 -070044'
Jeff King93c44d42007-05-12 02:42:00 -040045
Junio C Hamanoa4882c22007-08-15 14:12:14 -070046test_expect_success 'update touched correct path' '
Nanako Shiraishi0cb0e142008-09-03 17:59:27 +090047 test "$(git diff-files --name-status dir2/sub3)" = ""
Junio C Hamanoa4882c22007-08-15 14:12:14 -070048'
Jeff King93c44d42007-05-12 02:42:00 -040049
Junio C Hamanoa4882c22007-08-15 14:12:14 -070050test_expect_success 'update did not touch other tracked files' '
Nanako Shiraishi0cb0e142008-09-03 17:59:27 +090051 test "$(git diff-files --name-status check)" = "M check" &&
52 test "$(git diff-files --name-status top)" = "M top"
Junio C Hamanoa4882c22007-08-15 14:12:14 -070053'
54
55test_expect_success 'update did not touch untracked files' '
Nanako Shiraishi0cb0e142008-09-03 17:59:27 +090056 test "$(git ls-files dir2/other)" = ""
Junio C Hamanoa4882c22007-08-15 14:12:14 -070057'
58
59test_expect_success 'cache tree has not been corrupted' '
60
61 git ls-files -s |
62 sed -e "s/ 0 / /" >expect &&
63 git ls-tree -r $(git write-tree) |
64 sed -e "s/ blob / /" >current &&
Jeff King82ebb0b2008-03-12 17:36:36 -040065 test_cmp expect current
Junio C Hamanoa4882c22007-08-15 14:12:14 -070066
67'
Jeff King93c44d42007-05-12 02:42:00 -040068
Salikh Zakirov2ed2c222007-08-16 02:01:43 +090069test_expect_success 'update from a subdirectory' '
70 (
71 cd dir1 &&
72 echo more >sub2 &&
73 git add -u sub2
74 )
75'
76
77test_expect_success 'change gets noticed' '
78
79 test "$(git diff-files --name-status dir1)" = ""
80
81'
Jeff King93c44d42007-05-12 02:42:00 -040082
Jeff King7bf7a922013-03-14 02:44:04 -040083# Note that this is scheduled to change in Git 2.0, when
84# "git add -u" will become full-tree by default.
85test_expect_success 'non-limited update in subdir leaves root alone' '
86 (
87 cd dir1 &&
88 echo even more >>sub2 &&
89 git add -u
90 ) &&
91 cat >expect <<-\EOF &&
92 check
93 top
94 EOF
95 git diff-files --name-only >actual &&
96 test_cmp expect actual
97'
98
Johannes Sixt889c6f02013-06-07 22:53:28 +020099test_expect_success 'replace a file with a symlink' '
Benoit Sigoure43b98ac2007-09-14 10:29:04 +0200100
101 rm foo &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200102 test_ln_s_add top foo
Benoit Sigoure43b98ac2007-09-14 10:29:04 +0200103
104'
105
Junio C Hamano25487bd2007-11-11 18:44:16 -0800106test_expect_success 'add everything changed' '
107
108 git add -u &&
109 test -z "$(git diff-files)"
110
111'
112
113test_expect_success 'touch and then add -u' '
114
115 touch check &&
116 git add -u &&
117 test -z "$(git diff-files)"
118
119'
120
121test_expect_success 'touch and then add explicitly' '
122
123 touch check &&
124 git add check &&
125 test -z "$(git diff-files)"
126
127'
128
Junio C Hamanoc36f9412011-04-12 15:50:55 -0700129test_expect_success 'add -n -u should not add but just report' '
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700130
131 (
132 echo "add '\''check'\''" &&
133 echo "remove '\''top'\''"
134 ) >expect &&
135 before=$(git ls-files -s check top) &&
136 echo changed >>check &&
137 rm -f top &&
138 git add -n -u >actual &&
139 after=$(git ls-files -s check top) &&
140
141 test "$before" = "$after" &&
Junio C Hamanoc36f9412011-04-12 15:50:55 -0700142 test_i18ncmp expect actual
Junio C Hamano38ed1d82008-05-21 12:04:34 -0700143
144'
145
Junio C Hamano4cc8d6c2009-01-28 14:24:53 -0800146test_expect_success 'add -u resolves unmerged paths' '
147 git reset --hard &&
148 one=$(echo 1 | git hash-object -w --stdin) &&
149 two=$(echo 2 | git hash-object -w --stdin) &&
150 three=$(echo 3 | git hash-object -w --stdin) &&
151 {
152 for path in path1 path2
153 do
154 echo "100644 $one 1 $path"
155 echo "100644 $two 2 $path"
156 echo "100644 $three 3 $path"
157 done
158 echo "100644 $one 1 path3"
159 echo "100644 $one 1 path4"
160 echo "100644 $one 3 path5"
161 echo "100644 $one 3 path6"
162 } |
163 git update-index --index-info &&
164 echo 3 >path1 &&
165 echo 2 >path3 &&
166 echo 2 >path5 &&
Junio C Hamano75973b22011-04-20 18:11:19 -0700167
Junio C Hamano45c45e32011-04-19 12:18:20 -0700168 # Fail to explicitly resolve removed paths with "git add"
169 test_must_fail git add --no-all path4 &&
170 test_must_fail git add --no-all path6 &&
Junio C Hamano75973b22011-04-20 18:11:19 -0700171
172 # "add -u" should notice removals no matter what stages
173 # the index entries are in.
Junio C Hamano4cc8d6c2009-01-28 14:24:53 -0800174 git add -u &&
Johannes Sixt0aaaef72009-02-09 10:24:51 +0100175 git ls-files -s path1 path2 path3 path4 path5 path6 >actual &&
Junio C Hamano4cc8d6c2009-01-28 14:24:53 -0800176 {
177 echo "100644 $three 0 path1"
Junio C Hamano4cc8d6c2009-01-28 14:24:53 -0800178 echo "100644 $two 0 path3"
179 echo "100644 $two 0 path5"
Junio C Hamano75973b22011-04-20 18:11:19 -0700180 } >expect &&
181 test_cmp expect actual
Junio C Hamano4cc8d6c2009-01-28 14:24:53 -0800182'
183
Chris Packham1e7ef742010-02-09 17:30:48 -0500184test_expect_success '"add -u non-existent" should fail' '
185 test_must_fail git add -u non-existent &&
186 ! (git ls-files | grep "non-existent")
187'
188
Jeff King93c44d42007-05-12 02:42:00 -0400189test_done