4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
11 static struct strbuf git_default_name
= STRBUF_INIT
;
12 static struct strbuf git_default_email
= STRBUF_INIT
;
13 static struct strbuf git_default_date
= STRBUF_INIT
;
14 static int default_email_is_bogus
;
15 static int default_name_is_bogus
;
17 static int ident_use_config_only
;
19 #define IDENT_NAME_GIVEN 01
20 #define IDENT_MAIL_GIVEN 02
21 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
22 static int committer_ident_explicitly_given
;
23 static int author_ident_explicitly_given
;
24 static int ident_config_given
;
26 #ifdef NO_GECOS_IN_PWENT
27 #define get_gecos(ignored) "&"
29 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
32 static struct passwd
*xgetpwuid_self(int *is_bogus
)
37 pw
= getpwuid(getuid());
39 static struct passwd fallback
;
40 fallback
.pw_name
= "unknown";
41 #ifndef NO_GECOS_IN_PWENT
42 fallback
.pw_gecos
= "Unknown";
51 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
55 /* Traditionally GECOS field had office phone numbers etc, separated
56 * with commas. Also & stands for capitalized form of the login name.
59 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
62 strbuf_addch(name
, ch
);
64 /* Sorry, Mr. McDonald... */
65 strbuf_addch(name
, toupper(*w
->pw_name
));
66 strbuf_addstr(name
, w
->pw_name
+ 1);
71 static int add_mailname_host(struct strbuf
*buf
)
74 struct strbuf mailnamebuf
= STRBUF_INIT
;
76 mailname
= fopen("/etc/mailname", "r");
79 warning_errno("cannot open /etc/mailname");
82 if (strbuf_getline(&mailnamebuf
, mailname
) == EOF
) {
84 warning_errno("cannot read /etc/mailname");
85 strbuf_release(&mailnamebuf
);
90 strbuf_addbuf(buf
, &mailnamebuf
);
91 strbuf_release(&mailnamebuf
);
96 static int canonical_name(const char *host
, struct strbuf
*out
)
101 struct addrinfo hints
, *ai
;
102 memset (&hints
, '\0', sizeof (hints
));
103 hints
.ai_flags
= AI_CANONNAME
;
104 if (!getaddrinfo(host
, NULL
, &hints
, &ai
)) {
105 if (ai
&& ai
->ai_canonname
&& strchr(ai
->ai_canonname
, '.')) {
106 strbuf_addstr(out
, ai
->ai_canonname
);
112 struct hostent
*he
= gethostbyname(host
);
113 if (he
&& strchr(he
->h_name
, '.')) {
114 strbuf_addstr(out
, he
->h_name
);
122 static void add_domainname(struct strbuf
*out
, int *is_bogus
)
124 char buf
[HOST_NAME_MAX
+ 1];
126 if (xgethostname(buf
, sizeof(buf
))) {
127 warning_errno("cannot get host name");
128 strbuf_addstr(out
, "(none)");
132 if (strchr(buf
, '.'))
133 strbuf_addstr(out
, buf
);
134 else if (canonical_name(buf
, out
) < 0) {
135 strbuf_addf(out
, "%s.(none)", buf
);
140 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
,
144 * Make up a fake email address
145 * (name + '@' + hostname [+ '.' + domainname])
147 strbuf_addstr(email
, pw
->pw_name
);
148 strbuf_addch(email
, '@');
150 if (!add_mailname_host(email
))
151 return; /* read from "/etc/mailname" (Debian) */
152 add_domainname(email
, is_bogus
);
155 const char *ident_default_name(void)
157 if (!(ident_config_given
& IDENT_NAME_GIVEN
) && !git_default_name
.len
) {
158 copy_gecos(xgetpwuid_self(&default_name_is_bogus
), &git_default_name
);
159 strbuf_trim(&git_default_name
);
161 return git_default_name
.buf
;
164 const char *ident_default_email(void)
166 if (!(ident_config_given
& IDENT_MAIL_GIVEN
) && !git_default_email
.len
) {
167 const char *email
= getenv("EMAIL");
169 if (email
&& email
[0]) {
170 strbuf_addstr(&git_default_email
, email
);
171 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
172 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
174 copy_email(xgetpwuid_self(&default_email_is_bogus
),
175 &git_default_email
, &default_email_is_bogus
);
176 strbuf_trim(&git_default_email
);
178 return git_default_email
.buf
;
181 static const char *ident_default_date(void)
183 if (!git_default_date
.len
)
184 datestamp(&git_default_date
);
185 return git_default_date
.buf
;
188 void reset_ident_date(void)
190 strbuf_reset(&git_default_date
);
193 static int crud(unsigned char c
)
207 static int has_non_crud(const char *str
)
209 for (; *str
; str
++) {
217 * Copy over a string to the destination, but avoid special
218 * characters ('\n', '<' and '>') and remove crud at the end
220 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
225 /* Remove crud from the beginning.. */
226 while ((c
= *src
) != 0) {
232 /* Remove crud from the end.. */
242 * Copy the rest to the buffer, but avoid the special
243 * characters '\n' '<' and '>' that act as delimiters on
244 * an identification line. We can only remove crud, never add it,
245 * so 'len' is our maximum.
247 strbuf_grow(sb
, len
);
248 for (i
= 0; i
< len
; i
++) {
251 case '\n': case '<': case '>':
254 sb
->buf
[sb
->len
++] = c
;
256 sb
->buf
[sb
->len
] = '\0';
260 * Reverse of fmt_ident(); given an ident line, split the fields
261 * to allow the caller to parse it.
262 * Signal a success by returning 0, but date/tz fields of the result
263 * can still be NULL if the input line only has the name/email part
264 * (e.g. reading from a reflog entry).
266 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
272 memset(split
, 0, sizeof(*split
));
274 split
->name_begin
= line
;
275 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
277 split
->mail_begin
= cp
+ 1;
280 if (!split
->mail_begin
)
283 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
285 split
->name_end
= cp
+ 1;
288 if (!split
->name_end
) {
289 /* no human readable name */
290 split
->name_end
= split
->name_begin
;
293 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
295 split
->mail_end
= cp
;
298 if (!split
->mail_end
)
302 * Look from the end-of-line to find the trailing ">" of the mail
303 * address, even though we should already know it as split->mail_end.
304 * This can help in cases of broken idents with an extra ">" somewhere
305 * in the email address. Note that we are assuming the timestamp will
306 * never have a ">" in it.
308 * Note that we will always find some ">" before going off the front of
309 * the string, because will always hit the split->mail_end closing
312 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
315 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
317 if (line
+ len
<= cp
)
319 split
->date_begin
= cp
;
320 span
= strspn(cp
, "0123456789");
323 split
->date_end
= split
->date_begin
+ span
;
324 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
326 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
328 split
->tz_begin
= cp
;
329 span
= strspn(cp
+ 1, "0123456789");
332 split
->tz_end
= split
->tz_begin
+ 1 + span
;
336 split
->date_begin
= NULL
;
337 split
->date_end
= NULL
;
338 split
->tz_begin
= NULL
;
339 split
->tz_end
= NULL
;
343 static const char *env_hint
=
345 "*** Please tell me who you are.\n"
349 " git config --global user.email \"you@example.com\"\n"
350 " git config --global user.name \"Your Name\"\n"
352 "to set your account\'s default identity.\n"
353 "Omit --global to set the identity only in this repository.\n"
356 const char *fmt_ident(const char *name
, const char *email
,
357 const char *date_str
, int flag
)
359 static struct strbuf ident
= STRBUF_INIT
;
360 int strict
= (flag
& IDENT_STRICT
);
361 int want_date
= !(flag
& IDENT_NO_DATE
);
362 int want_name
= !(flag
& IDENT_NO_NAME
);
365 if (strict
&& ident_use_config_only
366 && !(ident_config_given
& IDENT_MAIL_GIVEN
)) {
367 fputs(_(env_hint
), stderr
);
368 die(_("no email was given and auto-detection is disabled"));
370 email
= ident_default_email();
371 if (strict
&& default_email_is_bogus
) {
372 fputs(_(env_hint
), stderr
);
373 die(_("unable to auto-detect email address (got '%s')"), email
);
378 int using_default
= 0;
380 if (strict
&& ident_use_config_only
381 && !(ident_config_given
& IDENT_NAME_GIVEN
)) {
382 fputs(_(env_hint
), stderr
);
383 die(_("no name was given and auto-detection is disabled"));
385 name
= ident_default_name();
387 if (strict
&& default_name_is_bogus
) {
388 fputs(_(env_hint
), stderr
);
389 die(_("unable to auto-detect name (got '%s')"), name
);
396 fputs(_(env_hint
), stderr
);
397 die(_("empty ident name (for <%s>) not allowed"), email
);
399 pw
= xgetpwuid_self(NULL
);
402 if (strict
&& !has_non_crud(name
))
403 die(_("name consists only of disallowed characters: %s"), name
);
406 strbuf_reset(&ident
);
408 strbuf_addstr_without_crud(&ident
, name
);
409 strbuf_addstr(&ident
, " <");
411 strbuf_addstr_without_crud(&ident
, email
);
413 strbuf_addch(&ident
, '>');
415 strbuf_addch(&ident
, ' ');
416 if (date_str
&& date_str
[0]) {
417 if (parse_date(date_str
, &ident
) < 0)
418 die(_("invalid date format: %s"), date_str
);
421 strbuf_addstr(&ident
, ident_default_date());
427 const char *fmt_name(const char *name
, const char *email
)
429 return fmt_ident(name
, email
, NULL
, IDENT_STRICT
| IDENT_NO_DATE
);
432 const char *git_author_info(int flag
)
434 if (getenv("GIT_AUTHOR_NAME"))
435 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
436 if (getenv("GIT_AUTHOR_EMAIL"))
437 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
438 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
439 getenv("GIT_AUTHOR_EMAIL"),
440 getenv("GIT_AUTHOR_DATE"),
444 const char *git_committer_info(int flag
)
446 if (getenv("GIT_COMMITTER_NAME"))
447 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
448 if (getenv("GIT_COMMITTER_EMAIL"))
449 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
450 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
451 getenv("GIT_COMMITTER_EMAIL"),
452 getenv("GIT_COMMITTER_DATE"),
456 static int ident_is_sufficient(int user_ident_explicitly_given
)
459 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
461 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
465 int committer_ident_sufficiently_given(void)
467 return ident_is_sufficient(committer_ident_explicitly_given
);
470 int author_ident_sufficiently_given(void)
472 return ident_is_sufficient(author_ident_explicitly_given
);
475 int git_ident_config(const char *var
, const char *value
, void *data
)
477 if (!strcmp(var
, "user.useconfigonly")) {
478 ident_use_config_only
= git_config_bool(var
, value
);
482 if (!strcmp(var
, "user.name")) {
484 return config_error_nonbool(var
);
485 strbuf_reset(&git_default_name
);
486 strbuf_addstr(&git_default_name
, value
);
487 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
488 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
489 ident_config_given
|= IDENT_NAME_GIVEN
;
493 if (!strcmp(var
, "user.email")) {
495 return config_error_nonbool(var
);
496 strbuf_reset(&git_default_email
);
497 strbuf_addstr(&git_default_email
, value
);
498 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
499 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
500 ident_config_given
|= IDENT_MAIL_GIVEN
;
507 static int buf_cmp(const char *a_begin
, const char *a_end
,
508 const char *b_begin
, const char *b_end
)
510 int a_len
= a_end
- a_begin
;
511 int b_len
= b_end
- b_begin
;
512 int min
= a_len
< b_len
? a_len
: b_len
;
515 cmp
= memcmp(a_begin
, b_begin
, min
);
519 return a_len
- b_len
;
522 int ident_cmp(const struct ident_split
*a
,
523 const struct ident_split
*b
)
527 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
528 b
->mail_begin
, b
->mail_end
);
532 return buf_cmp(a
->name_begin
, a
->name_end
,
533 b
->name_begin
, b
->name_end
);