From: Junio C Hamano Date: Mon, 16 Jul 2012 04:39:48 +0000 (-0700) Subject: Merge branch 'jk/maint-commit-amend-only-no-paths' X-Git-Tag: v1.7.12-rc0~27 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=b9a0801ad0222256e2ddbf38d8bcce9f32e0211c;p=git.git Merge branch 'jk/maint-commit-amend-only-no-paths' "git commit --amend --only --" was meant to allow "Clever" people to rewrite the commit message without making any change even when they have already changes for the next commit added to their index, but it never worked as advertised since it was introduced in 1.3.0 era. * jk/maint-commit-amend-only-no-paths: commit: fix "--amend --only" with no pathspec --- b9a0801ad0222256e2ddbf38d8bcce9f32e0211c diff --cc builtin/commit.c index 6515da278,b34fb0374..95eeab1d5 --- a/builtin/commit.c +++ b/builtin/commit.c @@@ -341,21 -323,16 +344,21 @@@ static char *prepare_index(int argc, co * * (1) return the name of the real index file. * - * The caller should run hooks on the real index, and run - * hooks on the real index, and create commit from the_index. + * The caller should run 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) || - commit_locked_index(&index_lock)) - die("unable to write new_index file"); + if (active_cache_changed) { + update_main_cache_tree(WRITE_TREE_SILENT); + if (write_cache(fd, active_cache, active_nr) || + commit_locked_index(&index_lock)) + die(_("unable to write new_index file")); + } else { + rollback_lock_file(&index_lock); + } commit_style = COMMIT_AS_IS; return get_index_file(); } diff --cc t/t7501-commit.sh index 0f83be80e,e1181f097..195e7477d --- a/t/t7501-commit.sh +++ b/t/t7501-commit.sh @@@ -14,228 -11,171 +14,238 @@@ author='The Real Author file && - git add file && \ - git status | grep 'Initial commit'" - -test_expect_success \ - "fail initial amend" \ - "test_must_fail git commit --amend" - -test_expect_success \ - "initial commit" \ - "git commit -m initial" - -test_expect_success \ - "invalid options 1" \ - "test_must_fail git commit -m foo -m bar -F file" - -test_expect_success \ - "invalid options 2" \ - "test_must_fail git commit -C HEAD -m illegal" - -test_expect_success \ - "using paths with -a" \ - "echo King of the bongo >file && - test_must_fail git commit -m foo -a file" - -test_expect_success PERL \ - "using paths with --interactive" \ - "echo bong-o-bong >file && - ! (echo 7 | git commit -m foo --interactive file)" - -test_expect_success \ - "using invalid commit with -C" \ - "test_must_fail git commit -C bogus" - -test_expect_success \ - "testing nothing to commit" \ - "test_must_fail git commit -m initial" - -test_expect_success \ - "next commit" \ - "echo 'bongo bongo bongo' >file \ - git commit -m next -a" - -test_expect_success \ - "commit message from non-existing file" \ - "echo 'more bongo: bongo bongo bongo bongo' >file && \ - test_must_fail git commit -F gah -a" - -# Empty except stray tabs and spaces on a few lines. -sed -e 's/@$//' >msg <msg && \ - git commit -F msg -a" - -cat >editor <<\EOF -#!/bin/sh -sed -e "s/a file/an amend commit/g" < "$1" > "$1-" -mv "$1-" "$1" -EOF -chmod 755 editor +test_expect_success 'initial status' ' + echo bongo bongo >file && + git add file && + git status >actual && + test_i18ngrep "Initial commit" actual +' + +test_expect_success 'fail initial amend' ' + test_must_fail git commit --amend +' + +test_expect_success 'setup: initial commit' ' + git commit -m initial +' + +test_expect_success '-m and -F do not mix' ' + git checkout HEAD file && echo >>file && git add file && + test_must_fail git commit -m foo -m bar -F file +' + +test_expect_success '-m and -C do not mix' ' + git checkout HEAD file && echo >>file && git add file && + test_must_fail git commit -C HEAD -m illegal +' + +test_expect_success 'paths and -a do not mix' ' + echo King of the bongo >file && + test_must_fail git commit -m foo -a file +' + +test_expect_success PERL 'can use paths with --interactive' ' + echo bong-o-bong >file && + # 2: update, 1:st path, that is all, 7: quit + ( echo 2; echo 1; echo; echo 7 ) | + git commit -m foo --interactive file && + git reset --hard HEAD^ +' + +test_expect_success 'using invalid commit with -C' ' + test_must_fail git commit -C bogus +' + +test_expect_success 'nothing to commit' ' + test_must_fail git commit -m initial +' + +test_expect_success 'setup: non-initial commit' ' + echo bongo bongo bongo >file && + git commit -m next -a +' + +test_expect_success 'commit message from non-existing file' ' + echo more bongo: bongo bongo bongo bongo >file && + test_must_fail git commit -F gah -a +' -test_expect_success \ - "amend commit" \ - "EDITOR=./editor git commit --amend" +test_expect_success 'empty commit message' ' + # Empty except stray tabs and spaces on a few lines. + sed -e "s/@//g" >msg <<-\EOF && + @ @ + @@ + @ @ + @Signed-off-by: hula@ + EOF + test_must_fail git commit -F msg -a +' + +test_expect_success 'template "emptyness" check does not kick in with -F' ' + git checkout HEAD file && echo >>file && git add file && + git commit -t file -F file +' + +test_expect_success 'template "emptyness" check' ' + git checkout HEAD file && echo >>file && git add file && + test_must_fail git commit -t file 2>err && + test_i18ngrep "did not edit" err +' + +test_expect_success 'setup: commit message from file' ' + git checkout HEAD file && echo >>file && git add file && + echo this is the commit message, coming from a file >msg && + git commit -F msg -a +' + +test_expect_success 'amend commit' ' + cat >editor <<-\EOF && + #!/bin/sh + sed -e "s/a file/an amend commit/g" < "$1" > "$1-" + mv "$1-" "$1" + EOF + chmod 755 editor && + 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 && \ - test_must_fail git commit -F msg -m amending ." +test_expect_success 'set up editor' ' + cat >editor <<-\EOF && + #!/bin/sh + sed -e "s/unamended/amended/g" <"$1" >"$1-" + mv "$1-" "$1" + EOF + chmod 755 editor +' -test_expect_success \ - "using message from other commit" \ - "git commit -C HEAD^ ." +test_expect_success 'amend without launching editor' ' + echo unamended >expect && + git commit --allow-empty -m "unamended" && + echo needs more bongo >file && + git add file && + EDITOR=./editor git commit --no-edit --amend && + git diff --exit-code HEAD -- file && + git diff-tree -s --format=%s HEAD >msg && + test_cmp expect msg +' -cat >editor <<\EOF -#!/bin/sh -sed -e "s/amend/older/g" < "$1" > "$1-" -mv "$1-" "$1" -EOF -chmod 755 editor - -test_expect_success \ - "editing message from other commit" \ - "echo 'hula hula' >file && \ - EDITOR=./editor git commit -c HEAD^ -a" - -test_expect_success \ - "message from stdin" \ - "echo 'silly new contents' >file && \ - echo commit message from stdin | git commit -F - -a" - -test_expect_success \ - "overriding author from command line" \ - "echo 'gak' >file && \ - git commit -m 'author' --author 'Rubber Duck ' -a >output 2>&1" - -test_expect_success \ - "commit --author output mentions author" \ - "grep Rubber.Duck output" - -test_expect_success PERL \ - "interactive add" \ - "echo 7 | git commit --interactive | grep 'What now'" - -test_expect_success \ - "showing committed revisions" \ - "git rev-list HEAD >current" - -cat >editor <<\EOF -#!/bin/sh -sed -e "s/good/bad/g" < "$1" > "$1-" -mv "$1-" "$1" -EOF -chmod 755 editor - -cat >msg <file && - EDITOR=./editor git commit -a -F msg && - git show -s --pretty=format:"%s" | grep -q good && - echo "quack" >file && - echo "Another good message." | EDITOR=./editor git commit -a -F - && - git show -s --pretty=format:"%s" | grep -q good - ' -# We could just check the head sha1, but checking each commit makes it -# easier to isolate bugs. - -cat >expected <<\EOF -72c0dc9855b0c9dadcbfd5a31cab072e0cb774ca -9b88fc14ce6b32e3d9ee021531a54f18a5cf38a2 -3536bbb352c3a1ef9a420f5b4242d48578b92aa7 -d381ac431806e53f3dd7ac2f1ae0534f36d738b9 -4fd44095ad6334f3ef72e4c5ec8ddf108174b54a -402702b49136e7587daa9280e91e4bb7cb2179f7 -EOF - -test_expect_success \ - 'validate git rev-list output.' \ - 'test_cmp expected current' +test_expect_success '--amend --edit' ' + echo amended >expect && + git commit --allow-empty -m "unamended" && + echo bongo again >file && + git add file && + EDITOR=./editor git commit --edit --amend && + git diff-tree -s --format=%s HEAD >msg && + test_cmp expect msg +' + +test_expect_success '--amend --edit of empty message' ' + cat >replace <<-\EOF && + #!/bin/sh + echo "amended" >"$1" + EOF + chmod 755 replace && + git commit --allow-empty --allow-empty-message -m "" && + echo more bongo >file && + git add file && + EDITOR=./replace git commit --edit --amend && + git diff-tree -s --format=%s HEAD >msg && + ./replace expect && + test_cmp expect msg +' + +test_expect_success '-m --edit' ' + echo amended >expect && + git commit --allow-empty -m buffer && + echo bongo bongo >file && + git add file && + EDITOR=./editor git commit -m unamended --edit && + git diff-tree -s --format=%s HEAD >msg && + test_cmp expect msg +' + +test_expect_success '-m and -F do not mix' ' + echo enough with the bongos >file && + test_must_fail git commit -F msg -m amending . +' + +test_expect_success 'using message from other commit' ' + git commit -C HEAD^ . +' + +test_expect_success 'editing message from other commit' ' + cat >editor <<-\EOF && + #!/bin/sh + sed -e "s/amend/older/g" < "$1" > "$1-" + mv "$1-" "$1" + EOF + chmod 755 editor && + echo hula hula >file && + EDITOR=./editor git commit -c HEAD^ -a +' + +test_expect_success 'message from stdin' ' + echo silly new contents >file && + echo commit message from stdin | + git commit -F - -a +' + +test_expect_success 'overriding author from command line' ' + echo gak >file && + git commit -m author \ + --author "Rubber Duck " -a >output 2>&1 && + grep Rubber.Duck output +' + +test_expect_success PERL 'interactive add' ' + echo 7 | + git commit --interactive | + grep "What now" +' + +test_expect_success PERL "commit --interactive doesn't change index if editor aborts" ' + echo zoo >file && + test_must_fail git diff --exit-code >diff1 && + (echo u ; echo "*" ; echo q) | + ( + EDITOR=: && + export EDITOR && + test_must_fail git commit --interactive + ) && + git diff >diff2 && + compare_diff_patch diff1 diff2 +' + +test_expect_success 'editor not invoked if -F is given' ' + cat >editor <<-\EOF && + #!/bin/sh + sed -e s/good/bad/g <"$1" >"$1-" + mv "$1-" "$1" + EOF + chmod 755 editor && + + echo A good commit message. >msg && + echo moo >file && + + EDITOR=./editor git commit -a -F msg && + git show -s --pretty=format:%s >subject && + grep -q good subject && + + echo quack >file && + echo Another good message. | + EDITOR=./editor git commit -a -F - && + git show -s --pretty=format:%s >subject && + grep -q good subject +' test_expect_success 'partial commit that involves removal (1)' '