Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Junio C Hamano committed Feb 17, 2016
2 parents dbda66b + 4d5c295 commit c37f9a1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 22 deletions.
10 changes: 10 additions & 0 deletions Documentation/config.txt
Expand Up @@ -2830,6 +2830,16 @@ user.name::
Can be overridden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME'
environment variables. See linkgit:git-commit-tree[1].

user.useConfigOnly::
Instruct Git to avoid trying to guess defaults for 'user.email'
and 'user.name', and instead retrieve the values only from the
configuration. For example, if you have multiple email addresses
and would like to use a different one for each repository, then
with this configuration option set to `true` in the global config
along with a name, Git will prompt you to set up an email before
making new commits in a newly cloned repository.
Defaults to `false`.

user.signingKey::
If linkgit:git-tag[1] or linkgit:git-commit[1] is not selecting the
key you want it to automatically when creating a signed tag or
Expand Down
62 changes: 40 additions & 22 deletions ident.c
Expand Up @@ -13,11 +13,14 @@ static struct strbuf git_default_date = STRBUF_INIT;
static int default_email_is_bogus;
static int default_name_is_bogus;

static int ident_use_config_only;

#define IDENT_NAME_GIVEN 01
#define IDENT_MAIL_GIVEN 02
#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
static int committer_ident_explicitly_given;
static int author_ident_explicitly_given;
static int ident_config_given;

#ifdef NO_GECOS_IN_PWENT
#define get_gecos(ignored) "&"
Expand Down Expand Up @@ -345,32 +348,40 @@ const char *fmt_ident(const char *name, const char *email,
int want_date = !(flag & IDENT_NO_DATE);
int want_name = !(flag & IDENT_NO_NAME);

if (want_name && !name)
name = ident_default_name();
if (!email)
email = ident_default_email();

if (want_name && !*name) {
struct passwd *pw;

if (strict) {
if (name == git_default_name.buf)
if (want_name) {
int using_default = 0;
if (!name) {
name = ident_default_name();
using_default = 1;
if (strict && default_name_is_bogus) {
fputs(env_hint, stderr);
die("empty ident name (for <%s>) not allowed", email);
die("unable to auto-detect name (got '%s')", name);
}
if (strict && ident_use_config_only
&& !(ident_config_given & IDENT_NAME_GIVEN))
die("user.useConfigOnly set but no name given");
}
if (!*name) {
struct passwd *pw;
if (strict) {
if (using_default)
fputs(env_hint, stderr);
die("empty ident name (for <%s>) not allowed", email);
}
pw = xgetpwuid_self(NULL);
name = pw->pw_name;
}
pw = xgetpwuid_self(NULL);
name = pw->pw_name;
}

if (want_name && strict &&
name == git_default_name.buf && default_name_is_bogus) {
fputs(env_hint, stderr);
die("unable to auto-detect name (got '%s')", name);
}

if (strict && email == git_default_email.buf && default_email_is_bogus) {
fputs(env_hint, stderr);
die("unable to auto-detect email address (got '%s')", email);
if (!email) {
email = ident_default_email();
if (strict && default_email_is_bogus) {
fputs(env_hint, stderr);
die("unable to auto-detect email address (got '%s')", email);
}
if (strict && ident_use_config_only
&& !(ident_config_given & IDENT_MAIL_GIVEN))
die("user.useConfigOnly set but no mail given");
}

strbuf_reset(&ident);
Expand Down Expand Up @@ -444,13 +455,19 @@ int author_ident_sufficiently_given(void)

int git_ident_config(const char *var, const char *value, void *data)
{
if (!strcmp(var, "user.useconfigonly")) {
ident_use_config_only = git_config_bool(var, value);
return 0;
}

if (!strcmp(var, "user.name")) {
if (!value)
return config_error_nonbool(var);
strbuf_reset(&git_default_name);
strbuf_addstr(&git_default_name, value);
committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
author_ident_explicitly_given |= IDENT_NAME_GIVEN;
ident_config_given |= IDENT_NAME_GIVEN;
return 0;
}

Expand All @@ -461,6 +478,7 @@ int git_ident_config(const char *var, const char *value, void *data)
strbuf_addstr(&git_default_email, value);
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
ident_config_given |= IDENT_MAIL_GIVEN;
return 0;
}

Expand Down
39 changes: 39 additions & 0 deletions t/t7517-per-repo-email.sh
@@ -0,0 +1,39 @@
#!/bin/sh
#
# Copyright (c) 2016 Dan Aloni
# Copyright (c) 2016 Jeff King
#

test_description='per-repo forced setting of email address'

. ./test-lib.sh

test_expect_success 'setup a likely user.useConfigOnly use case' '
# we want to make sure a reflog is written, since that needs
# a non-strict ident. So be sure we have an actual commit.
test_commit foo &&
sane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
sane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&
git config user.name "test" &&
git config --global user.useConfigOnly true
'

test_expect_success 'fails committing if clone email is not set' '
test_must_fail git commit --allow-empty -m msg
'

test_expect_success 'fails committing if clone email is not set, but EMAIL set' '
test_must_fail env EMAIL=test@fail.com git commit --allow-empty -m msg
'

test_expect_success 'succeeds committing if clone email is set' '
test_config user.email "test@ok.com" &&
git commit --allow-empty -m msg
'

test_expect_success 'succeeds cloning if global email is not set' '
git clone . clone
'

test_done

0 comments on commit c37f9a1

Please sign in to comment.