1#!/bin/sh
2#
3# Copyright (c) 2009 Erick Mattos
4#
56
test_description='git commit --reset-author'
78
. ./test-lib.sh
910
author_header () {
11git cat-file commit "$1" |
12sed -n -e '/^$/q' -e '/^author /p'
13}
1415
message_body () {
16git cat-file commit "$1" |
17sed -e '1,/^$/d'
18}
1920
test_expect_success '-C option copies authorship and message' '
21echo "Initial" >foo &&
22git add foo &&
23test_tick &&
24git commit -m "Initial Commit" --author Frigate\ \<flying@over.world\> &&
25git tag Initial &&
26echo "Test 1" >>foo &&
27test_tick &&
28git commit -a -C Initial &&
29author_header Initial >expect &&
30author_header HEAD >actual &&
31test_cmp expect actual &&
3233
message_body Initial >expect &&
34message_body HEAD >actual &&
35test_cmp expect actual
36'
3738
test_expect_success '-C option copies only the message with --reset-author' '
39echo "Test 2" >>foo &&
40test_tick &&
41git commit -a -C Initial --reset-author &&
42echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
43author_header HEAD >actual &&
44test_cmp expect actual &&
4546
message_body Initial >expect &&
47message_body HEAD >actual &&
48test_cmp expect actual
49'
5051
test_expect_success '-c option copies authorship and message' '
52echo "Test 3" >>foo &&
53test_tick &&
54EDITOR=: VISUAL=: git commit -a -c Initial &&
55author_header Initial >expect &&
56author_header HEAD >actual &&
57test_cmp expect actual
58'
5960
test_expect_success '-c option copies only the message with --reset-author' '
61echo "Test 4" >>foo &&
62test_tick &&
63EDITOR=: VISUAL=: git commit -a -c Initial --reset-author &&
64echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
65author_header HEAD >actual &&
66test_cmp expect actual &&
6768
message_body Initial >expect &&
69message_body HEAD >actual &&
70test_cmp expect actual
71'
7273
test_expect_success '--amend option copies authorship' '
74git checkout Initial &&
75echo "Test 5" >>foo &&
76test_tick &&
77git commit -a --amend -m "amend test" &&
78author_header Initial >expect &&
79author_header HEAD >actual &&
8081
echo "amend test" >expect &&
82message_body HEAD >actual &&
83test_cmp expect actual
84'
8586
sha1_file() {
87echo "$*" | sed "s#..#.git/objects/&/#"
88}
89remove_object() {
90rm -f $(sha1_file "$*")
91}
92no_reflog() {
93cp .git/config .git/config.saved &&
94echo "[core] logallrefupdates = false" >>.git/config &&
95test_when_finished "mv -f .git/config.saved .git/config" &&
9697
if test -e .git/logs
98then
99mv .git/logs . &&
100test_when_finished "mv logs .git/"
101fi
102}
103104
test_expect_success '--amend option with empty author' '
105git cat-file commit Initial >tmp &&
106sed "s/author [^<]* </author </" tmp >empty-author &&
107no_reflog &&
108sha=$(git hash-object -t commit -w empty-author) &&
109test_when_finished "remove_object $sha" &&
110git checkout $sha &&
111test_when_finished "git checkout Initial" &&
112echo "Empty author test" >>foo &&
113test_tick &&
114test_must_fail git commit -a -m "empty author" --amend 2>err &&
115grep "empty ident" err
116'
117118
test_expect_success '--amend option with missing author' '
119git cat-file commit Initial >tmp &&
120sed "s/author [^<]* </author </" tmp >malformed &&
121no_reflog &&
122sha=$(git hash-object -t commit -w malformed) &&
123test_when_finished "remove_object $sha" &&
124git checkout $sha &&
125test_when_finished "git checkout Initial" &&
126echo "Missing author test" >>foo &&
127test_tick &&
128test_must_fail git commit -a -m "malformed author" --amend 2>err &&
129grep "empty ident" err
130'
131132
test_expect_success '--reset-author makes the commit ours even with --amend option' '
133git checkout Initial &&
134echo "Test 6" >>foo &&
135test_tick &&
136git commit -a --reset-author -m "Changed again" --amend &&
137echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
138author_header HEAD >actual &&
139test_cmp expect actual &&
140141
echo "Changed again" >expect &&
142message_body HEAD >actual &&
143test_cmp expect actual
144'
145146
test_expect_success '--reset-author and --author are mutually exclusive' '
147git checkout Initial &&
148echo "Test 7" >>foo &&
149test_tick &&
150test_must_fail git commit -a --reset-author --author="Xyzzy <frotz@nitfol.xz>"
151'
152153
test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
154git checkout Initial &&
155echo "Test 7" >>foo &&
156test_tick &&
157test_must_fail git commit -a --reset-author -m done
158'
159160
test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
161echo "cherry-pick 1a" >>foo &&
162test_tick &&
163git commit -am "cherry-pick 1" --author="Cherry <cherry@pick.er>" &&
164git tag cherry-pick-head &&
165git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
166echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
167echo "cherry-pick 1b" >>foo &&
168test_tick &&
169git commit -a &&
170author_header cherry-pick-head >expect &&
171author_header HEAD >actual &&
172test_cmp expect actual &&
173174
echo "This is a MERGE_MSG" >expect &&
175message_body HEAD >actual &&
176test_cmp expect actual
177'
178179
test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
180git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
181echo "cherry-pick 2" >>foo &&
182test_tick &&
183git commit -am "cherry-pick 2" --reset-author &&
184echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
185author_header HEAD >actual &&
186test_cmp expect actual
187'
188189
test_done