t7501: add tests for --include and --only

Add tests for --only (-o) and --include (-i). This include testing with or without staged changes for both -i and -o. Also to test for committing untracked files with -i, -o and without -i/-o. Some tests already exist in t7501 for testing --only, however, it is only tested in combination with --amend and --allow-empty and on to-be-born branch. The addition of these tests check, when the pathspec is provided without using -only, that only the files matching the pathspec get committed. This behavior is same when we provide --only and it is checked by the tests. (as --only is the default mode of operation when pathspec is provided.) As for --include, there is no prior test for checking if --include also commits staged changes, thus add test for that. Along with the tests also document a potential bug, in which, when provided with -i and a pathspec that does not match any tracked path, commit does not fail if there are staged changes. And when there are no staged changes commit fails. However, no error is returned to stderr in either of the cases. This is described in the TODO comment before the relevent testcase. And also add a test for checking incompatibilty when using -o and -i together. Thus, these tests belong in t7501 with other similar existing tests, as described in the case of --only. Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Christian Couder <christian.couder@gmail.com> Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ghanshyam Thakkar committed Jan 17, 2024 at 21:43 UTC 4e4f576b0690ba039cc0db40e68ee4143acc9a4c
1 file changed +74 -1
t/t7501-commit-basic-functionality.sh
+74 -1
@@ -3,7 +3,7 @@
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,6 +92,34 @@ test_expect_success '--long fails with nothing to 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
@@ -117,6 +145,51 @@ test_expect_success '--long with stuff to commit returns ok' '
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