4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
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) "&"
35 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
38 static struct passwd
*xgetpwuid_self(int *is_bogus
)
43 pw
= getpwuid(getuid());
45 static struct passwd fallback
;
46 fallback
.pw_name
= "unknown";
47 #ifndef NO_GECOS_IN_PWENT
48 fallback
.pw_gecos
= "Unknown";
57 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
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
++) {
68 strbuf_addch(name
, ch
);
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
)
80 struct strbuf mailnamebuf
= STRBUF_INIT
;
82 mailname
= fopen_or_warn("/etc/mailname", "r");
86 if (strbuf_getline(&mailnamebuf
, mailname
) == EOF
) {
88 warning_errno("cannot read /etc/mailname");
89 strbuf_release(&mailnamebuf
);
94 strbuf_addbuf(buf
, &mailnamebuf
);
95 strbuf_release(&mailnamebuf
);
100 static int canonical_name(const char *host
, struct strbuf
*out
)
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
);
116 struct hostent
*he
= gethostbyname(host
);
117 if (he
&& strchr(he
->h_name
, '.')) {
118 strbuf_addstr(out
, he
->h_name
);
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)");
136 if (strchr(buf
, '.'))
137 strbuf_addstr(out
, buf
);
138 else if (canonical_name(buf
, out
) < 0) {
139 strbuf_addf(out
, "%s.(none)", buf
);
144 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
,
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
);
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
)
214 static int has_non_crud(const char *str
)
216 for (; *str
; str
++) {
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
)
232 /* Remove crud from the beginning.. */
233 while ((c
= *src
) != 0) {
239 /* Remove crud from the end.. */
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
++) {
258 case '\n': case '<': case '>':
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
)
279 memset(split
, 0, sizeof(*split
));
281 split
->name_begin
= line
;
282 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
284 split
->mail_begin
= cp
+ 1;
287 if (!split
->mail_begin
)
290 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
292 split
->name_end
= cp
+ 1;
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
++)
302 split
->mail_end
= cp
;
305 if (!split
->mail_end
)
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
319 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
322 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
324 if (line
+ len
<= cp
)
326 split
->date_begin
= cp
;
327 span
= strspn(cp
, "0123456789");
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
!= '-'))
335 split
->tz_begin
= cp
;
336 span
= strspn(cp
+ 1, "0123456789");
339 split
->tz_end
= split
->tz_begin
+ 1 + span
;
343 split
->date_begin
= NULL
;
344 split
->date_end
= NULL
;
345 split
->tz_begin
= NULL
;
346 split
->tz_end
= NULL
;
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
,
355 struct string_list
*mailmap
)
357 size_t namelen
, maillen
;
360 struct ident_split ident
;
362 if (split_ident_line(&ident
, person
, len
))
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
;
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
);
390 void apply_mailmap_to_header(struct strbuf
*buf
, const char **header
,
391 struct string_list
*mailmap
)
393 size_t buf_offset
= 0;
399 const char *person
, *line
;
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');
411 buf_offset
+= endp
- line
;
412 buf_offset
+= rewrite_ident_line(person
, endp
- person
, buf
, mailmap
);
417 buf_offset
= strchrnul(line
, '\n') - buf
->buf
;
418 if (buf
->buf
[buf_offset
] == '\n')
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
);
430 case WANT_COMMITTER_IDENT
:
431 fputs(_("Committer identity unknown\n"), stderr
);
438 "*** Please tell me who you are.\n"
442 " git config --global user.email \"you@example.com\"\n"
443 " git config --global user.name \"Your Name\"\n"
445 "to set your account\'s default identity.\n"
446 "Omit --global to set the identity only in this repository.\n"
450 const char *fmt_ident(const char *name
, const char *email
,
451 enum want_ident whose_ident
, const char *date_str
, int flag
)
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
);
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
;
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
);
482 int using_default
= 0;
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
;
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();
498 if (strict
&& default_name_is_bogus
) {
499 ident_env_hint(whose_ident
);
500 die(_("unable to auto-detect name (got '%s')"), name
);
507 ident_env_hint(whose_ident
);
508 die(_("empty ident name (for <%s>) not allowed"), email
);
510 pw
= xgetpwuid_self(NULL
);
513 if (strict
&& !has_non_crud(name
))
514 die(_("name consists only of disallowed characters: %s"), name
);
519 strbuf_addstr_without_crud(ident
, name
);
520 strbuf_addstr(ident
, " <");
522 strbuf_addstr_without_crud(ident
, email
);
524 strbuf_addch(ident
, '>');
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
);
532 strbuf_addstr(ident
, ident_default_date());
538 const char *fmt_name(enum want_ident whose_ident
)
543 switch (whose_ident
) {
544 case WANT_BLANK_IDENT
:
546 case WANT_AUTHOR_IDENT
:
547 name
= getenv("GIT_AUTHOR_NAME");
548 email
= getenv("GIT_AUTHOR_EMAIL");
550 case WANT_COMMITTER_IDENT
:
551 name
= getenv("GIT_COMMITTER_NAME");
552 email
= getenv("GIT_COMMITTER_EMAIL");
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"),
568 getenv("GIT_AUTHOR_DATE"),
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"),
585 static int ident_is_sufficient(int user_ident_explicitly_given
)
588 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
590 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
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")) {
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
;
616 if (!strcmp(var
, "author.email")) {
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
;
626 if (!strcmp(var
, "committer.name")) {
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
;
636 if (!strcmp(var
, "committer.email")) {
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
;
646 if (!strcmp(var
, "user.name")) {
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
;
657 if (!strcmp(var
, "user.email")) {
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
;
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
);
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);
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
;
709 cmp
= memcmp(a_begin
, b_begin
, min
);
713 return a_len
- b_len
;
716 int ident_cmp(const struct ident_split
*a
,
717 const struct ident_split
*b
)
721 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
722 b
->mail_begin
, b
->mail_end
);
726 return buf_cmp(a
->name_begin
, a
->name_end
,
727 b
->name_begin
, b
->name_end
);