Sync with 2.38.5
[git/debian.git] / ident.c
blob6de76f9421d57f38c478cca68fcb97c4ede2d36c
1 /*
2 * ident.c
4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "cache.h"
9 #include "config.h"
10 #include "date.h"
11 #include "mailmap.h"
13 static struct strbuf git_default_name = STRBUF_INIT;
14 static struct strbuf git_default_email = STRBUF_INIT;
15 static struct strbuf git_default_date = STRBUF_INIT;
16 static struct strbuf git_author_name = STRBUF_INIT;
17 static struct strbuf git_author_email = STRBUF_INIT;
18 static struct strbuf git_committer_name = STRBUF_INIT;
19 static struct strbuf git_committer_email = STRBUF_INIT;
20 static int default_email_is_bogus;
21 static int default_name_is_bogus;
23 static int ident_use_config_only;
25 #define IDENT_NAME_GIVEN 01
26 #define IDENT_MAIL_GIVEN 02
27 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
28 static int committer_ident_explicitly_given;
29 static int author_ident_explicitly_given;
30 static int ident_config_given;
32 #ifdef NO_GECOS_IN_PWENT
33 #define get_gecos(ignored) "&"
34 #else
35 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
36 #endif
38 static struct passwd *xgetpwuid_self(int *is_bogus)
40 struct passwd *pw;
42 errno = 0;
43 pw = getpwuid(getuid());
44 if (!pw) {
45 static struct passwd fallback;
46 fallback.pw_name = "unknown";
47 #ifndef NO_GECOS_IN_PWENT
48 fallback.pw_gecos = "Unknown";
49 #endif
50 pw = &fallback;
51 if (is_bogus)
52 *is_bogus = 1;
54 return pw;
57 static void copy_gecos(const struct passwd *w, struct strbuf *name)
59 char *src;
61 /* Traditionally GECOS field had office phone numbers etc, separated
62 * with commas. Also & stands for capitalized form of the login name.
65 for (src = get_gecos(w); *src && *src != ','; src++) {
66 int ch = *src;
67 if (ch != '&')
68 strbuf_addch(name, ch);
69 else {
70 /* Sorry, Mr. McDonald... */
71 strbuf_addch(name, toupper(*w->pw_name));
72 strbuf_addstr(name, w->pw_name + 1);
77 static int add_mailname_host(struct strbuf *buf)
79 FILE *mailname;
80 struct strbuf mailnamebuf = STRBUF_INIT;
82 mailname = fopen_or_warn("/etc/mailname", "r");
83 if (!mailname)
84 return -1;
86 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
87 if (ferror(mailname))
88 warning_errno("cannot read /etc/mailname");
89 strbuf_release(&mailnamebuf);
90 fclose(mailname);
91 return -1;
93 /* success! */
94 strbuf_addbuf(buf, &mailnamebuf);
95 strbuf_release(&mailnamebuf);
96 fclose(mailname);
97 return 0;
100 static int canonical_name(const char *host, struct strbuf *out)
102 int status = -1;
104 #ifndef NO_IPV6
105 struct addrinfo hints, *ai;
106 memset (&hints, '\0', sizeof (hints));
107 hints.ai_flags = AI_CANONNAME;
108 if (!getaddrinfo(host, NULL, &hints, &ai)) {
109 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
110 strbuf_addstr(out, ai->ai_canonname);
111 status = 0;
113 freeaddrinfo(ai);
115 #else
116 struct hostent *he = gethostbyname(host);
117 if (he && strchr(he->h_name, '.')) {
118 strbuf_addstr(out, he->h_name);
119 status = 0;
121 #endif /* NO_IPV6 */
123 return status;
126 static void add_domainname(struct strbuf *out, int *is_bogus)
128 char buf[HOST_NAME_MAX + 1];
130 if (xgethostname(buf, sizeof(buf))) {
131 warning_errno("cannot get host name");
132 strbuf_addstr(out, "(none)");
133 *is_bogus = 1;
134 return;
136 if (strchr(buf, '.'))
137 strbuf_addstr(out, buf);
138 else if (canonical_name(buf, out) < 0) {
139 strbuf_addf(out, "%s.(none)", buf);
140 *is_bogus = 1;
144 static void copy_email(const struct passwd *pw, struct strbuf *email,
145 int *is_bogus)
148 * Make up a fake email address
149 * (name + '@' + hostname [+ '.' + domainname])
151 strbuf_addstr(email, pw->pw_name);
152 strbuf_addch(email, '@');
154 if (!add_mailname_host(email))
155 return; /* read from "/etc/mailname" (Debian) */
156 add_domainname(email, is_bogus);
159 const char *ident_default_name(void)
161 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
162 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
163 strbuf_trim(&git_default_name);
165 return git_default_name.buf;
168 const char *ident_default_email(void)
170 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
171 const char *email = getenv("EMAIL");
173 if (email && email[0]) {
174 strbuf_addstr(&git_default_email, email);
175 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
176 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
177 } else if ((email = query_user_email()) && email[0]) {
178 strbuf_addstr(&git_default_email, email);
179 free((char *)email);
180 } else
181 copy_email(xgetpwuid_self(&default_email_is_bogus),
182 &git_default_email, &default_email_is_bogus);
183 strbuf_trim(&git_default_email);
185 return git_default_email.buf;
188 static const char *ident_default_date(void)
190 if (!git_default_date.len)
191 datestamp(&git_default_date);
192 return git_default_date.buf;
195 void reset_ident_date(void)
197 strbuf_reset(&git_default_date);
200 static int crud(unsigned char c)
202 return c <= 32 ||
203 c == '.' ||
204 c == ',' ||
205 c == ':' ||
206 c == ';' ||
207 c == '<' ||
208 c == '>' ||
209 c == '"' ||
210 c == '\\' ||
211 c == '\'';
214 static int has_non_crud(const char *str)
216 for (; *str; str++) {
217 if (!crud(*str))
218 return 1;
220 return 0;
224 * Copy over a string to the destination, but avoid special
225 * characters ('\n', '<' and '>') and remove crud at the end
227 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
229 size_t i, len;
230 unsigned char c;
232 /* Remove crud from the beginning.. */
233 while ((c = *src) != 0) {
234 if (!crud(c))
235 break;
236 src++;
239 /* Remove crud from the end.. */
240 len = strlen(src);
241 while (len > 0) {
242 c = src[len-1];
243 if (!crud(c))
244 break;
245 --len;
249 * Copy the rest to the buffer, but avoid the special
250 * characters '\n' '<' and '>' that act as delimiters on
251 * an identification line. We can only remove crud, never add it,
252 * so 'len' is our maximum.
254 strbuf_grow(sb, len);
255 for (i = 0; i < len; i++) {
256 c = *src++;
257 switch (c) {
258 case '\n': case '<': case '>':
259 continue;
261 sb->buf[sb->len++] = c;
263 sb->buf[sb->len] = '\0';
267 * Reverse of fmt_ident(); given an ident line, split the fields
268 * to allow the caller to parse it.
269 * Signal a success by returning 0, but date/tz fields of the result
270 * can still be NULL if the input line only has the name/email part
271 * (e.g. reading from a reflog entry).
273 int split_ident_line(struct ident_split *split, const char *line, int len)
275 const char *cp;
276 size_t span;
277 int status = -1;
279 memset(split, 0, sizeof(*split));
281 split->name_begin = line;
282 for (cp = line; *cp && cp < line + len; cp++)
283 if (*cp == '<') {
284 split->mail_begin = cp + 1;
285 break;
287 if (!split->mail_begin)
288 return status;
290 for (cp = split->mail_begin - 2; line <= cp; cp--)
291 if (!isspace(*cp)) {
292 split->name_end = cp + 1;
293 break;
295 if (!split->name_end) {
296 /* no human readable name */
297 split->name_end = split->name_begin;
300 for (cp = split->mail_begin; cp < line + len; cp++)
301 if (*cp == '>') {
302 split->mail_end = cp;
303 break;
305 if (!split->mail_end)
306 return status;
309 * Look from the end-of-line to find the trailing ">" of the mail
310 * address, even though we should already know it as split->mail_end.
311 * This can help in cases of broken idents with an extra ">" somewhere
312 * in the email address. Note that we are assuming the timestamp will
313 * never have a ">" in it.
315 * Note that we will always find some ">" before going off the front of
316 * the string, because will always hit the split->mail_end closing
317 * bracket.
319 for (cp = line + len - 1; *cp != '>'; cp--)
322 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
324 if (line + len <= cp)
325 goto person_only;
326 split->date_begin = cp;
327 span = strspn(cp, "0123456789");
328 if (!span)
329 goto person_only;
330 split->date_end = split->date_begin + span;
331 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
333 if (line + len <= cp || (*cp != '+' && *cp != '-'))
334 goto person_only;
335 split->tz_begin = cp;
336 span = strspn(cp + 1, "0123456789");
337 if (!span)
338 goto person_only;
339 split->tz_end = split->tz_begin + 1 + span;
340 return 0;
342 person_only:
343 split->date_begin = NULL;
344 split->date_end = NULL;
345 split->tz_begin = NULL;
346 split->tz_end = NULL;
347 return 0;
351 * Returns the difference between the new and old length of the ident line.
353 static ssize_t rewrite_ident_line(const char *person, size_t len,
354 struct strbuf *buf,
355 struct string_list *mailmap)
357 size_t namelen, maillen;
358 const char *name;
359 const char *mail;
360 struct ident_split ident;
362 if (split_ident_line(&ident, person, len))
363 return 0;
365 mail = ident.mail_begin;
366 maillen = ident.mail_end - ident.mail_begin;
367 name = ident.name_begin;
368 namelen = ident.name_end - ident.name_begin;
370 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
371 struct strbuf namemail = STRBUF_INIT;
372 size_t newlen;
374 strbuf_addf(&namemail, "%.*s <%.*s>",
375 (int)namelen, name, (int)maillen, mail);
377 strbuf_splice(buf, ident.name_begin - buf->buf,
378 ident.mail_end - ident.name_begin + 1,
379 namemail.buf, namemail.len);
380 newlen = namemail.len;
382 strbuf_release(&namemail);
384 return newlen - (ident.mail_end - ident.name_begin);
387 return 0;
390 void apply_mailmap_to_header(struct strbuf *buf, const char **header,
391 struct string_list *mailmap)
393 size_t buf_offset = 0;
395 if (!mailmap)
396 return;
398 for (;;) {
399 const char *person, *line;
400 size_t i;
401 int found_header = 0;
403 line = buf->buf + buf_offset;
404 if (!*line || *line == '\n')
405 return; /* End of headers */
407 for (i = 0; header[i]; i++)
408 if (skip_prefix(line, header[i], &person)) {
409 const char *endp = strchrnul(person, '\n');
410 found_header = 1;
411 buf_offset += endp - line;
412 buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
413 break;
416 if (!found_header) {
417 buf_offset = strchrnul(line, '\n') - buf->buf;
418 if (buf->buf[buf_offset] == '\n')
419 buf_offset++;
424 static void ident_env_hint(enum want_ident whose_ident)
426 switch (whose_ident) {
427 case WANT_AUTHOR_IDENT:
428 fputs(_("Author identity unknown\n"), stderr);
429 break;
430 case WANT_COMMITTER_IDENT:
431 fputs(_("Committer identity unknown\n"), stderr);
432 break;
433 default:
434 break;
437 fputs(_("\n"
438 "*** Please tell me who you are.\n"
439 "\n"
440 "Run\n"
441 "\n"
442 " git config --global user.email \"you@example.com\"\n"
443 " git config --global user.name \"Your Name\"\n"
444 "\n"
445 "to set your account\'s default identity.\n"
446 "Omit --global to set the identity only in this repository.\n"
447 "\n"), stderr);
450 const char *fmt_ident(const char *name, const char *email,
451 enum want_ident whose_ident, const char *date_str, int flag)
453 static int index;
454 static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
455 int strict = (flag & IDENT_STRICT);
456 int want_date = !(flag & IDENT_NO_DATE);
457 int want_name = !(flag & IDENT_NO_NAME);
459 struct strbuf *ident = &ident_pool[index];
460 index = (index + 1) % ARRAY_SIZE(ident_pool);
462 if (!email) {
463 if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
464 email = git_author_email.buf;
465 else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
466 email = git_committer_email.buf;
468 if (!email) {
469 if (strict && ident_use_config_only
470 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
471 ident_env_hint(whose_ident);
472 die(_("no email was given and auto-detection is disabled"));
474 email = ident_default_email();
475 if (strict && default_email_is_bogus) {
476 ident_env_hint(whose_ident);
477 die(_("unable to auto-detect email address (got '%s')"), email);
481 if (want_name) {
482 int using_default = 0;
483 if (!name) {
484 if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
485 name = git_author_name.buf;
486 else if (whose_ident == WANT_COMMITTER_IDENT &&
487 git_committer_name.len)
488 name = git_committer_name.buf;
490 if (!name) {
491 if (strict && ident_use_config_only
492 && !(ident_config_given & IDENT_NAME_GIVEN)) {
493 ident_env_hint(whose_ident);
494 die(_("no name was given and auto-detection is disabled"));
496 name = ident_default_name();
497 using_default = 1;
498 if (strict && default_name_is_bogus) {
499 ident_env_hint(whose_ident);
500 die(_("unable to auto-detect name (got '%s')"), name);
503 if (!*name) {
504 struct passwd *pw;
505 if (strict) {
506 if (using_default)
507 ident_env_hint(whose_ident);
508 die(_("empty ident name (for <%s>) not allowed"), email);
510 pw = xgetpwuid_self(NULL);
511 name = pw->pw_name;
513 if (strict && !has_non_crud(name))
514 die(_("name consists only of disallowed characters: %s"), name);
517 strbuf_reset(ident);
518 if (want_name) {
519 strbuf_addstr_without_crud(ident, name);
520 strbuf_addstr(ident, " <");
522 strbuf_addstr_without_crud(ident, email);
523 if (want_name)
524 strbuf_addch(ident, '>');
525 if (want_date) {
526 strbuf_addch(ident, ' ');
527 if (date_str && date_str[0]) {
528 if (parse_date(date_str, ident) < 0)
529 die(_("invalid date format: %s"), date_str);
531 else
532 strbuf_addstr(ident, ident_default_date());
535 return ident->buf;
538 const char *fmt_name(enum want_ident whose_ident)
540 char *name = NULL;
541 char *email = NULL;
543 switch (whose_ident) {
544 case WANT_BLANK_IDENT:
545 break;
546 case WANT_AUTHOR_IDENT:
547 name = getenv("GIT_AUTHOR_NAME");
548 email = getenv("GIT_AUTHOR_EMAIL");
549 break;
550 case WANT_COMMITTER_IDENT:
551 name = getenv("GIT_COMMITTER_NAME");
552 email = getenv("GIT_COMMITTER_EMAIL");
553 break;
555 return fmt_ident(name, email, whose_ident, NULL,
556 IDENT_STRICT | IDENT_NO_DATE);
559 const char *git_author_info(int flag)
561 if (getenv("GIT_AUTHOR_NAME"))
562 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
563 if (getenv("GIT_AUTHOR_EMAIL"))
564 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
565 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
566 getenv("GIT_AUTHOR_EMAIL"),
567 WANT_AUTHOR_IDENT,
568 getenv("GIT_AUTHOR_DATE"),
569 flag);
572 const char *git_committer_info(int flag)
574 if (getenv("GIT_COMMITTER_NAME"))
575 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
576 if (getenv("GIT_COMMITTER_EMAIL"))
577 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
578 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
579 getenv("GIT_COMMITTER_EMAIL"),
580 WANT_COMMITTER_IDENT,
581 getenv("GIT_COMMITTER_DATE"),
582 flag);
585 static int ident_is_sufficient(int user_ident_explicitly_given)
587 #ifndef WINDOWS
588 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
589 #else
590 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
591 #endif
594 int committer_ident_sufficiently_given(void)
596 return ident_is_sufficient(committer_ident_explicitly_given);
599 int author_ident_sufficiently_given(void)
601 return ident_is_sufficient(author_ident_explicitly_given);
604 static int set_ident(const char *var, const char *value)
606 if (!strcmp(var, "author.name")) {
607 if (!value)
608 return config_error_nonbool(var);
609 strbuf_reset(&git_author_name);
610 strbuf_addstr(&git_author_name, value);
611 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
612 ident_config_given |= IDENT_NAME_GIVEN;
613 return 0;
616 if (!strcmp(var, "author.email")) {
617 if (!value)
618 return config_error_nonbool(var);
619 strbuf_reset(&git_author_email);
620 strbuf_addstr(&git_author_email, value);
621 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
622 ident_config_given |= IDENT_MAIL_GIVEN;
623 return 0;
626 if (!strcmp(var, "committer.name")) {
627 if (!value)
628 return config_error_nonbool(var);
629 strbuf_reset(&git_committer_name);
630 strbuf_addstr(&git_committer_name, value);
631 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
632 ident_config_given |= IDENT_NAME_GIVEN;
633 return 0;
636 if (!strcmp(var, "committer.email")) {
637 if (!value)
638 return config_error_nonbool(var);
639 strbuf_reset(&git_committer_email);
640 strbuf_addstr(&git_committer_email, value);
641 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
642 ident_config_given |= IDENT_MAIL_GIVEN;
643 return 0;
646 if (!strcmp(var, "user.name")) {
647 if (!value)
648 return config_error_nonbool(var);
649 strbuf_reset(&git_default_name);
650 strbuf_addstr(&git_default_name, value);
651 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
652 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
653 ident_config_given |= IDENT_NAME_GIVEN;
654 return 0;
657 if (!strcmp(var, "user.email")) {
658 if (!value)
659 return config_error_nonbool(var);
660 strbuf_reset(&git_default_email);
661 strbuf_addstr(&git_default_email, value);
662 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
663 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
664 ident_config_given |= IDENT_MAIL_GIVEN;
665 return 0;
668 return 0;
671 int git_ident_config(const char *var, const char *value, void *data UNUSED)
673 if (!strcmp(var, "user.useconfigonly")) {
674 ident_use_config_only = git_config_bool(var, value);
675 return 0;
678 return set_ident(var, value);
681 static void set_env_if(const char *key, const char *value, int *given, int bit)
683 if ((*given & bit) || getenv(key))
684 return; /* nothing to do */
685 setenv(key, value, 0);
686 *given |= bit;
689 void prepare_fallback_ident(const char *name, const char *email)
691 set_env_if("GIT_AUTHOR_NAME", name,
692 &author_ident_explicitly_given, IDENT_NAME_GIVEN);
693 set_env_if("GIT_AUTHOR_EMAIL", email,
694 &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
695 set_env_if("GIT_COMMITTER_NAME", name,
696 &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
697 set_env_if("GIT_COMMITTER_EMAIL", email,
698 &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
701 static int buf_cmp(const char *a_begin, const char *a_end,
702 const char *b_begin, const char *b_end)
704 int a_len = a_end - a_begin;
705 int b_len = b_end - b_begin;
706 int min = a_len < b_len ? a_len : b_len;
707 int cmp;
709 cmp = memcmp(a_begin, b_begin, min);
710 if (cmp)
711 return cmp;
713 return a_len - b_len;
716 int ident_cmp(const struct ident_split *a,
717 const struct ident_split *b)
719 int cmp;
721 cmp = buf_cmp(a->mail_begin, a->mail_end,
722 b->mail_begin, b->mail_end);
723 if (cmp)
724 return cmp;
726 return buf_cmp(a->name_begin, a->name_end,
727 b->name_begin, b->name_end);