Commit | Line | Data |
---|---|---|
d1cc130a SG |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Steven Grimm | |
4 | # | |
5 | ||
47a528ad | 6 | test_description='git commit |
d1cc130a SG |
7 | |
8 | Tests for selected commit options.' | |
9 | ||
10 | . ./test-lib.sh | |
11 | ||
12 | commit_msg_is () { | |
13 | test "`git log --pretty=format:%s%b -1`" = "$1" | |
14 | } | |
15 | ||
16 | # A sanity check to see if commit is working at all. | |
17 | test_expect_success 'a basic commit in an empty tree should succeed' ' | |
18 | echo content > foo && | |
19 | git add foo && | |
20 | git commit -m "initial commit" | |
21 | ' | |
22 | ||
23 | test_expect_success 'nonexistent template file should return error' ' | |
24 | echo changes >> foo && | |
25 | git add foo && | |
d492b31c | 26 | test_must_fail git commit --template "$PWD"/notexist |
d1cc130a SG |
27 | ' |
28 | ||
29 | test_expect_success 'nonexistent template file in config should return error' ' | |
30 | git config commit.template "$PWD"/notexist && | |
d492b31c | 31 | test_must_fail git commit && |
d1cc130a SG |
32 | git config --unset commit.template |
33 | ' | |
34 | ||
35 | # From now on we'll use a template file that exists. | |
36 | TEMPLATE="$PWD"/template | |
37 | ||
38 | test_expect_success 'unedited template should not commit' ' | |
39 | echo "template line" > "$TEMPLATE" && | |
d492b31c | 40 | test_must_fail git commit --template "$TEMPLATE" |
d1cc130a SG |
41 | ' |
42 | ||
43 | test_expect_success 'unedited template with comments should not commit' ' | |
44 | echo "# comment in template" >> "$TEMPLATE" && | |
d492b31c | 45 | test_must_fail git commit --template "$TEMPLATE" |
d1cc130a SG |
46 | ' |
47 | ||
48 | test_expect_success 'a Signed-off-by line by itself should not commit' ' | |
bfdbee98 JH |
49 | ( |
50 | test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off && | |
51 | test_must_fail git commit --template "$TEMPLATE" | |
52 | ) | |
d1cc130a SG |
53 | ' |
54 | ||
55 | test_expect_success 'adding comments to a template should not commit' ' | |
bfdbee98 JH |
56 | ( |
57 | test_set_editor "$TEST_DIRECTORY"/t7500/add-comments && | |
58 | test_must_fail git commit --template "$TEMPLATE" | |
59 | ) | |
d1cc130a SG |
60 | ' |
61 | ||
62 | test_expect_success 'adding real content to a template should commit' ' | |
bfdbee98 JH |
63 | ( |
64 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
65 | git commit --template "$TEMPLATE" | |
66 | ) && | |
d1cc130a SG |
67 | commit_msg_is "template linecommit message" |
68 | ' | |
69 | ||
70 | test_expect_success '-t option should be short for --template' ' | |
71 | echo "short template" > "$TEMPLATE" && | |
72 | echo "new content" >> foo && | |
73 | git add foo && | |
bfdbee98 JH |
74 | ( |
75 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
76 | git commit -t "$TEMPLATE" | |
77 | ) && | |
d1cc130a SG |
78 | commit_msg_is "short templatecommit message" |
79 | ' | |
80 | ||
81 | test_expect_success 'config-specified template should commit' ' | |
82 | echo "new template" > "$TEMPLATE" && | |
83 | git config commit.template "$TEMPLATE" && | |
84 | echo "more content" >> foo && | |
85 | git add foo && | |
bfdbee98 JH |
86 | ( |
87 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
88 | git commit | |
89 | ) && | |
d1cc130a SG |
90 | git config --unset commit.template && |
91 | commit_msg_is "new templatecommit message" | |
92 | ' | |
93 | ||
94 | test_expect_success 'explicit commit message should override template' ' | |
95 | echo "still more content" >> foo && | |
96 | git add foo && | |
bfdbee98 | 97 | GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \ |
d1cc130a | 98 | -m "command line msg" && |
55246aac | 99 | commit_msg_is "command line msg" |
d1cc130a SG |
100 | ' |
101 | ||
102 | test_expect_success 'commit message from file should override template' ' | |
103 | echo "content galore" >> foo && | |
104 | git add foo && | |
105 | echo "standard input msg" | | |
bfdbee98 JH |
106 | ( |
107 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
108 | git commit --template "$TEMPLATE" --file - | |
109 | ) && | |
55246aac | 110 | commit_msg_is "standard input msg" |
d1cc130a SG |
111 | ' |
112 | ||
859a4dbc RV |
113 | test_expect_success 'using alternate GIT_INDEX_FILE (1)' ' |
114 | ||
115 | cp .git/index saved-index && | |
116 | ( | |
117 | echo some new content >file && | |
118 | GIT_INDEX_FILE=.git/another_index && | |
119 | export GIT_INDEX_FILE && | |
120 | git add file && | |
121 | git commit -m "commit using another index" && | |
122 | git diff-index --exit-code HEAD && | |
123 | git diff-files --exit-code | |
124 | ) && | |
125 | cmp .git/index saved-index >/dev/null | |
126 | ||
127 | ' | |
128 | ||
129 | test_expect_success 'using alternate GIT_INDEX_FILE (2)' ' | |
130 | ||
131 | cp .git/index saved-index && | |
132 | ( | |
133 | rm -f .git/no-such-index && | |
134 | GIT_INDEX_FILE=.git/no-such-index && | |
135 | export GIT_INDEX_FILE && | |
136 | git commit -m "commit using nonexistent index" && | |
137 | test -z "$(git ls-files)" && | |
138 | test -z "$(git ls-tree HEAD)" | |
139 | ||
140 | ) && | |
141 | cmp .git/index saved-index >/dev/null | |
13208572 | 142 | ' |
859a4dbc | 143 | |
13208572 JS |
144 | cat > expect << EOF |
145 | zort | |
2150554b | 146 | |
13208572 JS |
147 | Signed-off-by: C O Mitter <committer@example.com> |
148 | EOF | |
149 | ||
150 | test_expect_success '--signoff' ' | |
151 | echo "yet another content *narf*" >> foo && | |
7eb5bbdb | 152 | echo "zort" | git commit -s -F - foo && |
9524cf29 | 153 | git cat-file commit HEAD | sed "1,/^\$/d" > output && |
bfdbee98 | 154 | test_cmp expect output |
859a4dbc RV |
155 | ' |
156 | ||
dbd0f5c7 JH |
157 | test_expect_success 'commit message from file (1)' ' |
158 | mkdir subdir && | |
159 | echo "Log in top directory" >log && | |
160 | echo "Log in sub directory" >subdir/log && | |
161 | ( | |
162 | cd subdir && | |
163 | git commit --allow-empty -F log | |
164 | ) && | |
165 | commit_msg_is "Log in sub directory" | |
166 | ' | |
167 | ||
168 | test_expect_success 'commit message from file (2)' ' | |
169 | rm -f log && | |
170 | echo "Log in sub directory" >subdir/log && | |
171 | ( | |
172 | cd subdir && | |
173 | git commit --allow-empty -F log | |
174 | ) && | |
175 | commit_msg_is "Log in sub directory" | |
176 | ' | |
177 | ||
178 | test_expect_success 'commit message from stdin' ' | |
179 | ( | |
180 | cd subdir && | |
181 | echo "Log with foo word" | git commit --allow-empty -F - | |
182 | ) && | |
183 | commit_msg_is "Log with foo word" | |
184 | ' | |
185 | ||
aae94ffb SB |
186 | test_expect_success 'commit -F overrides -t' ' |
187 | ( | |
188 | cd subdir && | |
189 | echo "-F log" > f.log && | |
190 | echo "-t template" > t.template && | |
191 | git commit --allow-empty -F f.log -t t.template | |
192 | ) && | |
193 | commit_msg_is "-F log" | |
194 | ' | |
195 | ||
d1cc130a | 196 | test_done |