Merge branch 'jc/parse-options-reset'
[git.git] / ident.c
blob08be4d0747d7d68c8ce336886270a93fb2048987
1 /*
2 * ident.c
4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "git-compat-util.h"
9 #include "ident.h"
10 #include "config.h"
11 #include "date.h"
12 #include "gettext.h"
13 #include "mailmap.h"
14 #include "strbuf.h"
16 static struct strbuf git_default_name = STRBUF_INIT;
17 static struct strbuf git_default_email = STRBUF_INIT;
18 static struct strbuf git_default_date = STRBUF_INIT;
19 static struct strbuf git_author_name = STRBUF_INIT;
20 static struct strbuf git_author_email = STRBUF_INIT;
21 static struct strbuf git_committer_name = STRBUF_INIT;
22 static struct strbuf git_committer_email = STRBUF_INIT;
23 static int default_email_is_bogus;
24 static int default_name_is_bogus;
26 static int ident_use_config_only;
28 #define IDENT_NAME_GIVEN 01
29 #define IDENT_MAIL_GIVEN 02
30 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
31 static int committer_ident_explicitly_given;
32 static int author_ident_explicitly_given;
33 static int ident_config_given;
35 #ifdef NO_GECOS_IN_PWENT
36 #define get_gecos(ignored) "&"
37 #else
38 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
39 #endif
41 static struct passwd *xgetpwuid_self(int *is_bogus)
43 struct passwd *pw;
45 errno = 0;
46 pw = getpwuid(getuid());
47 if (!pw) {
48 static struct passwd fallback;
49 fallback.pw_name = "unknown";
50 #ifndef NO_GECOS_IN_PWENT
51 fallback.pw_gecos = "Unknown";
52 #endif
53 pw = &fallback;
54 if (is_bogus)
55 *is_bogus = 1;
57 return pw;
60 static void copy_gecos(const struct passwd *w, struct strbuf *name)
62 char *src;
64 /* Traditionally GECOS field had office phone numbers etc, separated
65 * with commas. Also & stands for capitalized form of the login name.
68 for (src = get_gecos(w); *src && *src != ','; src++) {
69 int ch = *src;
70 if (ch != '&')
71 strbuf_addch(name, ch);
72 else {
73 /* Sorry, Mr. McDonald... */
74 strbuf_addch(name, toupper(*w->pw_name));
75 strbuf_addstr(name, w->pw_name + 1);
80 static int add_mailname_host(struct strbuf *buf)
82 FILE *mailname;
83 struct strbuf mailnamebuf = STRBUF_INIT;
85 mailname = fopen_or_warn("/etc/mailname", "r");
86 if (!mailname)
87 return -1;
89 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
90 if (ferror(mailname))
91 warning_errno("cannot read /etc/mailname");
92 strbuf_release(&mailnamebuf);
93 fclose(mailname);
94 return -1;
96 /* success! */
97 strbuf_addbuf(buf, &mailnamebuf);
98 strbuf_release(&mailnamebuf);
99 fclose(mailname);
100 return 0;
103 static int canonical_name(const char *host, struct strbuf *out)
105 int status = -1;
107 #ifndef NO_IPV6
108 struct addrinfo hints, *ai;
109 memset (&hints, '\0', sizeof (hints));
110 hints.ai_flags = AI_CANONNAME;
111 if (!getaddrinfo(host, NULL, &hints, &ai)) {
112 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
113 strbuf_addstr(out, ai->ai_canonname);
114 status = 0;
116 freeaddrinfo(ai);
118 #else
119 struct hostent *he = gethostbyname(host);
120 if (he && strchr(he->h_name, '.')) {
121 strbuf_addstr(out, he->h_name);
122 status = 0;
124 #endif /* NO_IPV6 */
126 return status;
129 static void add_domainname(struct strbuf *out, int *is_bogus)
131 char buf[HOST_NAME_MAX + 1];
133 if (xgethostname(buf, sizeof(buf))) {
134 warning_errno("cannot get host name");
135 strbuf_addstr(out, "(none)");
136 *is_bogus = 1;
137 return;
139 if (strchr(buf, '.'))
140 strbuf_addstr(out, buf);
141 else if (canonical_name(buf, out) < 0) {
142 strbuf_addf(out, "%s.(none)", buf);
143 *is_bogus = 1;
147 static void copy_email(const struct passwd *pw, struct strbuf *email,
148 int *is_bogus)
151 * Make up a fake email address
152 * (name + '@' + hostname [+ '.' + domainname])
154 strbuf_addstr(email, pw->pw_name);
155 strbuf_addch(email, '@');
157 if (!add_mailname_host(email))
158 return; /* read from "/etc/mailname" (Debian) */
159 add_domainname(email, is_bogus);
162 const char *ident_default_name(void)
164 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
165 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
166 strbuf_trim(&git_default_name);
168 return git_default_name.buf;
171 const char *ident_default_email(void)
173 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
174 const char *email = getenv("EMAIL");
176 if (email && email[0]) {
177 strbuf_addstr(&git_default_email, email);
178 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
179 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
180 } else if ((email = query_user_email()) && email[0]) {
181 strbuf_addstr(&git_default_email, email);
182 free((char *)email);
183 } else
184 copy_email(xgetpwuid_self(&default_email_is_bogus),
185 &git_default_email, &default_email_is_bogus);
186 strbuf_trim(&git_default_email);
188 return git_default_email.buf;
191 static const char *ident_default_date(void)
193 if (!git_default_date.len)
194 datestamp(&git_default_date);
195 return git_default_date.buf;
198 void reset_ident_date(void)
200 strbuf_reset(&git_default_date);
203 static int crud(unsigned char c)
205 return c <= 32 ||
206 c == '.' ||
207 c == ',' ||
208 c == ':' ||
209 c == ';' ||
210 c == '<' ||
211 c == '>' ||
212 c == '"' ||
213 c == '\\' ||
214 c == '\'';
217 static int has_non_crud(const char *str)
219 for (; *str; str++) {
220 if (!crud(*str))
221 return 1;
223 return 0;
227 * Copy over a string to the destination, but avoid special
228 * characters ('\n', '<' and '>') and remove crud at the end
230 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
232 size_t i, len;
233 unsigned char c;
235 /* Remove crud from the beginning.. */
236 while ((c = *src) != 0) {
237 if (!crud(c))
238 break;
239 src++;
242 /* Remove crud from the end.. */
243 len = strlen(src);
244 while (len > 0) {
245 c = src[len-1];
246 if (!crud(c))
247 break;
248 --len;
252 * Copy the rest to the buffer, but avoid the special
253 * characters '\n' '<' and '>' that act as delimiters on
254 * an identification line. We can only remove crud, never add it,
255 * so 'len' is our maximum.
257 strbuf_grow(sb, len);
258 for (i = 0; i < len; i++) {
259 c = *src++;
260 switch (c) {
261 case '\n': case '<': case '>':
262 continue;
264 sb->buf[sb->len++] = c;
266 sb->buf[sb->len] = '\0';
270 * Reverse of fmt_ident(); given an ident line, split the fields
271 * to allow the caller to parse it.
272 * Signal a success by returning 0, but date/tz fields of the result
273 * can still be NULL if the input line only has the name/email part
274 * (e.g. reading from a reflog entry).
276 int split_ident_line(struct ident_split *split, const char *line, int len)
278 const char *cp;
279 size_t span;
280 int status = -1;
282 memset(split, 0, sizeof(*split));
284 split->name_begin = line;
285 for (cp = line; *cp && cp < line + len; cp++)
286 if (*cp == '<') {
287 split->mail_begin = cp + 1;
288 break;
290 if (!split->mail_begin)
291 return status;
293 for (cp = split->mail_begin - 2; line <= cp; cp--)
294 if (!isspace(*cp)) {
295 split->name_end = cp + 1;
296 break;
298 if (!split->name_end) {
299 /* no human readable name */
300 split->name_end = split->name_begin;
303 for (cp = split->mail_begin; cp < line + len; cp++)
304 if (*cp == '>') {
305 split->mail_end = cp;
306 break;
308 if (!split->mail_end)
309 return status;
312 * Look from the end-of-line to find the trailing ">" of the mail
313 * address, even though we should already know it as split->mail_end.
314 * This can help in cases of broken idents with an extra ">" somewhere
315 * in the email address. Note that we are assuming the timestamp will
316 * never have a ">" in it.
318 * Note that we will always find some ">" before going off the front of
319 * the string, because will always hit the split->mail_end closing
320 * bracket.
322 for (cp = line + len - 1; *cp != '>'; cp--)
325 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
327 if (line + len <= cp)
328 goto person_only;
329 split->date_begin = cp;
330 span = strspn(cp, "0123456789");
331 if (!span)
332 goto person_only;
333 split->date_end = split->date_begin + span;
334 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
336 if (line + len <= cp || (*cp != '+' && *cp != '-'))
337 goto person_only;
338 split->tz_begin = cp;
339 span = strspn(cp + 1, "0123456789");
340 if (!span)
341 goto person_only;
342 split->tz_end = split->tz_begin + 1 + span;
343 return 0;
345 person_only:
346 split->date_begin = NULL;
347 split->date_end = NULL;
348 split->tz_begin = NULL;
349 split->tz_end = NULL;
350 return 0;
354 * Returns the difference between the new and old length of the ident line.
356 static ssize_t rewrite_ident_line(const char *person, size_t len,
357 struct strbuf *buf,
358 struct string_list *mailmap)
360 size_t namelen, maillen;
361 const char *name;
362 const char *mail;
363 struct ident_split ident;
365 if (split_ident_line(&ident, person, len))
366 return 0;
368 mail = ident.mail_begin;
369 maillen = ident.mail_end - ident.mail_begin;
370 name = ident.name_begin;
371 namelen = ident.name_end - ident.name_begin;
373 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
374 struct strbuf namemail = STRBUF_INIT;
375 size_t newlen;
377 strbuf_addf(&namemail, "%.*s <%.*s>",
378 (int)namelen, name, (int)maillen, mail);
380 strbuf_splice(buf, ident.name_begin - buf->buf,
381 ident.mail_end - ident.name_begin + 1,
382 namemail.buf, namemail.len);
383 newlen = namemail.len;
385 strbuf_release(&namemail);
387 return newlen - (ident.mail_end - ident.name_begin);
390 return 0;
393 void apply_mailmap_to_header(struct strbuf *buf, const char **header,
394 struct string_list *mailmap)
396 size_t buf_offset = 0;
398 if (!mailmap)
399 return;
401 for (;;) {
402 const char *person, *line;
403 size_t i;
404 int found_header = 0;
406 line = buf->buf + buf_offset;
407 if (!*line || *line == '\n')
408 return; /* End of headers */
410 for (i = 0; header[i]; i++)
411 if (skip_prefix(line, header[i], &person)) {
412 const char *endp = strchrnul(person, '\n');
413 found_header = 1;
414 buf_offset += endp - line;
415 buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
416 break;
419 if (!found_header) {
420 buf_offset = strchrnul(line, '\n') - buf->buf;
421 if (buf->buf[buf_offset] == '\n')
422 buf_offset++;
427 static void ident_env_hint(enum want_ident whose_ident)
429 switch (whose_ident) {
430 case WANT_AUTHOR_IDENT:
431 fputs(_("Author identity unknown\n"), stderr);
432 break;
433 case WANT_COMMITTER_IDENT:
434 fputs(_("Committer identity unknown\n"), stderr);
435 break;
436 default:
437 break;
440 fputs(_("\n"
441 "*** Please tell me who you are.\n"
442 "\n"
443 "Run\n"
444 "\n"
445 " git config --global user.email \"you@example.com\"\n"
446 " git config --global user.name \"Your Name\"\n"
447 "\n"
448 "to set your account\'s default identity.\n"
449 "Omit --global to set the identity only in this repository.\n"
450 "\n"), stderr);
453 const char *fmt_ident(const char *name, const char *email,
454 enum want_ident whose_ident, const char *date_str, int flag)
456 static int index;
457 static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
458 int strict = (flag & IDENT_STRICT);
459 int want_date = !(flag & IDENT_NO_DATE);
460 int want_name = !(flag & IDENT_NO_NAME);
462 struct strbuf *ident = &ident_pool[index];
463 index = (index + 1) % ARRAY_SIZE(ident_pool);
465 if (!email) {
466 if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
467 email = git_author_email.buf;
468 else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
469 email = git_committer_email.buf;
471 if (!email) {
472 if (strict && ident_use_config_only
473 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
474 ident_env_hint(whose_ident);
475 die(_("no email was given and auto-detection is disabled"));
477 email = ident_default_email();
478 if (strict && default_email_is_bogus) {
479 ident_env_hint(whose_ident);
480 die(_("unable to auto-detect email address (got '%s')"), email);
484 if (want_name) {
485 int using_default = 0;
486 if (!name) {
487 if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
488 name = git_author_name.buf;
489 else if (whose_ident == WANT_COMMITTER_IDENT &&
490 git_committer_name.len)
491 name = git_committer_name.buf;
493 if (!name) {
494 if (strict && ident_use_config_only
495 && !(ident_config_given & IDENT_NAME_GIVEN)) {
496 ident_env_hint(whose_ident);
497 die(_("no name was given and auto-detection is disabled"));
499 name = ident_default_name();
500 using_default = 1;
501 if (strict && default_name_is_bogus) {
502 ident_env_hint(whose_ident);
503 die(_("unable to auto-detect name (got '%s')"), name);
506 if (!*name) {
507 struct passwd *pw;
508 if (strict) {
509 if (using_default)
510 ident_env_hint(whose_ident);
511 die(_("empty ident name (for <%s>) not allowed"), email);
513 pw = xgetpwuid_self(NULL);
514 name = pw->pw_name;
516 if (strict && !has_non_crud(name))
517 die(_("name consists only of disallowed characters: %s"), name);
520 strbuf_reset(ident);
521 if (want_name) {
522 strbuf_addstr_without_crud(ident, name);
523 strbuf_addstr(ident, " <");
525 strbuf_addstr_without_crud(ident, email);
526 if (want_name)
527 strbuf_addch(ident, '>');
528 if (want_date) {
529 strbuf_addch(ident, ' ');
530 if (date_str && date_str[0]) {
531 if (parse_date(date_str, ident) < 0)
532 die(_("invalid date format: %s"), date_str);
534 else
535 strbuf_addstr(ident, ident_default_date());
538 return ident->buf;
541 const char *fmt_name(enum want_ident whose_ident)
543 char *name = NULL;
544 char *email = NULL;
546 switch (whose_ident) {
547 case WANT_BLANK_IDENT:
548 break;
549 case WANT_AUTHOR_IDENT:
550 name = getenv("GIT_AUTHOR_NAME");
551 email = getenv("GIT_AUTHOR_EMAIL");
552 break;
553 case WANT_COMMITTER_IDENT:
554 name = getenv("GIT_COMMITTER_NAME");
555 email = getenv("GIT_COMMITTER_EMAIL");
556 break;
558 return fmt_ident(name, email, whose_ident, NULL,
559 IDENT_STRICT | IDENT_NO_DATE);
562 const char *git_author_info(int flag)
564 if (getenv("GIT_AUTHOR_NAME"))
565 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
566 if (getenv("GIT_AUTHOR_EMAIL"))
567 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
568 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
569 getenv("GIT_AUTHOR_EMAIL"),
570 WANT_AUTHOR_IDENT,
571 getenv("GIT_AUTHOR_DATE"),
572 flag);
575 const char *git_committer_info(int flag)
577 if (getenv("GIT_COMMITTER_NAME"))
578 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
579 if (getenv("GIT_COMMITTER_EMAIL"))
580 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
581 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
582 getenv("GIT_COMMITTER_EMAIL"),
583 WANT_COMMITTER_IDENT,
584 getenv("GIT_COMMITTER_DATE"),
585 flag);
588 static int ident_is_sufficient(int user_ident_explicitly_given)
590 #ifndef WINDOWS
591 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
592 #else
593 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
594 #endif
597 int committer_ident_sufficiently_given(void)
599 return ident_is_sufficient(committer_ident_explicitly_given);
602 int author_ident_sufficiently_given(void)
604 return ident_is_sufficient(author_ident_explicitly_given);
607 static int set_ident(const char *var, const char *value)
609 if (!strcmp(var, "author.name")) {
610 if (!value)
611 return config_error_nonbool(var);
612 strbuf_reset(&git_author_name);
613 strbuf_addstr(&git_author_name, value);
614 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
615 ident_config_given |= IDENT_NAME_GIVEN;
616 return 0;
619 if (!strcmp(var, "author.email")) {
620 if (!value)
621 return config_error_nonbool(var);
622 strbuf_reset(&git_author_email);
623 strbuf_addstr(&git_author_email, value);
624 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
625 ident_config_given |= IDENT_MAIL_GIVEN;
626 return 0;
629 if (!strcmp(var, "committer.name")) {
630 if (!value)
631 return config_error_nonbool(var);
632 strbuf_reset(&git_committer_name);
633 strbuf_addstr(&git_committer_name, value);
634 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
635 ident_config_given |= IDENT_NAME_GIVEN;
636 return 0;
639 if (!strcmp(var, "committer.email")) {
640 if (!value)
641 return config_error_nonbool(var);
642 strbuf_reset(&git_committer_email);
643 strbuf_addstr(&git_committer_email, value);
644 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
645 ident_config_given |= IDENT_MAIL_GIVEN;
646 return 0;
649 if (!strcmp(var, "user.name")) {
650 if (!value)
651 return config_error_nonbool(var);
652 strbuf_reset(&git_default_name);
653 strbuf_addstr(&git_default_name, value);
654 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
655 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
656 ident_config_given |= IDENT_NAME_GIVEN;
657 return 0;
660 if (!strcmp(var, "user.email")) {
661 if (!value)
662 return config_error_nonbool(var);
663 strbuf_reset(&git_default_email);
664 strbuf_addstr(&git_default_email, value);
665 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
666 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
667 ident_config_given |= IDENT_MAIL_GIVEN;
668 return 0;
671 return 0;
674 int git_ident_config(const char *var, const char *value,
675 const struct config_context *ctx UNUSED,
676 void *data UNUSED)
678 if (!strcmp(var, "user.useconfigonly")) {
679 ident_use_config_only = git_config_bool(var, value);
680 return 0;
683 return set_ident(var, value);
686 static void set_env_if(const char *key, const char *value, int *given, int bit)
688 if ((*given & bit) || getenv(key))
689 return; /* nothing to do */
690 setenv(key, value, 0);
691 *given |= bit;
694 void prepare_fallback_ident(const char *name, const char *email)
696 set_env_if("GIT_AUTHOR_NAME", name,
697 &author_ident_explicitly_given, IDENT_NAME_GIVEN);
698 set_env_if("GIT_AUTHOR_EMAIL", email,
699 &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
700 set_env_if("GIT_COMMITTER_NAME", name,
701 &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
702 set_env_if("GIT_COMMITTER_EMAIL", email,
703 &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
706 static int buf_cmp(const char *a_begin, const char *a_end,
707 const char *b_begin, const char *b_end)
709 int a_len = a_end - a_begin;
710 int b_len = b_end - b_begin;
711 int min = a_len < b_len ? a_len : b_len;
712 int cmp;
714 cmp = memcmp(a_begin, b_begin, min);
715 if (cmp)
716 return cmp;
718 return a_len - b_len;
721 int ident_cmp(const struct ident_split *a,
722 const struct ident_split *b)
724 int cmp;
726 cmp = buf_cmp(a->mail_begin, a->mail_end,
727 b->mail_begin, b->mail_end);
728 if (cmp)
729 return cmp;
731 return buf_cmp(a->name_begin, a->name_end,
732 b->name_begin, b->name_end);