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;
14 #define IDENT_NAME_GIVEN 01
15 #define IDENT_MAIL_GIVEN 02
16 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
17 static int committer_ident_explicitly_given;
18 static int author_ident_explicitly_given;
20 #ifdef NO_GECOS_IN_PWENT
21 #define get_gecos(ignored) "&"
23 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
26 static void copy_gecos(const struct passwd *w, struct strbuf *name)
30 /* Traditionally GECOS field had office phone numbers etc, separated
31 * with commas. Also & stands for capitalized form of the login name.
34 for (src = get_gecos(w); *src && *src != ','; src++) {
37 strbuf_addch(name, ch);
39 /* Sorry, Mr. McDonald... */
40 strbuf_addch(name, toupper(*w->pw_name));
41 strbuf_addstr(name, w->pw_name + 1);
46 static int add_mailname_host(struct strbuf *buf)
49 struct strbuf mailnamebuf = STRBUF_INIT;
51 mailname = fopen("/etc/mailname", "r");
54 warning("cannot open /etc/mailname: %s",
58 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
60 warning("cannot read /etc/mailname: %s",
62 strbuf_release(&mailnamebuf);
67 strbuf_addbuf(buf, &mailnamebuf);
68 strbuf_release(&mailnamebuf);
73 static int canonical_name(const char *host, struct strbuf *out)
78 struct addrinfo hints, *ai;
79 memset (&hints, '\0', sizeof (hints));
80 hints.ai_flags = AI_CANONNAME;
81 if (!getaddrinfo(host, NULL, &hints, &ai)) {
82 if (ai && strchr(ai->ai_canonname, '.')) {
83 strbuf_addstr(out, ai->ai_canonname);
89 struct hostent *he = gethostbyname(buf);
90 if (he && strchr(he->h_name, '.')) {
91 strbuf_addstr(out, he->h_name);
99 static void add_domainname(struct strbuf *out)
103 if (gethostname(buf, sizeof(buf))) {
104 warning("cannot get host name: %s", strerror(errno));
105 strbuf_addstr(out, "(none)");
108 if (strchr(buf, '.'))
109 strbuf_addstr(out, buf);
110 else if (canonical_name(buf, out) < 0)
111 strbuf_addf(out, "%s.(none)", buf);
114 static void copy_email(const struct passwd *pw, struct strbuf *email)
117 * Make up a fake email address
118 * (name + '@' + hostname [+ '.' + domainname])
120 strbuf_addstr(email, pw->pw_name);
121 strbuf_addch(email, '@');
123 if (!add_mailname_host(email))
124 return; /* read from "/etc/mailname" (Debian) */
125 add_domainname(email);
128 const char *ident_default_name(void)
130 if (!git_default_name.len) {
131 copy_gecos(xgetpwuid_self(), &git_default_name);
132 strbuf_trim(&git_default_name);
134 return git_default_name.buf;
137 const char *ident_default_email(void)
139 if (!git_default_email.len) {
140 const char *email = getenv("EMAIL");
142 if (email && email[0]) {
143 strbuf_addstr(&git_default_email, email);
144 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
145 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
147 copy_email(xgetpwuid_self(), &git_default_email);
148 strbuf_trim(&git_default_email);
150 return git_default_email.buf;
153 static const char *ident_default_date(void)
155 if (!git_default_date.len)
156 datestamp(&git_default_date);
157 return git_default_date.buf;
160 static int crud(unsigned char c)
175 * Copy over a string to the destination, but avoid special
176 * characters ('\n', '<' and '>') and remove crud at the end
178 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
183 /* Remove crud from the beginning.. */
184 while ((c = *src) != 0) {
190 /* Remove crud from the end.. */
200 * Copy the rest to the buffer, but avoid the special
201 * characters '\n' '<' and '>' that act as delimiters on
202 * an identification line. We can only remove crud, never add it,
203 * so 'len' is our maximum.
205 strbuf_grow(sb, len);
206 for (i = 0; i < len; i++) {
209 case '\n': case '<': case '>':
212 sb->buf[sb->len++] = c;
214 sb->buf[sb->len] = '\0';
218 * Reverse of fmt_ident(); given an ident line, split the fields
219 * to allow the caller to parse it.
220 * Signal a success by returning 0, but date/tz fields of the result
221 * can still be NULL if the input line only has the name/email part
222 * (e.g. reading from a reflog entry).
224 int split_ident_line(struct ident_split *split, const char *line, int len)
230 memset(split, 0, sizeof(*split));
232 split->name_begin = line;
233 for (cp = line; *cp && cp < line + len; cp++)
235 split->mail_begin = cp + 1;
238 if (!split->mail_begin)
241 for (cp = split->mail_begin - 2; line <= cp; cp--)
243 split->name_end = cp + 1;
246 if (!split->name_end) {
247 /* no human readable name */
248 split->name_end = split->name_begin;
251 for (cp = split->mail_begin; cp < line + len; cp++)
253 split->mail_end = cp;
256 if (!split->mail_end)
260 * Look from the end-of-line to find the trailing ">" of the mail
261 * address, even though we should already know it as split->mail_end.
262 * This can help in cases of broken idents with an extra ">" somewhere
263 * in the email address. Note that we are assuming the timestamp will
264 * never have a ">" in it.
266 * Note that we will always find some ">" before going off the front of
267 * the string, because will always hit the split->mail_end closing
270 for (cp = line + len - 1; *cp != '>'; cp--)
273 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
275 if (line + len <= cp)
277 split->date_begin = cp;
278 span = strspn(cp, "0123456789");
281 split->date_end = split->date_begin + span;
282 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
284 if (line + len <= cp || (*cp != '+' && *cp != '-'))
286 split->tz_begin = cp;
287 span = strspn(cp + 1, "0123456789");
290 split->tz_end = split->tz_begin + 1 + span;
294 split->date_begin = NULL;
295 split->date_end = NULL;
296 split->tz_begin = NULL;
297 split->tz_end = NULL;
301 static const char *env_hint =
303 "*** Please tell me who you are.\n"
307 " git config --global user.email \"you@example.com\"\n"
308 " git config --global user.name \"Your Name\"\n"
310 "to set your account\'s default identity.\n"
311 "Omit --global to set the identity only in this repository.\n"
314 const char *fmt_ident(const char *name, const char *email,
315 const char *date_str, int flag)
317 static struct strbuf ident = STRBUF_INIT;
318 int strict = (flag & IDENT_STRICT);
319 int want_date = !(flag & IDENT_NO_DATE);
320 int want_name = !(flag & IDENT_NO_NAME);
322 if (want_name && !name)
323 name = ident_default_name();
325 email = ident_default_email();
327 if (want_name && !*name) {
331 if (name == git_default_name.buf)
332 fputs(env_hint, stderr);
333 die("empty ident name (for <%s>) not allowed", email);
335 pw = xgetpwuid_self();
339 if (strict && email == git_default_email.buf &&
340 strstr(email, "(none)")) {
341 fputs(env_hint, stderr);
342 die("unable to auto-detect email address (got '%s')", email);
345 strbuf_reset(&ident);
347 strbuf_addstr_without_crud(&ident, name);
348 strbuf_addstr(&ident, " <");
350 strbuf_addstr_without_crud(&ident, email);
352 strbuf_addch(&ident, '>');
354 strbuf_addch(&ident, ' ');
355 if (date_str && date_str[0]) {
356 if (parse_date(date_str, &ident) < 0)
357 die("invalid date format: %s", date_str);
360 strbuf_addstr(&ident, ident_default_date());
366 const char *fmt_name(const char *name, const char *email)
368 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
371 const char *git_author_info(int flag)
373 if (getenv("GIT_AUTHOR_NAME"))
374 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
375 if (getenv("GIT_AUTHOR_EMAIL"))
376 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
377 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
378 getenv("GIT_AUTHOR_EMAIL"),
379 getenv("GIT_AUTHOR_DATE"),
383 const char *git_committer_info(int flag)
385 if (getenv("GIT_COMMITTER_NAME"))
386 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
387 if (getenv("GIT_COMMITTER_EMAIL"))
388 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
389 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
390 getenv("GIT_COMMITTER_EMAIL"),
391 getenv("GIT_COMMITTER_DATE"),
395 static int ident_is_sufficient(int user_ident_explicitly_given)
398 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
400 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
404 int committer_ident_sufficiently_given(void)
406 return ident_is_sufficient(committer_ident_explicitly_given);
409 int author_ident_sufficiently_given(void)
411 return ident_is_sufficient(author_ident_explicitly_given);
414 int git_ident_config(const char *var, const char *value, void *data)
416 if (!strcmp(var, "user.name")) {
418 return config_error_nonbool(var);
419 strbuf_reset(&git_default_name);
420 strbuf_addstr(&git_default_name, value);
421 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
422 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
426 if (!strcmp(var, "user.email")) {
428 return config_error_nonbool(var);
429 strbuf_reset(&git_default_email);
430 strbuf_addstr(&git_default_email, value);
431 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
432 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
439 static int buf_cmp(const char *a_begin, const char *a_end,
440 const char *b_begin, const char *b_end)
442 int a_len = a_end - a_begin;
443 int b_len = b_end - b_begin;
444 int min = a_len < b_len ? a_len : b_len;
447 cmp = memcmp(a_begin, b_begin, min);
451 return a_len - b_len;
454 int ident_cmp(const struct ident_split *a,
455 const struct ident_split *b)
459 cmp = buf_cmp(a->mail_begin, a->mail_end,
460 b->mail_begin, b->mail_end);
464 return buf_cmp(a->name_begin, a->name_end,
465 b->name_begin, b->name_end);