]> git.ipfire.org Git - thirdparty/git.git/blame - ident.c
The twentieth batch
[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 205 return c <= 32 ||
f64c81d4
AR
206 c == ',' ||
207 c == ':' ||
208 c == ';' ||
209 c == '<' ||
210 c == '>' ||
211 c == '"' ||
d404bf02 212 c == '\\' ||
f64c81d4 213 c == '\'';
6aa33f40
LT
214}
215
13b9a24e
JK
216static int has_non_crud(const char *str)
217{
218 for (; *str; str++) {
219 if (!crud(*str))
220 return 1;
221 }
222 return 0;
223}
224
6aa33f40
LT
225/*
226 * Copy over a string to the destination, but avoid special
227 * characters ('\n', '<' and '>') and remove crud at the end
228 */
c96f0c8d 229static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 230{
b0732115 231 size_t i, len;
6aa33f40
LT
232 unsigned char c;
233
234 /* Remove crud from the beginning.. */
235 while ((c = *src) != 0) {
236 if (!crud(c))
237 break;
238 src++;
239 }
240
241 /* Remove crud from the end.. */
242 len = strlen(src);
243 while (len > 0) {
244 c = src[len-1];
245 if (!crud(c))
246 break;
247 --len;
248 }
249
250 /*
251 * Copy the rest to the buffer, but avoid the special
82f9d58a 252 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
253 * an identification line. We can only remove crud, never add it,
254 * so 'len' is our maximum.
6aa33f40 255 */
c96f0c8d 256 strbuf_grow(sb, len);
6aa33f40
LT
257 for (i = 0; i < len; i++) {
258 c = *src++;
259 switch (c) {
260 case '\n': case '<': case '>':
261 continue;
262 }
c96f0c8d 263 sb->buf[sb->len++] = c;
6aa33f40 264 }
c96f0c8d 265 sb->buf[sb->len] = '\0';
6aa33f40
LT
266}
267
4b340cfa
JH
268/*
269 * Reverse of fmt_ident(); given an ident line, split the fields
270 * to allow the caller to parse it.
271 * Signal a success by returning 0, but date/tz fields of the result
272 * can still be NULL if the input line only has the name/email part
273 * (e.g. reading from a reflog entry).
274 */
275int split_ident_line(struct ident_split *split, const char *line, int len)
276{
277 const char *cp;
278 size_t span;
279 int status = -1;
280
281 memset(split, 0, sizeof(*split));
282
283 split->name_begin = line;
284 for (cp = line; *cp && cp < line + len; cp++)
285 if (*cp == '<') {
286 split->mail_begin = cp + 1;
287 break;
288 }
289 if (!split->mail_begin)
290 return status;
291
d9955fd6 292 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
293 if (!isspace(*cp)) {
294 split->name_end = cp + 1;
295 break;
296 }
e27ddb64
JH
297 if (!split->name_end) {
298 /* no human readable name */
299 split->name_end = split->name_begin;
300 }
4b340cfa
JH
301
302 for (cp = split->mail_begin; cp < line + len; cp++)
303 if (*cp == '>') {
304 split->mail_end = cp;
305 break;
306 }
307 if (!split->mail_end)
308 return status;
309
03818a4a
JK
310 /*
311 * Look from the end-of-line to find the trailing ">" of the mail
312 * address, even though we should already know it as split->mail_end.
313 * This can help in cases of broken idents with an extra ">" somewhere
314 * in the email address. Note that we are assuming the timestamp will
315 * never have a ">" in it.
316 *
317 * Note that we will always find some ">" before going off the front of
318 * the string, because will always hit the split->mail_end closing
319 * bracket.
320 */
321 for (cp = line + len - 1; *cp != '>'; cp--)
322 ;
323
324 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
4b340cfa
JH
325 ;
326 if (line + len <= cp)
327 goto person_only;
328 split->date_begin = cp;
329 span = strspn(cp, "0123456789");
330 if (!span)
331 goto person_only;
332 split->date_end = split->date_begin + span;
333 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
334 ;
335 if (line + len <= cp || (*cp != '+' && *cp != '-'))
336 goto person_only;
337 split->tz_begin = cp;
338 span = strspn(cp + 1, "0123456789");
339 if (!span)
340 goto person_only;
341 split->tz_end = split->tz_begin + 1 + span;
342 return 0;
343
344person_only:
345 split->date_begin = NULL;
346 split->date_end = NULL;
347 split->tz_begin = NULL;
348 split->tz_end = NULL;
349 return 0;
350}
351
dc88e349
SA
352/*
353 * Returns the difference between the new and old length of the ident line.
354 */
355static ssize_t rewrite_ident_line(const char *person, size_t len,
356 struct strbuf *buf,
357 struct string_list *mailmap)
358{
359 size_t namelen, maillen;
360 const char *name;
361 const char *mail;
362 struct ident_split ident;
363
364 if (split_ident_line(&ident, person, len))
365 return 0;
366
367 mail = ident.mail_begin;
368 maillen = ident.mail_end - ident.mail_begin;
369 name = ident.name_begin;
370 namelen = ident.name_end - ident.name_begin;
371
372 if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
373 struct strbuf namemail = STRBUF_INIT;
374 size_t newlen;
375
376 strbuf_addf(&namemail, "%.*s <%.*s>",
377 (int)namelen, name, (int)maillen, mail);
378
379 strbuf_splice(buf, ident.name_begin - buf->buf,
380 ident.mail_end - ident.name_begin + 1,
381 namemail.buf, namemail.len);
382 newlen = namemail.len;
383
384 strbuf_release(&namemail);
385
386 return newlen - (ident.mail_end - ident.name_begin);
387 }
388
389 return 0;
390}
391
66a8a953
SA
392void apply_mailmap_to_header(struct strbuf *buf, const char **header,
393 struct string_list *mailmap)
dc88e349
SA
394{
395 size_t buf_offset = 0;
396
397 if (!mailmap)
398 return;
399
400 for (;;) {
401 const char *person, *line;
402 size_t i;
403 int found_header = 0;
404
405 line = buf->buf + buf_offset;
406 if (!*line || *line == '\n')
407 return; /* End of headers */
408
409 for (i = 0; header[i]; i++)
410 if (skip_prefix(line, header[i], &person)) {
411 const char *endp = strchrnul(person, '\n');
412 found_header = 1;
413 buf_offset += endp - line;
414 buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap);
415 break;
416 }
417
418 if (!found_header) {
419 buf_offset = strchrnul(line, '\n') - buf->buf;
420 if (buf->buf[buf_offset] == '\n')
421 buf_offset++;
422 }
423 }
424}
9ed104e5
JH
425
426static void ident_env_hint(enum want_ident whose_ident)
427{
428 switch (whose_ident) {
429 case WANT_AUTHOR_IDENT:
430 fputs(_("Author identity unknown\n"), stderr);
431 break;
432 case WANT_COMMITTER_IDENT:
433 fputs(_("Committer identity unknown\n"), stderr);
434 break;
435 default:
436 break;
437 }
438
439 fputs(_("\n"
440 "*** Please tell me who you are.\n"
441 "\n"
442 "Run\n"
443 "\n"
444 " git config --global user.email \"you@example.com\"\n"
445 " git config --global user.name \"Your Name\"\n"
446 "\n"
447 "to set your account\'s default identity.\n"
448 "Omit --global to set the identity only in this repository.\n"
449 "\n"), stderr);
450}
749be728 451
774751a8 452const char *fmt_ident(const char *name, const char *email,
39ab4d09 453 enum want_ident whose_ident, const char *date_str, int flag)
6aa33f40 454{
e8cbe211
PW
455 static int index;
456 static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
f9bc573f 457 int strict = (flag & IDENT_STRICT);
359b27ad 458 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 459 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 460
e8cbe211
PW
461 struct strbuf *ident = &ident_pool[index];
462 index = (index + 1) % ARRAY_SIZE(ident_pool);
463
39ab4d09
WH
464 if (!email) {
465 if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
466 email = git_author_email.buf;
467 else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
468 email = git_committer_email.buf;
469 }
862e80a4
JK
470 if (!email) {
471 if (strict && ident_use_config_only
472 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
9ed104e5 473 ident_env_hint(whose_ident);
862e80a4
JK
474 die(_("no email was given and auto-detection is disabled"));
475 }
476 email = ident_default_email();
477 if (strict && default_email_is_bogus) {
9ed104e5 478 ident_env_hint(whose_ident);
862e80a4
JK
479 die(_("unable to auto-detect email address (got '%s')"), email);
480 }
481 }
482
59f92959
JK
483 if (want_name) {
484 int using_default = 0;
39ab4d09
WH
485 if (!name) {
486 if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
487 name = git_author_name.buf;
488 else if (whose_ident == WANT_COMMITTER_IDENT &&
489 git_committer_name.len)
490 name = git_committer_name.buf;
491 }
59f92959 492 if (!name) {
734c7789 493 if (strict && ident_use_config_only
d3c06c19 494 && !(ident_config_given & IDENT_NAME_GIVEN)) {
9ed104e5 495 ident_env_hint(whose_ident);
afb6c30b 496 die(_("no name was given and auto-detection is disabled"));
d3c06c19 497 }
59f92959
JK
498 name = ident_default_name();
499 using_default = 1;
500 if (strict && default_name_is_bogus) {
9ed104e5 501 ident_env_hint(whose_ident);
afb6c30b 502 die(_("unable to auto-detect name (got '%s')"), name);
59f92959
JK
503 }
504 }
505 if (!*name) {
506 struct passwd *pw;
507 if (strict) {
508 if (using_default)
9ed104e5 509 ident_env_hint(whose_ident);
afb6c30b 510 die(_("empty ident name (for <%s>) not allowed"), email);
59f92959
JK
511 }
512 pw = xgetpwuid_self(NULL);
513 name = pw->pw_name;
749be728 514 }
13b9a24e
JK
515 if (strict && !has_non_crud(name))
516 die(_("name consists only of disallowed characters: %s"), name);
92bcbb9b
JK
517 }
518
e8cbe211 519 strbuf_reset(ident);
c15e1987 520 if (want_name) {
e8cbe211
PW
521 strbuf_addstr_without_crud(ident, name);
522 strbuf_addstr(ident, " <");
d9ccfe77 523 }
e8cbe211 524 strbuf_addstr_without_crud(ident, email);
c15e1987 525 if (want_name)
e8cbe211 526 strbuf_addch(ident, '>');
359b27ad 527 if (want_date) {
e8cbe211 528 strbuf_addch(ident, ' ');
c33ddc2e 529 if (date_str && date_str[0]) {
e8cbe211 530 if (parse_date(date_str, ident) < 0)
afb6c30b 531 die(_("invalid date format: %s"), date_str);
c33ddc2e
JK
532 }
533 else
e8cbe211 534 strbuf_addstr(ident, ident_default_date());
d9ccfe77 535 }
c33ddc2e 536
e8cbe211 537 return ident->buf;
6aa33f40 538}
d289d136 539
39ab4d09 540const char *fmt_name(enum want_ident whose_ident)
d9ccfe77 541{
39ab4d09
WH
542 char *name = NULL;
543 char *email = NULL;
544
545 switch (whose_ident) {
546 case WANT_BLANK_IDENT:
547 break;
548 case WANT_AUTHOR_IDENT:
549 name = getenv("GIT_AUTHOR_NAME");
550 email = getenv("GIT_AUTHOR_EMAIL");
551 break;
552 case WANT_COMMITTER_IDENT:
553 name = getenv("GIT_COMMITTER_NAME");
554 email = getenv("GIT_COMMITTER_EMAIL");
555 break;
556 }
557 return fmt_ident(name, email, whose_ident, NULL,
558 IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
559}
560
774751a8 561const char *git_author_info(int flag)
d289d136 562{
d6991cee
JK
563 if (getenv("GIT_AUTHOR_NAME"))
564 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
565 if (getenv("GIT_AUTHOR_EMAIL"))
566 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 567 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 568 getenv("GIT_AUTHOR_EMAIL"),
39ab4d09 569 WANT_AUTHOR_IDENT,
749be728 570 getenv("GIT_AUTHOR_DATE"),
774751a8 571 flag);
d289d136
EB
572}
573
774751a8 574const char *git_committer_info(int flag)
d289d136 575{
91c38a21 576 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 577 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 578 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 579 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 580 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 581 getenv("GIT_COMMITTER_EMAIL"),
39ab4d09 582 WANT_COMMITTER_IDENT,
749be728 583 getenv("GIT_COMMITTER_DATE"),
774751a8 584 flag);
d289d136 585}
5aeb3a3a 586
d6991cee 587static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
588{
589#ifndef WINDOWS
590 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
591#else
592 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
593#endif
594}
9597921b 595
d6991cee
JK
596int committer_ident_sufficiently_given(void)
597{
598 return ident_is_sufficient(committer_ident_explicitly_given);
599}
600
601int author_ident_sufficiently_given(void)
602{
603 return ident_is_sufficient(author_ident_explicitly_given);
604}
605
39ab4d09 606static int set_ident(const char *var, const char *value)
9597921b 607{
39ab4d09
WH
608 if (!strcmp(var, "author.name")) {
609 if (!value)
610 return config_error_nonbool(var);
611 strbuf_reset(&git_author_name);
612 strbuf_addstr(&git_author_name, value);
613 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
614 ident_config_given |= IDENT_NAME_GIVEN;
615 return 0;
616 }
617
618 if (!strcmp(var, "author.email")) {
619 if (!value)
620 return config_error_nonbool(var);
621 strbuf_reset(&git_author_email);
622 strbuf_addstr(&git_author_email, value);
623 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
624 ident_config_given |= IDENT_MAIL_GIVEN;
625 return 0;
626 }
627
628 if (!strcmp(var, "committer.name")) {
629 if (!value)
630 return config_error_nonbool(var);
631 strbuf_reset(&git_committer_name);
632 strbuf_addstr(&git_committer_name, value);
633 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
634 ident_config_given |= IDENT_NAME_GIVEN;
635 return 0;
636 }
637
638 if (!strcmp(var, "committer.email")) {
639 if (!value)
640 return config_error_nonbool(var);
641 strbuf_reset(&git_committer_email);
642 strbuf_addstr(&git_committer_email, value);
643 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
644 ident_config_given |= IDENT_MAIL_GIVEN;
4d5c2956
DA
645 return 0;
646 }
647
9597921b
JK
648 if (!strcmp(var, "user.name")) {
649 if (!value)
650 return config_error_nonbool(var);
8587ead7
JK
651 strbuf_reset(&git_default_name);
652 strbuf_addstr(&git_default_name, value);
d6991cee
JK
653 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
654 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
4d5c2956 655 ident_config_given |= IDENT_NAME_GIVEN;
9597921b
JK
656 return 0;
657 }
658
659 if (!strcmp(var, "user.email")) {
660 if (!value)
661 return config_error_nonbool(var);
8587ead7
JK
662 strbuf_reset(&git_default_email);
663 strbuf_addstr(&git_default_email, value);
d6991cee
JK
664 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
665 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
4d5c2956 666 ident_config_given |= IDENT_MAIL_GIVEN;
9597921b
JK
667 return 0;
668 }
669
670 return 0;
671}
662cc30c 672
a4e7e317
GC
673int git_ident_config(const char *var, const char *value,
674 const struct config_context *ctx UNUSED,
675 void *data UNUSED)
39ab4d09
WH
676{
677 if (!strcmp(var, "user.useconfigonly")) {
678 ident_use_config_only = git_config_bool(var, value);
679 return 0;
680 }
681
682 return set_ident(var, value);
683}
684
fd5a5847
JS
685static void set_env_if(const char *key, const char *value, int *given, int bit)
686{
0640897d 687 if ((*given & bit) || getenv(key))
fd5a5847
JS
688 return; /* nothing to do */
689 setenv(key, value, 0);
690 *given |= bit;
691}
692
693void prepare_fallback_ident(const char *name, const char *email)
694{
695 set_env_if("GIT_AUTHOR_NAME", name,
696 &author_ident_explicitly_given, IDENT_NAME_GIVEN);
697 set_env_if("GIT_AUTHOR_EMAIL", email,
698 &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
699 set_env_if("GIT_COMMITTER_NAME", name,
700 &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
701 set_env_if("GIT_COMMITTER_EMAIL", email,
702 &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
703}
704
662cc30c
JK
705static int buf_cmp(const char *a_begin, const char *a_end,
706 const char *b_begin, const char *b_end)
707{
708 int a_len = a_end - a_begin;
709 int b_len = b_end - b_begin;
710 int min = a_len < b_len ? a_len : b_len;
711 int cmp;
712
713 cmp = memcmp(a_begin, b_begin, min);
714 if (cmp)
715 return cmp;
716
717 return a_len - b_len;
718}
719
720int ident_cmp(const struct ident_split *a,
721 const struct ident_split *b)
722{
723 int cmp;
724
725 cmp = buf_cmp(a->mail_begin, a->mail_end,
726 b->mail_begin, b->mail_end);
727 if (cmp)
728 return cmp;
729
730 return buf_cmp(a->name_begin, a->name_end,
731 b->name_begin, b->name_end);
732}