Skip to content

Commit 4d5c295

Browse files
da-xgitster
authored andcommitted
ident: add user.useConfigOnly boolean for when ident shouldn't be guessed
It used to be that: git config --global user.email "(none)" was a viable way for people to force themselves to set user.email in each repository. This was helpful for people with more than one email address, targeting different email addresses for different clones, as it barred git from creating a commit unless the user.email config was set in the per-repo config to the correct email address. A recent change, 19ce497 (ident: keep a flag for bogus default_email, 2015-12-10), however, declared that an explicitly configured user.email is not bogus, no matter what its value is, so this hack no longer works. Provide the same functionality by adding a new configuration variable user.useConfigOnly; when this variable is set, the user must explicitly set user.email configuration. Signed-off-by: Junio C Hamano <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Dan Aloni <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 59f9295 commit 4d5c295

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

Documentation/config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,16 @@ user.name::
27752775
Can be overridden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME'
27762776
environment variables. See linkgit:git-commit-tree[1].
27772777

2778+
user.useConfigOnly::
2779+
Instruct Git to avoid trying to guess defaults for 'user.email'
2780+
and 'user.name', and instead retrieve the values only from the
2781+
configuration. For example, if you have multiple email addresses
2782+
and would like to use a different one for each repository, then
2783+
with this configuration option set to `true` in the global config
2784+
along with a name, Git will prompt you to set up an email before
2785+
making new commits in a newly cloned repository.
2786+
Defaults to `false`.
2787+
27782788
user.signingKey::
27792789
If linkgit:git-tag[1] or linkgit:git-commit[1] is not selecting the
27802790
key you want it to automatically when creating a signed tag or

ident.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ static struct strbuf git_default_date = STRBUF_INIT;
1313
static int default_email_is_bogus;
1414
static int default_name_is_bogus;
1515

16+
static int ident_use_config_only;
17+
1618
#define IDENT_NAME_GIVEN 01
1719
#define IDENT_MAIL_GIVEN 02
1820
#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
1921
static int committer_ident_explicitly_given;
2022
static int author_ident_explicitly_given;
23+
static int ident_config_given;
2124

2225
#ifdef NO_GECOS_IN_PWENT
2326
#define get_gecos(ignored) "&"
@@ -354,6 +357,9 @@ const char *fmt_ident(const char *name, const char *email,
354357
fputs(env_hint, stderr);
355358
die("unable to auto-detect name (got '%s')", name);
356359
}
360+
if (strict && ident_use_config_only
361+
&& !(ident_config_given & IDENT_NAME_GIVEN))
362+
die("user.useConfigOnly set but no name given");
357363
}
358364
if (!*name) {
359365
struct passwd *pw;
@@ -373,6 +379,9 @@ const char *fmt_ident(const char *name, const char *email,
373379
fputs(env_hint, stderr);
374380
die("unable to auto-detect email address (got '%s')", email);
375381
}
382+
if (strict && ident_use_config_only
383+
&& !(ident_config_given & IDENT_MAIL_GIVEN))
384+
die("user.useConfigOnly set but no mail given");
376385
}
377386

378387
strbuf_reset(&ident);
@@ -446,13 +455,19 @@ int author_ident_sufficiently_given(void)
446455

447456
int git_ident_config(const char *var, const char *value, void *data)
448457
{
458+
if (!strcmp(var, "user.useconfigonly")) {
459+
ident_use_config_only = git_config_bool(var, value);
460+
return 0;
461+
}
462+
449463
if (!strcmp(var, "user.name")) {
450464
if (!value)
451465
return config_error_nonbool(var);
452466
strbuf_reset(&git_default_name);
453467
strbuf_addstr(&git_default_name, value);
454468
committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
455469
author_ident_explicitly_given |= IDENT_NAME_GIVEN;
470+
ident_config_given |= IDENT_NAME_GIVEN;
456471
return 0;
457472
}
458473

@@ -463,6 +478,7 @@ int git_ident_config(const char *var, const char *value, void *data)
463478
strbuf_addstr(&git_default_email, value);
464479
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
465480
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
481+
ident_config_given |= IDENT_MAIL_GIVEN;
466482
return 0;
467483
}
468484

t/t7517-per-repo-email.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
#
3+
# Copyright (c) 2016 Dan Aloni
4+
# Copyright (c) 2016 Jeff King
5+
#
6+
7+
test_description='per-repo forced setting of email address'
8+
9+
. ./test-lib.sh
10+
11+
test_expect_success 'setup a likely user.useConfigOnly use case' '
12+
# we want to make sure a reflog is written, since that needs
13+
# a non-strict ident. So be sure we have an actual commit.
14+
test_commit foo &&
15+
16+
sane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
17+
sane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&
18+
git config user.name "test" &&
19+
git config --global user.useConfigOnly true
20+
'
21+
22+
test_expect_success 'fails committing if clone email is not set' '
23+
test_must_fail git commit --allow-empty -m msg
24+
'
25+
26+
test_expect_success 'fails committing if clone email is not set, but EMAIL set' '
27+
test_must_fail env [email protected] git commit --allow-empty -m msg
28+
'
29+
30+
test_expect_success 'succeeds committing if clone email is set' '
31+
test_config user.email "[email protected]" &&
32+
git commit --allow-empty -m msg
33+
'
34+
35+
test_expect_success 'succeeds cloning if global email is not set' '
36+
git clone . clone
37+
'
38+
39+
test_done

0 commit comments

Comments
 (0)