git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] ident: keep a flag for bogus default_email

From
Jeff King <peff@peff.net>
Date
Dec 10, 2015, 21:35 UTC
Message-ID
<20151210213535.GB8374@sigill.intra.peff.net>
In-Reply-To
<20151210213228.GB29055@sigill.intra.peff.net>

If we have to deduce the user's email address and can't come up with something plausible for the hostname, we simply write "(none)" or ".(none)" in the hostname.

Later, our strict-check is forced to use strstr to look for this magic string. This is probably not a problem in practice, but it's rather ugly. Let's keep an extra flag that tells us the email is bogus, and check that instead.

We could get away with simply setting the global in add_domainname(); it only gets called to write into git_default_email. However, let's make the code a little more obvious to future readers by actually passing a pointer to our "bogus" flag down the call-chain.

Signed-off-by: Jeff King <peff@peff.net>
---
I waffled on the "passing it around" thing. The patch is a lot smaller
if we just set the global directly, and I doubt these static helpers
will ever get reused for anything else. But the pattern does get reused
in the next patch, which sets the "email" and "name" flags separately
depending on context.

I was also tempted to have a single "default_ident_is_flaky", but that does get some corner cases wrong (e.g., you have a bogus auto-guessed name, an OK auto-guessed email, and set GIT_COMMITTER_NAME but not GIT_COMMITTER_EMAIL). I doubt those come up in practice, but it's not that hard to do the right thing.

 ident.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/ident.c b/ident.c
index d7c70e2..085cfbe 100644
--- a/ident.c
+++ b/ident.c
@@ -10,6 +10,7 @@
 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;
 
 #define IDENT_NAME_GIVEN 01
 #define IDENT_MAIL_GIVEN 02
@@ -82,7 +83,7 @@ static int add_mailname_host(struct strbuf *buf)
 	return 0;
 }
 
-static void add_domainname(struct strbuf *out)
+static void add_domainname(struct strbuf *out, int *is_bogus)
 {
 	char buf[1024];
 	struct hostent *he;
@@ -90,17 +91,21 @@ static void add_domainname(struct strbuf *out)
 	if (gethostname(buf, sizeof(buf))) {
 		warning("cannot get host name: %s", strerror(errno));
 		strbuf_addstr(out, "(none)");
+		*is_bogus = 1;
 		return;
 	}
 	if (strchr(buf, '.'))
 		strbuf_addstr(out, buf);
 	else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
 		strbuf_addstr(out, he->h_name);
-	else
+	else {
 		strbuf_addf(out, "%s.(none)", buf);
+		*is_bogus = 1;
+	}
 }
 
-static void copy_email(const struct passwd *pw, struct strbuf *email)
+static void copy_email(const struct passwd *pw, struct strbuf *email,
+		       int *is_bogus)
 {
 	/*
 	 * Make up a fake email address
@@ -111,7 +116,7 @@ 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);
+	add_domainname(email, is_bogus);
 }
 
 const char *ident_default_name(void)
@@ -133,7 +138,8 @@ const char *ident_default_email(void)
 			committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 			author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
 		} else
-			copy_email(xgetpwuid_self(), &git_default_email);
+			copy_email(xgetpwuid_self(), &git_default_email,
+				   &default_email_is_bogus);
 		strbuf_trim(&git_default_email);
 	}
 	return git_default_email.buf;
@@ -325,8 +331,7 @@ const char *fmt_ident(const char *name, const char *email,
 		name = pw->pw_name;
 	}
 
-	if (strict && email == git_default_email.buf &&
-	    strstr(email, "(none)")) {
+	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);
 	}
-- 
2.6.3.636.g1460207
Previous: Junio C HamanoNext: Jeff King
Message 13 of 20 in “git-clone fails when current user is not in /etc/passwd”
  1. Taylor Braun-JonesDec 2, 2015
  2. Taylor Braun-JonesDec 9, 2015
  3. Duy NguyenDec 9, 2015
  4. Duy NguyenDec 9, 2015
  5. Taylor Braun-JonesDec 9, 2015
  6. Taylor Braun-JonesDec 10, 2015
  7. Jeff KingDec 10, 2015
  8. Junio C HamanoDec 10, 2015
  9. Jeff KingDec 10, 2015
  10. 0/3 git-clone fails when current user is not in /etc/passwdJeff King, Dec 10, 2015
  11. 1/3 ident: make xgetpwuid_self() a static local helperJeff King, Dec 10, 2015
  12. Junio C HamanoDec 10, 2015
  13. 2/3 ident: keep a flag for bogus default_emailJeff King, Dec 10, 2015
  14. Jeff KingDec 10, 2015
  15. 3/3 ident: loosen getpwuid error in non-strict modeJeff King, Dec 10, 2015
  16. Jeff KingDec 14, 2015
  17. Junio C HamanoDec 10, 2015
  18. Taylor Braun-JonesDec 11, 2015
  19. Junio C HamanoDec 10, 2015
  20. Taylor Braun-JonesDec 10, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.