commit: fix "--amend --only" with no pathspec

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>

Jeff King committed Jul 10, 2012 at 16:40 UTC ea2d4ed35902ce15959965ab86d80527731a177c
2 files changed +14 -1
builtin-commit.c
+4 -1
@@ -188,6 +188,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
188 int i;
189 char *m;
190
191 + if (!pattern)
192 + return 0;
193 +
194 for (i = 0; pattern[i]; i++)
195 ;
196 m = xcalloc(1, i);
@@ -324,7 +327,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
327 * hooks on the real index, and create commit from the_index.
328 * We still need to refresh the index here.
329 */
327 - if (!pathspec || !*pathspec) {
330 + if (!only && (!pathspec || !*pathspec)) {
331 fd = hold_locked_index(&index_lock, 1);
332 refresh_cache_or_die(refresh_flags);
333 if (write_cache(fd, active_cache, active_nr) ||
t/t7501-commit.sh
+10
@@ -88,6 +88,16 @@ test_expect_success \
88 "amend commit" \
89 "EDITOR=./editor git commit --amend"
90
91 +test_expect_success 'amend --only ignores staged contents' '
92 + cp file file.expect &&
93 + echo changed >file &&
94 + git add file &&
95 + git commit --no-edit --amend --only &&
96 + git cat-file blob HEAD:file >file.actual &&
97 + test_cmp file.expect file.actual &&
98 + git diff --exit-code
99 +'
100 +
101 test_expect_success \
102 "passing -m and -F" \
103 "echo 'enough with the bongos' >file && \