4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
8 #include "git-compat-util.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) "&"
38 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
41 static struct passwd
*xgetpwuid_self(int *is_bogus
)
46 pw
= getpwuid(getuid());
48 static struct passwd fallback
;
49 fallback
.pw_name
= "unknown";
50 #ifndef NO_GECOS_IN_PWENT
51 fallback
.pw_gecos
= "Unknown";
60 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
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
++) {
71 strbuf_addch(name
, ch
);
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
)
83 struct strbuf mailnamebuf
= STRBUF_INIT
;
85 mailname
= fopen_or_warn("/etc/mailname", "r");
89 if (strbuf_getline(&mailnamebuf
, mailname
) == EOF
) {
91 warning_errno("cannot read /etc/mailname");
92 strbuf_release(&mailnamebuf
);
97 strbuf_addbuf(buf
, &mailnamebuf
);
98 strbuf_release(&mailnamebuf
);
103 static int canonical_name(const char *host
, struct strbuf
*out
)
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
);
119 struct hostent
*he
= gethostbyname(host
);
120 if (he
&& strchr(he
->h_name
, '.')) {
121 strbuf_addstr(out
, he
->h_name
);
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)");
139 if (strchr(buf
, '.'))
140 strbuf_addstr(out
, buf
);
141 else if (canonical_name(buf
, out
) < 0) {
142 strbuf_addf(out
, "%s.(none)", buf
);
147 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
,
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
);
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
)
217 static int has_non_crud(const char *str
)
219 for (; *str
; str
++) {
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
)
235 /* Remove crud from the beginning.. */
236 while ((c
= *src
) != 0) {
242 /* Remove crud from the end.. */
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
++) {
261 case '\n': case '<': case '>':
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
)
282 memset(split
, 0, sizeof(*split
));
284 split
->name_begin
= line
;
285 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
287 split
->mail_begin
= cp
+ 1;
290 if (!split
->mail_begin
)
293 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
295 split
->name_end
= cp
+ 1;
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
++)
305 split
->mail_end
= cp
;
308 if (!split
->mail_end
)
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
322 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
325 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
327 if (line
+ len
<= cp
)
329 split
->date_begin
= cp
;
330 span
= strspn(cp
, "0123456789");
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
!= '-'))
338 split
->tz_begin
= cp
;
339 span
= strspn(cp
+ 1, "0123456789");
342 split
->tz_end
= split
->tz_begin
+ 1 + span
;
346 split
->date_begin
= NULL
;
347 split
->date_end
= NULL
;
348 split
->tz_begin
= NULL
;
349 split
->tz_end
= NULL
;
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
,
358 struct string_list
*mailmap
)
360 size_t namelen
, maillen
;
363 struct ident_split ident
;
365 if (split_ident_line(&ident
, person
, len
))
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
;
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
);
393 void apply_mailmap_to_header(struct strbuf
*buf
, const char **header
,
394 struct string_list
*mailmap
)
396 size_t buf_offset
= 0;
402 const char *person
, *line
;
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');
414 buf_offset
+= endp
- line
;
415 buf_offset
+= rewrite_ident_line(person
, endp
- person
, buf
, mailmap
);
420 buf_offset
= strchrnul(line
, '\n') - buf
->buf
;
421 if (buf
->buf
[buf_offset
] == '\n')
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
);
433 case WANT_COMMITTER_IDENT
:
434 fputs(_("Committer identity unknown\n"), stderr
);
441 "*** Please tell me who you are.\n"
445 " git config --global user.email \"you@example.com\"\n"
446 " git config --global user.name \"Your Name\"\n"
448 "to set your account\'s default identity.\n"
449 "Omit --global to set the identity only in this repository.\n"
453 const char *fmt_ident(const char *name
, const char *email
,
454 enum want_ident whose_ident
, const char *date_str
, int flag
)
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
);
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
;
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
);
485 int using_default
= 0;
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
;
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();
501 if (strict
&& default_name_is_bogus
) {
502 ident_env_hint(whose_ident
);
503 die(_("unable to auto-detect name (got '%s')"), name
);
510 ident_env_hint(whose_ident
);
511 die(_("empty ident name (for <%s>) not allowed"), email
);
513 pw
= xgetpwuid_self(NULL
);
516 if (strict
&& !has_non_crud(name
))
517 die(_("name consists only of disallowed characters: %s"), name
);
522 strbuf_addstr_without_crud(ident
, name
);
523 strbuf_addstr(ident
, " <");
525 strbuf_addstr_without_crud(ident
, email
);
527 strbuf_addch(ident
, '>');
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
);
535 strbuf_addstr(ident
, ident_default_date());
541 const char *fmt_name(enum want_ident whose_ident
)
546 switch (whose_ident
) {
547 case WANT_BLANK_IDENT
:
549 case WANT_AUTHOR_IDENT
:
550 name
= getenv("GIT_AUTHOR_NAME");
551 email
= getenv("GIT_AUTHOR_EMAIL");
553 case WANT_COMMITTER_IDENT
:
554 name
= getenv("GIT_COMMITTER_NAME");
555 email
= getenv("GIT_COMMITTER_EMAIL");
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"),
571 getenv("GIT_AUTHOR_DATE"),
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"),
588 static int ident_is_sufficient(int user_ident_explicitly_given
)
591 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
593 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
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")) {
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
;
619 if (!strcmp(var
, "author.email")) {
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
;
629 if (!strcmp(var
, "committer.name")) {
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
;
639 if (!strcmp(var
, "committer.email")) {
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
;
649 if (!strcmp(var
, "user.name")) {
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
;
660 if (!strcmp(var
, "user.email")) {
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
;
674 int git_ident_config(const char *var
, const char *value
,
675 const struct config_context
*ctx UNUSED
,
678 if (!strcmp(var
, "user.useconfigonly")) {
679 ident_use_config_only
= git_config_bool(var
, value
);
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);
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
;
714 cmp
= memcmp(a_begin
, b_begin
, min
);
718 return a_len
- b_len
;
721 int ident_cmp(const struct ident_split
*a
,
722 const struct ident_split
*b
)
726 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
727 b
->mail_begin
, b
->mail_end
);
731 return buf_cmp(a
->name_begin
, a
->name_end
,
732 b
->name_begin
, b
->name_end
);