Skip to content

Commit 2be9ccf

Browse files
committed
Merge branch 'gt/test-commit-o-i-options'
A few tests to "git commit -o <pathspec>" and "git commit -i <pathspec>" has been added. * gt/test-commit-o-i-options: t7501: add tests for --amend --signoff t7501: add tests for --include and --only
2 parents 93bc02f + cab11f4 commit 2be9ccf

File tree

1 file changed

+96
-2
lines changed

1 file changed

+96
-2
lines changed

t/t7501-commit-basic-functionality.sh

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# Copyright (c) 2007 Kristian Høgsberg <[email protected]>
44
#
55

6-
# FIXME: Test the various index usages, -i and -o, test reflog,
7-
# signoff
6+
# FIXME: Test the various index usages, test reflog
87

98
test_description='git commit'
109
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
@@ -92,6 +91,34 @@ test_expect_success '--long fails with nothing to commit' '
9291
test_must_fail git commit -m initial --long
9392
'
9493

94+
test_expect_success 'fail to commit untracked file (even with --include/--only)' '
95+
echo content >baz &&
96+
error="error: pathspec .baz. did not match any file(s) known to git" &&
97+
98+
test_must_fail git commit -m "baz" baz 2>err &&
99+
test_grep -e "$error" err &&
100+
101+
test_must_fail git commit --only -m "baz" baz 2>err &&
102+
test_grep -e "$error" err &&
103+
104+
# TODO: as for --include, the below command will fail because
105+
# nothing is staged. If something was staged, it would not fail
106+
# even though the provided pathspec does not match any tracked
107+
# path. (However, the untracked paths that match the pathspec are
108+
# not committed and only the staged changes get committed.)
109+
# In either cases, no error is returned to stderr like in (--only
110+
# and without --only/--include) cases. In a similar manner,
111+
# "git add -u baz" also does not error out.
112+
#
113+
# Therefore, the below test is just to document the current behavior
114+
# and is not an endorsement to the current behavior, and we may
115+
# want to fix this. And when that happens, this test should be
116+
# updated accordingly.
117+
118+
test_must_fail git commit --include -m "baz" baz 2>err &&
119+
test_must_be_empty err
120+
'
121+
95122
test_expect_success 'setup: non-initial commit' '
96123
echo bongo bongo bongo >file &&
97124
git commit -m next -a
@@ -117,6 +144,51 @@ test_expect_success '--long with stuff to commit returns ok' '
117144
git commit -m next -a --long
118145
'
119146

147+
for opt in "" "-o" "--only"
148+
do
149+
test_expect_success 'exclude additional staged changes when given pathspec' '
150+
echo content >>file &&
151+
echo content >>baz &&
152+
git add baz &&
153+
git commit $opt -m "file" file &&
154+
155+
git diff --name-only >actual &&
156+
test_must_be_empty actual &&
157+
158+
test_write_lines baz >expect &&
159+
git diff --name-only --cached >actual &&
160+
test_cmp expect actual &&
161+
162+
test_write_lines file >expect &&
163+
git diff --name-only HEAD^ HEAD >actual &&
164+
test_cmp expect actual
165+
'
166+
done
167+
168+
test_expect_success '-i/--include includes staged changes' '
169+
echo content >>file &&
170+
echo content >>baz &&
171+
git add file &&
172+
173+
# baz is in the index, therefore, it will be committed
174+
git commit --include -m "file and baz" baz &&
175+
176+
git diff --name-only HEAD >remaining &&
177+
test_must_be_empty remaining &&
178+
179+
test_write_lines baz file >expect &&
180+
git diff --name-only HEAD^ HEAD >actual &&
181+
test_cmp expect actual
182+
'
183+
184+
test_expect_success '--include and --only do not mix' '
185+
test_when_finished "git reset --hard" &&
186+
echo content >>file &&
187+
echo content >>baz &&
188+
test_must_fail git commit --include --only -m "file baz" file baz 2>actual &&
189+
test_grep -e "fatal: options .-i/--include. and .-o/--only. cannot be used together" actual
190+
'
191+
120192
test_expect_success 'commit message from non-existing file' '
121193
echo more bongo: bongo bongo bongo bongo >file &&
122194
test_must_fail git commit -F gah -a
@@ -389,6 +461,28 @@ test_expect_success 'amend commit to fix date' '
389461
390462
'
391463

464+
test_expect_success 'amend commit to add signoff' '
465+
466+
test_commit "msg" file content &&
467+
git commit --amend --signoff &&
468+
test_commit_message HEAD <<-EOF
469+
msg
470+
471+
Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
472+
EOF
473+
'
474+
475+
test_expect_success 'amend does not add signoff if it already exists' '
476+
477+
test_commit --signoff "tenor" file newcontent &&
478+
git commit --amend --signoff &&
479+
test_commit_message HEAD <<-EOF
480+
tenor
481+
482+
Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
483+
EOF
484+
'
485+
392486
test_expect_success 'commit mentions forced date in output' '
393487
git commit --amend --date=2010-01-02T03:04:05 >output &&
394488
grep "Date: *Sat Jan 2 03:04:05 2010" output

0 commit comments

Comments
 (0)