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