Skip to content

Commit ea2d4ed

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbcab75 commit ea2d4ed

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

builtin-commit.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
188188
int i;
189189
char *m;
190190

191+
if (!pattern)
192+
return 0;
193+
191194
for (i = 0; pattern[i]; i++)
192195
;
193196
m = xcalloc(1, i);
@@ -324,7 +327,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
324327
* hooks on the real index, and create commit from the_index.
325328
* We still need to refresh the index here.
326329
*/
327-
if (!pathspec || !*pathspec) {
330+
if (!only && (!pathspec || !*pathspec)) {
328331
fd = hold_locked_index(&index_lock, 1);
329332
refresh_cache_or_die(refresh_flags);
330333
if (write_cache(fd, active_cache, active_nr) ||

t/t7501-commit.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ test_expect_success \
8888
"amend commit" \
8989
"EDITOR=./editor git commit --amend"
9090

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+
91101
test_expect_success \
92102
"passing -m and -F" \
93103
"echo 'enough with the bongos' >file && \

0 commit comments

Comments
 (0)