10
10
static struct strbuf git_default_name = STRBUF_INIT ;
11
11
static struct strbuf git_default_email = STRBUF_INIT ;
12
12
static struct strbuf git_default_date = STRBUF_INIT ;
13
+ static int default_email_is_bogus ;
14
+ static int default_name_is_bogus ;
13
15
14
16
#define IDENT_NAME_GIVEN 01
15
17
#define IDENT_MAIL_GIVEN 02
@@ -23,6 +25,25 @@ static int author_ident_explicitly_given;
23
25
#define get_gecos (struct_passwd ) ((struct_passwd)->pw_gecos)
24
26
#endif
25
27
28
+ static struct passwd * xgetpwuid_self (int * is_bogus )
29
+ {
30
+ struct passwd * pw ;
31
+
32
+ errno = 0 ;
33
+ pw = getpwuid (getuid ());
34
+ if (!pw ) {
35
+ static struct passwd fallback ;
36
+ fallback .pw_name = "unknown" ;
37
+ #ifndef NO_GECOS_IN_PWENT
38
+ fallback .pw_gecos = "Unknown" ;
39
+ #endif
40
+ pw = & fallback ;
41
+ if (is_bogus )
42
+ * is_bogus = 1 ;
43
+ }
44
+ return pw ;
45
+ }
46
+
26
47
static void copy_gecos (const struct passwd * w , struct strbuf * name )
27
48
{
28
49
char * src ;
@@ -96,22 +117,26 @@ static int canonical_name(const char *host, struct strbuf *out)
96
117
return status ;
97
118
}
98
119
99
- static void add_domainname (struct strbuf * out )
120
+ static void add_domainname (struct strbuf * out , int * is_bogus )
100
121
{
101
122
char buf [1024 ];
102
123
103
124
if (gethostname (buf , sizeof (buf ))) {
104
125
warning ("cannot get host name: %s" , strerror (errno ));
105
126
strbuf_addstr (out , "(none)" );
127
+ * is_bogus = 1 ;
106
128
return ;
107
129
}
108
130
if (strchr (buf , '.' ))
109
131
strbuf_addstr (out , buf );
110
- else if (canonical_name (buf , out ) < 0 )
132
+ else if (canonical_name (buf , out ) < 0 ) {
111
133
strbuf_addf (out , "%s.(none)" , buf );
134
+ * is_bogus = 1 ;
135
+ }
112
136
}
113
137
114
- static void copy_email (const struct passwd * pw , struct strbuf * email )
138
+ static void copy_email (const struct passwd * pw , struct strbuf * email ,
139
+ int * is_bogus )
115
140
{
116
141
/*
117
142
* Make up a fake email address
@@ -122,13 +147,13 @@ static void copy_email(const struct passwd *pw, struct strbuf *email)
122
147
123
148
if (!add_mailname_host (email ))
124
149
return ; /* read from "/etc/mailname" (Debian) */
125
- add_domainname (email );
150
+ add_domainname (email , is_bogus );
126
151
}
127
152
128
153
const char * ident_default_name (void )
129
154
{
130
155
if (!git_default_name .len ) {
131
- copy_gecos (xgetpwuid_self (), & git_default_name );
156
+ copy_gecos (xgetpwuid_self (& default_name_is_bogus ), & git_default_name );
132
157
strbuf_trim (& git_default_name );
133
158
}
134
159
return git_default_name .buf ;
@@ -144,7 +169,8 @@ const char *ident_default_email(void)
144
169
committer_ident_explicitly_given |= IDENT_MAIL_GIVEN ;
145
170
author_ident_explicitly_given |= IDENT_MAIL_GIVEN ;
146
171
} else
147
- copy_email (xgetpwuid_self (), & git_default_email );
172
+ copy_email (xgetpwuid_self (& default_email_is_bogus ),
173
+ & git_default_email , & default_email_is_bogus );
148
174
strbuf_trim (& git_default_email );
149
175
}
150
176
return git_default_email .buf ;
@@ -332,12 +358,17 @@ const char *fmt_ident(const char *name, const char *email,
332
358
fputs (env_hint , stderr );
333
359
die ("empty ident name (for <%s>) not allowed" , email );
334
360
}
335
- pw = xgetpwuid_self ();
361
+ pw = xgetpwuid_self (NULL );
336
362
name = pw -> pw_name ;
337
363
}
338
364
339
- if (strict && email == git_default_email .buf &&
340
- strstr (email , "(none)" )) {
365
+ if (want_name && strict &&
366
+ name == git_default_name .buf && default_name_is_bogus ) {
367
+ fputs (env_hint , stderr );
368
+ die ("unable to auto-detect name (got '%s')" , name );
369
+ }
370
+
371
+ if (strict && email == git_default_email .buf && default_email_is_bogus ) {
341
372
fputs (env_hint , stderr );
342
373
die ("unable to auto-detect email address (got '%s')" , email );
343
374
}
0 commit comments