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_or_warn("/etc/mailname", "r");
80 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
82 warning_errno("cannot read /etc/mailname");
83 strbuf_release(&mailnamebuf);
88 strbuf_addbuf(buf, &mailnamebuf);
89 strbuf_release(&mailnamebuf);
94 static int canonical_name(const char *host, struct strbuf *out)
99 struct addrinfo hints, *ai;
100 memset (&hints, '\0', sizeof (hints));
101 hints.ai_flags = AI_CANONNAME;
102 if (!getaddrinfo(host, NULL, &hints, &ai)) {
103 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
104 strbuf_addstr(out, ai->ai_canonname);
110 struct hostent *he = gethostbyname(host);
111 if (he && strchr(he->h_name, '.')) {
112 strbuf_addstr(out, he->h_name);
120 static void add_domainname(struct strbuf *out, int *is_bogus)
122 char buf[HOST_NAME_MAX + 1];
124 if (xgethostname(buf, sizeof(buf))) {
125 warning_errno("cannot get host name");
126 strbuf_addstr(out, "(none)");
130 if (strchr(buf, '.'))
131 strbuf_addstr(out, buf);
132 else if (canonical_name(buf, out) < 0) {
133 strbuf_addf(out, "%s.(none)", buf);
138 static void copy_email(const struct passwd *pw, struct strbuf *email,
142 * Make up a fake email address
143 * (name + '@' + hostname [+ '.' + domainname])
145 strbuf_addstr(email, pw->pw_name);
146 strbuf_addch(email, '@');
148 if (!add_mailname_host(email))
149 return; /* read from "/etc/mailname" (Debian) */
150 add_domainname(email, is_bogus);
153 const char *ident_default_name(void)
155 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
156 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
157 strbuf_trim(&git_default_name);
159 return git_default_name.buf;
162 const char *ident_default_email(void)
164 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
165 const char *email = getenv("EMAIL");
167 if (email && email[0]) {
168 strbuf_addstr(&git_default_email, email);
169 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
170 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
171 } else if ((email = query_user_email()) && email[0]) {
172 strbuf_addstr(&git_default_email, email);
175 copy_email(xgetpwuid_self(&default_email_is_bogus),
176 &git_default_email, &default_email_is_bogus);
177 strbuf_trim(&git_default_email);
179 return git_default_email.buf;
182 static const char *ident_default_date(void)
184 if (!git_default_date.len)
185 datestamp(&git_default_date);
186 return git_default_date.buf;
189 void reset_ident_date(void)
191 strbuf_reset(&git_default_date);
194 static int crud(unsigned char c)
208 static int has_non_crud(const char *str)
210 for (; *str; str++) {
218 * Copy over a string to the destination, but avoid special
219 * characters ('\n', '<' and '>') and remove crud at the end
221 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
226 /* Remove crud from the beginning.. */
227 while ((c = *src) != 0) {
233 /* Remove crud from the end.. */
243 * Copy the rest to the buffer, but avoid the special
244 * characters '\n' '<' and '>' that act as delimiters on
245 * an identification line. We can only remove crud, never add it,
246 * so 'len' is our maximum.
248 strbuf_grow(sb, len);
249 for (i = 0; i < len; i++) {
252 case '\n': case '<': case '>':
255 sb->buf[sb->len++] = c;
257 sb->buf[sb->len] = '\0';
261 * Reverse of fmt_ident(); given an ident line, split the fields
262 * to allow the caller to parse it.
263 * Signal a success by returning 0, but date/tz fields of the result
264 * can still be NULL if the input line only has the name/email part
265 * (e.g. reading from a reflog entry).
267 int split_ident_line(struct ident_split *split, const char *line, int len)
273 memset(split, 0, sizeof(*split));
275 split->name_begin = line;
276 for (cp = line; *cp && cp < line + len; cp++)
278 split->mail_begin = cp + 1;
281 if (!split->mail_begin)
284 for (cp = split->mail_begin - 2; line <= cp; cp--)
286 split->name_end = cp + 1;
289 if (!split->name_end) {
290 /* no human readable name */
291 split->name_end = split->name_begin;
294 for (cp = split->mail_begin; cp < line + len; cp++)
296 split->mail_end = cp;
299 if (!split->mail_end)
303 * Look from the end-of-line to find the trailing ">" of the mail
304 * address, even though we should already know it as split->mail_end.
305 * This can help in cases of broken idents with an extra ">" somewhere
306 * in the email address. Note that we are assuming the timestamp will
307 * never have a ">" in it.
309 * Note that we will always find some ">" before going off the front of
310 * the string, because will always hit the split->mail_end closing
313 for (cp = line + len - 1; *cp != '>'; cp--)
316 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
318 if (line + len <= cp)
320 split->date_begin = cp;
321 span = strspn(cp, "0123456789");
324 split->date_end = split->date_begin + span;
325 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
327 if (line + len <= cp || (*cp != '+' && *cp != '-'))
329 split->tz_begin = cp;
330 span = strspn(cp + 1, "0123456789");
333 split->tz_end = split->tz_begin + 1 + span;
337 split->date_begin = NULL;
338 split->date_end = NULL;
339 split->tz_begin = NULL;
340 split->tz_end = NULL;
344 static const char *env_hint =
346 "*** Please tell me who you are.\n"
350 " git config --global user.email \"you@example.com\"\n"
351 " git config --global user.name \"Your Name\"\n"
353 "to set your account\'s default identity.\n"
354 "Omit --global to set the identity only in this repository.\n"
357 const char *fmt_ident(const char *name, const char *email,
358 const char *date_str, int flag)
360 static struct strbuf ident = STRBUF_INIT;
361 int strict = (flag & IDENT_STRICT);
362 int want_date = !(flag & IDENT_NO_DATE);
363 int want_name = !(flag & IDENT_NO_NAME);
366 if (strict && ident_use_config_only
367 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
368 fputs(_(env_hint), stderr);
369 die(_("no email was given and auto-detection is disabled"));
371 email = ident_default_email();
372 if (strict && default_email_is_bogus) {
373 fputs(_(env_hint), stderr);
374 die(_("unable to auto-detect email address (got '%s')"), email);
379 int using_default = 0;
381 if (strict && ident_use_config_only
382 && !(ident_config_given & IDENT_NAME_GIVEN)) {
383 fputs(_(env_hint), stderr);
384 die(_("no name was given and auto-detection is disabled"));
386 name = ident_default_name();
388 if (strict && default_name_is_bogus) {
389 fputs(_(env_hint), stderr);
390 die(_("unable to auto-detect name (got '%s')"), name);
397 fputs(_(env_hint), stderr);
398 die(_("empty ident name (for <%s>) not allowed"), email);
400 pw = xgetpwuid_self(NULL);
403 if (strict && !has_non_crud(name))
404 die(_("name consists only of disallowed characters: %s"), name);
407 strbuf_reset(&ident);
409 strbuf_addstr_without_crud(&ident, name);
410 strbuf_addstr(&ident, " <");
412 strbuf_addstr_without_crud(&ident, email);
414 strbuf_addch(&ident, '>');
416 strbuf_addch(&ident, ' ');
417 if (date_str && date_str[0]) {
418 if (parse_date(date_str, &ident) < 0)
419 die(_("invalid date format: %s"), date_str);
422 strbuf_addstr(&ident, ident_default_date());
428 const char *fmt_name(const char *name, const char *email)
430 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
433 const char *git_author_info(int flag)
435 if (getenv("GIT_AUTHOR_NAME"))
436 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
437 if (getenv("GIT_AUTHOR_EMAIL"))
438 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
439 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
440 getenv("GIT_AUTHOR_EMAIL"),
441 getenv("GIT_AUTHOR_DATE"),
445 const char *git_committer_info(int flag)
447 if (getenv("GIT_COMMITTER_NAME"))
448 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
449 if (getenv("GIT_COMMITTER_EMAIL"))
450 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
451 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
452 getenv("GIT_COMMITTER_EMAIL"),
453 getenv("GIT_COMMITTER_DATE"),
457 static int ident_is_sufficient(int user_ident_explicitly_given)
460 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
462 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
466 int committer_ident_sufficiently_given(void)
468 return ident_is_sufficient(committer_ident_explicitly_given);
471 int author_ident_sufficiently_given(void)
473 return ident_is_sufficient(author_ident_explicitly_given);
476 int git_ident_config(const char *var, const char *value, void *data)
478 if (!strcmp(var, "user.useconfigonly")) {
479 ident_use_config_only = git_config_bool(var, value);
483 if (!strcmp(var, "user.name")) {
485 return config_error_nonbool(var);
486 strbuf_reset(&git_default_name);
487 strbuf_addstr(&git_default_name, value);
488 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
489 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
490 ident_config_given |= IDENT_NAME_GIVEN;
494 if (!strcmp(var, "user.email")) {
496 return config_error_nonbool(var);
497 strbuf_reset(&git_default_email);
498 strbuf_addstr(&git_default_email, value);
499 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
500 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
501 ident_config_given |= IDENT_MAIL_GIVEN;
508 static int buf_cmp(const char *a_begin, const char *a_end,
509 const char *b_begin, const char *b_end)
511 int a_len = a_end - a_begin;
512 int b_len = b_end - b_begin;
513 int min = a_len < b_len ? a_len : b_len;
516 cmp = memcmp(a_begin, b_begin, min);
520 return a_len - b_len;
523 int ident_cmp(const struct ident_split *a,
524 const struct ident_split *b)
528 cmp = buf_cmp(a->mail_begin, a->mail_end,
529 b->mail_begin, b->mail_end);
533 return buf_cmp(a->name_begin, a->name_end,
534 b->name_begin, b->name_end);