Skip to content

Commit c37f9a1

Browse files
committed
Merge branch 'da/user-useconfigonly'
The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name configuration variables, serving as a reminder for those who work on multiple projects and do not want to put these in their $HOME/.gitconfig. * da/user-useconfigonly: ident: add user.useConfigOnly boolean for when ident shouldn't be guessed fmt_ident: refactor strictness checks
2 parents dbda66b + 4d5c295 commit c37f9a1

File tree

3 files changed

+89
-22
lines changed

3 files changed

+89
-22
lines changed

‎Documentation/config.txt‎

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

2833+
user.useConfigOnly::
2834+
Instruct Git to avoid trying to guess defaults for 'user.email'
2835+
and 'user.name', and instead retrieve the values only from the
2836+
configuration. For example, if you have multiple email addresses
2837+
and would like to use a different one for each repository, then
2838+
with this configuration option set to `true` in the global config
2839+
along with a name, Git will prompt you to set up an email before
2840+
making new commits in a newly cloned repository.
2841+
Defaults to `false`.
2842+
28332843
user.signingKey::
28342844
If linkgit:git-tag[1] or linkgit:git-commit[1] is not selecting the
28352845
key you want it to automatically when creating a signed tag or

‎ident.c‎

Lines changed: 40 additions & 22 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) "&"
@@ -345,32 +348,40 @@ const char *fmt_ident(const char *name, const char *email,
345348
int want_date = !(flag & IDENT_NO_DATE);
346349
int want_name = !(flag & IDENT_NO_NAME);
347350

348-
if (want_name && !name)
349-
name = ident_default_name();
350-
if (!email)
351-
email = ident_default_email();
352-
353-
if (want_name && !*name) {
354-
struct passwd *pw;
355-
356-
if (strict) {
357-
if (name == git_default_name.buf)
351+
if (want_name) {
352+
int using_default = 0;
353+
if (!name) {
354+
name = ident_default_name();
355+
using_default = 1;
356+
if (strict && default_name_is_bogus) {
358357
fputs(env_hint, stderr);
359-
die("empty ident name (for <%s>) not allowed", email);
358+
die("unable to auto-detect name (got '%s')", name);
359+
}
360+
if (strict && ident_use_config_only
361+
&& !(ident_config_given & IDENT_NAME_GIVEN))
362+
die("user.useConfigOnly set but no name given");
363+
}
364+
if (!*name) {
365+
struct passwd *pw;
366+
if (strict) {
367+
if (using_default)
368+
fputs(env_hint, stderr);
369+
die("empty ident name (for <%s>) not allowed", email);
370+
}
371+
pw = xgetpwuid_self(NULL);
372+
name = pw->pw_name;
360373
}
361-
pw = xgetpwuid_self(NULL);
362-
name = pw->pw_name;
363-
}
364-
365-
if (want_name && strict &&
366-
name == git_default_name.buf && default_name_is_bogus) {
367-
fputs(env_hint, stderr);
368-
die("unable to auto-detect name (got '%s')", name);
369374
}
370375

371-
if (strict && email == git_default_email.buf && default_email_is_bogus) {
372-
fputs(env_hint, stderr);
373-
die("unable to auto-detect email address (got '%s')", email);
376+
if (!email) {
377+
email = ident_default_email();
378+
if (strict && default_email_is_bogus) {
379+
fputs(env_hint, stderr);
380+
die("unable to auto-detect email address (got '%s')", email);
381+
}
382+
if (strict && ident_use_config_only
383+
&& !(ident_config_given & IDENT_MAIL_GIVEN))
384+
die("user.useConfigOnly set but no mail given");
374385
}
375386

376387
strbuf_reset(&ident);
@@ -444,13 +455,19 @@ int author_ident_sufficiently_given(void)
444455

445456
int git_ident_config(const char *var, const char *value, void *data)
446457
{
458+
if (!strcmp(var, "user.useconfigonly")) {
459+
ident_use_config_only = git_config_bool(var, value);
460+
return 0;
461+
}
462+
447463
if (!strcmp(var, "user.name")) {
448464
if (!value)
449465
return config_error_nonbool(var);
450466
strbuf_reset(&git_default_name);
451467
strbuf_addstr(&git_default_name, value);
452468
committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
453469
author_ident_explicitly_given |= IDENT_NAME_GIVEN;
470+
ident_config_given |= IDENT_NAME_GIVEN;
454471
return 0;
455472
}
456473

@@ -461,6 +478,7 @@ int git_ident_config(const char *var, const char *value, void *data)
461478
strbuf_addstr(&git_default_email, value);
462479
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
463480
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
481+
ident_config_given |= IDENT_MAIL_GIVEN;
464482
return 0;
465483
}
466484

‎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)