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_or_warn("/etc/mailname", "r");
79 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
81 warning_errno("cannot read /etc/mailname");
82 strbuf_release(&mailnamebuf);
87 strbuf_addbuf(buf, &mailnamebuf);
88 strbuf_release(&mailnamebuf);
93 static int canonical_name(const char *host, struct strbuf *out)
98 struct addrinfo hints, *ai;
99 memset (&hints, '\0', sizeof (hints));
100 hints.ai_flags = AI_CANONNAME;
101 if (!getaddrinfo(host, NULL, &hints, &ai)) {
102 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
103 strbuf_addstr(out, ai->ai_canonname);
109 struct hostent *he = gethostbyname(host);
110 if (he && strchr(he->h_name, '.')) {
111 strbuf_addstr(out, he->h_name);
119 static void add_domainname(struct strbuf *out, int *is_bogus)
121 char buf[HOST_NAME_MAX + 1];
123 if (xgethostname(buf, sizeof(buf))) {
124 warning_errno("cannot get host name");
125 strbuf_addstr(out, "(none)");
129 if (strchr(buf, '.'))
130 strbuf_addstr(out, buf);
131 else if (canonical_name(buf, out) < 0) {
132 strbuf_addf(out, "%s.(none)", buf);
137 static void copy_email(const struct passwd *pw, struct strbuf *email,
141 * Make up a fake email address
142 * (name + '@' + hostname [+ '.' + domainname])
144 strbuf_addstr(email, pw->pw_name);
145 strbuf_addch(email, '@');
147 if (!add_mailname_host(email))
148 return; /* read from "/etc/mailname" (Debian) */
149 add_domainname(email, is_bogus);
152 const char *ident_default_name(void)
154 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
155 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
156 strbuf_trim(&git_default_name);
158 return git_default_name.buf;
161 const char *ident_default_email(void)
163 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
164 const char *email = getenv("EMAIL");
166 if (email && email[0]) {
167 strbuf_addstr(&git_default_email, email);
168 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
169 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
171 copy_email(xgetpwuid_self(&default_email_is_bogus),
172 &git_default_email, &default_email_is_bogus);
173 strbuf_trim(&git_default_email);
175 return git_default_email.buf;
178 static const char *ident_default_date(void)
180 if (!git_default_date.len)
181 datestamp(&git_default_date);
182 return git_default_date.buf;
185 void reset_ident_date(void)
187 strbuf_reset(&git_default_date);
190 static int crud(unsigned char c)
204 static int has_non_crud(const char *str)
206 for (; *str; str++) {
214 * Copy over a string to the destination, but avoid special
215 * characters ('\n', '<' and '>') and remove crud at the end
217 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
222 /* Remove crud from the beginning.. */
223 while ((c = *src) != 0) {
229 /* Remove crud from the end.. */
239 * Copy the rest to the buffer, but avoid the special
240 * characters '\n' '<' and '>' that act as delimiters on
241 * an identification line. We can only remove crud, never add it,
242 * so 'len' is our maximum.
244 strbuf_grow(sb, len);
245 for (i = 0; i < len; i++) {
248 case '\n': case '<': case '>':
251 sb->buf[sb->len++] = c;
253 sb->buf[sb->len] = '\0';
257 * Reverse of fmt_ident(); given an ident line, split the fields
258 * to allow the caller to parse it.
259 * Signal a success by returning 0, but date/tz fields of the result
260 * can still be NULL if the input line only has the name/email part
261 * (e.g. reading from a reflog entry).
263 int split_ident_line(struct ident_split *split, const char *line, int len)
269 memset(split, 0, sizeof(*split));
271 split->name_begin = line;
272 for (cp = line; *cp && cp < line + len; cp++)
274 split->mail_begin = cp + 1;
277 if (!split->mail_begin)
280 for (cp = split->mail_begin - 2; line <= cp; cp--)
282 split->name_end = cp + 1;
285 if (!split->name_end) {
286 /* no human readable name */
287 split->name_end = split->name_begin;
290 for (cp = split->mail_begin; cp < line + len; cp++)
292 split->mail_end = cp;
295 if (!split->mail_end)
299 * Look from the end-of-line to find the trailing ">" of the mail
300 * address, even though we should already know it as split->mail_end.
301 * This can help in cases of broken idents with an extra ">" somewhere
302 * in the email address. Note that we are assuming the timestamp will
303 * never have a ">" in it.
305 * Note that we will always find some ">" before going off the front of
306 * the string, because will always hit the split->mail_end closing
309 for (cp = line + len - 1; *cp != '>'; cp--)
312 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
314 if (line + len <= cp)
316 split->date_begin = cp;
317 span = strspn(cp, "0123456789");
320 split->date_end = split->date_begin + span;
321 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
323 if (line + len <= cp || (*cp != '+' && *cp != '-'))
325 split->tz_begin = cp;
326 span = strspn(cp + 1, "0123456789");
329 split->tz_end = split->tz_begin + 1 + span;
333 split->date_begin = NULL;
334 split->date_end = NULL;
335 split->tz_begin = NULL;
336 split->tz_end = NULL;
340 static const char *env_hint =
342 "*** Please tell me who you are.\n"
346 " git config --global user.email \"you@example.com\"\n"
347 " git config --global user.name \"Your Name\"\n"
349 "to set your account\'s default identity.\n"
350 "Omit --global to set the identity only in this repository.\n"
353 const char *fmt_ident(const char *name, const char *email,
354 const char *date_str, int flag)
356 static struct strbuf ident = STRBUF_INIT;
357 int strict = (flag & IDENT_STRICT);
358 int want_date = !(flag & IDENT_NO_DATE);
359 int want_name = !(flag & IDENT_NO_NAME);
362 if (strict && ident_use_config_only
363 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
364 fputs(_(env_hint), stderr);
365 die(_("no email was given and auto-detection is disabled"));
367 email = ident_default_email();
368 if (strict && default_email_is_bogus) {
369 fputs(_(env_hint), stderr);
370 die(_("unable to auto-detect email address (got '%s')"), email);
375 int using_default = 0;
377 if (strict && ident_use_config_only
378 && !(ident_config_given & IDENT_NAME_GIVEN)) {
379 fputs(_(env_hint), stderr);
380 die(_("no name was given and auto-detection is disabled"));
382 name = ident_default_name();
384 if (strict && default_name_is_bogus) {
385 fputs(_(env_hint), stderr);
386 die(_("unable to auto-detect name (got '%s')"), name);
393 fputs(_(env_hint), stderr);
394 die(_("empty ident name (for <%s>) not allowed"), email);
396 pw = xgetpwuid_self(NULL);
399 if (strict && !has_non_crud(name))
400 die(_("name consists only of disallowed characters: %s"), name);
403 strbuf_reset(&ident);
405 strbuf_addstr_without_crud(&ident, name);
406 strbuf_addstr(&ident, " <");
408 strbuf_addstr_without_crud(&ident, email);
410 strbuf_addch(&ident, '>');
412 strbuf_addch(&ident, ' ');
413 if (date_str && date_str[0]) {
414 if (parse_date(date_str, &ident) < 0)
415 die(_("invalid date format: %s"), date_str);
418 strbuf_addstr(&ident, ident_default_date());
424 const char *fmt_name(const char *name, const char *email)
426 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
429 const char *git_author_info(int flag)
431 if (getenv("GIT_AUTHOR_NAME"))
432 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
433 if (getenv("GIT_AUTHOR_EMAIL"))
434 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
435 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
436 getenv("GIT_AUTHOR_EMAIL"),
437 getenv("GIT_AUTHOR_DATE"),
441 const char *git_committer_info(int flag)
443 if (getenv("GIT_COMMITTER_NAME"))
444 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
445 if (getenv("GIT_COMMITTER_EMAIL"))
446 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
447 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
448 getenv("GIT_COMMITTER_EMAIL"),
449 getenv("GIT_COMMITTER_DATE"),
453 static int ident_is_sufficient(int user_ident_explicitly_given)
456 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
458 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
462 int committer_ident_sufficiently_given(void)
464 return ident_is_sufficient(committer_ident_explicitly_given);
467 int author_ident_sufficiently_given(void)
469 return ident_is_sufficient(author_ident_explicitly_given);
472 int git_ident_config(const char *var, const char *value, void *data)
474 if (!strcmp(var, "user.useconfigonly")) {
475 ident_use_config_only = git_config_bool(var, value);
479 if (!strcmp(var, "user.name")) {
481 return config_error_nonbool(var);
482 strbuf_reset(&git_default_name);
483 strbuf_addstr(&git_default_name, value);
484 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
485 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
486 ident_config_given |= IDENT_NAME_GIVEN;
490 if (!strcmp(var, "user.email")) {
492 return config_error_nonbool(var);
493 strbuf_reset(&git_default_email);
494 strbuf_addstr(&git_default_email, value);
495 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
496 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
497 ident_config_given |= IDENT_MAIL_GIVEN;
504 static int buf_cmp(const char *a_begin, const char *a_end,
505 const char *b_begin, const char *b_end)
507 int a_len = a_end - a_begin;
508 int b_len = b_end - b_begin;
509 int min = a_len < b_len ? a_len : b_len;
512 cmp = memcmp(a_begin, b_begin, min);
516 return a_len - b_len;
519 int ident_cmp(const struct ident_split *a,
520 const struct ident_split *b)
524 cmp = buf_cmp(a->mail_begin, a->mail_end,
525 b->mail_begin, b->mail_end);
529 return buf_cmp(a->name_begin, a->name_end,
530 b->name_begin, b->name_end);