--author arg on commit only works if there is an email configured already
Hello everyone,
It seems the --author arg on the git commit command only works if an author email is configured already somewhere:
Sample that I would expect to work and that does not work:
``` $ docker run --rm -it golang /bin/bash -c 'cd $(mktemp -d); git init; touch test; git add test; git commit -m message --author "A U Thor <[email protected]>"' Initialized empty Git repository in /tmp/tmp.TiNqOZsw9C/.git/ *** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'root@aedfbe0df193.(none)') ```
When configuring any mail first, this works and uses the mail specified in the --author arg:
``` $ docker run --rm -it golang /bin/bash -c 'cd $(mktemp -d); git init; touch test; git add test; git config --local user.email "[email protected]"; git commit -m message --author "A U Thor <[email protected]>"' Initialized empty Git repository in /tmp/tmp.1drhE2Rgmh/.git/ [master (root-commit) b3dad37] message Author: A U Thor <[email protected]> 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test ```
The git version:
$ docker run --rm -it golang git --version git version 2.20
Quoted 2 lines · Alvaro Aleman
It seems the
--authorarg on thegit commitcommand only works if an author email is configured already somewhere:
The `--author' argument only sets the author, but git still need to fill in the committer.
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."
Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Alvaro Aleman
It seems the
--authorarg on thegit commitcommand only works if an author email is configured already somewhere:The `--author' argument only sets the author, but git still need to fill in the committer.
In other words, the --author option works just fine. You still need to tell Git what committer identity you want to use, and the easiest way to do so is with user.{name,email} configuration variables.
I wonder if it helps to give an extra line of message like the attached (untested) patch, though.
ident.c | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-)
ident.c +34 -17
@@ -345,18 +345,35 @@ int split_ident_line(struct ident_split *split, const char *line, int len)return 0 ; } -static const char *env_hint = -N_ ("\n" -"*** Please tell me who you are.\n" -"\n" -"Run\n" -"\n" -" git config --global user.email \"[email protected]\"\n" -" git config --global user.name \"Your Name\"\n" -"\n" -"to set your account\'s default identity.\n" -"Omit --global to set the identity only in this repository.\n" -"\n" ); + +static void ident_env_hint (enum want_ident whose_ident) +{ +static const char *env_hint = +N_ ("\n" +"*** Please tell me who you are.\n" +"\n" +"Run\n" +"\n" +" git config --global user.email \"[email protected]\"\n" +" git config --global user.name \"Your Name\"\n" +"\n" +"to set your account\'s default identity.\n" +"Omit --global to set the identity only in this repository.\n" +"\n" ); + +switch (whose_ident) { +case WANT_AUTHOR_IDENT : +fputs (_ ("Author identity unknown\n" ), stderr); +break ; +case WANT_COMMITTER_IDENT : +fputs (_ ("Committer identity unknown\n" ), stderr); +break ; +default : +break ; + } + +fputs (_ (env_hint), stderr); +}const char *fmt_ident (const char *name,const char *email,enum want_ident whose_ident,const char *date_str,int flag)
@@ -375,12 +392,12 @@ const char *fmt_ident(const char *name, const char *email,if (!email) {if (strict && ident_use_config_only && !(ident_config_given &IDENT_MAIL_GIVEN )) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("no email was given and auto-detection is disabled" )); } email =ident_default_email ();if (strict && default_email_is_bogus) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("unable to auto-detect email address (got '%s')" ), email); } }
@@ -397,13 +414,13 @@ const char *fmt_ident(const char *name, const char *email,if (!name) {if (strict && ident_use_config_only && !(ident_config_given &IDENT_NAME_GIVEN )) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("no name was given and auto-detection is disabled" )); } name =ident_default_name (); using_default =1 ;if (strict && default_name_is_bogus) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("unable to auto-detect name (got '%s')" ), name); } }
@@ -411,7 +428,7 @@ const char *fmt_ident(const char *name, const char *email,struct passwd *pw;if (strict) {if (using_default) -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("empty ident name (for <%s>) not allowed" ), email); } pw =xgetpwuid_self (NULL );
Thank you for the explanation. Yes, having the error explicitly mention t about which of author/committer this is would be helpful.
Quoted 114 lines · Junio C Hamano
Quoted 7 lines · Andreas Schwab
Quoted 2 lines · Alvaro Aleman
It seems the
--authorarg on thegit commitcommand only works if an author email is configured already somewhere:The `--author' argument only sets the author, but git still need to fill in the committer.
In other words, the --author option works just fine. You still need to tell Git what committer identity you want to use, and the easiest way to do so is with user.{name,email} configuration variables.
I wonder if it helps to give an extra line of message like the attached (untested) patch, though.
ident.c | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-)ident.c +34
-17@@ -345,18 +345,35 @@ int split_ident_line(struct ident_split *split, const char *line, int len)return 0 ; } -static const char *env_hint = -N_ ("\n" -"*** Please tell me who you are.\n" -"\n" -"Run\n" -"\n" -" git config --global user.email \"[email protected]\"\n" -" git config --global user.name \"Your Name\"\n" -"\n" -"to set your account\'s default identity.\n" -"Omit --global to set the identity only in this repository.\n" -"\n" ); + +static void ident_env_hint (enum want_ident whose_ident) +{ +static const char *env_hint = +N_ ("\n" +"*** Please tell me who you are.\n" +"\n" +"Run\n" +"\n" +" git config --global user.email \"[email protected]\"\n" +" git config --global user.name \"Your Name\"\n" +"\n" +"to set your account\'s default identity.\n" +"Omit --global to set the identity only in this repository.\n" +"\n" ); + +switch (whose_ident) { +case WANT_AUTHOR_IDENT : +fputs (_ ("Author identity unknown\n" ), stderr); +break ; +case WANT_COMMITTER_IDENT : +fputs (_ ("Committer identity unknown\n" ), stderr); +break ; +default : +break ; + } + +fputs (_ (env_hint), stderr); +}const char *fmt_ident (const char *name,const char *email,enum want_ident whose_ident,const char *date_str,int flag)@@ -375,12 +392,12 @@ const char *fmt_ident(const char *name, const char *email,if (!email) {if (strict && ident_use_config_only && !(ident_config_given &IDENT_MAIL_GIVEN )) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("no email was given and auto-detection is disabled" )); } email =ident_default_email ();if (strict && default_email_is_bogus) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("unable to auto-detect email address (got '%s')" ), email); } }@@ -397,13 +414,13 @@ const char *fmt_ident(const char *name, const char *email,if (!name) {if (strict && ident_use_config_only && !(ident_config_given &IDENT_NAME_GIVEN )) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("no name was given and auto-detection is disabled" )); } name =ident_default_name (); using_default =1 ;if (strict && default_name_is_bogus) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("unable to auto-detect name (got '%s')" ), name); } }@@ -411,7 +428,7 @@ const char *fmt_ident(const char *name, const char *email,struct passwd *pw;if (strict) {if (using_default) -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("empty ident name (for <%s>) not allowed" ), email); } pw =xgetpwuid_self (NULL );
PATCH 1/1 ident: say whose identity is missing when giving user.name hint
OK, so here is essentially the same patch but with a bit of cleaning up, with a test update and a proposed log message.
-- >8 --
When the author or the committer identity is missing when required, "git" errors out with a message that suggests to set these configuration variables at the per-user level as the easiest way forward. This message is given to a brand-new user, whose ~/.gitconfig hasn't been configured for user.name and user.email, who runs "git commit --author=...", too, but such a user may find it confusing ("why? I just gave you a name and e-mail").
State whose identity is missing as the reason why we are erroring out, when we give the hint, to help reduce the confusion.
--- ident.c | 48 ++++++++++++++++++++++------------- t/t7518-ident-corner-cases.sh | 13 +++++++++- 2 files changed, 43 insertions(+), 18 deletions(-)
ident.c +31 -17
@@ -345,18 +345,32 @@ int split_ident_line(struct ident_split *split, const char *line, int len)return 0 ; } -static const char *env_hint = -N_ ("\n" -"*** Please tell me who you are.\n" -"\n" -"Run\n" -"\n" -" git config --global user.email \"[email protected]\"\n" -" git config --global user.name \"Your Name\"\n" -"\n" -"to set your account\'s default identity.\n" -"Omit --global to set the identity only in this repository.\n" -"\n" ); + +static void ident_env_hint (enum want_ident whose_ident) +{ +switch (whose_ident) { +case WANT_AUTHOR_IDENT : +fputs (_ ("Author identity unknown\n" ), stderr); +break ; +case WANT_COMMITTER_IDENT : +fputs (_ ("Committer identity unknown\n" ), stderr); +break ; +default : +break ; + } + +fputs (_ ("\n" +"*** Please tell me who you are.\n" +"\n" +"Run\n" +"\n" +" git config --global user.email \"[email protected]\"\n" +" git config --global user.name \"Your Name\"\n" +"\n" +"to set your account\'s default identity.\n" +"Omit --global to set the identity only in this repository.\n" +"\n" ), stderr); +}const char *fmt_ident (const char *name,const char *email,enum want_ident whose_ident,const char *date_str,int flag)
@@ -375,12 +389,12 @@ const char *fmt_ident(const char *name, const char *email,if (!email) {if (strict && ident_use_config_only && !(ident_config_given &IDENT_MAIL_GIVEN )) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("no email was given and auto-detection is disabled" )); } email =ident_default_email ();if (strict && default_email_is_bogus) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("unable to auto-detect email address (got '%s')" ), email); } }
@@ -397,13 +411,13 @@ const char *fmt_ident(const char *name, const char *email,if (!name) {if (strict && ident_use_config_only && !(ident_config_given &IDENT_NAME_GIVEN )) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("no name was given and auto-detection is disabled" )); } name =ident_default_name (); using_default =1 ;if (strict && default_name_is_bogus) { -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("unable to auto-detect name (got '%s')" ), name); } }
@@ -411,7 +425,7 @@ const char *fmt_ident(const char *name, const char *email,struct passwd *pw;if (strict) {if (using_default) -fputs (_ (env_hint), stderr); +ident_env_hint (whose_ident);die (_ ("empty ident name (for <%s>) not allowed" ), email); } pw =xgetpwuid_self (NULL );
t/t7518-ident-corner-cases.sh +12 -1
@@ -29,7 +29,18 @@ test_expect_success 'empty configured name does not auto-detect' ' sane_unset GIT_AUTHOR_NAME && test_must_fail \ git -c user.name= commit --allow-empty -m foo 2>err && - test_i18ngrep "empty ident name" err + test_i18ngrep "empty ident name" err && + test_i18ngrep "Author identity unknown" err + ) +' + +test_expect_success 'empty configured name does not auto-detect for committer' ' + ( + sane_unset GIT_COMMITTER_NAME && + test_must_fail \ + git -c user.name= commit --allow-empty -m foo 2>err && + test_i18ngrep "empty ident name" err && + test_i18ngrep "Committer identity unknown" err ) '
Signature · 1 line
2.28.0-416-g2947c83ecf
Re: [PATCH] ident: say whose identity is missing when giving user.name hint
Quoted 10 lines · Junio C Hamano
When the author or the committer identity is missing when required, "git" errors out with a message that suggests to set these configuration variables at the per-user level as the easiest way forward. This message is given to a brand-new user, whose ~/.gitconfig hasn't been configured for user.name and user.email, who runs "git commit --author=...", too, but such a user may find it confusing ("why? I just gave you a name and e-mail").
State whose identity is missing as the reason why we are erroring out, when we give the hint, to help reduce the confusion.
I had trouble following the first paragraph due to the run-on nature of the second sentence. Perhaps the entire message could be rewritten something like this:
If user.name and user.email have not been configured and the user invokes:
git commit --author=...
without without specifying --committer=, then Git errors out with a message asking the user to configure user.name and user.email but doesn't tell the user which attribution was missing. This can be confusing for a user new to Git who isn't aware of the distinction between user, author, and committer. Give such users a bit more help by extending the error message to also say which attribution is expected.
Re: [PATCH] ident: say whose identity is missing when giving user.name hint
Quoted 5 lines · Eric Sunshine
If
user.nameanduser.emailhave not been configured and the user invokes:git commit --author=...
Quoted 7 lines
without without specifying
--committer=, then Git errors out with a message asking the user to configureuser.nameanduser.emailbut doesn't tell the user which attribution was missing. This can be confusing for a user new to Git who isn't aware of the distinction between user, author, and committer. Give such users a bit more help by extending the error message to also say which attribution is expected.
Much easier to read. Will steal.
Re: [PATCH] ident: say whose identity is missing when giving user.name hint
One nit though: There is no --committer flag for git commit, unless this has changed after the v2.28.0 release:
``` $ g commit -m message --committer [email protected] error: unknown option `committer' $ git --version git version 2.28.0 ```
Quoted 18 lines · Junio C Hamano
Quoted 5 lines · Eric Sunshine
If
user.nameanduser.emailhave not been configured and the user invokes:git commit --author=...
Quoted 7 lines
without without specifying
--committer=, then Git errors out with a message asking the user to configureuser.nameanduser.emailbut doesn't tell the user which attribution was missing. This can be confusing for a user new to Git who isn't aware of the distinction between user, author, and committer. Give such users a bit more help by extending the error message to also say which attribution is expected.Much easier to read. Will steal.
Re: [PATCH] ident: say whose identity is missing when giving user.name hint
Quoted 1 line · Alvaro Aleman
One nit though: There is no
--committerflag forgit commit,
Indeed, that --committer= was a last-second edit (without checking docs). How about this?
If user.name and user.email have not been configured and the user invokes:
git commit --author=...
without specifying the committer, then Git errors out with a message asking the user to configure user.name and user.email but doesn't tell the user which attribution was missing. This can be confusing for a user new to Git who isn't aware of the distinction between user, author, and committer. Give such users a bit more help by extending the error message to also say which attribution is expected.
Re: [PATCH] ident: say whose identity is missing when giving user.name hint
Quoted 5 lines · Eric Sunshine
Quoted 1 line · Alvaro Aleman
One nit though: There is no
--committerflag forgit commit,Indeed, that
--committer=was a last-second edit (without checking docs).
Yeah, I forgot that we deliberately omit the command line option for the committer info.
Quoted 7 lines
How about this?
If
user.nameanduser.emailhave not been configured and the user invokes:git commit --author=...
Quoted 7 lines
without specifying the committer, then Git errors out with a message asking the user to configure
user.nameanduser.emailbut doesn't tell the user which attribution was missing. This can be confusing for a user new to Git who isn't aware of the distinction between user, author, and committer. Give such users a bit more help by extending the error message to also say which attribution is expected.
OK.