OSDN Git Service

commit: fix "--amend --only" with no pathspec
authorJeff King <peff@peff.net>
Tue, 10 Jul 2012 20:40:29 +0000 (16:40 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 10 Jul 2012 21:16:41 +0000 (14:16 -0700)
When we do not have any pathspec, we typically disallow an
explicit "--only", because it makes no sense (your commit
would, by definition, be empty). But since 6a74642
(git-commit --amend: two fixes., 2006-04-20), we have
allowed "--amend --only" with the intent that it would amend
the commit, ignoring any contents staged in the index.

However, while that commit allowed the combination, we never
actually implemented the logic to make it work. The current
code notices that we have no pathspec and assumes we want to
do an as-is commit (i.e., the "--only" is ignored).

Instead, we must make sure to follow the partial-commit
code-path. We also need to tweak the list_paths function to
handle a NULL pathspec.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-commit.c
t/t7501-commit.sh

index f4c7344..b34fb03 100644 (file)
@@ -188,6 +188,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
        int i;
        char *m;
 
+       if (!pattern)
+               return 0;
+
        for (i = 0; pattern[i]; i++)
                ;
        m = xcalloc(1, i);
@@ -324,7 +327,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
         * hooks on the real index, and create commit from the_index.
         * We still need to refresh the index here.
         */
-       if (!pathspec || !*pathspec) {
+       if (!only && (!pathspec || !*pathspec)) {
                fd = hold_locked_index(&index_lock, 1);
                refresh_cache_or_die(refresh_flags);
                if (write_cache(fd, active_cache, active_nr) ||
index 7940901..e1181f0 100755 (executable)
@@ -88,6 +88,16 @@ test_expect_success \
        "amend commit" \
        "EDITOR=./editor git commit --amend"
 
+test_expect_success 'amend --only ignores staged contents' '
+       cp file file.expect &&
+       echo changed >file &&
+       git add file &&
+       git commit --no-edit --amend --only &&
+       git cat-file blob HEAD:file >file.actual &&
+       test_cmp file.expect file.actual &&
+       git diff --exit-code
+'
+
 test_expect_success \
        "passing -m and -F" \
        "echo 'enough with the bongos' >file && \