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