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