blob: 163ae8046850e729ef5e329f6589faf5381e9cdc [file] [log] [blame]
Dan Aloni4d5c2952016-02-06 08:23:36 +02001#!/bin/sh
2#
3# Copyright (c) 2016 Dan Aloni
4# Copyright (c) 2016 Jeff King
5#
6
7test_description='per-repo forced setting of email address'
8
Johannes Schindelin1e2ae142020-11-18 23:44:40 +00009GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +000010export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11
Dan Aloni4d5c2952016-02-06 08:23:36 +020012. ./test-lib.sh
13
14test_expect_success 'setup a likely user.useConfigOnly use case' '
15 # we want to make sure a reflog is written, since that needs
16 # a non-strict ident. So be sure we have an actual commit.
17 test_commit foo &&
18
19 sane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
20 sane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&
21 git config user.name "test" &&
22 git config --global user.useConfigOnly true
23'
24
25test_expect_success 'fails committing if clone email is not set' '
26 test_must_fail git commit --allow-empty -m msg
27'
28
29test_expect_success 'fails committing if clone email is not set, but EMAIL set' '
30 test_must_fail env EMAIL=test@fail.com git commit --allow-empty -m msg
31'
32
33test_expect_success 'succeeds committing if clone email is set' '
34 test_config user.email "test@ok.com" &&
35 git commit --allow-empty -m msg
36'
37
38test_expect_success 'succeeds cloning if global email is not set' '
39 git clone . clone
40'
41
Jeff King1e461c42016-07-29 18:31:35 -040042test_expect_success 'set up rebase scenarios' '
43 # temporarily enable an actual ident for this setup
44 test_config user.email foo@example.com &&
45 test_commit new &&
46 git branch side-without-commit HEAD^ &&
47 git checkout -b side-with-commit HEAD^ &&
48 test_commit side
49'
50
51test_expect_success 'fast-forward rebase does not care about ident' '
52 git checkout -B tmp side-without-commit &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000053 git rebase main
Jeff King1e461c42016-07-29 18:31:35 -040054'
55
56test_expect_success 'non-fast-forward rebase refuses to write commits' '
57 test_when_finished "git rebase --abort || true" &&
58 git checkout -B tmp side-with-commit &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000059 test_must_fail git rebase main
Jeff King1e461c42016-07-29 18:31:35 -040060'
61
62test_expect_success 'fast-forward rebase does not care about ident (interactive)' '
63 git checkout -B tmp side-without-commit &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000064 git rebase -i main
Jeff King1e461c42016-07-29 18:31:35 -040065'
66
67test_expect_success 'non-fast-forward rebase refuses to write commits (interactive)' '
68 test_when_finished "git rebase --abort || true" &&
69 git checkout -B tmp side-with-commit &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000070 test_must_fail git rebase -i main
Jeff King1e461c42016-07-29 18:31:35 -040071'
72
73test_expect_success 'noop interactive rebase does not care about ident' '
74 git checkout -B tmp side-with-commit &&
75 git rebase -i HEAD^
76'
77
William Hubbs39ab4d02019-02-04 12:48:50 -060078test_expect_success 'author.name overrides user.name' '
79 test_config user.name user &&
80 test_config user.email user@example.com &&
81 test_config author.name author &&
82 test_commit author-name-override-user &&
83 echo author user@example.com > expected-author &&
84 echo user user@example.com > expected-committer &&
85 git log --format="%an %ae" -1 > actual-author &&
86 git log --format="%cn %ce" -1 > actual-committer &&
87 test_cmp expected-author actual-author &&
88 test_cmp expected-committer actual-committer
89'
90
91test_expect_success 'author.email overrides user.email' '
92 test_config user.name user &&
93 test_config user.email user@example.com &&
94 test_config author.email author@example.com &&
95 test_commit author-email-override-user &&
96 echo user author@example.com > expected-author &&
97 echo user user@example.com > expected-committer &&
98 git log --format="%an %ae" -1 > actual-author &&
99 git log --format="%cn %ce" -1 > actual-committer &&
100 test_cmp expected-author actual-author &&
101 test_cmp expected-committer actual-committer
102'
103
104test_expect_success 'committer.name overrides user.name' '
105 test_config user.name user &&
106 test_config user.email user@example.com &&
107 test_config committer.name committer &&
108 test_commit committer-name-override-user &&
109 echo user user@example.com > expected-author &&
110 echo committer user@example.com > expected-committer &&
111 git log --format="%an %ae" -1 > actual-author &&
112 git log --format="%cn %ce" -1 > actual-committer &&
113 test_cmp expected-author actual-author &&
114 test_cmp expected-committer actual-committer
115'
116
117test_expect_success 'committer.email overrides user.email' '
118 test_config user.name user &&
119 test_config user.email user@example.com &&
120 test_config committer.email committer@example.com &&
121 test_commit committer-email-override-user &&
122 echo user user@example.com > expected-author &&
123 echo user committer@example.com > expected-committer &&
124 git log --format="%an %ae" -1 > actual-author &&
125 git log --format="%cn %ce" -1 > actual-committer &&
126 test_cmp expected-author actual-author &&
127 test_cmp expected-committer actual-committer
128'
129
130test_expect_success 'author and committer environment variables override config settings' '
131 test_config user.name user &&
132 test_config user.email user@example.com &&
133 test_config author.name author &&
134 test_config author.email author@example.com &&
135 test_config committer.name committer &&
136 test_config committer.email committer@example.com &&
137 GIT_AUTHOR_NAME=env_author && export GIT_AUTHOR_NAME &&
138 GIT_AUTHOR_EMAIL=env_author@example.com && export GIT_AUTHOR_EMAIL &&
139 GIT_COMMITTER_NAME=env_commit && export GIT_COMMITTER_NAME &&
140 GIT_COMMITTER_EMAIL=env_commit@example.com && export GIT_COMMITTER_EMAIL &&
141 test_commit env-override-conf &&
142 echo env_author env_author@example.com > expected-author &&
143 echo env_commit env_commit@example.com > expected-committer &&
144 git log --format="%an %ae" -1 > actual-author &&
145 git log --format="%cn %ce" -1 > actual-committer &&
146 sane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
147 sane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&
148 test_cmp expected-author actual-author &&
149 test_cmp expected-committer actual-committer
150'
151
Dan Aloni4d5c2952016-02-06 08:23:36 +0200152test_done