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