Merge branch 'jc/commit-amend-no-edit'

* jc/commit-amend-no-edit:
  test: commit --amend should honor --no-edit
  commit: honour --no-edit
  t7501 (commit): modernize style
  test: remove a porcelain test that hard-codes commit names
  test: add missing "&&" after echo command
This commit is contained in:
Junio C Hamano 2011-12-16 22:33:56 -08:00
commit a2add8570c
2 changed files with 163 additions and 147 deletions

View file

@ -81,7 +81,8 @@ static const char *template_file;
static const char *author_message, *author_message_buffer; static const char *author_message, *author_message_buffer;
static char *edit_message, *use_message; static char *edit_message, *use_message;
static char *fixup_message, *squash_message; static char *fixup_message, *squash_message;
static int all, edit_flag, also, interactive, patch_interactive, only, amend, signoff; static int all, also, interactive, patch_interactive, only, amend, signoff;
static int edit_flag = -1; /* unspecified */
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship; static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
static int no_post_rewrite, allow_empty_message; static int no_post_rewrite, allow_empty_message;
static char *untracked_files_arg, *force_date, *ignore_submodule_arg; static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
@ -141,7 +142,7 @@ static struct option builtin_commit_options[] = {
OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"), OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"),
OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
OPT_FILENAME('t', "template", &template_file, "use specified template file"), OPT_FILENAME('t', "template", &template_file, "use specified template file"),
OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"), OPT_BOOL('e', "edit", &edit_flag, "force edit of commit"),
OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"), OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"), OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
/* end commit message options */ /* end commit message options */
@ -1020,8 +1021,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
if (logfile || message.len || use_message || fixup_message) if (logfile || message.len || use_message || fixup_message)
use_editor = 0; use_editor = 0;
if (edit_flag) if (0 <= edit_flag)
use_editor = 1; use_editor = edit_flag;
if (!use_editor) if (!use_editor)
setenv("GIT_EDITOR", ":", 1); setenv("GIT_EDITOR", ":", 1);

View file

