Code

i18n: git-status "Initial commit" message
[git.git] / t / t7501-commit.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
4 #
6 # FIXME: Test the various index usages, -i and -o, test reflog,
7 # signoff
9 test_description='git commit'
10 . ./test-lib.sh
12 test_tick
14 test_expect_success \
15         "initial status" \
16         "echo 'bongo bongo' >file &&
17          git add file"
19 test_expect_success C_LOCALE_OUTPUT \
20         "Constructing initial commit" \
21         "git status | grep 'Initial commit'"
23 test_expect_success \
24         "fail initial amend" \
25         "test_must_fail git commit --amend"
27 test_expect_success \
28         "initial commit" \
29         "git commit -m initial"
31 test_expect_success \
32         "invalid options 1" \
33         "test_must_fail git commit -m foo -m bar -F file"
35 test_expect_success \
36         "invalid options 2" \
37         "test_must_fail git commit -C HEAD -m illegal"
39 test_expect_success \
40         "using paths with -a" \
41         "echo King of the bongo >file &&
42         test_must_fail git commit -m foo -a file"
44 test_expect_success PERL \
45         "using paths with --interactive" \
46         "echo bong-o-bong >file &&
47         ! (echo 7 | git commit -m foo --interactive file)"
49 test_expect_success \
50         "using invalid commit with -C" \
51         "test_must_fail git commit -C bogus"
53 test_expect_success \
54         "testing nothing to commit" \
55         "test_must_fail git commit -m initial"
57 test_expect_success \
58         "next commit" \
59         "echo 'bongo bongo bongo' >file \
60          git commit -m next -a"
62 test_expect_success \
63         "commit message from non-existing file" \
64         "echo 'more bongo: bongo bongo bongo bongo' >file && \
65          test_must_fail git commit -F gah -a"
67 # Empty except stray tabs and spaces on a few lines.
68 sed -e 's/@$//' >msg <<EOF
69                 @
71   @
72 Signed-off-by: hula
73 EOF
74 test_expect_success \
75         "empty commit message" \
76         "test_must_fail git commit -F msg -a"
78 test_expect_success \
79         "commit message from file" \
80         "echo 'this is the commit message, coming from a file' >msg && \
81          git commit -F msg -a"
83 cat >editor <<\EOF
84 #!/bin/sh
85 sed -e "s/a file/an amend commit/g" < "$1" > "$1-"
86 mv "$1-" "$1"
87 EOF
88 chmod 755 editor
90 test_expect_success \
91         "amend commit" \
92         "EDITOR=./editor git commit --amend"
94 test_expect_success \
95         "passing -m and -F" \
96         "echo 'enough with the bongos' >file && \
97          test_must_fail git commit -F msg -m amending ."
99 test_expect_success \
100         "using message from other commit" \
101         "git commit -C HEAD^ ."
103 cat >editor <<\EOF
104 #!/bin/sh
105 sed -e "s/amend/older/g"  < "$1" > "$1-"
106 mv "$1-" "$1"
107 EOF
108 chmod 755 editor
110 test_expect_success \
111         "editing message from other commit" \
112         "echo 'hula hula' >file && \
113          EDITOR=./editor git commit -c HEAD^ -a"
115 test_expect_success \
116         "message from stdin" \
117         "echo 'silly new contents' >file && \
118          echo commit message from stdin | git commit -F - -a"
120 test_expect_success \
121         "overriding author from command line" \
122         "echo 'gak' >file && \
123          git commit -m 'author' --author 'Rubber Duck <rduck@convoy.org>' -a >output 2>&1"
125 test_expect_success \
126         "commit --author output mentions author" \
127         "grep Rubber.Duck output"
129 test_expect_success PERL \
130         "interactive add" \
131         "echo 7 | git commit --interactive | grep 'What now'"
133 test_expect_success \
134         "showing committed revisions" \
135         "git rev-list HEAD >current"
137 cat >editor <<\EOF
138 #!/bin/sh
139 sed -e "s/good/bad/g" < "$1" > "$1-"
140 mv "$1-" "$1"
141 EOF
142 chmod 755 editor
144 cat >msg <<EOF
145 A good commit message.
146 EOF
148 test_expect_success \
149         'editor not invoked if -F is given' '
150          echo "moo" >file &&
151          EDITOR=./editor git commit -a -F msg &&
152          git show -s --pretty=format:"%s" | grep -q good &&
153          echo "quack" >file &&
154          echo "Another good message." | EDITOR=./editor git commit -a -F - &&
155          git show -s --pretty=format:"%s" | grep -q good
156          '
157 # We could just check the head sha1, but checking each commit makes it
158 # easier to isolate bugs.
160 cat >expected <<\EOF
161 72c0dc9855b0c9dadcbfd5a31cab072e0cb774ca
162 9b88fc14ce6b32e3d9ee021531a54f18a5cf38a2
163 3536bbb352c3a1ef9a420f5b4242d48578b92aa7
164 d381ac431806e53f3dd7ac2f1ae0534f36d738b9
165 4fd44095ad6334f3ef72e4c5ec8ddf108174b54a
166 402702b49136e7587daa9280e91e4bb7cb2179f7
167 EOF
169 test_expect_success \
170     'validate git rev-list output.' \
171     'test_cmp expected current'
173 test_expect_success 'partial commit that involves removal (1)' '
175         git rm --cached file &&
176         mv file elif &&
177         git add elif &&
178         git commit -m "Partial: add elif" elif &&
179         git diff-tree --name-status HEAD^ HEAD >current &&
180         echo "A elif" >expected &&
181         test_cmp expected current
185 test_expect_success 'partial commit that involves removal (2)' '
187         git commit -m "Partial: remove file" file &&
188         git diff-tree --name-status HEAD^ HEAD >current &&
189         echo "D file" >expected &&
190         test_cmp expected current
194 test_expect_success 'partial commit that involves removal (3)' '
196         git rm --cached elif &&
197         echo elif >elif &&
198         git commit -m "Partial: modify elif" elif &&
199         git diff-tree --name-status HEAD^ HEAD >current &&
200         echo "M elif" >expected &&
201         test_cmp expected current
205 author="The Real Author <someguy@his.email.org>"
206 test_expect_success 'amend commit to fix author' '
208         oldtick=$GIT_AUTHOR_DATE &&
209         test_tick &&
210         git reset --hard &&
211         git cat-file -p HEAD |
212         sed -e "s/author.*/author $author $oldtick/" \
213                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
214                 expected &&
215         git commit --amend --author="$author" &&
216         git cat-file -p HEAD > current &&
217         test_cmp expected current
221 test_expect_success 'amend commit to fix date' '
223         test_tick &&
224         newtick=$GIT_AUTHOR_DATE &&
225         git reset --hard &&
226         git cat-file -p HEAD |
227         sed -e "s/author.*/author $author $newtick/" \
228                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
229                 expected &&
230         git commit --amend --date="$newtick" &&
231         git cat-file -p HEAD > current &&
232         test_cmp expected current
236 test_expect_success 'commit complains about bogus date' '
237         test_must_fail git commit --amend --date=10.11.2010
240 test_expect_success 'sign off (1)' '
242         echo 1 >positive &&
243         git add positive &&
244         git commit -s -m "thank you" &&
245         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
246         (
247                 echo thank you
248                 echo
249                 git var GIT_COMMITTER_IDENT |
250                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
251         ) >expected &&
252         test_cmp expected actual
256 test_expect_success 'sign off (2)' '
258         echo 2 >positive &&
259         git add positive &&
260         existing="Signed-off-by: Watch This <watchthis@example.com>" &&
261         git commit -s -m "thank you
263 $existing" &&
264         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
265         (
266                 echo thank you
267                 echo
268                 echo $existing
269                 git var GIT_COMMITTER_IDENT |
270                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
271         ) >expected &&
272         test_cmp expected actual
276 test_expect_success 'signoff gap' '
278         echo 3 >positive &&
279         git add positive &&
280         alt="Alt-RFC-822-Header: Value" &&
281         git commit -s -m "welcome
283 $alt" &&
284         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
285         (
286                 echo welcome
287                 echo
288                 echo $alt
289                 git var GIT_COMMITTER_IDENT |
290                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
291         ) >expected &&
292         test_cmp expected actual
295 test_expect_success 'signoff gap 2' '
297         echo 4 >positive &&
298         git add positive &&
299         alt="fixed: 34" &&
300         git commit -s -m "welcome
302 We have now
303 $alt" &&
304         git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
305         (
306                 echo welcome
307                 echo
308                 echo We have now
309                 echo $alt
310                 echo
311                 git var GIT_COMMITTER_IDENT |
312                 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /"
313         ) >expected &&
314         test_cmp expected actual
317 test_expect_success 'multiple -m' '
319         >negative &&
320         git add negative &&
321         git commit -m "one" -m "two" -m "three" &&
322         git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
323         (
324                 echo one
325                 echo
326                 echo two
327                 echo
328                 echo three
329         ) >expected &&
330         test_cmp expected actual
334 author="The Real Author <someguy@his.email.org>"
335 test_expect_success 'amend commit to fix author' '
337         oldtick=$GIT_AUTHOR_DATE &&
338         test_tick &&
339         git reset --hard &&
340         git cat-file -p HEAD |
341         sed -e "s/author.*/author $author $oldtick/" \
342                 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" > \
343                 expected &&
344         git commit --amend --author="$author" &&
345         git cat-file -p HEAD > current &&
346         test_cmp expected current
350 test_expect_success 'git commit <file> with dirty index' '
351         echo tacocat > elif &&
352         echo tehlulz > chz &&
353         git add chz &&
354         git commit elif -m "tacocat is a palindrome" &&
355         git show --stat | grep elif &&
356         git diff --cached | grep chz
359 test_expect_success 'same tree (single parent)' '
361         git reset --hard
363         if git commit -m empty
364         then
365                 echo oops -- should have complained
366                 false
367         else
368                 : happy
369         fi
373 test_expect_success 'same tree (single parent) --allow-empty' '
375         git commit --allow-empty -m "forced empty" &&
376         git cat-file commit HEAD | grep forced
380 test_expect_success 'same tree (merge and amend merge)' '
382         git checkout -b side HEAD^ &&
383         echo zero >zero &&
384         git add zero &&
385         git commit -m "add zero" &&
386         git checkout master &&
388         git merge -s ours side -m "empty ok" &&
389         git diff HEAD^ HEAD >actual &&
390         : >expected &&
391         test_cmp expected actual &&
393         git commit --amend -m "empty really ok" &&
394         git diff HEAD^ HEAD >actual &&
395         : >expected &&
396         test_cmp expected actual
400 test_expect_success 'amend using the message from another commit' '
402         git reset --hard &&
403         test_tick &&
404         git commit --allow-empty -m "old commit" &&
405         old=$(git rev-parse --verify HEAD) &&
406         test_tick &&
407         git commit --allow-empty -m "new commit" &&
408         new=$(git rev-parse --verify HEAD) &&
409         test_tick &&
410         git commit --allow-empty --amend -C "$old" &&
411         git show --pretty="format:%ad %s" "$old" >expected &&
412         git show --pretty="format:%ad %s" HEAD >actual &&
413         test_cmp expected actual
417 test_expect_success 'amend using the message from a commit named with tag' '
419         git reset --hard &&
420         test_tick &&
421         git commit --allow-empty -m "old commit" &&
422         old=$(git rev-parse --verify HEAD) &&
423         git tag -a -m "tag on old" tagged-old HEAD &&
424         test_tick &&
425         git commit --allow-empty -m "new commit" &&
426         new=$(git rev-parse --verify HEAD) &&
427         test_tick &&
428         git commit --allow-empty --amend -C tagged-old &&
429         git show --pretty="format:%ad %s" "$old" >expected &&
430         git show --pretty="format:%ad %s" HEAD >actual &&
431         test_cmp expected actual
435 test_expect_success 'amend can copy notes' '
437         git config notes.rewrite.amend true &&
438         git config notes.rewriteRef "refs/notes/*" &&
439         test_commit foo &&
440         git notes add -m"a note" &&
441         test_tick &&
442         git commit --amend -m"new foo" &&
443         test "$(git notes show)" = "a note"
447 test_done