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 &&
80test_cmp expect actual &&
8182
echo "amend test" >expect &&
83message_body HEAD >actual &&
84test_cmp expect actual
85'
8687
sha1_file() {
88echo "$*" | sed "s#..#.git/objects/&/#"
89}
90remove_object() {
91rm -f $(sha1_file "$*")
92}
93no_reflog() {
94cp .git/config .git/config.saved &&
95echo "[core] logallrefupdates = false" >>.git/config &&
96test_when_finished "mv -f .git/config.saved .git/config" &&
9798
if test -e .git/logs
99then
100mv .git/logs . &&
101test_when_finished "mv logs .git/"
102fi
103}
104105
test_expect_success '--amend option with empty author' '
106git cat-file commit Initial >tmp &&
107sed "s/author [^<]* </author </" tmp >empty-author &&
108no_reflog &&
109sha=$(git hash-object -t commit -w empty-author) &&
110test_when_finished "remove_object $sha" &&
111git checkout $sha &&
112test_when_finished "git checkout Initial" &&
113echo "Empty author test" >>foo &&
114test_tick &&
115test_must_fail git commit -a -m "empty author" --amend 2>err &&
116grep "empty ident" err
117'
118119
test_expect_success '--amend option with missing author' '
120git cat-file commit Initial >tmp &&
121sed "s/author [^<]* </author </" tmp >malformed &&
122no_reflog &&
123sha=$(git hash-object -t commit -w malformed) &&
124test_when_finished "remove_object $sha" &&
125git checkout $sha &&
126test_when_finished "git checkout Initial" &&
127echo "Missing author test" >>foo &&
128test_tick &&
129test_must_fail git commit -a -m "malformed author" --amend 2>err &&
130grep "empty ident" err
131'
132133
test_expect_success '--reset-author makes the commit ours even with --amend option' '
134git checkout Initial &&
135echo "Test 6" >>foo &&
136test_tick &&
137git commit -a --reset-author -m "Changed again" --amend &&
138echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
139author_header HEAD >actual &&
140test_cmp expect actual &&
141142
echo "Changed again" >expect &&
143message_body HEAD >actual &&
144test_cmp expect actual
145'
146147
test_expect_success '--reset-author and --author are mutually exclusive' '
148git checkout Initial &&
149echo "Test 7" >>foo &&
150test_tick &&
151test_must_fail git commit -a --reset-author --author="Xyzzy <frotz@nitfol.xz>"
152'
153154
test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
155git checkout Initial &&
156echo "Test 7" >>foo &&
157test_tick &&
158test_must_fail git commit -a --reset-author -m done
159'
160161
test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
162echo "cherry-pick 1a" >>foo &&
163test_tick &&
164git commit -am "cherry-pick 1" --author="Cherry <cherry@pick.er>" &&
165git tag cherry-pick-head &&
166git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
167echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
168echo "cherry-pick 1b" >>foo &&
169test_tick &&
170git commit -a &&
171author_header cherry-pick-head >expect &&
172author_header HEAD >actual &&
173test_cmp expect actual &&
174175
echo "This is a MERGE_MSG" >expect &&
176message_body HEAD >actual &&
177test_cmp expect actual
178'
179180
test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
181git rev-parse cherry-pick-head >.git/CHERRY_PICK_HEAD &&
182echo "cherry-pick 2" >>foo &&
183test_tick &&
184git commit -am "cherry-pick 2" --reset-author &&
185echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
186author_header HEAD >actual &&
187test_cmp expect actual
188'
189190
test_done