commit: make --only --allow-empty work without paths
authorAndreas Krey <a.krey@gmx.de>
Fri, 2 Dec 2016 22:15:13 +0000 (23:15 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 5 Dec 2016 20:41:06 +0000 (12:41 -0800)
--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>
Documentation/git-commit.txt
builtin/commit.c
t/t7501-commit.sh
index b0a294d3b5e13b853f6c2c60201c2458788d4f00..374808b966e3d67d7bf24fb54d4da3a6ff6cbc55 100644 (file)
@@ -263,7 +263,8 @@ FROM UPSTREAM REBASE" section in linkgit:git-rebase[1].)
        If this option is specified together with `--amend`, then
        no paths need to be specified, which can be used to amend
        the last commit without committing changes that have
-       already been staged.
+       already been staged. If used together with `--allow-empty`
+       paths are also not required, and an empty commit will be created.
 
 -u[<mode>]::
 --untracked-files[=<mode>]::
index 77e3dc849419e697abe8f2f4c7d753cce17634a8..58b799c10fb242c361f73a1eb588d264b13b222d 100644 (file)
@@ -1201,7 +1201,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
 
        if (also + only + all + interactive > 1)
                die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
-       if (argc == 0 && (also || (only && !amend)))
+       if (argc == 0 && (also || (only && !amend && !allow_empty)))
                die(_("No paths with --include/--only does not make sense."));
        if (argc == 0 && only && amend)
                only_include_assumed = _("Clever... amending the last one with dirty index.");
index d84897a67a3c365e280f88b36f43fc49e1ac9d7b..0d8d893090e18af677b8c63b19c30c574b237ac9 100755 (executable)
@@ -155,6 +155,15 @@ test_expect_success 'amend --only ignores staged contents' '
        git diff --exit-code
 '
 
+test_expect_success 'allow-empty --only ignores staged contents' '
+       echo changed-again >file &&
+       git add file &&
+       git commit --allow-empty --only -m "empty" &&
+       git cat-file blob HEAD:file >file.actual &&
+       test_cmp file.expect file.actual &&
+       git diff --exit-code
+'
+
 test_expect_success 'set up editor' '
        cat >editor <<-\EOF &&
        #!/bin/sh