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