]> git.ipfire.org Git - thirdparty/git.git/blame - ident.c
l10n: zh_CN v2.38.0 rounds 1 & 2
[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"
88c7b4c3 10#include "date.h"
dc88e349 11#include "mailmap.h"
6aa33f40 12
8587ead7
JK
13static struct strbuf git_default_name = STRBUF_INIT;
14static struct strbuf git_default_email = STRBUF_INIT;
c33ddc2e 15static struct strbuf git_default_date = STRBUF_INIT;
39ab4d09
WH
16static struct strbuf git_author_name = STRBUF_INIT;
17static struct strbuf git_author_email = STRBUF_INIT;
18static struct strbuf git_committer_name = STRBUF_INIT;
19static struct strbuf git_committer_email = STRBUF_INIT;
19ce497c 20static int default_email_is_bogus;
92bcbb9b 21static int default_name_is_bogus;
45280230 22
4d5c2956
DA
23static int ident_use_config_only;
24
45280230
JK
25#define IDENT_NAME_GIVEN 01
26#define IDENT_MAIL_GIVEN 02
27#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
d6991cee
JK
28static int committer_ident_explicitly_given;
29static int author_ident_explicitly_given;
4d5c2956 30static int ident_config_given;
6aa33f40 31
590e081d
RG
32#ifdef NO_GECOS_IN_PWENT
33#define get_gecos(ignored) "&"
34#else
35#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
36#endif
37
92bcbb9b 38static struct passwd *xgetpwuid_self(int *is_bogus)
e850194c
JK
39{
40 struct passwd *pw;
41
42 errno = 0;
43 pw = getpwuid(getuid());
92bcbb9b
JK
44 if (!pw) {
45 static struct passwd fallback;
46 fallback.pw_name = "unknown";
47#ifndef NO_GECOS_IN_PWENT
48 fallback.pw_gecos = "Unknown";
49#endif
50 pw = &fallback;
51 if (is_bogus)
52 *is_bogus = 1;
53 }
e850194c
JK
54 return pw;
55}
56
8587ead7 57static void copy_gecos(const struct passwd *w, struct strbuf *name)
e9bacb4f 58{
8587ead7 59 char *src;
e9bacb4f
JH
60
61 /* Traditionally GECOS field had office phone numbers etc, separated
62 * with commas. Also & stands for capitalized form of the login name.
63 */
64
8587ead7 65 for (src = get_gecos(w); *src && *src != ','; src++) {
e9bacb4f 66 int ch = *src;
8587ead7
JK
67 if (ch != '&')
68 strbuf_addch(name, ch);
69 else {
e9bacb4f 70 /* Sorry, Mr. McDonald... */
8587ead7
JK
71 strbuf_addch(name, toupper(*w->pw_name));
72 strbuf_addstr(name, w->pw_name + 1);
e9bacb4f
JH
73 }
74 }
e9bacb4f
JH
75}
76
8587ead7 77static int add_mailname_host(struct strbuf *buf)
8a55caa8
JN
78{
79 FILE *mailname;
dc342a25 80 struct strbuf mailnamebuf = STRBUF_INIT;
8a55caa8 81
e9d983f1
NTND
82 mailname = fopen_or_warn("/etc/mailname", "r");
83 if (!mailname)
8a55caa8 84 return -1;
e9d983f1 85
1f3b1efd 86 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
8a55caa8 87 if (ferror(mailname))
a26f4ed6 88 warning_errno("cannot read /etc/mailname");
dc342a25 89 strbuf_release(&mailnamebuf);
8a55caa8
JN
90 fclose(mailname);
91 return -1;
92 }
93 /* success! */
dc342a25
JN
94 strbuf_addbuf(buf, &mailnamebuf);
95 strbuf_release(&mailnamebuf);
8a55caa8
JN
96 fclose(mailname);
97 return 0;
98}
99
00bce77f
EP
100static int canonical_name(const char *host, struct strbuf *out)
101{
102 int status = -1;
103
104#ifndef NO_IPV6
105 struct addrinfo hints, *ai;
106 memset (&hints, '\0', sizeof (hints));
107 hints.ai_flags = AI_CANONNAME;
108 if (!getaddrinfo(host, NULL, &hints, &ai)) {
c375a7ef 109 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
00bce77f
EP
110 strbuf_addstr(out, ai->ai_canonname);
111 status = 0;
112 }
113 freeaddrinfo(ai);
114 }
115#else
58d29ece 116 struct hostent *he = gethostbyname(host);
00bce77f
EP
117 if (he && strchr(he->h_name, '.')) {
118 strbuf_addstr(out, he->h_name);
119 status = 0;
120 }
121#endif /* NO_IPV6 */
122
123 return status;
124}
125
19ce497c 126static void add_domainname(struct strbuf *out, int *is_bogus)
8a55caa8 127{
da25bdb7 128 char buf[HOST_NAME_MAX + 1];
8a55caa8 129
5781a9a2 130 if (xgethostname(buf, sizeof(buf))) {
a26f4ed6 131 warning_errno("cannot get host name");
8587ead7 132 strbuf_addstr(out, "(none)");
19ce497c 133 *is_bogus = 1;
8a55caa8
JN
134 return;
135 }
8587ead7 136 if (strchr(buf, '.'))
f8254d32 137 strbuf_addstr(out, buf);
5498c57c 138 else if (canonical_name(buf, out) < 0) {
f8254d32 139 strbuf_addf(out, "%s.(none)", buf);
19ce497c
JK
140 *is_bogus = 1;
141 }
8a55caa8
JN
142}
143
19ce497c
JK
144static void copy_email(const struct passwd *pw, struct strbuf *email,
145 int *is_bogus)
6aa33f40 146{
01754769
JH
147 /*
148 * Make up a fake email address
149 * (name + '@' + hostname [+ '.' + domainname])
150 */
8587ead7
JK
151 strbuf_addstr(email, pw->pw_name);
152 strbuf_addch(email, '@');
153
154 if (!add_mailname_host(email))
8a55caa8 155 return; /* read from "/etc/mailname" (Debian) */
19ce497c 156 add_domainname(email, is_bogus);
01754769
JH
157}
158
9830534e 159const char *ident_default_name(void)
01754769 160{
94425552 161 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
92bcbb9b 162 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
be641abd 163 strbuf_trim(&git_default_name);
01754769 164 }
8587ead7 165 return git_default_name.buf;
bcb2b004 166}
01754769 167
bcb2b004
JK
168const char *ident_default_email(void)
169{
94425552 170 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
46f74f00
MK
171 const char *email = getenv("EMAIL");
172
99178c83 173 if (email && email[0]) {
8587ead7 174 strbuf_addstr(&git_default_email, email);
d6991cee
JK
175 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
176 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
501afcb8
JS
177 } else if ((email = query_user_email()) && email[0]) {
178 strbuf_addstr(&git_default_email, email);
179 free((char *)email);
2f705875 180 } else
92bcbb9b
JK
181 copy_email(xgetpwuid_self(&default_email_is_bogus),
182 &git_default_email, &default_email_is_bogus);
be641abd 183 strbuf_trim(&git_default_email);
01754769 184 }
8587ead7 185 return git_default_email.buf;
6aa33f40
LT
186}
187
dad148c3 188static const char *ident_default_date(void)
6aa33f40 189{
c33ddc2e
JK
190 if (!git_default_date.len)
191 datestamp(&git_default_date);
192 return git_default_date.buf;
6aa33f40
LT
193}
194
4d9c7e6f
JK
195void reset_ident_date(void)
196{
197 strbuf_reset(&git_default_date);
198}
199
6aa33f40
LT
200static int crud(unsigned char c)
201{
f64c81d4
AR
202 return c <= 32 ||
203 c == '.' ||
204 c == ',' ||
205 c == ':' ||
206 c == ';' ||
207 c == '<' ||
208 c == '>' ||
209 c == '"' ||
d404bf02 210 c == '\\' ||
f64c81d4 211 c == '\'';
6aa33f40
LT
212}
213
13b9a24e
JK
214static int has_non_crud(const char *str)
215{
216 for (; *str; str++) {
217 if (!crud(*str))
218 return 1;
219 }
220 return 0;
221}
222
6aa33f40
LT
223/*
224 * Copy over a string to the destination, but avoid special
225 * characters ('\n', '<' and '>') and remove crud at the end
226 */
c96f0c8d 227static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 228{
b0732115 229 size_t i, len;
6aa33f40
LT
230 unsigned char c;
231
232 /* Remove crud from the beginning.. */
233 while ((c = *src) != 0) {
234 if (!crud(c))
235 break;
236 src++;
237 }
238
239 /* Remove crud from the end.. */
240 len = strlen(src);
241 while (len > 0) {
242 c = src[len-1];
243 if (!crud(c))
244 break;
245 --len;
246 }
247
248 /*
249 * Copy the rest to the buffer, but avoid the special
82f9d58a 250 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
251 * an identification line. We can only remove crud, never add it,
252 * so 'len' is our maximum.
6aa33f40 253 */
c96f0c8d 254 strbuf_grow(sb, len);
6aa33f40
LT
255 for (i = 0; i < len; i++) {
256 c = *src++;
257 switch (c) {
258 case '\n': case '<': case '>':
259 continue;
260 }
c96f0c8d 261 sb->buf[sb->len++] = c;
6aa33f40 262 }
c96f0c8d 263 sb->buf[sb->len] = '\0';
6aa33f40
LT
264}
265
4b340cfa
JH
266/*
267 * Reverse of fmt_ident(); given an ident line, split the fields
268 * to allow the caller to parse it.
269 * Signal a success by returning 0, but date/tz fields of the result
270 * can still be NULL if the input line only has the name/email part
271 * (e.g. reading from a reflog entry).
272 */
273int split_ident_line(struct ident_split *split, const char *line, int len)
274{
275 const char *cp;
276 size_t span;
277 int status = -1;
278
279 memset(split, 0, sizeof(*split));
280
281 split->name_begin = line;
282 for (cp = line; *cp && cp < line + len; cp++)
283 if (*cp == '<') {
284 split->mail_begin = cp + 1;
285 break;
286 }
287 if (!split->mail_begin)
288 return status;
289
d9955fd6 290 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
291 if (!isspace(*cp)) {
292 split->name_end = cp + 1;
293 break;
294 }
e27ddb64
JH
295 if (!split->name_end) {
296 /* no human readable name */
297 split->name_end = split->name_begin;
298 }
4b340cfa
JH
299
300 for (cp = split->mail_begin; cp < line + len; cp++)
301 if (*cp == '>') {
302 split->mail_end = cp;
303 break;
304 }
305 if (!split->mail_end)
306 return status;
307
03818a4a
JK
308 /*
309 * Look from the end-of-line to find the trailing ">" of the mail
310 * address, even though we should already know it as split->mail_end.
311 * This can help in cases of broken idents with an extra ">" somewhere
312 * in the email address. Note that we are assuming the timestamp will
313 * never have a ">" in it.
314 *
315 * Note that we will always find some ">" before going off the front of
316 * the string, because will always hit the split->mail_end closing
317 * bracket.
318 */
319 for (cp = line + len - 1; *cp != '>'; cp--)
320 ;
321
322 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
4b340cfa
JH
323 ;
324 if (line + len <= cp)
325 goto person_only;
326 split->date_begin = cp;
327 span = strspn(cp, "0123456789");
328 if (!span)
329 goto person_only;
330 split->date_end = split->date_begin + span;
331 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
332 ;
333 if (line + len <= cp || (*cp != '+' && *cp != '-'))
334 goto person_only;
335 split->tz_begin = cp;
336 span = strspn(cp + 1, "0123456789");
337 if (!span)
338 goto person_only;
339 split->tz_end = split->tz_begin + 1 + span;
340 return 0;
341
342person_only:
343 split->date_begin = NULL;
344 split->date_end = NULL;
345 split->tz_begin = NULL;
346 split->tz_end = NULL;
347 return 0;
348}
349
dc88e349
SA
350/*
351 * Returns the difference between the new and old length of the ident line.
352 */
353static ssize_t rewrite_ident_line(const char *person, size_t len,
354 struct strbuf *buf,
355 struct string_list *mailmap)
356{
357 size_t namelen, maillen;
358 const char *name;
359 const char *mail;
360 struct ident_split ident;
361
362 if (split_ident_line(&ident, person, len))
363 return 0;
364
365 mail = ident.mail_begin;
366 maillen = ident.mail_end - ident.mail_begin;
367 name = ident.name_begin;
368 namelen = ident.name_end - ident.name_begin;
369
370 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
371 struct strbuf namemail = STRBUF_INIT;
372 size_t newlen;
373
374 strbuf_addf(&namemail, "%.*s <%.*s>",
375 (int)namelen, name, (int)maillen, mail);
376
377 strbuf_splice(buf, ident.name_begin - buf->buf,
378 ident.mail_end - ident.name_begin + 1,
379 namemail.buf, namemail.len);
380 newlen = namemail.len;
381
382 strbuf_release(&namemail);
383
384 return newlen - (ident.mail_end - ident.name_begin);
385 }
386
387 return 0;
388}
389
66a8a953
SA
390void apply_mailmap_to_header(struct strbuf *buf, const char **header,
391 struct string_list *mailmap)
dc88e349
SA
392{
393 size_t buf_offset = 0;
394
395 if (!mailmap)
396 return;
397
398 for (;;) {
399 const char *person, *line;
400 size_t i;
401 int found_header = 0;
402
403 line = buf->buf + buf_offset;
404 if (!*line || *line == '\n')
405 return; /* End of headers */
406
407 for (i = 0; header[i]; i++)
408 if (skip_prefix(line, header[i], &person)) {
409 const char *endp = strchrnul(person, '\n');
410 found_header = 1;
411 buf_offset += endp - line;
412 buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
413 break;
414 }
415
416 if (!found_header) {
417 buf_offset = strchrnul(line, '\n') - buf->buf;
418 if (buf->buf[buf_offset] == '\n')
419 buf_offset++;
420 }
421 }
422}
9ed104e5
JH
423
424static void ident_env_hint(enum want_ident whose_ident)
425{
426 switch (whose_ident) {
427 case WANT_AUTHOR_IDENT:
428 fputs(_("Author identity unknown\n"), stderr);
429 break;
430 case WANT_COMMITTER_IDENT:
431 fputs(_("Committer identity unknown\n"), stderr);
432 break;
433 default:
434 break;
435 }
436
437 fputs(_("\n"
438 "*** Please tell me who you are.\n"
439 "\n"
440 "Run\n"
441 "\n"
442 " git config --global user.email \"you@example.com\"\n"
443 " git config --global user.name \"Your Name\"\n"
444 "\n"
445 "to set your account\'s default identity.\n"
446 "Omit --global to set the identity only in this repository.\n"
447 "\n"), stderr);
448}
749be728 449
774751a8 450const char *fmt_ident(const char *name, const char *email,
39ab4d09 451 enum want_ident whose_ident, const char *date_str, int flag)
6aa33f40 452{
e8cbe211
PW
453 static int index;
454 static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
f9bc573f 455 int strict = (flag & IDENT_STRICT);
359b27ad 456 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 457 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 458
e8cbe211
PW
459 struct strbuf *ident = &ident_pool[index];
460 index = (index + 1) % ARRAY_SIZE(ident_pool);
461
39ab4d09
WH
462 if (!email) {
463 if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
464 email = git_author_email.buf;
465 else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
466 email = git_committer_email.buf;
467 }
862e80a4
JK
468 if (!email) {
469 if (strict && ident_use_config_only
470 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
9ed104e5 471 ident_env_hint(whose_ident);
862e80a4
JK
472 die(_("no email was given and auto-detection is disabled"));
473 }
474 email = ident_default_email();
475 if (strict && default_email_is_bogus) {
9ed104e5 476 ident_env_hint(whose_ident);
862e80a4
JK
477 die(_("unable to auto-detect email address (got '%s')"), email);
478 }
479 }
480
59f92959
JK
481 if (want_name) {
482 int using_default = 0;
39ab4d09
WH
483 if (!name) {
484 if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
485 name = git_author_name.buf;
486 else if (whose_ident == WANT_COMMITTER_IDENT &&
487 git_committer_name.len)
488 name = git_committer_name.buf;
489 }
59f92959 490 if (!name) {
734c7789 491 if (strict && ident_use_config_only
d3c06c19 492 && !(ident_config_given & IDENT_NAME_GIVEN)) {
9ed104e5 493 ident_env_hint(whose_ident);
afb6c30b 494 die(_("no name was given and auto-detection is disabled"));
d3c06c19 495 }
59f92959
JK
496 name = ident_default_name();
497 using_default = 1;
498 if (strict && default_name_is_bogus) {
9ed104e5 499 ident_env_hint(whose_ident);
afb6c30b 500 die(_("unable to auto-detect name (got '%s')"), name);
59f92959
JK
501 }
502 }
503 if (!*name) {
504 struct passwd *pw;
505 if (strict) {
506 if (using_default)
9ed104e5 507 ident_env_hint(whose_ident);
afb6c30b 508 die(_("empty ident name (for <%s>) not allowed"), email);
59f92959
JK
509 }
510 pw = xgetpwuid_self(NULL);
511 name = pw->pw_name;
749be728 512 }
13b9a24e
JK
513 if (strict && !has_non_crud(name))
514 die(_("name consists only of disallowed characters: %s"), name);
92bcbb9b
JK
515 }
516
e8cbe211 517 strbuf_reset(ident);
c15e1987 518 if (want_name) {
e8cbe211
PW
519 strbuf_addstr_without_crud(ident, name);
520 strbuf_addstr(ident, " <");
d9ccfe77 521 }
e8cbe211 522 strbuf_addstr_without_crud(ident, email);
c15e1987 523 if (want_name)
e8cbe211 524 strbuf_addch(ident, '>');
359b27ad 525 if (want_date) {
e8cbe211 526 strbuf_addch(ident, ' ');
c33ddc2e 527 if (date_str && date_str[0]) {
e8cbe211 528 if (parse_date(date_str, ident) < 0)
afb6c30b 529 die(_("invalid date format: %s"), date_str);
c33ddc2e
JK
530 }
531 else
e8cbe211 532 strbuf_addstr(ident, ident_default_date());
d9ccfe77 533 }
c33ddc2e 534
e8cbe211 535 return ident->buf;
6aa33f40 536}
d289d136 537
39ab4d09 538const char *fmt_name(enum want_ident whose_ident)
d9ccfe77 539{
39ab4d09
WH
540 char *name = NULL;
541 char *email = NULL;
542
543 switch (whose_ident) {
544 case WANT_BLANK_IDENT:
545 break;
546 case WANT_AUTHOR_IDENT:
547 name = getenv("GIT_AUTHOR_NAME");
548 email = getenv("GIT_AUTHOR_EMAIL");
549 break;
550 case WANT_COMMITTER_IDENT:
551 name = getenv("GIT_COMMITTER_NAME");
552 email = getenv("GIT_COMMITTER_EMAIL");
553 break;
554 }
555 return fmt_ident(name, email, whose_ident, NULL,
556 IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
557}
558
774751a8 559const char *git_author_info(int flag)
d289d136 560{
d6991cee
JK
561 if (getenv("GIT_AUTHOR_NAME"))
562 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
563 if (getenv("GIT_AUTHOR_EMAIL"))
564 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 565 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 566 getenv("GIT_AUTHOR_EMAIL"),
39ab4d09 567 WANT_AUTHOR_IDENT,
749be728 568 getenv("GIT_AUTHOR_DATE"),
774751a8 569 flag);
d289d136
EB
570}
571
774751a8 572const char *git_committer_info(int flag)
d289d136 573{
91c38a21 574 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 575 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 576 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 577 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 578 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 579 getenv("GIT_COMMITTER_EMAIL"),
39ab4d09 580 WANT_COMMITTER_IDENT,
749be728 581 getenv("GIT_COMMITTER_DATE"),
774751a8 582 flag);
d289d136 583}
5aeb3a3a 584
d6991cee 585static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
586{
587#ifndef WINDOWS
588 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
589#else
590 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
591#endif
592}
9597921b 593
d6991cee
JK
594int committer_ident_sufficiently_given(void)
595{
596 return ident_is_sufficient(committer_ident_explicitly_given);
597}
598
599int author_ident_sufficiently_given(void)
600{
601 return ident_is_sufficient(author_ident_explicitly_given);
602}
603
39ab4d09 604static int set_ident(const char *var, const char *value)
9597921b 605{
39ab4d09
WH
606 if (!strcmp(var, "author.name")) {
607 if (!value)
608 return config_error_nonbool(var);
609 strbuf_reset(&git_author_name);
610 strbuf_addstr(&git_author_name, value);
611 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
612 ident_config_given |= IDENT_NAME_GIVEN;
613 return 0;
614 }
615
616 if (!strcmp(var, "author.email")) {
617 if (!value)
618 return config_error_nonbool(var);
619 strbuf_reset(&git_author_email);
620 strbuf_addstr(&git_author_email, value);
621 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
622 ident_config_given |= IDENT_MAIL_GIVEN;
623 return 0;
624 }
625
626 if (!strcmp(var, "committer.name")) {
627 if (!value)
628 return config_error_nonbool(var);
629 strbuf_reset(&git_committer_name);
630 strbuf_addstr(&git_committer_name, value);
631 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
632 ident_config_given |= IDENT_NAME_GIVEN;
633 return 0;
634 }
635
636 if (!strcmp(var, "committer.email")) {
637 if (!value)
638 return config_error_nonbool(var);
639 strbuf_reset(&git_committer_email);
640 strbuf_addstr(&git_committer_email, value);
641 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
642 ident_config_given |= IDENT_MAIL_GIVEN;
4d5c2956
DA
643 return 0;
644 }
645
9597921b
JK
646 if (!strcmp(var, "user.name")) {
647 if (!value)
648 return config_error_nonbool(var);
8587ead7
JK
649 strbuf_reset(&git_default_name);
650 strbuf_addstr(&git_default_name, value);
d6991cee
JK
651 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
652 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
4d5c2956 653 ident_config_given |= IDENT_NAME_GIVEN;
9597921b
JK
654 return 0;
655 }
656
657 if (!strcmp(var, "user.email")) {
658 if (!value)
659 return config_error_nonbool(var);
8587ead7
JK
660 strbuf_reset(&git_default_email);
661 strbuf_addstr(&git_default_email, value);
d6991cee
JK
662 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
663 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
4d5c2956 664 ident_config_given |= IDENT_MAIL_GIVEN;
9597921b
JK
665 return 0;
666 }
667
668 return 0;
669}
662cc30c 670
5cf88fd8 671int git_ident_config(const char *var, const char *value, void *data UNUSED)
39ab4d09
WH
672{
673 if (!strcmp(var, "user.useconfigonly")) {
674 ident_use_config_only = git_config_bool(var, value);
675 return 0;
676 }
677
678 return set_ident(var, value);
679}
680
fd5a5847
JS
681static void set_env_if(const char *key, const char *value, int *given, int bit)
682{
0640897d 683 if ((*given & bit) || getenv(key))
fd5a5847
JS
684 return; /* nothing to do */
685 setenv(key, value, 0);
686 *given |= bit;
687}
688
689void prepare_fallback_ident(const char *name, const char *email)
690{
691 set_env_if("GIT_AUTHOR_NAME", name,
692 &author_ident_explicitly_given, IDENT_NAME_GIVEN);
693 set_env_if("GIT_AUTHOR_EMAIL", email,
694 &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
695 set_env_if("GIT_COMMITTER_NAME", name,
696 &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
697 set_env_if("GIT_COMMITTER_EMAIL", email,
698 &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
699}
700
662cc30c
JK
701static int buf_cmp(const char *a_begin, const char *a_end,
702 const char *b_begin, const char *b_end)
703{
704 int a_len = a_end - a_begin;
705 int b_len = b_end - b_begin;
706 int min = a_len < b_len ? a_len : b_len;
707 int cmp;
708
709 cmp = memcmp(a_begin, b_begin, min);
710 if (cmp)
711 return cmp;
712
713 return a_len - b_len;
714}
715
716int ident_cmp(const struct ident_split *a,
717 const struct ident_split *b)
718{
719 int cmp;
720
721 cmp = buf_cmp(a->mail_begin, a->mail_end,
722 b->mail_begin, b->mail_end);
723 if (cmp)
724 return cmp;
725
726 return buf_cmp(a->name_begin, a->name_end,
727 b->name_begin, b->name_end);
728}