* [PATCH v6] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed @ 2016-02-05 7:42 Dan Aloni 2016-02-05 7:42 ` [PATCH v6 1/3] fmt_ident: refactor strictness checks Dan Aloni ` (2 more replies) 0 siblings, 3 replies; 11+ messages in thread From: Dan Aloni @ 2016-02-05 7:42 UTC (permalink / raw) To: git; +Cc: Junio C Hamano Change between v5 -> v6: * Removed trailing comma in 'enum ident_person'. v5: http://article.gmane.org/gmane.comp.version-control.git/285544 ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v6 1/3] fmt_ident: refactor strictness checks 2016-02-05 7:42 [PATCH v6] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni @ 2016-02-05 7:42 ` Dan Aloni 2016-02-05 7:42 ` [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni 2016-02-05 7:42 ` [PATCH v6 3/3] ident: cleanup wrt ident's source Dan Aloni 2 siblings, 0 replies; 11+ messages in thread From: Dan Aloni @ 2016-02-05 7:42 UTC (permalink / raw) To: git; +Cc: Junio C Hamano, Jeff King From: Jeff King <peff@peff.net> This function has evolved quite a bit over time, and as a result, the logic for "is this an OK ident" has been sprinkled throughout. This ends up with a lot of redundant conditionals, like checking want_name repeatedly. Worse, we want to know in many cases whether we are using the "default" ident, and we do so by comparing directly to the global strbuf, which violates the abstraction of the ident_default_* functions. Let's reorganize the function into a hierarchy of conditionals to handle similar cases together. The only case that doesn't just work naturally for this is that of an empty name, where our advice is different based on whether we came from ident_default_name() or not. We can use a simple flag to cover this case. Signed-off-by: Jeff King <peff@peff.net> --- ident.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/ident.c b/ident.c index 3da555634290..f3a431f738cc 100644 --- a/ident.c +++ b/ident.c @@ -345,32 +345,34 @@ 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 (!*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); + } } strbuf_reset(&ident); -- 2.5.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed 2016-02-05 7:42 [PATCH v6] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni 2016-02-05 7:42 ` [PATCH v6 1/3] fmt_ident: refactor strictness checks Dan Aloni @ 2016-02-05 7:42 ` Dan Aloni 2016-02-05 19:18 ` Jeff King 2016-02-05 19:31 ` Junio C Hamano 2016-02-05 7:42 ` [PATCH v6 3/3] ident: cleanup wrt ident's source Dan Aloni 2 siblings, 2 replies; 11+ messages in thread From: Dan Aloni @ 2016-02-05 7:42 UTC (permalink / raw) To: git; +Cc: Junio C Hamano, Eric Sunshine 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 commit unless the user.email config was set in the per-repo config to the correct email address. A recent change, 19ce497c (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: Dan Aloni <alonid@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com> Cc: Eric Sunshine <sunshine@sunshineco.com> --- Documentation/config.txt | 9 ++++++++ ident.c | 16 +++++++++++++ t/t9904-per-repo-email.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100755 t/t9904-per-repo-email.sh diff --git a/Documentation/config.txt b/Documentation/config.txt index 02bcde6bb596..25cf7ce4e83a 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -2821,6 +2821,15 @@ user.name:: Can be overridden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME' environment variables. See linkgit:git-commit-tree[1]. +user.useConfigOnly:: + This instruct Git to avoid trying to guess defaults for 'user.email' + and 'user.name' other than strictly from environment or config. + If you have multiple email addresses that you would like to set + up per repository, you may want to set this to 'true' in the global + config, and then Git would prompt you to set user.email separately, + in each of the cloned repositories. + 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 diff --git a/ident.c b/ident.c index f3a431f738cc..9bd6ac69bfe8 100644 --- a/ident.c +++ b/ident.c @@ -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) "&" @@ -354,6 +357,9 @@ const char *fmt_ident(const char *name, const char *email, fputs(env_hint, stderr); 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; @@ -373,6 +379,9 @@ const char *fmt_ident(const char *name, const char *email, 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); @@ -446,6 +455,11 @@ 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); @@ -453,6 +467,7 @@ int git_ident_config(const char *var, const char *value, void *data) 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; } @@ -463,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; } diff --git a/t/t9904-per-repo-email.sh b/t/t9904-per-repo-email.sh new file mode 100755 index 000000000000..0430f2e38434 --- /dev/null +++ b/t/t9904-per-repo-email.sh @@ -0,0 +1,58 @@ +#!/bin/sh +# +# Copyright (c) 2016 Dan Aloni +# + +test_description='per-repo forced setting of email address' + +. ./test-lib.sh + +prepare () { + # Have a non-empty repository + rm -fr .git + git init + echo "Initial" >foo && + git add foo && + git commit -m foo && + + # Setup a likely user.useConfigOnly use case + sane_unset GIT_AUTHOR_NAME && + sane_unset GIT_AUTHOR_EMAIL && + test_unconfig --global user.name && + test_unconfig --global user.email && + test_config user.name "test" && + test_unconfig user.email && + test_config_global user.useConfigOnly true +} + +about_to_commit () { + echo "Second" >>foo && + git add foo +} + +test_expect_success 'fails committing if clone email is not set' ' + prepare && about_to_commit && + + test_must_fail git commit -m msg +' + +test_expect_success 'fails committing if clone email is not set, but EMAIL set' ' + prepare && about_to_commit && + + test_must_fail env EMAIL=test@fail.com git commit -m msg +' + +test_expect_success 'succeeds committing if clone email is set' ' + prepare && about_to_commit && + + test_config user.email "test@ok.com" && + git commit -m msg +' + +test_expect_success 'succeeds cloning if global email is not set' ' + prepare && + + git clone . clone +' + +test_done -- 2.5.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed 2016-02-05 7:42 ` [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni @ 2016-02-05 19:18 ` Jeff King 2016-02-05 19:30 ` Eric Sunshine 2016-02-05 19:31 ` Junio C Hamano 1 sibling, 1 reply; 11+ messages in thread From: Jeff King @ 2016-02-05 19:18 UTC (permalink / raw) To: Dan Aloni; +Cc: git, Junio C Hamano, Eric Sunshine On Fri, Feb 05, 2016 at 09:42:27AM +0200, Dan Aloni wrote: > diff --git a/Documentation/config.txt b/Documentation/config.txt > index 02bcde6bb596..25cf7ce4e83a 100644 > --- a/Documentation/config.txt > +++ b/Documentation/config.txt > @@ -2821,6 +2821,15 @@ user.name:: > Can be overridden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME' > environment variables. See linkgit:git-commit-tree[1]. > > +user.useConfigOnly:: > + This instruct Git to avoid trying to guess defaults for 'user.email' s/instruct/instructs/ > + and 'user.name' other than strictly from environment or config. I find mention of the environment a bit ambiguous. Given our discussion, I'm sure you mean $GIT_AUTHOR_EMAIL, etc, and not $EMAIL. But I don't think that is clear to somebody who has not been looking at this patch series. I actually think we could simply say "other than strictly from the config", as people don't generally use $GIT_* themselves (rather, they get used mostly for inter-process communication, so at most script authors need to know about them). > + If you have multiple email addresses that you would like to set > + up per repository, you may want to set this to 'true' in the global I parsed this sentence as "multiple addresses per repository". Maybe: If you have multiple email addresses and would like to use a different one for each repository, you may... would be more clear? > +test_description='per-repo forced setting of email address' > + > +. ./test-lib.sh > + > +prepare () { > + # Have a non-empty repository > + rm -fr .git > + git init > + echo "Initial" >foo && > + git add foo && > + git commit -m foo && > + > + # Setup a likely user.useConfigOnly use case > + sane_unset GIT_AUTHOR_NAME && > + sane_unset GIT_AUTHOR_EMAIL && > + test_unconfig --global user.name && > + test_unconfig --global user.email && > + test_config user.name "test" && > + test_unconfig user.email && > + test_config_global user.useConfigOnly true > +} > + > +about_to_commit () { > + echo "Second" >>foo && > + git add foo > +} > + > +test_expect_success 'fails committing if clone email is not set' ' > + prepare && about_to_commit && > + > + test_must_fail git commit -m msg > +' The flow of this test script is a bit different than what we usually write. Typically we have some early test_expect_success blocks do setup for the whole script, and then progress through a sequence (and we rely on the test harness to do things like "git init"). IOW, most of your "prepare" would go in the first block, and then the rest of the tests rely on it. The only thing I really see that needs to be repeated for each test is setting up the "about to commit" scenario. But you can simply use "commit --allow-empty" so that the tests work no matter what state the previous test left us in. We care about the ident, not what gets committed. -Peff ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed 2016-02-05 19:18 ` Jeff King @ 2016-02-05 19:30 ` Eric Sunshine 0 siblings, 0 replies; 11+ messages in thread From: Eric Sunshine @ 2016-02-05 19:30 UTC (permalink / raw) To: Jeff King; +Cc: Dan Aloni, Git List, Junio C Hamano On Fri, Feb 5, 2016 at 2:18 PM, Jeff King <peff@peff.net> wrote: > On Fri, Feb 05, 2016 at 09:42:27AM +0200, Dan Aloni wrote: >> +prepare () { >> + # Have a non-empty repository >> + rm -fr .git >> + git init >> + echo "Initial" >foo && >> + git add foo && >> + git commit -m foo && >> + >> + # Setup a likely user.useConfigOnly use case >> + sane_unset GIT_AUTHOR_NAME && >> + sane_unset GIT_AUTHOR_EMAIL && >> + test_unconfig --global user.name && >> + test_unconfig --global user.email && >> + test_config user.name "test" && >> + test_unconfig user.email && >> + test_config_global user.useConfigOnly true >> +} > > The flow of this test script is a bit different than what we usually > write. Typically we have some early test_expect_success blocks do setup > for the whole script, and then progress through a sequence (and we rely > on the test harness to do things like "git init"). > > IOW, most of your "prepare" would go in the first block, and then the > rest of the tests rely on it. > > The only thing I really see that needs to be repeated for each test is > setting up the "about to commit" scenario. But you can simply use > "commit --allow-empty" so that the tests work no matter what state the > previous test left us in. We care about the ident, not what gets > committed. I was going to make all the same suggestions, so thanks. One thing to add is that the &&-chain is broken in the prepare() function. ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed 2016-02-05 7:42 ` [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni 2016-02-05 19:18 ` Jeff King @ 2016-02-05 19:31 ` Junio C Hamano 2016-02-05 21:14 ` Dan Aloni 1 sibling, 1 reply; 11+ messages in thread From: Junio C Hamano @ 2016-02-05 19:31 UTC (permalink / raw) To: Dan Aloni; +Cc: git, Eric Sunshine Dan Aloni <alonid@gmail.com> writes: > +user.useConfigOnly:: > + This instruct Git to avoid trying to guess defaults for 'user.email' > + and 'user.name' other than strictly from environment or config. OK. > + If you have multiple email addresses that you would like to set > + up per repository, you may want to set this to 'true' in the global > + config, and then Git would prompt you to set user.email separately, > + in each of the cloned repositories. The first sentence mentioned both name and email, but here the example is only about email. A first time reader might be led into thinking this is only about email and not name, but I am assuming that is not the intention (i.e. this is merely showing just one use case). > + 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 > diff --git a/ident.c b/ident.c > index f3a431f738cc..9bd6ac69bfe8 100644 > --- a/ident.c > +++ b/ident.c > @@ -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) "&" > @@ -354,6 +357,9 @@ const char *fmt_ident(const char *name, const char *email, > fputs(env_hint, stderr); > 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; > @@ -373,6 +379,9 @@ const char *fmt_ident(const char *name, const char *email, > 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"); I can read the split expression either with && hanging at the end of line or && leading the next line just fine, but you'd want to be consistent especially when you are writing two almost identical things. > diff --git a/t/t9904-per-repo-email.sh b/t/t9904-per-repo-email.sh > new file mode 100755 > index 000000000000..0430f2e38434 > --- /dev/null > +++ b/t/t9904-per-repo-email.sh > @@ -0,0 +1,58 @@ > +#!/bin/sh > +# > +# Copyright (c) 2016 Dan Aloni > +# > + > +test_description='per-repo forced setting of email address' > + > +. ./test-lib.sh > + > +prepare () { > + # Have a non-empty repository > + rm -fr .git > + git init Hmm, do we do something drastic like this in any of our existing tests? When your test script starts by dot-sourcing test-lib.sh, you will be given an initial repository with an empty history, so I'd expect that you would be able to use that without calling "prepare" over and over again. The usual convention is to do this kind of setup to establish a reasonable baseline in the first test titled 'setup'. I think the part up to where you unset the environment variables (by the way, don't you need to unset GIT_COMMITTER_* variables, too?) belongs to the 'setup' that is done only once at the beginning of this script. Each of your tests will make changes to the state by setting or unsetting configuration variables and possibly making commits, that would affect the state of the repository that will be used by the next and subsequent tests. test_when_finished helper can register the clean-up procedure to revert these possible state changes, and you can further avoid code duplication by calling that same clean-up procedure at the end of the setup test. So if this followed the style of a typical existing test, we would probably do something along these lines: reprepare () { git reset --hard initial && echo Second >foo && git add foo } test_expect_success setup ' echo Initial >foo && git add foo && git commit -m foo && git tag initial && sane_unset GIT_AUTHOR_NAME GIT_COMMITTER_NAME ... && git config --global user.name test && git config --global user.useConfigOnly true && reprepare ' test_expect_success 'fail without email anywhere' ' test_when_finished reprepare && test_must_fail git commit -m msg ' test_expect_success 'suceed with config' ' test_when_finished reprepare && test_config user.email test@ok.com && test_must_fail git commit -m msg ' Note that you do not need "test_unconfig user.email" in reprepare, as the variable is set in one test with test_config, which uses test_when_finished to arrange the variable to be removed after running the test. > + echo "Initial" >foo && > + git add foo && > + git commit -m foo && > + > + # Setup a likely user.useConfigOnly use case > + sane_unset GIT_AUTHOR_NAME && > + sane_unset GIT_AUTHOR_EMAIL && > + test_unconfig --global user.name && > + test_unconfig --global user.email && > + test_config user.name "test" && > + test_unconfig user.email && > + test_config_global user.useConfigOnly true > +} > + > +about_to_commit () { > + echo "Second" >>foo && > + git add foo > +} > +test_expect_success 'fails committing if clone email is not set' ' > + prepare && about_to_commit && > + > + test_must_fail git commit -m msg > +' > +test_expect_success 'fails committing if clone email is not set, but EMAIL set' ' > + prepare && about_to_commit && > + > + test_must_fail env EMAIL=test@fail.com git commit -m msg > +' > + > +test_expect_success 'succeeds committing if clone email is set' ' > + prepare && about_to_commit && > + > + test_config user.email "test@ok.com" && > + git commit -m msg > +' > + > +test_expect_success 'succeeds cloning if global email is not set' ' > + prepare && > + > + git clone . clone > +' > + > +test_done ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed 2016-02-05 19:31 ` Junio C Hamano @ 2016-02-05 21:14 ` Dan Aloni 0 siblings, 0 replies; 11+ messages in thread From: Dan Aloni @ 2016-02-05 21:14 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, Eric Sunshine On Fri, Feb 05, 2016 at 11:31:34AM -0800, Junio C Hamano wrote: > > + If you have multiple email addresses that you would like to set > > + up per repository, you may want to set this to 'true' in the global > > + config, and then Git would prompt you to set user.email separately, > > + in each of the cloned repositories. > > The first sentence mentioned both name and email, but here the > example is only about email. A first time reader might be led into > thinking this is only about email and not name, but I am assuming > that is not the intention (i.e. this is merely showing just one use > case). >[..] Going to revise per yours and Jeff's suggestions. >[..] > I can read the split expression either with && hanging at the end of > line or && leading the next line just fine, but you'd want to be > consistent especially when you are writing two almost identical > things. Sure. >[..] > test_expect_success 'suceed with config' ' > test_when_finished reprepare && > test_config user.email test@ok.com && > test_must_fail git commit -m msg > ' > > Note that you do not need "test_unconfig user.email" in reprepare, > as the variable is set in one test with test_config, which uses > test_when_finished to arrange the variable to be removed after > running the test. Alright. It was worth to understand the differing behavior between 'test_config' and 'git config'. -- Dan Aloni ^ permalink raw reply [flat|nested] 11+ messages in thread
* [PATCH v6 3/3] ident: cleanup wrt ident's source 2016-02-05 7:42 [PATCH v6] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni 2016-02-05 7:42 ` [PATCH v6 1/3] fmt_ident: refactor strictness checks Dan Aloni 2016-02-05 7:42 ` [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni @ 2016-02-05 7:42 ` Dan Aloni 2016-02-05 19:05 ` Junio C Hamano 2 siblings, 1 reply; 11+ messages in thread From: Dan Aloni @ 2016-02-05 7:42 UTC (permalink / raw) To: git; +Cc: Junio C Hamano This change condenses the variables that tells where we got the user's ident into single enum, instead of a collection of booleans. In addtion, also have {committer,author}_ident_sufficiently_given directly probe the environment and the afformentioned enum instead of relying on git_{committer,author}_info to do so. Signed-off-by: Dan Aloni <alonid@gmail.com> Helped-by: Jeff King <peff@peff.net> Helped-by: Junio C Hamano <gitster@pobox.com> --- ident.c | 126 ++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 80 insertions(+), 46 deletions(-) diff --git a/ident.c b/ident.c index 9bd6ac69bfe8..9bb05912b59a 100644 --- a/ident.c +++ b/ident.c @@ -10,17 +10,19 @@ static struct strbuf git_default_name = STRBUF_INIT; static struct strbuf git_default_email = STRBUF_INIT; static struct strbuf git_default_date = STRBUF_INIT; -static int default_email_is_bogus; -static int default_name_is_bogus; + +enum ident_source { + IDENT_SOURCE_UNKNOWN = 0, + IDENT_SOURCE_CONFIG, + IDENT_SOURCE_ENVIRONMENT, + IDENT_SOURCE_GUESSED, + IDENT_SOURCE_GUESSED_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; +static enum ident_source source_of_default_email = IDENT_SOURCE_UNKNOWN; +static enum ident_source source_of_default_name = IDENT_SOURCE_UNKNOWN; #ifdef NO_GECOS_IN_PWENT #define get_gecos(ignored) "&" @@ -28,7 +30,7 @@ static int ident_config_given; #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) #endif -static struct passwd *xgetpwuid_self(int *is_bogus) +static struct passwd *xgetpwuid_self(enum ident_source *source) { struct passwd *pw; @@ -41,9 +43,13 @@ static struct passwd *xgetpwuid_self(int *is_bogus) fallback.pw_gecos = "Unknown"; #endif pw = &fallback; - if (is_bogus) - *is_bogus = 1; + if (source) + *source = IDENT_SOURCE_GUESSED_BOGUS; + } else { + if (source) + *source = IDENT_SOURCE_GUESSED; } + return pw; } @@ -120,26 +126,26 @@ static int canonical_name(const char *host, struct strbuf *out) return status; } -static void add_domainname(struct strbuf *out, int *is_bogus) +static void add_domainname(struct strbuf *out, enum ident_source *source) { char buf[1024]; if (gethostname(buf, sizeof(buf))) { warning("cannot get host name: %s", strerror(errno)); strbuf_addstr(out, "(none)"); - *is_bogus = 1; + *source = IDENT_SOURCE_GUESSED_BOGUS; return; } if (strchr(buf, '.')) strbuf_addstr(out, buf); else if (canonical_name(buf, out) < 0) { strbuf_addf(out, "%s.(none)", buf); - *is_bogus = 1; + *source = IDENT_SOURCE_GUESSED_BOGUS; } } static void copy_email(const struct passwd *pw, struct strbuf *email, - int *is_bogus) + enum ident_source *source) { /* * Make up a fake email address @@ -150,13 +156,13 @@ static void copy_email(const struct passwd *pw, struct strbuf *email, if (!add_mailname_host(email)) return; /* read from "/etc/mailname" (Debian) */ - add_domainname(email, is_bogus); + add_domainname(email, source); } const char *ident_default_name(void) { if (!git_default_name.len) { - copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); + copy_gecos(xgetpwuid_self(&source_of_default_name), &git_default_name); strbuf_trim(&git_default_name); } return git_default_name.buf; @@ -169,11 +175,12 @@ const char *ident_default_email(void) if (email && email[0]) { strbuf_addstr(&git_default_email, email); - committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; - author_ident_explicitly_given |= IDENT_MAIL_GIVEN; - } else - copy_email(xgetpwuid_self(&default_email_is_bogus), - &git_default_email, &default_email_is_bogus); + source_of_default_email = IDENT_SOURCE_ENVIRONMENT; + } else { + copy_email(xgetpwuid_self(&source_of_default_email), + &git_default_email, &source_of_default_email); + } + strbuf_trim(&git_default_email); } return git_default_email.buf; @@ -353,12 +360,13 @@ const char *fmt_ident(const char *name, const char *email, if (!name) { name = ident_default_name(); using_default = 1; - if (strict && default_name_is_bogus) { + if (strict && + source_of_default_name == IDENT_SOURCE_GUESSED_BOGUS) { fputs(env_hint, stderr); die("unable to auto-detect name (got '%s')", name); } if (strict && ident_use_config_only && - !(ident_config_given & IDENT_NAME_GIVEN)) + source_of_default_name != IDENT_SOURCE_CONFIG) die("user.useConfigOnly set but no name given"); } if (!*name) { @@ -375,12 +383,12 @@ const char *fmt_ident(const char *name, const char *email, if (!email) { email = ident_default_email(); - if (strict && default_email_is_bogus) { + if (strict && source_of_default_email == IDENT_SOURCE_GUESSED_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)) + if (strict && ident_use_config_only && + source_of_default_email != IDENT_SOURCE_CONFIG) die("user.useConfigOnly set but no mail given"); } @@ -412,10 +420,6 @@ const char *fmt_name(const char *name, const char *email) const char *git_author_info(int flag) { - if (getenv("GIT_AUTHOR_NAME")) - author_ident_explicitly_given |= IDENT_NAME_GIVEN; - if (getenv("GIT_AUTHOR_EMAIL")) - author_ident_explicitly_given |= IDENT_MAIL_GIVEN; return fmt_ident(getenv("GIT_AUTHOR_NAME"), getenv("GIT_AUTHOR_EMAIL"), getenv("GIT_AUTHOR_DATE"), @@ -424,33 +428,67 @@ const char *git_author_info(int flag) const char *git_committer_info(int flag) { - if (getenv("GIT_COMMITTER_NAME")) - committer_ident_explicitly_given |= IDENT_NAME_GIVEN; - if (getenv("GIT_COMMITTER_EMAIL")) - committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; return fmt_ident(getenv("GIT_COMMITTER_NAME"), getenv("GIT_COMMITTER_EMAIL"), getenv("GIT_COMMITTER_DATE"), flag); } -static int ident_is_sufficient(int user_ident_explicitly_given) +enum ident_person { + IDENT_PERSON_COMMITTER, + IDENT_PERSON_AUTHOR +}; + +static int ident_source_is_sufficient(enum ident_source source) +{ + switch (source) { + case IDENT_SOURCE_CONFIG: + case IDENT_SOURCE_ENVIRONMENT: + return 1; + default: + break; + } + + return 0; +} + +static int ident_is_sufficient(enum ident_person person) { + const char *str_name, *str_email; + int name, email; + + switch (person) { + case IDENT_PERSON_COMMITTER: + str_name = getenv("GIT_COMMITTER_NAME"); + str_email = getenv("GIT_COMMITTER_EMAIL"); + break; + case IDENT_PERSON_AUTHOR: + str_name = getenv("GIT_AUTHOR_NAME"); + str_email = getenv("GIT_AUTHOR_EMAIL"); + break; + default: + die("invalid parameter to ident_is_sufficient()"); + } + + name = !!str_name || ident_source_is_sufficient(source_of_default_name); + email = !!str_email || ident_source_is_sufficient(source_of_default_email); + #ifndef WINDOWS - return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); + (void)name; + return email; #else - return (user_ident_explicitly_given == IDENT_ALL_GIVEN); + return name && email; #endif } int committer_ident_sufficiently_given(void) { - return ident_is_sufficient(committer_ident_explicitly_given); + return ident_is_sufficient(IDENT_PERSON_COMMITTER); } int author_ident_sufficiently_given(void) { - return ident_is_sufficient(author_ident_explicitly_given); + return ident_is_sufficient(IDENT_PERSON_AUTHOR); } int git_ident_config(const char *var, const char *value, void *data) @@ -465,9 +503,7 @@ int git_ident_config(const char *var, const char *value, void *data) 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; + source_of_default_name = IDENT_SOURCE_CONFIG; return 0; } @@ -476,9 +512,7 @@ int git_ident_config(const char *var, const char *value, void *data) return config_error_nonbool(var); strbuf_reset(&git_default_email); 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; + source_of_default_email = IDENT_SOURCE_CONFIG; return 0; } -- 2.5.0 ^ permalink raw reply related [flat|nested] 11+ messages in thread
* Re: [PATCH v6 3/3] ident: cleanup wrt ident's source 2016-02-05 7:42 ` [PATCH v6 3/3] ident: cleanup wrt ident's source Dan Aloni @ 2016-02-05 19:05 ` Junio C Hamano 2016-02-05 19:24 ` Jeff King 0 siblings, 1 reply; 11+ messages in thread From: Junio C Hamano @ 2016-02-05 19:05 UTC (permalink / raw) To: Jeff King; +Cc: git, Dan Aloni Dan Aloni <alonid@gmail.com> writes: > This change condenses the variables that tells where we got the user's > ident into single enum, instead of a collection of booleans. > > In addtion, also have {committer,author}_ident_sufficiently_given > directly probe the environment and the afformentioned enum instead of > relying on git_{committer,author}_info to do so. > > Signed-off-by: Dan Aloni <alonid@gmail.com> > Helped-by: Jeff King <peff@peff.net> > Helped-by: Junio C Hamano <gitster@pobox.com> > --- > ident.c | 126 ++++++++++++++++++++++++++++++++++++++++------------------------ > 1 file changed, 80 insertions(+), 46 deletions(-) Peff what do you think? I am asking you because personally I do not find this particularly easier to read than the original, but since you stared at the code around here recently much longer than I did when doing the 1/3, I thought you may be a better judge than I am. > diff --git a/ident.c b/ident.c > index 9bd6ac69bfe8..9bb05912b59a 100644 > --- a/ident.c > +++ b/ident.c > @@ -10,17 +10,19 @@ > static struct strbuf git_default_name = STRBUF_INIT; > static struct strbuf git_default_email = STRBUF_INIT; > static struct strbuf git_default_date = STRBUF_INIT; > -static int default_email_is_bogus; > -static int default_name_is_bogus; > + > +enum ident_source { > + IDENT_SOURCE_UNKNOWN = 0, > + IDENT_SOURCE_CONFIG, > + IDENT_SOURCE_ENVIRONMENT, > + IDENT_SOURCE_GUESSED, > + IDENT_SOURCE_GUESSED_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; > +static enum ident_source source_of_default_email = IDENT_SOURCE_UNKNOWN; > +static enum ident_source source_of_default_name = IDENT_SOURCE_UNKNOWN; > > #ifdef NO_GECOS_IN_PWENT > #define get_gecos(ignored) "&" > @@ -28,7 +30,7 @@ static int ident_config_given; > #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) > #endif > > -static struct passwd *xgetpwuid_self(int *is_bogus) > +static struct passwd *xgetpwuid_self(enum ident_source *source) > { > struct passwd *pw; > > @@ -41,9 +43,13 @@ static struct passwd *xgetpwuid_self(int *is_bogus) > fallback.pw_gecos = "Unknown"; > #endif > pw = &fallback; > - if (is_bogus) > - *is_bogus = 1; > + if (source) > + *source = IDENT_SOURCE_GUESSED_BOGUS; > + } else { > + if (source) > + *source = IDENT_SOURCE_GUESSED; > } > + > return pw; > } > > @@ -120,26 +126,26 @@ static int canonical_name(const char *host, struct strbuf *out) > return status; > } > > -static void add_domainname(struct strbuf *out, int *is_bogus) > +static void add_domainname(struct strbuf *out, enum ident_source *source) > { > char buf[1024]; > > if (gethostname(buf, sizeof(buf))) { > warning("cannot get host name: %s", strerror(errno)); > strbuf_addstr(out, "(none)"); > - *is_bogus = 1; > + *source = IDENT_SOURCE_GUESSED_BOGUS; > return; > } > if (strchr(buf, '.')) > strbuf_addstr(out, buf); > else if (canonical_name(buf, out) < 0) { > strbuf_addf(out, "%s.(none)", buf); > - *is_bogus = 1; > + *source = IDENT_SOURCE_GUESSED_BOGUS; > } > } > > static void copy_email(const struct passwd *pw, struct strbuf *email, > - int *is_bogus) > + enum ident_source *source) > { > /* > * Make up a fake email address > @@ -150,13 +156,13 @@ static void copy_email(const struct passwd *pw, struct strbuf *email, > > if (!add_mailname_host(email)) > return; /* read from "/etc/mailname" (Debian) */ > - add_domainname(email, is_bogus); > + add_domainname(email, source); > } > > const char *ident_default_name(void) > { > if (!git_default_name.len) { > - copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); > + copy_gecos(xgetpwuid_self(&source_of_default_name), &git_default_name); > strbuf_trim(&git_default_name); > } > return git_default_name.buf; > @@ -169,11 +175,12 @@ const char *ident_default_email(void) > > if (email && email[0]) { > strbuf_addstr(&git_default_email, email); > - committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; > - author_ident_explicitly_given |= IDENT_MAIL_GIVEN; > - } else > - copy_email(xgetpwuid_self(&default_email_is_bogus), > - &git_default_email, &default_email_is_bogus); > + source_of_default_email = IDENT_SOURCE_ENVIRONMENT; > + } else { > + copy_email(xgetpwuid_self(&source_of_default_email), > + &git_default_email, &source_of_default_email); > + } > + > strbuf_trim(&git_default_email); > } > return git_default_email.buf; > @@ -353,12 +360,13 @@ const char *fmt_ident(const char *name, const char *email, > if (!name) { > name = ident_default_name(); > using_default = 1; > - if (strict && default_name_is_bogus) { > + if (strict && > + source_of_default_name == IDENT_SOURCE_GUESSED_BOGUS) { > fputs(env_hint, stderr); > die("unable to auto-detect name (got '%s')", name); > } > if (strict && ident_use_config_only && > - !(ident_config_given & IDENT_NAME_GIVEN)) > + source_of_default_name != IDENT_SOURCE_CONFIG) > die("user.useConfigOnly set but no name given"); > } > if (!*name) { > @@ -375,12 +383,12 @@ const char *fmt_ident(const char *name, const char *email, > > if (!email) { > email = ident_default_email(); > - if (strict && default_email_is_bogus) { > + if (strict && source_of_default_email == IDENT_SOURCE_GUESSED_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)) > + if (strict && ident_use_config_only && > + source_of_default_email != IDENT_SOURCE_CONFIG) > die("user.useConfigOnly set but no mail given"); > } > > @@ -412,10 +420,6 @@ const char *fmt_name(const char *name, const char *email) > > const char *git_author_info(int flag) > { > - if (getenv("GIT_AUTHOR_NAME")) > - author_ident_explicitly_given |= IDENT_NAME_GIVEN; > - if (getenv("GIT_AUTHOR_EMAIL")) > - author_ident_explicitly_given |= IDENT_MAIL_GIVEN; > return fmt_ident(getenv("GIT_AUTHOR_NAME"), > getenv("GIT_AUTHOR_EMAIL"), > getenv("GIT_AUTHOR_DATE"), > @@ -424,33 +428,67 @@ const char *git_author_info(int flag) > > const char *git_committer_info(int flag) > { > - if (getenv("GIT_COMMITTER_NAME")) > - committer_ident_explicitly_given |= IDENT_NAME_GIVEN; > - if (getenv("GIT_COMMITTER_EMAIL")) > - committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; > return fmt_ident(getenv("GIT_COMMITTER_NAME"), > getenv("GIT_COMMITTER_EMAIL"), > getenv("GIT_COMMITTER_DATE"), > flag); > } > > -static int ident_is_sufficient(int user_ident_explicitly_given) > +enum ident_person { > + IDENT_PERSON_COMMITTER, > + IDENT_PERSON_AUTHOR > +}; > + > +static int ident_source_is_sufficient(enum ident_source source) > +{ > + switch (source) { > + case IDENT_SOURCE_CONFIG: > + case IDENT_SOURCE_ENVIRONMENT: > + return 1; > + default: > + break; > + } > + > + return 0; > +} > + > +static int ident_is_sufficient(enum ident_person person) > { > + const char *str_name, *str_email; > + int name, email; Can we call this "name_ok" or something? Also it feels strange to do the all name-related things in this function even though we immediately discard. The code in this patch is still better than sprinkling more #ifdef to conditionally grab names only on platforms that require names, but it may be an indication of the design flaw of trying to squeeze this entire logic into this single function. Perhaps another little helper function can clean it up? e.g. ident_is_sufficient(committer_or_author?) { #ifdef WINDOWS if (!explicitly_given(committer_or_author?, NAME)) return 0; #endif return explicitly_given(committer_or_author?, EMAIL)) } or something? I dunno. > + > + switch (person) { > + case IDENT_PERSON_COMMITTER: > + str_name = getenv("GIT_COMMITTER_NAME"); > + str_email = getenv("GIT_COMMITTER_EMAIL"); > + break; > + case IDENT_PERSON_AUTHOR: > + str_name = getenv("GIT_AUTHOR_NAME"); > + str_email = getenv("GIT_AUTHOR_EMAIL"); > + break; > + default: > + die("invalid parameter to ident_is_sufficient()"); > + } > + > + name = !!str_name || ident_source_is_sufficient(source_of_default_name); > + email = !!str_email || ident_source_is_sufficient(source_of_default_email); > #ifndef WINDOWS > - return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); > + (void)name; > + return email; > #else > - return (user_ident_explicitly_given == IDENT_ALL_GIVEN); > + return name && email; > #endif > } ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v6 3/3] ident: cleanup wrt ident's source 2016-02-05 19:05 ` Junio C Hamano @ 2016-02-05 19:24 ` Jeff King 2016-02-05 21:03 ` Dan Aloni 0 siblings, 1 reply; 11+ messages in thread From: Jeff King @ 2016-02-05 19:24 UTC (permalink / raw) To: Junio C Hamano; +Cc: git, Dan Aloni On Fri, Feb 05, 2016 at 11:05:19AM -0800, Junio C Hamano wrote: > Dan Aloni <alonid@gmail.com> writes: > > > This change condenses the variables that tells where we got the user's > > ident into single enum, instead of a collection of booleans. > > > > In addtion, also have {committer,author}_ident_sufficiently_given > > directly probe the environment and the afformentioned enum instead of > > relying on git_{committer,author}_info to do so. > > > > Signed-off-by: Dan Aloni <alonid@gmail.com> > > Helped-by: Jeff King <peff@peff.net> > > Helped-by: Junio C Hamano <gitster@pobox.com> > > --- > > ident.c | 126 ++++++++++++++++++++++++++++++++++++++++------------------------ > > 1 file changed, 80 insertions(+), 46 deletions(-) > > Peff what do you think? I am asking you because personally I do not > find this particularly easier to read than the original, but since > you stared at the code around here recently much longer than I did > when doing the 1/3, I thought you may be a better judge than I am. I'm not sure it is really worth it unless we are going to expose this to the user, and let them say "I am OK with IDENT_SOURCE_GUESSED, but not IDENT_SOURCE_GUESSED_BOGUS" or similar. Without that, I think it is probably just making things a bit more brittle. -Peff ^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [PATCH v6 3/3] ident: cleanup wrt ident's source 2016-02-05 19:24 ` Jeff King @ 2016-02-05 21:03 ` Dan Aloni 0 siblings, 0 replies; 11+ messages in thread From: Dan Aloni @ 2016-02-05 21:03 UTC (permalink / raw) To: Jeff King; +Cc: Junio C Hamano, git On Fri, Feb 05, 2016 at 02:24:13PM -0500, Jeff King wrote: > On Fri, Feb 05, 2016 at 11:05:19AM -0800, Junio C Hamano wrote: > > > Dan Aloni <alonid@gmail.com> writes: > > > > > This change condenses the variables that tells where we got the user's > > > ident into single enum, instead of a collection of booleans. > > > > > > In addtion, also have {committer,author}_ident_sufficiently_given > > > directly probe the environment and the afformentioned enum instead of > > > relying on git_{committer,author}_info to do so. > > > > > > Signed-off-by: Dan Aloni <alonid@gmail.com> > > > Helped-by: Jeff King <peff@peff.net> > > > Helped-by: Junio C Hamano <gitster@pobox.com> > > > --- > > > ident.c | 126 ++++++++++++++++++++++++++++++++++++++++------------------------ > > > 1 file changed, 80 insertions(+), 46 deletions(-) > > > > Peff what do you think? I am asking you because personally I do not > > find this particularly easier to read than the original, but since > > you stared at the code around here recently much longer than I did > > when doing the 1/3, I thought you may be a better judge than I am. > > I'm not sure it is really worth it unless we are going to expose this to > the user, and let them say "I am OK with IDENT_SOURCE_GUESSED, but not > IDENT_SOURCE_GUESSED_BOGUS" or similar. > > Without that, I think it is probably just making things a bit more > brittle. Okay, will drop it now. -- Dan Aloni ^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2016-02-05 21:14 UTC | newest] Thread overview: 11+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2016-02-05 7:42 [PATCH v6] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni 2016-02-05 7:42 ` [PATCH v6 1/3] fmt_ident: refactor strictness checks Dan Aloni 2016-02-05 7:42 ` [PATCH v6 2/3] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed Dan Aloni 2016-02-05 19:18 ` Jeff King 2016-02-05 19:30 ` Eric Sunshine 2016-02-05 19:31 ` Junio C Hamano 2016-02-05 21:14 ` Dan Aloni 2016-02-05 7:42 ` [PATCH v6 3/3] ident: cleanup wrt ident's source Dan Aloni 2016-02-05 19:05 ` Junio C Hamano 2016-02-05 19:24 ` Jeff King 2016-02-05 21:03 ` Dan Aloni
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).