{"thread":{"id":"41354","subject":"[PATCH v9 1/2] fmt_ident: refactor strictness checks","startedAt":"2016-02-06T06:23:34Z","lastAt":"2016-02-08T19:07:24Z","messageCount":4,"participants":["Dan Aloni","Junio C Hamano"],"isPatch":true,"patchVersion":9,"patchTotal":2},"messages":[{"id":"277645","messageId":"1454739816-24007-1-git-send-email-alonid@gmail.com","threadId":"41354","inReplyTo":null,"subject":"[PATCH v9] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed","fromName":"Dan Aloni","fromEmail":"alonid@gmail.com","sentAt":"2016-02-06T06:23:34Z","receivedAt":"2016-02-06T06:23:34Z","isPatch":true,"sender":{"key":"alonid@gmail.com","avatar":"https://avatars.githubusercontent.com/u/321273?v=4"},"body":"\nChanges between v8 -> v9:\n\n* Rebased and tested on v2.7.1.\n* Made a small correction suggested by Junio in the documentation for\n  the new option: s/upon/before\n\nv8: http://article.gmane.org/gmane.comp.version-control.git/285646\n"},{"id":"277644","messageId":"1454739816-24007-2-git-send-email-alonid@gmail.com","threadId":"41354","inReplyTo":"1454739816-24007-1-git-send-email-alonid@gmail.com","subject":"[PATCH v9 1/2] fmt_ident: refactor strictness checks","fromName":"Dan Aloni","fromEmail":"alonid@gmail.com","sentAt":"2016-02-06T06:23:35Z","receivedAt":"2016-02-06T06:23:35Z","isPatch":true,"sender":{"key":"alonid@gmail.com","avatar":"https://avatars.githubusercontent.com/u/321273?v=4"},"body":"From: Jeff King <peff@peff.net>\n\nThis function has evolved quite a bit over time, and as a\nresult, the logic for \"is this an OK ident\" has been\nsprinkled throughout. This ends up with a lot of redundant\nconditionals, like checking want_name repeatedly. Worse,\nwe want to know in many cases whether we are using the\n\"default\" ident, and we do so by comparing directly to the\nglobal strbuf, which violates the abstraction of the\nident_default_* functions.\n\nLet's reorganize the function into a hierarchy of\nconditionals to handle similar cases together. The only\ncase that doesn't just work naturally for this is that of an\nempty name, where our advice is different based on whether\nwe came from ident_default_name() or not. We can use a\nsimple flag to cover this case.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n ident.c | 46 ++++++++++++++++++++++++----------------------\n 1 file changed, 24 insertions(+), 22 deletions(-)\n\ndiff --git a/ident.c b/ident.c\nindex 3da555634290..f3a431f738cc 100644\n--- a/ident.c\n+++ b/ident.c\n@@ -345,32 +345,34 @@ const char *fmt_ident(const char *name, const char *email,\n \tint want_date = !(flag & IDENT_NO_DATE);\n \tint want_name = !(flag & IDENT_NO_NAME);\n \n-\tif (want_name && !name)\n-\t\tname = ident_default_name();\n-\tif (!email)\n-\t\temail = ident_default_email();\n-\n-\tif (want_name && !*name) {\n-\t\tstruct passwd *pw;\n-\n-\t\tif (strict) {\n-\t\t\tif (name == git_default_name.buf)\n+\tif (want_name) {\n+\t\tint using_default = 0;\n+\t\tif (!name) {\n+\t\t\tname = ident_default_name();\n+\t\t\tusing_default = 1;\n+\t\t\tif (strict && default_name_is_bogus) {\n \t\t\t\tfputs(env_hint, stderr);\n-\t\t\tdie(\"empty ident name (for <%s>) not allowed\", email);\n+\t\t\t\tdie(\"unable to auto-detect name (got '%s')\", name);\n+\t\t\t}\n+\t\t}\n+\t\tif (!*name) {\n+\t\t\tstruct passwd *pw;\n+\t\t\tif (strict) {\n+\t\t\t\tif (using_default)\n+\t\t\t\t\tfputs(env_hint, stderr);\n+\t\t\t\tdie(\"empty ident name (for <%s>) not allowed\", email);\n+\t\t\t}\n+\t\t\tpw = xgetpwuid_self(NULL);\n+\t\t\tname = pw->pw_name;\n \t\t}\n-\t\tpw = xgetpwuid_self(NULL);\n-\t\tname = pw->pw_name;\n-\t}\n-\n-\tif (want_name && strict &&\n-\t    name == git_default_name.buf && default_name_is_bogus) {\n-\t\tfputs(env_hint, stderr);\n-\t\tdie(\"unable to auto-detect name (got '%s')\", name);\n \t}\n \n-\tif (strict && email == git_default_email.buf && default_email_is_bogus) {\n-\t\tfputs(env_hint, stderr);\n-\t\tdie(\"unable to auto-detect email address (got '%s')\", email);\n+\tif (!email) {\n+\t\temail = ident_default_email();\n+\t\tif (strict && default_email_is_bogus) {\n+\t\t\tfputs(env_hint, stderr);\n+\t\t\tdie(\"unable to auto-detect email address (got '%s')\", email);\n+\t\t}\n \t}\n \n \tstrbuf_reset(&ident);\n-- \n2.5.0\n"},{"id":"277646","messageId":"1454739816-24007-3-git-send-email-alonid@gmail.com","threadId":"41354","inReplyTo":"1454739816-24007-1-git-send-email-alonid@gmail.com","subject":"[PATCH v9 2/2] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed","fromName":"Dan Aloni","fromEmail":"alonid@gmail.com","sentAt":"2016-02-06T06:23:36Z","receivedAt":"2016-02-06T06:23:36Z","isPatch":true,"sender":{"key":"alonid@gmail.com","avatar":"https://avatars.githubusercontent.com/u/321273?v=4"},"body":"It used to be that:\n\n   git config --global user.email \"(none)\"\n\nwas a viable way for people to force themselves to set user.email in\neach repository.  This was helpful for people with more than one\nemail address, targeting different email addresses for different\nclones, as it barred git from creating a commit unless the user.email\nconfig was set in the per-repo config to the correct email address.\n\nA recent change, 19ce497c (ident: keep a flag for bogus\ndefault_email, 2015-12-10), however, declared that an explicitly\nconfigured user.email is not bogus, no matter what its value is, so\nthis hack no longer works.\n\nProvide the same functionality by adding a new configuration\nvariable user.useConfigOnly; when this variable is set, the\nuser must explicitly set user.email configuration.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Jeff King <peff@peff.net>\nSigned-off-by: Dan Aloni <alonid@gmail.com>\n---\n Documentation/config.txt  | 10 ++++++++++\n ident.c                   | 16 ++++++++++++++++\n t/t7517-per-repo-email.sh | 39 +++++++++++++++++++++++++++++++++++++++\n 3 files changed, 65 insertions(+)\n create mode 100755 t/t7517-per-repo-email.sh\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 02bcde6bb596..cfb7d0e7652d 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -2821,6 +2821,16 @@ user.name::\n \tCan be overridden by the 'GIT_AUTHOR_NAME' and 'GIT_COMMITTER_NAME'\n \tenvironment variables.  See linkgit:git-commit-tree[1].\n \n+user.useConfigOnly::\n+\tInstruct Git to avoid trying to guess defaults for 'user.email'\n+\tand 'user.name', and instead retrieve the values only from the\n+\tconfiguration. For example, if you have multiple email addresses\n+\tand would like to use a different one for each repository, then\n+\twith this configuration option set to `true` in the global config\n+\talong with a name, Git will prompt you to set up an email before\n+\tmaking new commits in a newly cloned repository.\n+\tDefaults to `false`.\n+\n user.signingKey::\n \tIf linkgit:git-tag[1] or linkgit:git-commit[1] is not selecting the\n \tkey you want it to automatically when creating a signed tag or\ndiff --git a/ident.c b/ident.c\nindex f3a431f738cc..6e125821f056 100644\n--- a/ident.c\n+++ b/ident.c\n@@ -13,11 +13,14 @@ static struct strbuf git_default_date = STRBUF_INIT;\n static int default_email_is_bogus;\n static int default_name_is_bogus;\n \n+static int ident_use_config_only;\n+\n #define IDENT_NAME_GIVEN 01\n #define IDENT_MAIL_GIVEN 02\n #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)\n static int committer_ident_explicitly_given;\n static int author_ident_explicitly_given;\n+static int ident_config_given;\n \n #ifdef NO_GECOS_IN_PWENT\n #define get_gecos(ignored) \"&\"\n@@ -354,6 +357,9 @@ const char *fmt_ident(const char *name, const char *email,\n \t\t\t\tfputs(env_hint, stderr);\n \t\t\t\tdie(\"unable to auto-detect name (got '%s')\", name);\n \t\t\t}\n+\t\t\tif (strict && ident_use_config_only\n+\t\t\t    && !(ident_config_given & IDENT_NAME_GIVEN))\n+\t\t\t\tdie(\"user.useConfigOnly set but no name given\");\n \t\t}\n \t\tif (!*name) {\n \t\t\tstruct passwd *pw;\n@@ -373,6 +379,9 @@ const char *fmt_ident(const char *name, const char *email,\n \t\t\tfputs(env_hint, stderr);\n \t\t\tdie(\"unable to auto-detect email address (got '%s')\", email);\n \t\t}\n+\t\tif (strict && ident_use_config_only\n+\t\t    && !(ident_config_given & IDENT_MAIL_GIVEN))\n+\t\t\tdie(\"user.useConfigOnly set but no mail given\");\n \t}\n \n \tstrbuf_reset(&ident);\n@@ -446,6 +455,11 @@ int author_ident_sufficiently_given(void)\n \n int git_ident_config(const char *var, const char *value, void *data)\n {\n+\tif (!strcmp(var, \"user.useconfigonly\")) {\n+\t\tident_use_config_only = git_config_bool(var, value);\n+\t\treturn 0;\n+\t}\n+\n \tif (!strcmp(var, \"user.name\")) {\n \t\tif (!value)\n \t\t\treturn config_error_nonbool(var);\n@@ -453,6 +467,7 @@ int git_ident_config(const char *var, const char *value, void *data)\n \t\tstrbuf_addstr(&git_default_name, value);\n \t\tcommitter_ident_explicitly_given |= IDENT_NAME_GIVEN;\n \t\tauthor_ident_explicitly_given |= IDENT_NAME_GIVEN;\n+\t\tident_config_given |= IDENT_NAME_GIVEN;\n \t\treturn 0;\n \t}\n \n@@ -463,6 +478,7 @@ int git_ident_config(const char *var, const char *value, void *data)\n \t\tstrbuf_addstr(&git_default_email, value);\n \t\tcommitter_ident_explicitly_given |= IDENT_MAIL_GIVEN;\n \t\tauthor_ident_explicitly_given |= IDENT_MAIL_GIVEN;\n+\t\tident_config_given |= IDENT_MAIL_GIVEN;\n \t\treturn 0;\n \t}\n \ndiff --git a/t/t7517-per-repo-email.sh b/t/t7517-per-repo-email.sh\nnew file mode 100755\nindex 000000000000..337e6e30c35f\n--- /dev/null\n+++ b/t/t7517-per-repo-email.sh\n@@ -0,0 +1,39 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2016 Dan Aloni\n+# Copyright (c) 2016 Jeff King\n+#\n+\n+test_description='per-repo forced setting of email address'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup a likely user.useConfigOnly use case' '\n+\t# we want to make sure a reflog is written, since that needs\n+\t# a non-strict ident. So be sure we have an actual commit.\n+\ttest_commit foo &&\n+\n+\tsane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&\n+\tsane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&\n+\tgit config user.name \"test\" &&\n+\tgit config --global user.useConfigOnly true\n+'\n+\n+test_expect_success 'fails committing if clone email is not set' '\n+\ttest_must_fail git commit --allow-empty -m msg\n+'\n+\n+test_expect_success 'fails committing if clone email is not set, but EMAIL set' '\n+\ttest_must_fail env EMAIL=test@fail.com git commit --allow-empty -m msg\n+'\n+\n+test_expect_success 'succeeds committing if clone email is set' '\n+\ttest_config user.email \"test@ok.com\" &&\n+\tgit commit --allow-empty -m msg\n+'\n+\n+test_expect_success 'succeeds cloning if global email is not set' '\n+\tgit clone . clone\n+'\n+\n+test_done\n-- \n2.5.0\n"},{"id":"277753","messageId":"xmqqvb5z81hv.fsf@gitster.mtv.corp.google.com","threadId":"41354","inReplyTo":"1454739816-24007-1-git-send-email-alonid@gmail.com","subject":"Re: [PATCH v9] ident: add user.useConfigOnly boolean for when ident shouldn't be guessed","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-02-08T19:07:24Z","receivedAt":"2016-02-08T19:07:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.\n"}]}