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