1#!/bin/sh
2#
3# Copyright (c) 2007 Steven Grimm
4#
56
test_description='git-commit
78
Tests for selected commit options.'
910
. ./test-lib.sh
1112
commit_msg_is () {
13test "`git log --pretty=format:%s%b -1`" = "$1"
14}
1516
# A sanity check to see if commit is working at all.
17test_expect_success 'a basic commit in an empty tree should succeed' '
18echo content > foo &&
19git add foo &&
20git commit -m "initial commit"
21'
2223
test_expect_success 'nonexistent template file should return error' '
24echo changes >> foo &&
25git add foo &&
26! git commit --template "$PWD"/notexist
27'
2829
test_expect_success 'nonexistent template file in config should return error' '
30git config commit.template "$PWD"/notexist &&
31! git commit &&
32git config --unset commit.template
33'
3435
# From now on we'll use a template file that exists.
36TEMPLATE="$PWD"/template
3738
test_expect_success 'unedited template should not commit' '
39echo "template line" > "$TEMPLATE" &&
40! git commit --template "$TEMPLATE"
41'
4243
test_expect_success 'unedited template with comments should not commit' '
44echo "# comment in template" >> "$TEMPLATE" &&
45! git commit --template "$TEMPLATE"
46'
4748
test_expect_success 'a Signed-off-by line by itself should not commit' '
49! GIT_EDITOR=../t7500/add-signed-off git commit --template "$TEMPLATE"
50'
5152
test_expect_success 'adding comments to a template should not commit' '
53! GIT_EDITOR=../t7500/add-comments git commit --template "$TEMPLATE"
54'
5556
test_expect_success 'adding real content to a template should commit' '
57GIT_EDITOR=../t7500/add-content git commit --template "$TEMPLATE" &&
58commit_msg_is "template linecommit message"
59'
6061
test_expect_success '-t option should be short for --template' '
62echo "short template" > "$TEMPLATE" &&
63echo "new content" >> foo &&
64git add foo &&
65GIT_EDITOR=../t7500/add-content git commit -t "$TEMPLATE" &&
66commit_msg_is "short templatecommit message"
67'
6869
test_expect_success 'config-specified template should commit' '
70echo "new template" > "$TEMPLATE" &&
71git config commit.template "$TEMPLATE" &&
72echo "more content" >> foo &&
73git add foo &&
74GIT_EDITOR=../t7500/add-content git commit &&
75git config --unset commit.template &&
76commit_msg_is "new templatecommit message"
77'
7879
test_expect_success 'explicit commit message should override template' '
80echo "still more content" >> foo &&
81git add foo &&
82GIT_EDITOR=../t7500/add-content git commit --template "$TEMPLATE" \
83-m "command line msg" &&
84commit_msg_is "command line msg"
85'
8687
test_expect_success 'commit message from file should override template' '
88echo "content galore" >> foo &&
89git add foo &&
90echo "standard input msg" |
91GIT_EDITOR=../t7500/add-content git commit \
92--template "$TEMPLATE" --file - &&
93commit_msg_is "standard input msg"
94'
9596
test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
9798
cp .git/index saved-index &&
99(
100echo some new content >file &&
101GIT_INDEX_FILE=.git/another_index &&
102export GIT_INDEX_FILE &&
103git add file &&
104git commit -m "commit using another index" &&
105git diff-index --exit-code HEAD &&
106git diff-files --exit-code
107) &&
108cmp .git/index saved-index >/dev/null
109110
'
111112
test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
113114
cp .git/index saved-index &&
115(
116rm -f .git/no-such-index &&
117GIT_INDEX_FILE=.git/no-such-index &&
118export GIT_INDEX_FILE &&
119git commit -m "commit using nonexistent index" &&
120test -z "$(git ls-files)" &&
121test -z "$(git ls-tree HEAD)"
122123
) &&
124cmp .git/index saved-index >/dev/null
125126
'
127128
test_done