commit: make --only --allow-empty work without paths
--only is implied when paths are present, and required them unless --amend. But with --allow-empty it should be allowed as well - it is the only way to create an empty commit in the presence of staged changes. Signed-off-by: Andreas Krey <a.krey@gmx.de> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Andreas Krey committed
Dec 2, 2016 at 23:15 UTC
319d835240b752b94ccb67f6515759824955937b
3 files changed
+12
-2
Documentation/git-commit.txt
+2
-1
@@ -263,7 +263,8 @@ FROM UPSTREAM REBASE" section in linkgit:git-rebase[1].)
263
If this option is specified together with `--amend`, then
264
no paths need to be specified, which can be used to amend
265
the last commit without committing changes that have
266
- already been staged.
266
+ already been staged. If used together with `--allow-empty`
267
+ paths are also not required, and an empty commit will be created.
268
269
-u[<mode>]::
270
--untracked-files[=<mode>]::
builtin/commit.c
+1
-1
@@ -1201,7 +1201,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
1201
1202
if (also + only + all + interactive > 1)
1203
die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1204
- if (argc == 0 && (also || (only && !amend)))
1204
+ if (argc == 0 && (also || (only && !amend && !allow_empty)))
1205
die(_("No paths with --include/--only does not make sense."));
1206
if (argc == 0 && only && amend)
1207
only_include_assumed = _("Clever... amending the last one with dirty index.");
t/t7501-commit.sh
+9
@@ -155,6 +155,15 @@ test_expect_success 'amend --only ignores staged contents' '
155
git diff --exit-code
156
'
157
158
+test_expect_success 'allow-empty --only ignores staged contents' '
159
+ echo changed-again >file &&
160
+ git add file &&
161
+ git commit --allow-empty --only -m "empty" &&
162
+ git cat-file blob HEAD:file >file.actual &&
163
+ test_cmp file.expect file.actual &&
164
+ git diff --exit-code
165
+'
166
+
167
test_expect_success 'set up editor' '
168
cat >editor <<-\EOF &&
169
#!/bin/sh