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