3
# Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
4
#
5
6
-# FIXME: Test the various index usages, -i and -o, test reflog,
6
+# FIXME: Test the various index usages, test reflog,
7
# signoff
8
9
test_description='git commit'
92
test_must_fail git commit -m initial --long
93
'
94
95
+test_expect_success 'fail to commit untracked file (even with --include/--only)' '
96
+ echo content >baz &&
97
+ error="error: pathspec .baz. did not match any file(s) known to git" &&
98
+
99
+ test_must_fail git commit -m "baz" baz 2>err &&
100
+ test_grep -e "$error" err &&
101
+
102
+ test_must_fail git commit --only -m "baz" baz 2>err &&
103
+ test_grep -e "$error" err &&
104
+
105
+ # TODO: as for --include, the below command will fail because
106
+ # nothing is staged. If something was staged, it would not fail
107
+ # even though the provided pathspec does not match any tracked
108
+ # path. (However, the untracked paths that match the pathspec are
109
+ # not committed and only the staged changes get committed.)
110
+ # In either cases, no error is returned to stderr like in (--only
111
+ # and without --only/--include) cases. In a similar manner,
112
+ # "git add -u baz" also does not error out.
113
+ #
114
+ # Therefore, the below test is just to document the current behavior
115
+ # and is not an endorsement to the current behavior, and we may
116
+ # want to fix this. And when that happens, this test should be
117
+ # updated accordingly.
118
+
119
+ test_must_fail git commit --include -m "baz" baz 2>err &&
120
+ test_must_be_empty err
121
+'
122
+
123
test_expect_success 'setup: non-initial commit' '
124
echo bongo bongo bongo >file &&
125
git commit -m next -a
145
git commit -m next -a --long
146
'
147
148
+for opt in "" "-o" "--only"
149
+do
150
+ test_expect_success 'exclude additional staged changes when given pathspec' '
151
+ echo content >>file &&
152
+ echo content >>baz &&
153
+ git add baz &&
154
+ git commit $opt -m "file" file &&
155
+
156
+ git diff --name-only >actual &&
157
+ test_must_be_empty actual &&
158
+
159
+ test_write_lines baz >expect &&
160
+ git diff --name-only --cached >actual &&
161
+ test_cmp expect actual &&
162
+
163
+ test_write_lines file >expect &&
164
+ git diff --name-only HEAD^ HEAD >actual &&
165
+ test_cmp expect actual
166
+ '
167
+done
168
+
169
+test_expect_success '-i/--include includes staged changes' '
170
+ echo content >>file &&
171
+ echo content >>baz &&
172
+ git add file &&
173
+
174
+ # baz is in the index, therefore, it will be committed
175
+ git commit --include -m "file and baz" baz &&
176
+
177
+ git diff --name-only HEAD >remaining &&
178
+ test_must_be_empty remaining &&
179
+
180
+ test_write_lines baz file >expect &&
181
+ git diff --name-only HEAD^ HEAD >actual &&
182
+ test_cmp expect actual
183
+'
184
+
185
+test_expect_success '--include and --only do not mix' '
186
+ test_when_finished "git reset --hard" &&
187
+ echo content >>file &&
188
+ echo content >>baz &&
189
+ test_must_fail git commit --include --only -m "file baz" file baz 2>actual &&
190
+ test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual
191
+'
192
+
193
test_expect_success 'commit message from non-existing file' '
194
echo more bongo: bongo bongo bongo bongo >file &&
195
test_must_fail git commit -F gah -a