ident: keep a flag for bogus default_email
[git/git.git] / ident.c
CommitLineData
6aa33f40
LT
1/*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8#include "cache.h"
9
8587ead7
JK
10static struct strbuf git_default_name = STRBUF_INIT;
11static struct strbuf git_default_email = STRBUF_INIT;
c33ddc2e 12static struct strbuf git_default_date = STRBUF_INIT;
19ce497c 13static int default_email_is_bogus;
45280230
JK
14
15#define IDENT_NAME_GIVEN 01
16#define IDENT_MAIL_GIVEN 02
17#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
d6991cee
JK
18static int committer_ident_explicitly_given;
19static int author_ident_explicitly_given;
6aa33f40 20
590e081d
RG
21#ifdef NO_GECOS_IN_PWENT
22#define get_gecos(ignored) "&"
23#else
24#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
25#endif
26
e850194c
JK
27static struct passwd *xgetpwuid_self(void)
28{
29 struct passwd *pw;
30
31 errno = 0;
32 pw = getpwuid(getuid());
33 if (!pw)
34 die(_("unable to look up current user in the passwd file: %s"),
35 errno ? strerror(errno) : _("no such user"));
36 return pw;
37}
38
8587ead7 39static void copy_gecos(const struct passwd *w, struct strbuf *name)
e9bacb4f 40{
8587ead7 41 char *src;
e9bacb4f
JH
42
43 /* Traditionally GECOS field had office phone numbers etc, separated
44 * with commas. Also & stands for capitalized form of the login name.
45 */
46
8587ead7 47 for (src = get_gecos(w); *src && *src != ','; src++) {
e9bacb4f 48 int ch = *src;
8587ead7
JK
49 if (ch != '&')
50 strbuf_addch(name, ch);
51 else {
e9bacb4f 52 /* Sorry, Mr. McDonald... */
8587ead7
JK
53 strbuf_addch(name, toupper(*w->pw_name));
54 strbuf_addstr(name, w->pw_name + 1);
e9bacb4f
JH
55 }
56 }
e9bacb4f
JH
57}
58
8587ead7 59static int add_mailname_host(struct strbuf *buf)
8a55caa8
JN
60{
61 FILE *mailname;
dc342a25 62 struct strbuf mailnamebuf = STRBUF_INIT;
8a55caa8
JN
63
64 mailname = fopen("/etc/mailname", "r");
65 if (!mailname) {
66 if (errno != ENOENT)
67 warning("cannot open /etc/mailname: %s",
68 strerror(errno));
69 return -1;
70 }
dc342a25 71 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
8a55caa8
JN
72 if (ferror(mailname))
73 warning("cannot read /etc/mailname: %s",
74 strerror(errno));
dc342a25 75 strbuf_release(&mailnamebuf);
8a55caa8
JN
76 fclose(mailname);
77 return -1;
78 }
79 /* success! */
dc342a25
JN
80 strbuf_addbuf(buf, &mailnamebuf);
81 strbuf_release(&mailnamebuf);
8a55caa8
JN
82 fclose(mailname);
83 return 0;
84}
85
19ce497c 86static void add_domainname(struct strbuf *out, int *is_bogus)
8a55caa8 87{
8587ead7 88 char buf[1024];
8a55caa8 89 struct hostent *he;
8a55caa8 90
8587ead7 91 if (gethostname(buf, sizeof(buf))) {
8a55caa8 92 warning("cannot get host name: %s", strerror(errno));
8587ead7 93 strbuf_addstr(out, "(none)");
19ce497c 94 *is_bogus = 1;
8a55caa8
JN
95 return;
96 }
8587ead7 97 if (strchr(buf, '.'))
f8254d32
JK
98 strbuf_addstr(out, buf);
99 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
100 strbuf_addstr(out, he->h_name);
19ce497c 101 else {
f8254d32 102 strbuf_addf(out, "%s.(none)", buf);
19ce497c
JK
103 *is_bogus = 1;
104 }
8a55caa8
JN
105}
106
19ce497c
JK
107static void copy_email(const struct passwd *pw, struct strbuf *email,
108 int *is_bogus)
6aa33f40 109{
01754769
JH
110 /*
111 * Make up a fake email address
112 * (name + '@' + hostname [+ '.' + domainname])
113 */
8587ead7
JK
114 strbuf_addstr(email, pw->pw_name);
115 strbuf_addch(email, '@');
116
117 if (!add_mailname_host(email))
8a55caa8 118 return; /* read from "/etc/mailname" (Debian) */
19ce497c 119 add_domainname(email, is_bogus);
01754769
JH
120}
121
9830534e 122const char *ident_default_name(void)
01754769 123{
be641abd 124 if (!git_default_name.len) {
2f705875 125 copy_gecos(xgetpwuid_self(), &git_default_name);
be641abd 126 strbuf_trim(&git_default_name);
01754769 127 }
8587ead7 128 return git_default_name.buf;
bcb2b004 129}
01754769 130
bcb2b004
JK
131const char *ident_default_email(void)
132{
8587ead7 133 if (!git_default_email.len) {
46f74f00
MK
134 const char *email = getenv("EMAIL");
135
99178c83 136 if (email && email[0]) {
8587ead7 137 strbuf_addstr(&git_default_email, email);
d6991cee
JK
138 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
139 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
2f705875 140 } else
19ce497c
JK
141 copy_email(xgetpwuid_self(), &git_default_email,
142 &default_email_is_bogus);
be641abd 143 strbuf_trim(&git_default_email);
01754769 144 }
8587ead7 145 return git_default_email.buf;
6aa33f40
LT
146}
147
dad148c3 148static const char *ident_default_date(void)
6aa33f40 149{
c33ddc2e
JK
150 if (!git_default_date.len)
151 datestamp(&git_default_date);
152 return git_default_date.buf;
6aa33f40
LT
153}
154
155static int crud(unsigned char c)
156{
f64c81d4
AR
157 return c <= 32 ||
158 c == '.' ||
159 c == ',' ||
160 c == ':' ||
161 c == ';' ||
162 c == '<' ||
163 c == '>' ||
164 c == '"' ||
d404bf02 165 c == '\\' ||
f64c81d4 166 c == '\'';
6aa33f40
LT
167}
168
169/*
170 * Copy over a string to the destination, but avoid special
171 * characters ('\n', '<' and '>') and remove crud at the end
172 */
c96f0c8d 173static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 174{
b0732115 175 size_t i, len;
6aa33f40
LT
176 unsigned char c;
177
178 /* Remove crud from the beginning.. */
179 while ((c = *src) != 0) {
180 if (!crud(c))
181 break;
182 src++;
183 }
184
185 /* Remove crud from the end.. */
186 len = strlen(src);
187 while (len > 0) {
188 c = src[len-1];
189 if (!crud(c))
190 break;
191 --len;
192 }
193
194 /*
195 * Copy the rest to the buffer, but avoid the special
82f9d58a 196 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
197 * an identification line. We can only remove crud, never add it,
198 * so 'len' is our maximum.
6aa33f40 199 */
c96f0c8d 200 strbuf_grow(sb, len);
6aa33f40
LT
201 for (i = 0; i < len; i++) {
202 c = *src++;
203 switch (c) {
204 case '\n': case '<': case '>':
205 continue;
206 }
c96f0c8d 207 sb->buf[sb->len++] = c;
6aa33f40 208 }
c96f0c8d 209 sb->buf[sb->len] = '\0';
6aa33f40
LT
210}
211
4b340cfa
JH
212/*
213 * Reverse of fmt_ident(); given an ident line, split the fields
214 * to allow the caller to parse it.
215 * Signal a success by returning 0, but date/tz fields of the result
216 * can still be NULL if the input line only has the name/email part
217 * (e.g. reading from a reflog entry).
218 */
219int split_ident_line(struct ident_split *split, const char *line, int len)
220{
221 const char *cp;
222 size_t span;
223 int status = -1;
224
225 memset(split, 0, sizeof(*split));
226
227 split->name_begin = line;
228 for (cp = line; *cp && cp < line + len; cp++)
229 if (*cp == '<') {
230 split->mail_begin = cp + 1;
231 break;
232 }
233 if (!split->mail_begin)
234 return status;
235
d9955fd6 236 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
237 if (!isspace(*cp)) {
238 split->name_end = cp + 1;
239 break;
240 }
e27ddb64
JH
241 if (!split->name_end) {
242 /* no human readable name */
243 split->name_end = split->name_begin;
244 }
4b340cfa
JH
245
246 for (cp = split->mail_begin; cp < line + len; cp++)
247 if (*cp == '>') {
248 split->mail_end = cp;
249 break;
250 }
251 if (!split->mail_end)
252 return status;
253
03818a4a
JK
254 /*
255 * Look from the end-of-line to find the trailing ">" of the mail
256 * address, even though we should already know it as split->mail_end.
257 * This can help in cases of broken idents with an extra ">" somewhere
258 * in the email address. Note that we are assuming the timestamp will
259 * never have a ">" in it.
260 *
261 * Note that we will always find some ">" before going off the front of
262 * the string, because will always hit the split->mail_end closing
263 * bracket.
264 */
265 for (cp = line + len - 1; *cp != '>'; cp--)
266 ;
267
268 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
4b340cfa
JH
269 ;
270 if (line + len <= cp)
271 goto person_only;
272 split->date_begin = cp;
273 span = strspn(cp, "0123456789");
274 if (!span)
275 goto person_only;
276 split->date_end = split->date_begin + span;
277 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
278 ;
279 if (line + len <= cp || (*cp != '+' && *cp != '-'))
280 goto person_only;
281 split->tz_begin = cp;
282 span = strspn(cp + 1, "0123456789");
283 if (!span)
284 goto person_only;
285 split->tz_end = split->tz_begin + 1 + span;
286 return 0;
287
288person_only:
289 split->date_begin = NULL;
290 split->date_end = NULL;
291 split->tz_begin = NULL;
292 split->tz_end = NULL;
293 return 0;
294}
295
749be728 296static const char *env_hint =
d5cc2de9 297"\n"
6c293d40 298"*** Please tell me who you are.\n"
d5cc2de9
HWN
299"\n"
300"Run\n"
301"\n"
8e7425da 302" git config --global user.email \"you@example.com\"\n"
180787c4 303" git config --global user.name \"Your Name\"\n"
d5cc2de9 304"\n"
180787c4
SP
305"to set your account\'s default identity.\n"
306"Omit --global to set the identity only in this repository.\n"
d5cc2de9 307"\n";
749be728 308
774751a8
JH
309const char *fmt_ident(const char *name, const char *email,
310 const char *date_str, int flag)
6aa33f40 311{
c96f0c8d 312 static struct strbuf ident = STRBUF_INIT;
f9bc573f 313 int strict = (flag & IDENT_STRICT);
359b27ad 314 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 315 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 316
c15e1987 317 if (want_name && !name)
bcb2b004
JK
318 name = ident_default_name();
319 if (!email)
320 email = ident_default_email();
dfdd309e 321
c15e1987 322 if (want_name && !*name) {
cb280e10
JH
323 struct passwd *pw;
324
f9bc573f 325 if (strict) {
8587ead7 326 if (name == git_default_name.buf)
b9f0ac17 327 fputs(env_hint, stderr);
b00f6cfc 328 die("empty ident name (for <%s>) not allowed", email);
749be728 329 }
2f705875 330 pw = xgetpwuid_self();
060d4bb3 331 name = pw->pw_name;
749be728 332 }
dfdd309e 333
19ce497c 334 if (strict && email == git_default_email.buf && default_email_is_bogus) {
8c5b1ae1
JK
335 fputs(env_hint, stderr);
336 die("unable to auto-detect email address (got '%s')", email);
749be728 337 }
dfdd309e 338
c96f0c8d 339 strbuf_reset(&ident);
c15e1987
JK
340 if (want_name) {
341 strbuf_addstr_without_crud(&ident, name);
342 strbuf_addstr(&ident, " <");
d9ccfe77 343 }
c96f0c8d 344 strbuf_addstr_without_crud(&ident, email);
c15e1987
JK
345 if (want_name)
346 strbuf_addch(&ident, '>');
359b27ad 347 if (want_date) {
c96f0c8d 348 strbuf_addch(&ident, ' ');
c33ddc2e
JK
349 if (date_str && date_str[0]) {
350 if (parse_date(date_str, &ident) < 0)
351 die("invalid date format: %s", date_str);
352 }
353 else
354 strbuf_addstr(&ident, ident_default_date());
d9ccfe77 355 }
c33ddc2e 356
c96f0c8d 357 return ident.buf;
6aa33f40 358}
d289d136 359
d9ccfe77
JH
360const char *fmt_name(const char *name, const char *email)
361{
f9bc573f 362 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
363}
364
774751a8 365const char *git_author_info(int flag)
d289d136 366{
d6991cee
JK
367 if (getenv("GIT_AUTHOR_NAME"))
368 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
369 if (getenv("GIT_AUTHOR_EMAIL"))
370 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 371 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 372 getenv("GIT_AUTHOR_EMAIL"),
749be728 373 getenv("GIT_AUTHOR_DATE"),
774751a8 374 flag);
d289d136
EB
375}
376
774751a8 377const char *git_committer_info(int flag)
d289d136 378{
91c38a21 379 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 380 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 381 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 382 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 383 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 384 getenv("GIT_COMMITTER_EMAIL"),
749be728 385 getenv("GIT_COMMITTER_DATE"),
774751a8 386 flag);
d289d136 387}
5aeb3a3a 388
d6991cee 389static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
390{
391#ifndef WINDOWS
392 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
393#else
394 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
395#endif
396}
9597921b 397
d6991cee
JK
398int committer_ident_sufficiently_given(void)
399{
400 return ident_is_sufficient(committer_ident_explicitly_given);
401}
402
403int author_ident_sufficiently_given(void)
404{
405 return ident_is_sufficient(author_ident_explicitly_given);
406}
407
9597921b
JK
408int git_ident_config(const char *var, const char *value, void *data)
409{
410 if (!strcmp(var, "user.name")) {
411 if (!value)
412 return config_error_nonbool(var);
8587ead7
JK
413 strbuf_reset(&git_default_name);
414 strbuf_addstr(&git_default_name, value);
d6991cee
JK
415 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
416 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
9597921b
JK
417 return 0;
418 }
419
420 if (!strcmp(var, "user.email")) {
421 if (!value)
422 return config_error_nonbool(var);
8587ead7
JK
423 strbuf_reset(&git_default_email);
424 strbuf_addstr(&git_default_email, value);
d6991cee
JK
425 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
426 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
9597921b
JK
427 return 0;
428 }
429
430 return 0;
431}
662cc30c
JK
432
433static int buf_cmp(const char *a_begin, const char *a_end,
434 const char *b_begin, const char *b_end)
435{
436 int a_len = a_end - a_begin;
437 int b_len = b_end - b_begin;
438 int min = a_len < b_len ? a_len : b_len;
439 int cmp;
440
441 cmp = memcmp(a_begin, b_begin, min);
442 if (cmp)
443 return cmp;
444
445 return a_len - b_len;
446}
447
448int ident_cmp(const struct ident_split *a,
449 const struct ident_split *b)
450{
451 int cmp;
452
453 cmp = buf_cmp(a->mail_begin, a->mail_end,
454 b->mail_begin, b->mail_end);
455 if (cmp)
456 return cmp;
457
458 return buf_cmp(a->name_begin, a->name_end,
459 b->name_begin, b->name_end);
460}