Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2009 Erick Mattos
4 #
5
6 test_description='commit tests of various authorhip options. '
7
8 . ./test-lib.sh
9
10 author_header () {
11 git cat-file commit "$1" |
12 sed -n -e '/^$/q' -e '/^author /p'
13 }
14
15 message_body () {
16 git cat-file commit "$1" |
17 sed -e '1,/^$/d'
18 }
19
20 test_expect_success '-C option copies authorship and message' '
21 test_commit --author Frigate\ \<flying@over.world\> \
22 "Initial Commit" foo Initial Initial &&
23 echo "Test 1" >>foo &&
24 test_tick &&
25 git commit -a -C Initial &&
26 author_header Initial >expect &&
27 author_header HEAD >actual &&
28 test_cmp expect actual &&
29
30 message_body Initial >expect &&
31 message_body HEAD >actual &&
32 test_cmp expect actual
33 '
34
35 test_expect_success '-C option copies only the message with --reset-author' '
36 echo "Test 2" >>foo &&
37 test_tick &&
38 git commit -a -C Initial --reset-author &&
39 echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
40 author_header HEAD >actual &&
41 test_cmp expect actual &&
42
43 message_body Initial >expect &&
44 message_body HEAD >actual &&
45 test_cmp expect actual
46 '
47
48 test_expect_success '-c option copies authorship and message' '
49 echo "Test 3" >>foo &&
50 test_tick &&
51 EDITOR=: VISUAL=: git commit -a -c Initial &&
52 author_header Initial >expect &&
53 author_header HEAD >actual &&
54 test_cmp expect actual
55 '
56
57 test_expect_success '-c option copies only the message with --reset-author' '
58 echo "Test 4" >>foo &&
59 test_tick &&
60 EDITOR=: VISUAL=: git commit -a -c Initial --reset-author &&
61 echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
62 author_header HEAD >actual &&
63 test_cmp expect actual &&
64
65 message_body Initial >expect &&
66 message_body HEAD >actual &&
67 test_cmp expect actual
68 '
69
70 test_expect_success '--amend option copies authorship' '
71 git checkout Initial &&
72 echo "Test 5" >>foo &&
73 test_tick &&
74 git commit -a --amend -m "amend test" &&
75 author_header Initial >expect &&
76 author_header HEAD >actual &&
77 test_cmp expect actual &&
78
79 echo "amend test" >expect &&
80 message_body HEAD >actual &&
81 test_cmp expect actual
82 '
83
84 sha1_file() {
85 echo "$*" | sed "s#..#.git/objects/&/#"
86 }
87 remove_object() {
88 rm -f $(sha1_file "$*")
89 }
90
91 test_expect_success '--amend option with empty author' '
92 git cat-file commit Initial >tmp &&
93 sed "s/author [^<]* </author </" tmp >empty-author &&
94 sha=$(git hash-object -t commit -w empty-author) &&
95 test_when_finished "remove_object $sha" &&
96 git checkout $sha &&
97 test_when_finished "git checkout Initial" &&
98 echo "Empty author test" >>foo &&
99 test_tick &&
100 test_must_fail git commit -a -m "empty author" --amend 2>err &&
101 test_grep "empty ident" err
102 '
103
104 test_expect_success '--amend option with missing author' '
105 git cat-file commit Initial >tmp &&
106 sed "s/author [^<]* </author </" tmp >malformed &&
107 sha=$(git hash-object --literally -t commit -w malformed) &&
108 test_when_finished "remove_object $sha" &&
109 git checkout $sha &&
110 test_when_finished "git checkout Initial" &&
111 echo "Missing author test" >>foo &&
112 test_tick &&
113 test_must_fail git commit -a -m "malformed author" --amend 2>err &&
114 test_grep "empty ident" err
115 '
116
117 test_expect_success '--reset-author makes the commit ours even with --amend option' '
118 git checkout Initial &&
119 echo "Test 6" >>foo &&
120 test_tick &&
121 git commit -a --reset-author -m "Changed again" --amend &&
122 echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
123 author_header HEAD >actual &&
124 test_cmp expect actual &&
125
126 echo "Changed again" >expect &&
127 message_body HEAD >actual &&
128 test_cmp expect actual
129 '
130
131 test_expect_success '--reset-author and --author are mutually exclusive' '
132 git checkout Initial &&
133 echo "Test 7" >>foo &&
134 test_tick &&
135 test_must_fail git commit -a --reset-author --author="Xyzzy <frotz@nitfol.xz>"
136 '
137
138 test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
139 git checkout Initial &&
140 echo "Test 7" >>foo &&
141 test_tick &&
142 test_must_fail git commit -a --reset-author -m done
143 '
144
145 test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
146 echo "cherry-pick 1a" >>foo &&
147 test_tick &&
148 git commit -am "cherry-pick 1" --author="Cherry <cherry@pick.er>" &&
149 git tag cherry-pick-head &&
150 git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) &&
151 echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
152 echo "cherry-pick 1b" >>foo &&
153 test_tick &&
154 git commit -a &&
155 author_header cherry-pick-head >expect &&
156 author_header HEAD >actual &&
157 test_cmp expect actual &&
158
159 echo "This is a MERGE_MSG" >expect &&
160 message_body HEAD >actual &&
161 test_cmp expect actual
162 '
163
164 test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
165 git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) &&
166 echo "cherry-pick 2" >>foo &&
167 test_tick &&
168 git commit -am "cherry-pick 2" --reset-author &&
169 echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
170 author_header HEAD >actual &&
171 test_cmp expect actual
172 '
173
174 test_done