4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
10 static struct strbuf git_default_name = STRBUF_INIT;
11 static struct strbuf git_default_email = STRBUF_INIT;
12 static struct strbuf git_default_date = STRBUF_INIT;
13 static int default_email_is_bogus;
14 static int default_name_is_bogus;
16 static int ident_use_config_only;
18 #define IDENT_NAME_GIVEN 01
19 #define IDENT_MAIL_GIVEN 02
20 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
21 static int committer_ident_explicitly_given;
22 static int author_ident_explicitly_given;
23 static int ident_config_given;
25 #ifdef NO_GECOS_IN_PWENT
26 #define get_gecos(ignored) "&"
28 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
31 static struct passwd *xgetpwuid_self(int *is_bogus)
36 pw = getpwuid(getuid());
38 static struct passwd fallback;
39 fallback.pw_name = "unknown";
40 #ifndef NO_GECOS_IN_PWENT
41 fallback.pw_gecos = "Unknown";
50 static void copy_gecos(const struct passwd *w, struct strbuf *name)
54 /* Traditionally GECOS field had office phone numbers etc, separated
55 * with commas. Also & stands for capitalized form of the login name.
58 for (src = get_gecos(w); *src && *src != ','; src++) {
61 strbuf_addch(name, ch);
63 /* Sorry, Mr. McDonald... */
64 strbuf_addch(name, toupper(*w->pw_name));
65 strbuf_addstr(name, w->pw_name + 1);
70 static int add_mailname_host(struct strbuf *buf)
73 struct strbuf mailnamebuf = STRBUF_INIT;
75 mailname = fopen("/etc/mailname", "r");
78 warning_errno("cannot open /etc/mailname");
81 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
83 warning_errno("cannot read /etc/mailname");
84 strbuf_release(&mailnamebuf);
89 strbuf_addbuf(buf, &mailnamebuf);
90 strbuf_release(&mailnamebuf);
95 static int canonical_name(const char *host, struct strbuf *out)
100 struct addrinfo hints, *ai;
101 memset (&hints, '\0', sizeof (hints));
102 hints.ai_flags = AI_CANONNAME;
103 if (!getaddrinfo(host, NULL, &hints, &ai)) {
104 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
105 strbuf_addstr(out, ai->ai_canonname);
111 struct hostent *he = gethostbyname(host);
112 if (he && strchr(he->h_name, '.')) {
113 strbuf_addstr(out, he->h_name);
121 static void add_domainname(struct strbuf *out, int *is_bogus)
125 if (gethostname(buf, sizeof(buf))) {
126 warning_errno("cannot get host name");
127 strbuf_addstr(out, "(none)");
131 if (strchr(buf, '.'))
132 strbuf_addstr(out, buf);
133 else if (canonical_name(buf, out) < 0) {
134 strbuf_addf(out, "%s.(none)", buf);
139 static void copy_email(const struct passwd *pw, struct strbuf *email,
143 * Make up a fake email address
144 * (name + '@' + hostname [+ '.' + domainname])
146 strbuf_addstr(email, pw->pw_name);
147 strbuf_addch(email, '@');
149 if (!add_mailname_host(email))
150 return; /* read from "/etc/mailname" (Debian) */
151 add_domainname(email, is_bogus);
154 const char *ident_default_name(void)
156 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
157 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
158 strbuf_trim(&git_default_name);
160 return git_default_name.buf;
163 const char *ident_default_email(void)
165 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
166 const char *email = getenv("EMAIL");
168 if (email && email[0]) {
169 strbuf_addstr(&git_default_email, email);
170 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
171 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
173 copy_email(xgetpwuid_self(&default_email_is_bogus),
174 &git_default_email, &default_email_is_bogus);
175 strbuf_trim(&git_default_email);
177 return git_default_email.buf;
180 static const char *ident_default_date(void)
182 if (!git_default_date.len)
183 datestamp(&git_default_date);
184 return git_default_date.buf;
187 void reset_ident_date(void)
189 strbuf_reset(&git_default_date);
192 static int crud(unsigned char c)
206 static int has_non_crud(const char *str)
208 for (; *str; str++) {
216 * Copy over a string to the destination, but avoid special
217 * characters ('\n', '<' and '>') and remove crud at the end
219 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
224 /* Remove crud from the beginning.. */
225 while ((c = *src) != 0) {
231 /* Remove crud from the end.. */
241 * Copy the rest to the buffer, but avoid the special
242 * characters '\n' '<' and '>' that act as delimiters on
243 * an identification line. We can only remove crud, never add it,
244 * so 'len' is our maximum.
246 strbuf_grow(sb, len);
247 for (i = 0; i < len; i++) {
250 case '\n': case '<': case '>':
253 sb->buf[sb->len++] = c;
255 sb->buf[sb->len] = '\0';
259 * Reverse of fmt_ident(); given an ident line, split the fields
260 * to allow the caller to parse it.
261 * Signal a success by returning 0, but date/tz fields of the result
262 * can still be NULL if the input line only has the name/email part
263 * (e.g. reading from a reflog entry).
265 int split_ident_line(struct ident_split *split, const char *line, int len)
271 memset(split, 0, sizeof(*split));
273 split->name_begin = line;
274 for (cp = line; *cp && cp < line + len; cp++)
276 split->mail_begin = cp + 1;
279 if (!split->mail_begin)
282 for (cp = split->mail_begin - 2; line <= cp; cp--)
284 split->name_end = cp + 1;
287 if (!split->name_end) {
288 /* no human readable name */
289 split->name_end = split->name_begin;
292 for (cp = split->mail_begin; cp < line + len; cp++)
294 split->mail_end = cp;
297 if (!split->mail_end)
301 * Look from the end-of-line to find the trailing ">" of the mail
302 * address, even though we should already know it as split->mail_end.
303 * This can help in cases of broken idents with an extra ">" somewhere
304 * in the email address. Note that we are assuming the timestamp will
305 * never have a ">" in it.
307 * Note that we will always find some ">" before going off the front of
308 * the string, because will always hit the split->mail_end closing
311 for (cp = line + len - 1; *cp != '>'; cp--)
314 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
316 if (line + len <= cp)
318 split->date_begin = cp;
319 span = strspn(cp, "0123456789");
322 split->date_end = split->date_begin + span;
323 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
325 if (line + len <= cp || (*cp != '+' && *cp != '-'))
327 split->tz_begin = cp;
328 span = strspn(cp + 1, "0123456789");
331 split->tz_end = split->tz_begin + 1 + span;
335 split->date_begin = NULL;
336 split->date_end = NULL;
337 split->tz_begin = NULL;
338 split->tz_end = NULL;
342 static const char *env_hint =
344 "*** Please tell me who you are.\n"
348 " git config --global user.email \"you@example.com\"\n"
349 " git config --global user.name \"Your Name\"\n"
351 "to set your account\'s default identity.\n"
352 "Omit --global to set the identity only in this repository.\n"
355 const char *fmt_ident(const char *name, const char *email,
356 const char *date_str, int flag)
358 static struct strbuf ident = STRBUF_INIT;
359 int strict = (flag & IDENT_STRICT);
360 int want_date = !(flag & IDENT_NO_DATE);
361 int want_name = !(flag & IDENT_NO_NAME);
364 if (strict && ident_use_config_only
365 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
366 fputs(_(env_hint), stderr);
367 die(_("no email was given and auto-detection is disabled"));
369 email = ident_default_email();
370 if (strict && default_email_is_bogus) {
371 fputs(_(env_hint), stderr);
372 die(_("unable to auto-detect email address (got '%s')"), email);
377 int using_default = 0;
379 if (strict && ident_use_config_only
380 && !(ident_config_given & IDENT_NAME_GIVEN)) {
381 fputs(_(env_hint), stderr);
382 die(_("no name was given and auto-detection is disabled"));
384 name = ident_default_name();
386 if (strict && default_name_is_bogus) {
387 fputs(_(env_hint), stderr);
388 die(_("unable to auto-detect name (got '%s')"), name);
395 fputs(_(env_hint), stderr);
396 die(_("empty ident name (for <%s>) not allowed"), email);
398 pw = xgetpwuid_self(NULL);
401 if (strict && !has_non_crud(name))
402 die(_("name consists only of disallowed characters: %s"), name);
405 strbuf_reset(&ident);
407 strbuf_addstr_without_crud(&ident, name);
408 strbuf_addstr(&ident, " <");
410 strbuf_addstr_without_crud(&ident, email);
412 strbuf_addch(&ident, '>');
414 strbuf_addch(&ident, ' ');
415 if (date_str && date_str[0]) {
416 if (parse_date(date_str, &ident) < 0)
417 die(_("invalid date format: %s"), date_str);
420 strbuf_addstr(&ident, ident_default_date());
426 const char *fmt_name(const char *name, const char *email)
428 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
431 const char *git_author_info(int flag)
433 if (getenv("GIT_AUTHOR_NAME"))
434 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
435 if (getenv("GIT_AUTHOR_EMAIL"))
436 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
437 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
438 getenv("GIT_AUTHOR_EMAIL"),
439 getenv("GIT_AUTHOR_DATE"),
443 const char *git_committer_info(int flag)
445 if (getenv("GIT_COMMITTER_NAME"))
446 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
447 if (getenv("GIT_COMMITTER_EMAIL"))
448 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
449 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
450 getenv("GIT_COMMITTER_EMAIL"),
451 getenv("GIT_COMMITTER_DATE"),
455 static int ident_is_sufficient(int user_ident_explicitly_given)
458 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
460 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
464 int committer_ident_sufficiently_given(void)
466 return ident_is_sufficient(committer_ident_explicitly_given);
469 int author_ident_sufficiently_given(void)
471 return ident_is_sufficient(author_ident_explicitly_given);
474 int git_ident_config(const char *var, const char *value, void *data)
476 if (!strcmp(var, "user.useconfigonly")) {
477 ident_use_config_only = git_config_bool(var, value);
481 if (!strcmp(var, "user.name")) {
483 return config_error_nonbool(var);
484 strbuf_reset(&git_default_name);
485 strbuf_addstr(&git_default_name, value);
486 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
487 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
488 ident_config_given |= IDENT_NAME_GIVEN;
492 if (!strcmp(var, "user.email")) {
494 return config_error_nonbool(var);
495 strbuf_reset(&git_default_email);
496 strbuf_addstr(&git_default_email, value);
497 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
498 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
499 ident_config_given |= IDENT_MAIL_GIVEN;
506 static int buf_cmp(const char *a_begin, const char *a_end,
507 const char *b_begin, const char *b_end)
509 int a_len = a_end - a_begin;
510 int b_len = b_end - b_begin;
511 int min = a_len < b_len ? a_len : b_len;
514 cmp = memcmp(a_begin, b_begin, min);
518 return a_len - b_len;
521 int ident_cmp(const struct ident_split *a,
522 const struct ident_split *b)
526 cmp = buf_cmp(a->mail_begin, a->mail_end,
527 b->mail_begin, b->mail_end);
531 return buf_cmp(a->name_begin, a->name_end,
532 b->name_begin, b->name_end);