4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
12 static struct strbuf git_default_name
= STRBUF_INIT
;
13 static struct strbuf git_default_email
= STRBUF_INIT
;
14 static struct strbuf git_default_date
= STRBUF_INIT
;
15 static struct strbuf git_author_name
= STRBUF_INIT
;
16 static struct strbuf git_author_email
= STRBUF_INIT
;
17 static struct strbuf git_committer_name
= STRBUF_INIT
;
18 static struct strbuf git_committer_email
= STRBUF_INIT
;
19 static int default_email_is_bogus
;
20 static int default_name_is_bogus
;
22 static int ident_use_config_only
;
24 #define IDENT_NAME_GIVEN 01
25 #define IDENT_MAIL_GIVEN 02
26 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
27 static int committer_ident_explicitly_given
;
28 static int author_ident_explicitly_given
;
29 static int ident_config_given
;
31 #ifdef NO_GECOS_IN_PWENT
32 #define get_gecos(ignored) "&"
34 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
37 static struct passwd
*xgetpwuid_self(int *is_bogus
)
42 pw
= getpwuid(getuid());
44 static struct passwd fallback
;
45 fallback
.pw_name
= "unknown";
46 #ifndef NO_GECOS_IN_PWENT
47 fallback
.pw_gecos
= "Unknown";
56 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
60 /* Traditionally GECOS field had office phone numbers etc, separated
61 * with commas. Also & stands for capitalized form of the login name.
64 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
67 strbuf_addch(name
, ch
);
69 /* Sorry, Mr. McDonald... */
70 strbuf_addch(name
, toupper(*w
->pw_name
));
71 strbuf_addstr(name
, w
->pw_name
+ 1);
76 static int add_mailname_host(struct strbuf
*buf
)
79 struct strbuf mailnamebuf
= STRBUF_INIT
;
81 mailname
= fopen_or_warn("/etc/mailname", "r");
85 if (strbuf_getline(&mailnamebuf
, mailname
) == EOF
) {
87 warning_errno("cannot read /etc/mailname");
88 strbuf_release(&mailnamebuf
);
93 strbuf_addbuf(buf
, &mailnamebuf
);
94 strbuf_release(&mailnamebuf
);
99 static int canonical_name(const char *host
, struct strbuf
*out
)
104 struct addrinfo hints
, *ai
;
105 memset (&hints
, '\0', sizeof (hints
));
106 hints
.ai_flags
= AI_CANONNAME
;
107 if (!getaddrinfo(host
, NULL
, &hints
, &ai
)) {
108 if (ai
&& ai
->ai_canonname
&& strchr(ai
->ai_canonname
, '.')) {
109 strbuf_addstr(out
, ai
->ai_canonname
);
115 struct hostent
*he
= gethostbyname(host
);
116 if (he
&& strchr(he
->h_name
, '.')) {
117 strbuf_addstr(out
, he
->h_name
);
125 static void add_domainname(struct strbuf
*out
, int *is_bogus
)
127 char buf
[HOST_NAME_MAX
+ 1];
129 if (xgethostname(buf
, sizeof(buf
))) {
130 warning_errno("cannot get host name");
131 strbuf_addstr(out
, "(none)");
135 if (strchr(buf
, '.'))
136 strbuf_addstr(out
, buf
);
137 else if (canonical_name(buf
, out
) < 0) {
138 strbuf_addf(out
, "%s.(none)", buf
);
143 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
,
147 * Make up a fake email address
148 * (name + '@' + hostname [+ '.' + domainname])
150 strbuf_addstr(email
, pw
->pw_name
);
151 strbuf_addch(email
, '@');
153 if (!add_mailname_host(email
))
154 return; /* read from "/etc/mailname" (Debian) */
155 add_domainname(email
, is_bogus
);
158 const char *ident_default_name(void)
160 if (!(ident_config_given
& IDENT_NAME_GIVEN
) && !git_default_name
.len
) {
161 copy_gecos(xgetpwuid_self(&default_name_is_bogus
), &git_default_name
);
162 strbuf_trim(&git_default_name
);
164 return git_default_name
.buf
;
167 const char *ident_default_email(void)
169 if (!(ident_config_given
& IDENT_MAIL_GIVEN
) && !git_default_email
.len
) {
170 const char *email
= getenv("EMAIL");
172 if (email
&& email
[0]) {
173 strbuf_addstr(&git_default_email
, email
);
174 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
175 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
176 } else if ((email
= query_user_email()) && email
[0]) {
177 strbuf_addstr(&git_default_email
, email
);
180 copy_email(xgetpwuid_self(&default_email_is_bogus
),
181 &git_default_email
, &default_email_is_bogus
);
182 strbuf_trim(&git_default_email
);
184 return git_default_email
.buf
;
187 static const char *ident_default_date(void)
189 if (!git_default_date
.len
)
190 datestamp(&git_default_date
);
191 return git_default_date
.buf
;
194 void reset_ident_date(void)
196 strbuf_reset(&git_default_date
);
199 static int crud(unsigned char c
)
213 static int has_non_crud(const char *str
)
215 for (; *str
; str
++) {
223 * Copy over a string to the destination, but avoid special
224 * characters ('\n', '<' and '>') and remove crud at the end
226 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
231 /* Remove crud from the beginning.. */
232 while ((c
= *src
) != 0) {
238 /* Remove crud from the end.. */
248 * Copy the rest to the buffer, but avoid the special
249 * characters '\n' '<' and '>' that act as delimiters on
250 * an identification line. We can only remove crud, never add it,
251 * so 'len' is our maximum.
253 strbuf_grow(sb
, len
);
254 for (i
= 0; i
< len
; i
++) {
257 case '\n': case '<': case '>':
260 sb
->buf
[sb
->len
++] = c
;
262 sb
->buf
[sb
->len
] = '\0';
266 * Reverse of fmt_ident(); given an ident line, split the fields
267 * to allow the caller to parse it.
268 * Signal a success by returning 0, but date/tz fields of the result
269 * can still be NULL if the input line only has the name/email part
270 * (e.g. reading from a reflog entry).
272 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
278 memset(split
, 0, sizeof(*split
));
280 split
->name_begin
= line
;
281 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
283 split
->mail_begin
= cp
+ 1;
286 if (!split
->mail_begin
)
289 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
291 split
->name_end
= cp
+ 1;
294 if (!split
->name_end
) {
295 /* no human readable name */
296 split
->name_end
= split
->name_begin
;
299 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
301 split
->mail_end
= cp
;
304 if (!split
->mail_end
)
308 * Look from the end-of-line to find the trailing ">" of the mail
309 * address, even though we should already know it as split->mail_end.
310 * This can help in cases of broken idents with an extra ">" somewhere
311 * in the email address. Note that we are assuming the timestamp will
312 * never have a ">" in it.
314 * Note that we will always find some ">" before going off the front of
315 * the string, because will always hit the split->mail_end closing
318 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
321 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
323 if (line
+ len
<= cp
)
325 split
->date_begin
= cp
;
326 span
= strspn(cp
, "0123456789");
329 split
->date_end
= split
->date_begin
+ span
;
330 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
332 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
334 split
->tz_begin
= cp
;
335 span
= strspn(cp
+ 1, "0123456789");
338 split
->tz_end
= split
->tz_begin
+ 1 + span
;
342 split
->date_begin
= NULL
;
343 split
->date_end
= NULL
;
344 split
->tz_begin
= NULL
;
345 split
->tz_end
= NULL
;
350 static void ident_env_hint(enum want_ident whose_ident
)
352 switch (whose_ident
) {
353 case WANT_AUTHOR_IDENT
:
354 fputs(_("Author identity unknown\n"), stderr
);
356 case WANT_COMMITTER_IDENT
:
357 fputs(_("Committer identity unknown\n"), stderr
);
364 "*** Please tell me who you are.\n"
368 " git config --global user.email \"you@example.com\"\n"
369 " git config --global user.name \"Your Name\"\n"
371 "to set your account\'s default identity.\n"
372 "Omit --global to set the identity only in this repository.\n"
376 const char *fmt_ident(const char *name
, const char *email
,
377 enum want_ident whose_ident
, const char *date_str
, int flag
)
380 static struct strbuf ident_pool
[2] = { STRBUF_INIT
, STRBUF_INIT
};
381 int strict
= (flag
& IDENT_STRICT
);
382 int want_date
= !(flag
& IDENT_NO_DATE
);
383 int want_name
= !(flag
& IDENT_NO_NAME
);
385 struct strbuf
*ident
= &ident_pool
[index
];
386 index
= (index
+ 1) % ARRAY_SIZE(ident_pool
);
389 if (whose_ident
== WANT_AUTHOR_IDENT
&& git_author_email
.len
)
390 email
= git_author_email
.buf
;
391 else if (whose_ident
== WANT_COMMITTER_IDENT
&& git_committer_email
.len
)
392 email
= git_committer_email
.buf
;
395 if (strict
&& ident_use_config_only
396 && !(ident_config_given
& IDENT_MAIL_GIVEN
)) {
397 ident_env_hint(whose_ident
);
398 die(_("no email was given and auto-detection is disabled"));
400 email
= ident_default_email();
401 if (strict
&& default_email_is_bogus
) {
402 ident_env_hint(whose_ident
);
403 die(_("unable to auto-detect email address (got '%s')"), email
);
408 int using_default
= 0;
410 if (whose_ident
== WANT_AUTHOR_IDENT
&& git_author_name
.len
)
411 name
= git_author_name
.buf
;
412 else if (whose_ident
== WANT_COMMITTER_IDENT
&&
413 git_committer_name
.len
)
414 name
= git_committer_name
.buf
;
417 if (strict
&& ident_use_config_only
418 && !(ident_config_given
& IDENT_NAME_GIVEN
)) {
419 ident_env_hint(whose_ident
);
420 die(_("no name was given and auto-detection is disabled"));
422 name
= ident_default_name();
424 if (strict
&& default_name_is_bogus
) {
425 ident_env_hint(whose_ident
);
426 die(_("unable to auto-detect name (got '%s')"), name
);
433 ident_env_hint(whose_ident
);
434 die(_("empty ident name (for <%s>) not allowed"), email
);
436 pw
= xgetpwuid_self(NULL
);
439 if (strict
&& !has_non_crud(name
))
440 die(_("name consists only of disallowed characters: %s"), name
);
445 strbuf_addstr_without_crud(ident
, name
);
446 strbuf_addstr(ident
, " <");
448 strbuf_addstr_without_crud(ident
, email
);
450 strbuf_addch(ident
, '>');
452 strbuf_addch(ident
, ' ');
453 if (date_str
&& date_str
[0]) {
454 if (parse_date(date_str
, ident
) < 0)
455 die(_("invalid date format: %s"), date_str
);
458 strbuf_addstr(ident
, ident_default_date());
464 const char *fmt_name(enum want_ident whose_ident
)
469 switch (whose_ident
) {
470 case WANT_BLANK_IDENT
:
472 case WANT_AUTHOR_IDENT
:
473 name
= getenv("GIT_AUTHOR_NAME");
474 email
= getenv("GIT_AUTHOR_EMAIL");
476 case WANT_COMMITTER_IDENT
:
477 name
= getenv("GIT_COMMITTER_NAME");
478 email
= getenv("GIT_COMMITTER_EMAIL");
481 return fmt_ident(name
, email
, whose_ident
, NULL
,
482 IDENT_STRICT
| IDENT_NO_DATE
);
485 const char *git_author_info(int flag
)
487 if (getenv("GIT_AUTHOR_NAME"))
488 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
489 if (getenv("GIT_AUTHOR_EMAIL"))
490 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
491 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
492 getenv("GIT_AUTHOR_EMAIL"),
494 getenv("GIT_AUTHOR_DATE"),
498 const char *git_committer_info(int flag
)
500 if (getenv("GIT_COMMITTER_NAME"))
501 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
502 if (getenv("GIT_COMMITTER_EMAIL"))
503 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
504 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
505 getenv("GIT_COMMITTER_EMAIL"),
506 WANT_COMMITTER_IDENT
,
507 getenv("GIT_COMMITTER_DATE"),
511 static int ident_is_sufficient(int user_ident_explicitly_given
)
514 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
516 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
520 int committer_ident_sufficiently_given(void)
522 return ident_is_sufficient(committer_ident_explicitly_given
);
525 int author_ident_sufficiently_given(void)
527 return ident_is_sufficient(author_ident_explicitly_given
);
530 static int set_ident(const char *var
, const char *value
)
532 if (!strcmp(var
, "author.name")) {
534 return config_error_nonbool(var
);
535 strbuf_reset(&git_author_name
);
536 strbuf_addstr(&git_author_name
, value
);
537 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
538 ident_config_given
|= IDENT_NAME_GIVEN
;
542 if (!strcmp(var
, "author.email")) {
544 return config_error_nonbool(var
);
545 strbuf_reset(&git_author_email
);
546 strbuf_addstr(&git_author_email
, value
);
547 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
548 ident_config_given
|= IDENT_MAIL_GIVEN
;
552 if (!strcmp(var
, "committer.name")) {
554 return config_error_nonbool(var
);
555 strbuf_reset(&git_committer_name
);
556 strbuf_addstr(&git_committer_name
, value
);
557 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
558 ident_config_given
|= IDENT_NAME_GIVEN
;
562 if (!strcmp(var
, "committer.email")) {
564 return config_error_nonbool(var
);
565 strbuf_reset(&git_committer_email
);
566 strbuf_addstr(&git_committer_email
, value
);
567 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
568 ident_config_given
|= IDENT_MAIL_GIVEN
;
572 if (!strcmp(var
, "user.name")) {
574 return config_error_nonbool(var
);
575 strbuf_reset(&git_default_name
);
576 strbuf_addstr(&git_default_name
, value
);
577 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
578 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
579 ident_config_given
|= IDENT_NAME_GIVEN
;
583 if (!strcmp(var
, "user.email")) {
585 return config_error_nonbool(var
);
586 strbuf_reset(&git_default_email
);
587 strbuf_addstr(&git_default_email
, value
);
588 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
589 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
590 ident_config_given
|= IDENT_MAIL_GIVEN
;
597 int git_ident_config(const char *var
, const char *value
, void *data
)
599 if (!strcmp(var
, "user.useconfigonly")) {
600 ident_use_config_only
= git_config_bool(var
, value
);
604 return set_ident(var
, value
);
607 static void set_env_if(const char *key
, const char *value
, int *given
, int bit
)
609 if ((*given
& bit
) || getenv(key
))
610 return; /* nothing to do */
611 setenv(key
, value
, 0);
615 void prepare_fallback_ident(const char *name
, const char *email
)
617 set_env_if("GIT_AUTHOR_NAME", name
,
618 &author_ident_explicitly_given
, IDENT_NAME_GIVEN
);
619 set_env_if("GIT_AUTHOR_EMAIL", email
,
620 &author_ident_explicitly_given
, IDENT_MAIL_GIVEN
);
621 set_env_if("GIT_COMMITTER_NAME", name
,
622 &committer_ident_explicitly_given
, IDENT_NAME_GIVEN
);
623 set_env_if("GIT_COMMITTER_EMAIL", email
,
624 &committer_ident_explicitly_given
, IDENT_MAIL_GIVEN
);
627 static int buf_cmp(const char *a_begin
, const char *a_end
,
628 const char *b_begin
, const char *b_end
)
630 int a_len
= a_end
- a_begin
;
631 int b_len
= b_end
- b_begin
;
632 int min
= a_len
< b_len
? a_len
: b_len
;
635 cmp
= memcmp(a_begin
, b_begin
, min
);
639 return a_len
- b_len
;
642 int ident_cmp(const struct ident_split
*a
,
643 const struct ident_split
*b
)
647 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
648 b
->mail_begin
, b
->mail_end
);
652 return buf_cmp(a
->name_begin
, a
->name_end
,
653 b
->name_begin
, b
->name_end
);