@ -8,39 +8,39 @@
test_description='git commit' test_description='git commit'
. ./test-lib.sh . ./test-lib.sh
. "$TEST_DIRECTORY/diff-lib.sh"
author='The Real Author <someguy@his.email.org>'
test_tick test_tick
test_expect_success \ test_expect_success 'initial status' '
"initial status" \ echo bongo bongo >file &&
"echo 'bongo bongo' >file && git add file &&
git add file"
test_expect_success "Constructing initial commit" '
git status >actual && git status >actual &&
test_i18ngrep "Initial commit" actual test_i18ngrep "Initial commit" actual
' '
test_expect_success \ test_expect_success 'fail initial amend' '
"fail initial amend" \ test_must_fail git commit --amend
"test_must_fail git commit --amend" '
test_expect_success \ test_expect_success 'setup: initial commit' '
"initial commit" \ git commit -m initial
"git commit -m initial" '
test_expect_success \ test_expect_success '-m and -F do not mix' '
"invalid options 1" \ test_must_fail git commit -m foo -m bar -F file
"test_must_fail git commit -m foo -m bar -F file" '
test_expect_success \ test_expect_success '-m and -C do not mix' '
"invalid options 2" \ test_must_fail git commit -C HEAD -m illegal
"test_must_fail git commit -C HEAD -m illegal" '
test_expect_success \ test_expect_success 'paths and -a do not mix' '
"using paths with -a" \ echo King of the bongo >file &&
"echo King of the bongo >file && test_must_fail git commit -m foo -a file
test_must_fail git commit -m foo -a file" '
test_expect_success PERL 'can use paths with --interactive' ' test_expect_success PERL 'can use paths with --interactive' '
echo bong-o-bong >file && echo bong-o-bong >file &&
@ -50,139 +50,163 @@ test_expect_success PERL 'can use paths with --interactive' '
git reset --hard HEAD^ git reset --hard HEAD^
' '
test_expect_success \ test_expect_success 'using invalid commit with -C' '
"using invalid commit with -C" \ test_must_fail git commit -C bogus
"test_must_fail git commit -C bogus" '
test_expect_success \ test_expect_success 'nothing to commit' '
"testing nothing to commit" \ test_must_fail git commit -m initial
"test_must_fail git commit -m initial" '
test_expect_success \ test_expect_success 'setup: non-initial commit' '
"next commit" \ echo bongo bongo bongo >file &&
"echo 'bongo bongo bongo' >file \ git commit -m next -a
git commit -m next -a" '
test_expect_success \ test_expect_success 'commit message from non-existing file' '
"commit message from non-existing file" \ echo more bongo: bongo bongo bongo bongo >file &&
"echo 'more bongo: bongo bongo bongo bongo' >file && \ test_must_fail git commit -F gah -a
test_must_fail git commit -F gah -a" '
# Empty except stray tabs and spaces on a few lines. test_expect_success 'empty commit message' '
sed -e 's/@$//' >msg <<EOF # 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 'setup: commit message from file' '
Signed-off-by: hula echo this is the commit message, coming from a file >msg &&
EOF git commit -F msg -a
test_expect_success \ '
"empty commit message" \
"test_must_fail git commit -F msg -a"
test_expect_success \ test_expect_success 'amend commit' '
"commit message from file" \ cat >editor <<-\EOF &&
"echo 'this is the commit message, coming from a file' >msg && \ #!/bin/sh
git commit -F msg -a" sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
mv "$1-" "$1"
EOF
chmod 755 editor &&
EDITOR=./editor git commit --amend
'
cat >editor <<\EOF test_expect_success 'set up editor' '
#!/bin/sh cat >editor <<-\EOF &&
sed -e "s/a file/an amend commit/g" < "$1" > "$1-" #!/bin/sh
mv "$1-" "$1" sed -e "s/unamended/amended/g" <"$1" >"$1-"
EOF mv "$1-" "$1"
chmod 755 editor EOF
chmod 755 editor
'
test_expect_success \ test_expect_success 'amend without launching editor' '
"amend commit" \ echo unamended >expect &&
"EDITOR=./editor git commit --amend" 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
'
test_expect_success \ test_expect_success '--amend --edit' '
"passing -m and -F" \ echo amended >expect &&
"echo 'enough with the bongos' >file && \ git commit --allow-empty -m "unamended" &&
test_must_fail git commit -F msg -m amending ." 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 \ test_expect_success '-m --edit' '
"using message from other commit" \ echo amended >expect &&
"git commit -C HEAD^ ." 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
'
cat >editor <<\EOF test_expect_success '-m and -F do not mix' '
#!/bin/sh echo enough with the bongos >file &&
sed -e "s/amend/older/g" < "$1" > "$1-" test_must_fail git commit -F msg -m amending .
mv "$1-" "$1" '
EOF
chmod 755 editor
test_expect_success \ test_expect_success 'using message from other commit' '
"editing message from other commit" \ git commit -C HEAD^ .
"echo 'hula hula' >file && \ '
EDITOR=./editor git commit -c HEAD^ -a"
test_expect_success \ test_expect_success 'editing message from other commit' '
"message from stdin" \ cat >editor <<-\EOF &&
"echo 'silly new contents' >file && \ #!/bin/sh
echo commit message from stdin | git commit -F - -a" 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 \ test_expect_success 'message from stdin' '
"overriding author from command line" \ echo silly new contents >file &&
"echo 'gak' >file && \ echo commit message from stdin |
git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a >output 2>&1" git commit -F - -a
'
test_expect_success \ test_expect_success 'overriding author from command line' '
"commit --author output mentions author" \ echo gak >file &&
"grep Rubber.Duck output" git commit -m author \
--author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
grep Rubber.Duck output
'
test_expect_success PERL \ test_expect_success PERL 'interactive add' '
"interactive add" \ echo 7 |
"echo 7 | git commit --interactive | grep 'What now'" git commit --interactive |
grep "What now"
'
test_expect_success PERL \ test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
"commit --interactive doesn't change index if editor aborts" \ echo zoo >file &&
"echo zoo >file &&
test_must_fail git diff --exit-code >diff1 && test_must_fail git diff --exit-code >diff1 &&
(echo u ; echo '*' ; echo q) | (echo u ; echo "*" ; echo q) |
(EDITOR=: && export EDITOR && (
test_must_fail git commit --interactive) && EDITOR=: &&
export EDITOR &&
test_must_fail git commit --interactive
) &&
git diff >diff2 && git diff >diff2 &&
test_cmp diff1 diff2" compare_diff_patch diff1 diff2
'
test_expect_success \ test_expect_success 'editor not invoked if -F is given' '
"showing committed revisions" \ cat >editor <<-\EOF &&
"git rev-list HEAD >current" #!/bin/sh
sed -e s/good/bad/g <"$1" >"$1-"
mv "$1-" "$1"
EOF
chmod 755 editor &&
cat >editor <<\EOF echo A good commit message. >msg &&
#!/bin/sh echo moo >file &&
sed -e "s/good/bad/g" < "$1" > "$1-"
mv "$1-" "$1"
EOF
chmod 755 editor
cat >msg <<EOF EDITOR=./editor git commit -a -F msg &&
A good commit message. git show -s --pretty=format:%s >subject &&
EOF grep -q good subject &&
test_expect_success \ echo quack >file &&
'editor not invoked if -F is given' ' echo Another good message. |
echo "moo" >file && EDITOR=./editor git commit -a -F - &&
EDITOR=./editor git commit -a -F msg && git show -s --pretty=format:%s >subject &&
git show -s --pretty=format:"%s" | grep -q good && grep -q good subject
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 'partial commit that involves removal (1)' ' test_expect_success 'partial commit that involves removal (1)' '
@ -216,7 +240,6 @@ test_expect_success 'partial commit that involves removal (3)' '
' '
author="The Real Author <someguy@his.email.org>"
test_expect_success 'amend commit to fix author' ' test_expect_success 'amend commit to fix author' '
oldtick=$GIT_AUTHOR_DATE && oldtick=$GIT_AUTHOR_DATE &&
@ -345,7 +368,6 @@ test_expect_success 'multiple -m' '
' '
author="The Real Author <someguy@his.email.org>"
test_expect_success 'amend commit to fix author' ' test_expect_success 'amend commit to fix author' '
oldtick=$GIT_AUTHOR_DATE && oldtick=$GIT_AUTHOR_DATE &&
@ -372,15 +394,8 @@ test_expect_success 'git commit <file> with dirty index' '
test_expect_success 'same tree (single parent)' ' test_expect_success 'same tree (single parent)' '
git reset --hard git reset --hard &&
test_must_fail git commit -m empty
if git commit -m empty
then
echo oops -- should have complained
false
else
: happy
fi
' '