ident: loosen getpwuid error in non-strict mode
[git/git.git] / ident.c
CommitLineData
6aa33f40
LT
1/*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8#include "cache.h"
9
8587ead7
JK
10static struct strbuf git_default_name = STRBUF_INIT;
11static struct strbuf git_default_email = STRBUF_INIT;
c33ddc2e 12static struct strbuf git_default_date = STRBUF_INIT;
19ce497c 13static int default_email_is_bogus;
92bcbb9b 14static int default_name_is_bogus;
45280230
JK
15
16#define IDENT_NAME_GIVEN 01
17#define IDENT_MAIL_GIVEN 02
18#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
d6991cee
JK
19static int committer_ident_explicitly_given;
20static int author_ident_explicitly_given;
6aa33f40 21
590e081d
RG
22#ifdef NO_GECOS_IN_PWENT
23#define get_gecos(ignored) "&"
24#else
25#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
26#endif
27
92bcbb9b 28static struct passwd *xgetpwuid_self(int *is_bogus)
e850194c
JK
29{
30 struct passwd *pw;
31
32 errno = 0;
33 pw = getpwuid(getuid());
92bcbb9b
JK
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 }
e850194c
JK
44 return pw;
45}
46
8587ead7 47static void copy_gecos(const struct passwd *w, struct strbuf *name)
e9bacb4f 48{
8587ead7 49 char *src;
e9bacb4f
JH
50
51 /* Traditionally GECOS field had office phone numbers etc, separated
52 * with commas. Also & stands for capitalized form of the login name.
53 */
54
8587ead7 55 for (src = get_gecos(w); *src && *src != ','; src++) {
e9bacb4f 56 int ch = *src;
8587ead7
JK
57 if (ch != '&')
58 strbuf_addch(name, ch);
59 else {
e9bacb4f 60 /* Sorry, Mr. McDonald... */
8587ead7
JK
61 strbuf_addch(name, toupper(*w->pw_name));
62 strbuf_addstr(name, w->pw_name + 1);
e9bacb4f
JH
63 }
64 }
e9bacb4f
JH
65}
66
8587ead7 67static int add_mailname_host(struct strbuf *buf)
8a55caa8
JN
68{
69 FILE *mailname;
dc342a25 70 struct strbuf mailnamebuf = STRBUF_INIT;
8a55caa8
JN
71
72 mailname = fopen("/etc/mailname", "r");
73 if (!mailname) {
74 if (errno != ENOENT)
75 warning("cannot open /etc/mailname: %s",
76 strerror(errno));
77 return -1;
78 }
dc342a25 79 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
8a55caa8
JN
80 if (ferror(mailname))
81 warning("cannot read /etc/mailname: %s",
82 strerror(errno));
dc342a25 83 strbuf_release(&mailnamebuf);
8a55caa8
JN
84 fclose(mailname);
85 return -1;
86 }
87 /* success! */
dc342a25
JN
88 strbuf_addbuf(buf, &mailnamebuf);
89 strbuf_release(&mailnamebuf);
8a55caa8
JN
90 fclose(mailname);
91 return 0;
92}
93
19ce497c 94static void add_domainname(struct strbuf *out, int *is_bogus)
8a55caa8 95{
8587ead7 96 char buf[1024];
8a55caa8 97 struct hostent *he;
8a55caa8 98
8587ead7 99 if (gethostname(buf, sizeof(buf))) {
8a55caa8 100 warning("cannot get host name: %s", strerror(errno));
8587ead7 101 strbuf_addstr(out, "(none)");
19ce497c 102 *is_bogus = 1;
8a55caa8
JN
103 return;
104 }
8587ead7 105 if (strchr(buf, '.'))
f8254d32
JK
106 strbuf_addstr(out, buf);
107 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
108 strbuf_addstr(out, he->h_name);
19ce497c 109 else {
f8254d32 110 strbuf_addf(out, "%s.(none)", buf);
19ce497c
JK
111 *is_bogus = 1;
112 }
8a55caa8
JN
113}
114
19ce497c
JK
115static void copy_email(const struct passwd *pw, struct strbuf *email,
116 int *is_bogus)
6aa33f40 117{
01754769
JH
118 /*
119 * Make up a fake email address
120 * (name + '@' + hostname [+ '.' + domainname])
121 */
8587ead7
JK
122 strbuf_addstr(email, pw->pw_name);
123 strbuf_addch(email, '@');
124
125 if (!add_mailname_host(email))
8a55caa8 126 return; /* read from "/etc/mailname" (Debian) */
19ce497c 127 add_domainname(email, is_bogus);
01754769
JH
128}
129
9830534e 130const char *ident_default_name(void)
01754769 131{
be641abd 132 if (!git_default_name.len) {
92bcbb9b 133 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
be641abd 134 strbuf_trim(&git_default_name);
01754769 135 }
8587ead7 136 return git_default_name.buf;
bcb2b004 137}
01754769 138
bcb2b004
JK
139const char *ident_default_email(void)
140{
8587ead7 141 if (!git_default_email.len) {
46f74f00
MK
142 const char *email = getenv("EMAIL");
143
99178c83 144 if (email && email[0]) {
8587ead7 145 strbuf_addstr(&git_default_email, email);
d6991cee
JK
146 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
147 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
2f705875 148 } else
92bcbb9b
JK
149 copy_email(xgetpwuid_self(&default_email_is_bogus),
150 &git_default_email, &default_email_is_bogus);
be641abd 151 strbuf_trim(&git_default_email);
01754769 152 }
8587ead7 153 return git_default_email.buf;
6aa33f40
LT
154}
155
dad148c3 156static const char *ident_default_date(void)
6aa33f40 157{
c33ddc2e
JK
158 if (!git_default_date.len)
159 datestamp(&git_default_date);
160 return git_default_date.buf;
6aa33f40
LT
161}
162
163static int crud(unsigned char c)
164{
f64c81d4
AR
165 return c <= 32 ||
166 c == '.' ||
167 c == ',' ||
168 c == ':' ||
169 c == ';' ||
170 c == '<' ||
171 c == '>' ||
172 c == '"' ||
d404bf02 173 c == '\\' ||
f64c81d4 174 c == '\'';
6aa33f40
LT
175}
176
177/*
178 * Copy over a string to the destination, but avoid special
179 * characters ('\n', '<' and '>') and remove crud at the end
180 */
c96f0c8d 181static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 182{
b0732115 183 size_t i, len;
6aa33f40
LT
184 unsigned char c;
185
186 /* Remove crud from the beginning.. */
187 while ((c = *src) != 0) {
188 if (!crud(c))
189 break;
190 src++;
191 }
192
193 /* Remove crud from the end.. */
194 len = strlen(src);
195 while (len > 0) {
196 c = src[len-1];
197 if (!crud(c))
198 break;
199 --len;
200 }
201
202 /*
203 * Copy the rest to the buffer, but avoid the special
82f9d58a 204 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
205 * an identification line. We can only remove crud, never add it,
206 * so 'len' is our maximum.
6aa33f40 207 */
c96f0c8d 208 strbuf_grow(sb, len);
6aa33f40
LT
209 for (i = 0; i < len; i++) {
210 c = *src++;
211 switch (c) {
212 case '\n': case '<': case '>':
213 continue;
214 }
c96f0c8d 215 sb->buf[sb->len++] = c;
6aa33f40 216 }
c96f0c8d 217 sb->buf[sb->len] = '\0';
6aa33f40
LT
218}
219
4b340cfa
JH
220/*
221 * Reverse of fmt_ident(); given an ident line, split the fields
222 * to allow the caller to parse it.
223 * Signal a success by returning 0, but date/tz fields of the result
224 * can still be NULL if the input line only has the name/email part
225 * (e.g. reading from a reflog entry).
226 */
227int split_ident_line(struct ident_split *split, const char *line, int len)
228{
229 const char *cp;
230 size_t span;
231 int status = -1;
232
233 memset(split, 0, sizeof(*split));
234
235 split->name_begin = line;
236 for (cp = line; *cp && cp < line + len; cp++)
237 if (*cp == '<') {
238 split->mail_begin = cp + 1;
239 break;
240 }
241 if (!split->mail_begin)
242 return status;
243
d9955fd6 244 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
245 if (!isspace(*cp)) {
246 split->name_end = cp + 1;
247 break;
248 }
e27ddb64
JH
249 if (!split->name_end) {
250 /* no human readable name */
251 split->name_end = split->name_begin;
252 }
4b340cfa
JH
253
254 for (cp = split->mail_begin; cp < line + len; cp++)
255 if (*cp == '>') {
256 split->mail_end = cp;
257 break;
258 }
259 if (!split->mail_end)
260 return status;
261
03818a4a
JK
262 /*
263 * Look from the end-of-line to find the trailing ">" of the mail
264 * address, even though we should already know it as split->mail_end.
265 * This can help in cases of broken idents with an extra ">" somewhere
266 * in the email address. Note that we are assuming the timestamp will
267 * never have a ">" in it.
268 *
269 * Note that we will always find some ">" before going off the front of
270 * the string, because will always hit the split->mail_end closing
271 * bracket.
272 */
273 for (cp = line + len - 1; *cp != '>'; cp--)
274 ;
275
276 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
4b340cfa
JH
277 ;
278 if (line + len <= cp)
279 goto person_only;
280 split->date_begin = cp;
281 span = strspn(cp, "0123456789");
282 if (!span)
283 goto person_only;
284 split->date_end = split->date_begin + span;
285 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
286 ;
287 if (line + len <= cp || (*cp != '+' && *cp != '-'))
288 goto person_only;
289 split->tz_begin = cp;
290 span = strspn(cp + 1, "0123456789");
291 if (!span)
292 goto person_only;
293 split->tz_end = split->tz_begin + 1 + span;
294 return 0;
295
296person_only:
297 split->date_begin = NULL;
298 split->date_end = NULL;
299 split->tz_begin = NULL;
300 split->tz_end = NULL;
301 return 0;
302}
303
749be728 304static const char *env_hint =
d5cc2de9 305"\n"
6c293d40 306"*** Please tell me who you are.\n"
d5cc2de9
HWN
307"\n"
308"Run\n"
309"\n"
8e7425da 310" git config --global user.email \"you@example.com\"\n"
180787c4 311" git config --global user.name \"Your Name\"\n"
d5cc2de9 312"\n"
180787c4
SP
313"to set your account\'s default identity.\n"
314"Omit --global to set the identity only in this repository.\n"
d5cc2de9 315"\n";
749be728 316
774751a8
JH
317const char *fmt_ident(const char *name, const char *email,
318 const char *date_str, int flag)
6aa33f40 319{
c96f0c8d 320 static struct strbuf ident = STRBUF_INIT;
f9bc573f 321 int strict = (flag & IDENT_STRICT);
359b27ad 322 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 323 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 324
c15e1987 325 if (want_name && !name)
bcb2b004
JK
326 name = ident_default_name();
327 if (!email)
328 email = ident_default_email();
dfdd309e 329
c15e1987 330 if (want_name && !*name) {
cb280e10
JH
331 struct passwd *pw;
332
f9bc573f 333 if (strict) {
8587ead7 334 if (name == git_default_name.buf)
b9f0ac17 335 fputs(env_hint, stderr);
b00f6cfc 336 die("empty ident name (for <%s>) not allowed", email);
749be728 337 }
92bcbb9b 338 pw = xgetpwuid_self(NULL);
060d4bb3 339 name = pw->pw_name;
749be728 340 }
dfdd309e 341
92bcbb9b
JK
342 if (want_name && strict &&
343 name == git_default_name.buf && default_name_is_bogus) {
344 fputs(env_hint, stderr);
345 die("unable to auto-detect name (got '%s')", name);
346 }
347
19ce497c 348 if (strict && email == git_default_email.buf && default_email_is_bogus) {
8c5b1ae1
JK
349 fputs(env_hint, stderr);
350 die("unable to auto-detect email address (got '%s')", email);
749be728 351 }
dfdd309e 352
c96f0c8d 353 strbuf_reset(&ident);
c15e1987
JK
354 if (want_name) {
355 strbuf_addstr_without_crud(&ident, name);
356 strbuf_addstr(&ident, " <");
d9ccfe77 357 }
c96f0c8d 358 strbuf_addstr_without_crud(&ident, email);
c15e1987
JK
359 if (want_name)
360 strbuf_addch(&ident, '>');
359b27ad 361 if (want_date) {
c96f0c8d 362 strbuf_addch(&ident, ' ');
c33ddc2e
JK
363 if (date_str && date_str[0]) {
364 if (parse_date(date_str, &ident) < 0)
365 die("invalid date format: %s", date_str);
366 }
367 else
368 strbuf_addstr(&ident, ident_default_date());
d9ccfe77 369 }
c33ddc2e 370
c96f0c8d 371 return ident.buf;
6aa33f40 372}
d289d136 373
d9ccfe77
JH
374const char *fmt_name(const char *name, const char *email)
375{
f9bc573f 376 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
377}
378
774751a8 379const char *git_author_info(int flag)
d289d136 380{
d6991cee
JK
381 if (getenv("GIT_AUTHOR_NAME"))
382 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
383 if (getenv("GIT_AUTHOR_EMAIL"))
384 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 385 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 386 getenv("GIT_AUTHOR_EMAIL"),
749be728 387 getenv("GIT_AUTHOR_DATE"),
774751a8 388 flag);
d289d136
EB
389}
390
774751a8 391const char *git_committer_info(int flag)
d289d136 392{
91c38a21 393 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 394 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 395 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 396 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 397 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 398 getenv("GIT_COMMITTER_EMAIL"),
749be728 399 getenv("GIT_COMMITTER_DATE"),
774751a8 400 flag);
d289d136 401}
5aeb3a3a 402
d6991cee 403static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
404{
405#ifndef WINDOWS
406 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
407#else
408 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
409#endif
410}
9597921b 411
d6991cee
JK
412int committer_ident_sufficiently_given(void)
413{
414 return ident_is_sufficient(committer_ident_explicitly_given);
415}
416
417int author_ident_sufficiently_given(void)
418{
419 return ident_is_sufficient(author_ident_explicitly_given);
420}
421
9597921b
JK
422int git_ident_config(const char *var, const char *value, void *data)
423{
424 if (!strcmp(var, "user.name")) {
425 if (!value)
426 return config_error_nonbool(var);
8587ead7
JK
427 strbuf_reset(&git_default_name);
428 strbuf_addstr(&git_default_name, value);
d6991cee
JK
429 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
430 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
9597921b
JK
431 return 0;
432 }
433
434 if (!strcmp(var, "user.email")) {
435 if (!value)
436 return config_error_nonbool(var);
8587ead7
JK
437 strbuf_reset(&git_default_email);
438 strbuf_addstr(&git_default_email, value);
d6991cee
JK
439 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
440 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
9597921b
JK
441 return 0;
442 }
443
444 return 0;
445}
662cc30c
JK
446
447static int buf_cmp(const char *a_begin, const char *a_end,
448 const char *b_begin, const char *b_end)
449{
450 int a_len = a_end - a_begin;
451 int b_len = b_end - b_begin;
452 int min = a_len < b_len ? a_len : b_len;
453 int cmp;
454
455 cmp = memcmp(a_begin, b_begin, min);
456 if (cmp)
457 return cmp;
458
459 return a_len - b_len;
460}
461
462int ident_cmp(const struct ident_split *a,
463 const struct ident_split *b)
464{
465 int cmp;
466
467 cmp = buf_cmp(a->mail_begin, a->mail_end,
468 b->mail_begin, b->mail_end);
469 if (cmp)
470 return cmp;
471
472 return buf_cmp(a->name_begin, a->name_end,
473 b->name_begin, b->name_end);
474}