blob: 6aba4b5cb6f2cb3bf469f4f9111845a70a0e5031 [file] [log] [blame]
Linus Torvalds6aa33f42005-07-12 11:49:27 -07001/*
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"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07009#include "config.h"
Linus Torvalds6aa33f42005-07-12 11:49:27 -070010
Jeff King8587ead2012-05-21 19:10:17 -040011static struct strbuf git_default_name = STRBUF_INIT;
12static struct strbuf git_default_email = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -040013static struct strbuf git_default_date = STRBUF_INIT;
William Hubbs39ab4d02019-02-04 12:48:50 -060014static struct strbuf git_author_name = STRBUF_INIT;
15static struct strbuf git_author_email = STRBUF_INIT;
16static struct strbuf git_committer_name = STRBUF_INIT;
17static struct strbuf git_committer_email = STRBUF_INIT;
Jeff King19ce4972015-12-10 16:35:36 -050018static int default_email_is_bogus;
Jeff King92bcbb92015-12-10 16:41:29 -050019static int default_name_is_bogus;
Jeff King45280232012-11-14 16:34:05 -080020
Dan Aloni4d5c2952016-02-06 08:23:36 +020021static int ident_use_config_only;
22
Jeff King45280232012-11-14 16:34:05 -080023#define IDENT_NAME_GIVEN 01
24#define IDENT_MAIL_GIVEN 02
25#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
Jeff Kingd6991ce2012-11-14 16:34:13 -080026static int committer_ident_explicitly_given;
27static int author_ident_explicitly_given;
Dan Aloni4d5c2952016-02-06 08:23:36 +020028static int ident_config_given;
Linus Torvalds6aa33f42005-07-12 11:49:27 -070029
Rafael Gieschke590e0812011-05-19 13:37:55 +020030#ifdef NO_GECOS_IN_PWENT
31#define get_gecos(ignored) "&"
32#else
33#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
34#endif
35
Jeff King92bcbb92015-12-10 16:41:29 -050036static struct passwd *xgetpwuid_self(int *is_bogus)
Jeff Kinge8501942015-12-10 16:33:05 -050037{
38 struct passwd *pw;
39
40 errno = 0;
41 pw = getpwuid(getuid());
Jeff King92bcbb92015-12-10 16:41:29 -050042 if (!pw) {
43 static struct passwd fallback;
44 fallback.pw_name = "unknown";
45#ifndef NO_GECOS_IN_PWENT
46 fallback.pw_gecos = "Unknown";
47#endif
48 pw = &fallback;
49 if (is_bogus)
50 *is_bogus = 1;
51 }
Jeff Kinge8501942015-12-10 16:33:05 -050052 return pw;
53}
54
Jeff King8587ead2012-05-21 19:10:17 -040055static void copy_gecos(const struct passwd *w, struct strbuf *name)
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070056{
Jeff King8587ead2012-05-21 19:10:17 -040057 char *src;
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070058
59 /* Traditionally GECOS field had office phone numbers etc, separated
60 * with commas. Also & stands for capitalized form of the login name.
61 */
62
Jeff King8587ead2012-05-21 19:10:17 -040063 for (src = get_gecos(w); *src && *src != ','; src++) {
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070064 int ch = *src;
Jeff King8587ead2012-05-21 19:10:17 -040065 if (ch != '&')
66 strbuf_addch(name, ch);
67 else {
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070068 /* Sorry, Mr. McDonald... */
Jeff King8587ead2012-05-21 19:10:17 -040069 strbuf_addch(name, toupper(*w->pw_name));
70 strbuf_addstr(name, w->pw_name + 1);
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070071 }
72 }
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070073}
74
Jeff King8587ead2012-05-21 19:10:17 -040075static int add_mailname_host(struct strbuf *buf)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050076{
77 FILE *mailname;
Jonathan Niederdc342a22013-01-24 15:21:46 -080078 struct strbuf mailnamebuf = STRBUF_INIT;
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050079
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +070080 mailname = fopen_or_warn("/etc/mailname", "r");
81 if (!mailname)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050082 return -1;
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +070083
Junio C Hamano1f3b1ef2015-10-28 13:24:41 -070084 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050085 if (ferror(mailname))
Nguyễn Thái Ngọc Duya26f4ed2016-05-08 16:47:49 +070086 warning_errno("cannot read /etc/mailname");
Jonathan Niederdc342a22013-01-24 15:21:46 -080087 strbuf_release(&mailnamebuf);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050088 fclose(mailname);
89 return -1;
90 }
91 /* success! */
Jonathan Niederdc342a22013-01-24 15:21:46 -080092 strbuf_addbuf(buf, &mailnamebuf);
93 strbuf_release(&mailnamebuf);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050094 fclose(mailname);
95 return 0;
96}
97
Elia Pinto00bce772015-11-27 14:08:27 +000098static int canonical_name(const char *host, struct strbuf *out)
99{
100 int status = -1;
101
102#ifndef NO_IPV6
103 struct addrinfo hints, *ai;
104 memset (&hints, '\0', sizeof (hints));
105 hints.ai_flags = AI_CANONNAME;
106 if (!getaddrinfo(host, NULL, &hints, &ai)) {
Jeff Kingc375a7e2016-09-23 00:37:53 -0400107 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
Elia Pinto00bce772015-11-27 14:08:27 +0000108 strbuf_addstr(out, ai->ai_canonname);
109 status = 0;
110 }
111 freeaddrinfo(ai);
112 }
113#else
Jeff King58d29ec2015-12-14 15:52:41 -0500114 struct hostent *he = gethostbyname(host);
Elia Pinto00bce772015-11-27 14:08:27 +0000115 if (he && strchr(he->h_name, '.')) {
116 strbuf_addstr(out, he->h_name);
117 status = 0;
118 }
119#endif /* NO_IPV6 */
120
121 return status;
122}
123
Jeff King19ce4972015-12-10 16:35:36 -0500124static void add_domainname(struct strbuf *out, int *is_bogus)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -0500125{
René Scharfeda25bdb2017-04-18 17:57:42 -0400126 char buf[HOST_NAME_MAX + 1];
Jonathan Nieder8a55caa2011-10-03 01:16:33 -0500127
David Turner5781a9a2017-04-18 17:57:43 -0400128 if (xgethostname(buf, sizeof(buf))) {
Nguyễn Thái Ngọc Duya26f4ed2016-05-08 16:47:49 +0700129 warning_errno("cannot get host name");
Jeff King8587ead2012-05-21 19:10:17 -0400130 strbuf_addstr(out, "(none)");
Jeff King19ce4972015-12-10 16:35:36 -0500131 *is_bogus = 1;
Jonathan Nieder8a55caa2011-10-03 01:16:33 -0500132 return;
133 }
Jeff King8587ead2012-05-21 19:10:17 -0400134 if (strchr(buf, '.'))
Jeff Kingf8254d32012-05-21 19:10:23 -0400135 strbuf_addstr(out, buf);
Junio C Hamano5498c572015-12-21 10:59:07 -0800136 else if (canonical_name(buf, out) < 0) {
Jeff Kingf8254d32012-05-21 19:10:23 -0400137 strbuf_addf(out, "%s.(none)", buf);
Jeff King19ce4972015-12-10 16:35:36 -0500138 *is_bogus = 1;
139 }
Jonathan Nieder8a55caa2011-10-03 01:16:33 -0500140}
141
Jeff King19ce4972015-12-10 16:35:36 -0500142static void copy_email(const struct passwd *pw, struct strbuf *email,
143 int *is_bogus)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700144{
Junio C Hamano01754762007-01-28 00:50:53 -0800145 /*
146 * Make up a fake email address
147 * (name + '@' + hostname [+ '.' + domainname])
148 */
Jeff King8587ead2012-05-21 19:10:17 -0400149 strbuf_addstr(email, pw->pw_name);
150 strbuf_addch(email, '@');
Petr Baudisadc3dbc2005-10-21 03:57:39 +0200151
Jeff King8587ead2012-05-21 19:10:17 -0400152 if (!add_mailname_host(email))
Jonathan Nieder8a55caa2011-10-03 01:16:33 -0500153 return; /* read from "/etc/mailname" (Debian) */
Jeff King19ce4972015-12-10 16:35:36 -0500154 add_domainname(email, is_bogus);
Junio C Hamano01754762007-01-28 00:50:53 -0800155}
156
Matthieu Moy98305342014-07-25 21:11:34 +0200157const char *ident_default_name(void)
Junio C Hamano01754762007-01-28 00:50:53 -0800158{
Jeff King94425552017-02-23 03:17:08 -0500159 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
Jeff King92bcbb92015-12-10 16:41:29 -0500160 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
Jeff Kingbe641ab2012-05-21 19:10:29 -0400161 strbuf_trim(&git_default_name);
Junio C Hamano01754762007-01-28 00:50:53 -0800162 }
Jeff King8587ead2012-05-21 19:10:17 -0400163 return git_default_name.buf;
Jeff Kingbcb2b002012-05-21 19:09:43 -0400164}
Junio C Hamano01754762007-01-28 00:50:53 -0800165
Jeff Kingbcb2b002012-05-21 19:09:43 -0400166const char *ident_default_email(void)
167{
Jeff King94425552017-02-23 03:17:08 -0500168 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
Matt Kraai46f74f02007-07-05 17:29:41 -0700169 const char *email = getenv("EMAIL");
170
Junio C Hamano99178c82010-01-08 08:01:10 -0800171 if (email && email[0]) {
Jeff King8587ead2012-05-21 19:10:17 -0400172 strbuf_addstr(&git_default_email, email);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800173 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
174 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Johannes Schindelin501afcb2018-10-15 02:47:08 -0700175 } else if ((email = query_user_email()) && email[0]) {
176 strbuf_addstr(&git_default_email, email);
177 free((char *)email);
Jeff King2f705872012-05-21 19:10:20 -0400178 } else
Jeff King92bcbb92015-12-10 16:41:29 -0500179 copy_email(xgetpwuid_self(&default_email_is_bogus),
180 &git_default_email, &default_email_is_bogus);
Jeff Kingbe641ab2012-05-21 19:10:29 -0400181 strbuf_trim(&git_default_email);
Junio C Hamano01754762007-01-28 00:50:53 -0800182 }
Jeff King8587ead2012-05-21 19:10:17 -0400183 return git_default_email.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700184}
185
Junio C Hamanodad148c2012-09-15 22:50:09 -0700186static const char *ident_default_date(void)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700187{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400188 if (!git_default_date.len)
189 datestamp(&git_default_date);
190 return git_default_date.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700191}
192
Jeff King4d9c7e62016-08-01 15:37:00 -0400193void reset_ident_date(void)
194{
195 strbuf_reset(&git_default_date);
196}
197
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700198static int crud(unsigned char c)
199{
Alex Riesenf64c81d2007-12-03 20:11:43 +0100200 return c <= 32 ||
201 c == '.' ||
202 c == ',' ||
203 c == ':' ||
204 c == ';' ||
205 c == '<' ||
206 c == '>' ||
207 c == '"' ||
Linus Torvaldsd404bf02008-12-01 08:41:50 -0800208 c == '\\' ||
Alex Riesenf64c81d2007-12-03 20:11:43 +0100209 c == '\'';
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700210}
211
Jeff King13b9a242017-02-23 03:15:55 -0500212static int has_non_crud(const char *str)
213{
214 for (; *str; str++) {
215 if (!crud(*str))
216 return 1;
217 }
218 return 0;
219}
220
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700221/*
222 * Copy over a string to the destination, but avoid special
223 * characters ('\n', '<' and '>') and remove crud at the end
224 */
Jeff Kingc96f0c82012-05-21 19:10:26 -0400225static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700226{
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300227 size_t i, len;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700228 unsigned char c;
229
230 /* Remove crud from the beginning.. */
231 while ((c = *src) != 0) {
232 if (!crud(c))
233 break;
234 src++;
235 }
236
237 /* Remove crud from the end.. */
238 len = strlen(src);
239 while (len > 0) {
240 c = src[len-1];
241 if (!crud(c))
242 break;
243 --len;
244 }
245
246 /*
247 * Copy the rest to the buffer, but avoid the special
Junio C Hamano82f9d582005-12-29 01:30:08 -0800248 * characters '\n' '<' and '>' that act as delimiters on
Jeff Kingc96f0c82012-05-21 19:10:26 -0400249 * an identification line. We can only remove crud, never add it,
250 * so 'len' is our maximum.
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700251 */
Jeff Kingc96f0c82012-05-21 19:10:26 -0400252 strbuf_grow(sb, len);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700253 for (i = 0; i < len; i++) {
254 c = *src++;
255 switch (c) {
256 case '\n': case '<': case '>':
257 continue;
258 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400259 sb->buf[sb->len++] = c;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700260 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400261 sb->buf[sb->len] = '\0';
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700262}
263
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800264/*
265 * Reverse of fmt_ident(); given an ident line, split the fields
266 * to allow the caller to parse it.
267 * Signal a success by returning 0, but date/tz fields of the result
268 * can still be NULL if the input line only has the name/email part
269 * (e.g. reading from a reflog entry).
270 */
271int split_ident_line(struct ident_split *split, const char *line, int len)
272{
273 const char *cp;
274 size_t span;
275 int status = -1;
276
277 memset(split, 0, sizeof(*split));
278
279 split->name_begin = line;
280 for (cp = line; *cp && cp < line + len; cp++)
281 if (*cp == '<') {
282 split->mail_begin = cp + 1;
283 break;
284 }
285 if (!split->mail_begin)
286 return status;
287
Jeff Kingd9955fd2012-05-22 02:12:20 -0400288 for (cp = split->mail_begin - 2; line <= cp; cp--)
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800289 if (!isspace(*cp)) {
290 split->name_end = cp + 1;
291 break;
292 }
Junio C Hamanoe27ddb62012-08-31 14:54:18 -0700293 if (!split->name_end) {
294 /* no human readable name */
295 split->name_end = split->name_begin;
296 }
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800297
298 for (cp = split->mail_begin; cp < line + len; cp++)
299 if (*cp == '>') {
300 split->mail_end = cp;
301 break;
302 }
303 if (!split->mail_end)
304 return status;
305
Jeff King03818a42013-10-14 18:45:00 -0400306 /*
307 * Look from the end-of-line to find the trailing ">" of the mail
308 * address, even though we should already know it as split->mail_end.
309 * This can help in cases of broken idents with an extra ">" somewhere
310 * in the email address. Note that we are assuming the timestamp will
311 * never have a ">" in it.
312 *
313 * Note that we will always find some ">" before going off the front of
314 * the string, because will always hit the split->mail_end closing
315 * bracket.
316 */
317 for (cp = line + len - 1; *cp != '>'; cp--)
318 ;
319
320 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800321 ;
322 if (line + len <= cp)
323 goto person_only;
324 split->date_begin = cp;
325 span = strspn(cp, "0123456789");
326 if (!span)
327 goto person_only;
328 split->date_end = split->date_begin + span;
329 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
330 ;
331 if (line + len <= cp || (*cp != '+' && *cp != '-'))
332 goto person_only;
333 split->tz_begin = cp;
334 span = strspn(cp + 1, "0123456789");
335 if (!span)
336 goto person_only;
337 split->tz_end = split->tz_begin + 1 + span;
338 return 0;
339
340person_only:
341 split->date_begin = NULL;
342 split->date_end = NULL;
343 split->tz_begin = NULL;
344 split->tz_end = NULL;
345 return 0;
346}
347
Junio C Hamano9ed104e2020-08-21 13:36:28 -0700348
349static void ident_env_hint(enum want_ident whose_ident)
350{
351 switch (whose_ident) {
352 case WANT_AUTHOR_IDENT:
353 fputs(_("Author identity unknown\n"), stderr);
354 break;
355 case WANT_COMMITTER_IDENT:
356 fputs(_("Committer identity unknown\n"), stderr);
357 break;
358 default:
359 break;
360 }
361
362 fputs(_("\n"
363 "*** Please tell me who you are.\n"
364 "\n"
365 "Run\n"
366 "\n"
367 " git config --global user.email \"you@example.com\"\n"
368 " git config --global user.name \"Your Name\"\n"
369 "\n"
370 "to set your account\'s default identity.\n"
371 "Omit --global to set the identity only in this repository.\n"
372 "\n"), stderr);
373}
Junio C Hamano749be722006-02-18 20:31:05 -0800374
Junio C Hamano774751a2007-12-08 17:32:08 -0800375const char *fmt_ident(const char *name, const char *email,
William Hubbs39ab4d02019-02-04 12:48:50 -0600376 enum want_ident whose_ident, const char *date_str, int flag)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700377{
Phillip Woode8cbe212020-08-17 18:40:01 +0100378 static int index;
379 static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT };
Jeff Kingf9bc5732012-05-24 19:28:40 -0400380 int strict = (flag & IDENT_STRICT);
Jeff King359b27a2012-05-24 19:26:50 -0400381 int want_date = !(flag & IDENT_NO_DATE);
Jeff Kingc15e1982012-05-24 19:27:24 -0400382 int want_name = !(flag & IDENT_NO_NAME);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700383
Phillip Woode8cbe212020-08-17 18:40:01 +0100384 struct strbuf *ident = &ident_pool[index];
385 index = (index + 1) % ARRAY_SIZE(ident_pool);
386
Jeff King862e80a2017-02-23 03:13:53 -0500387 if (!email) {
William Hubbs39ab4d02019-02-04 12:48:50 -0600388 if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len)
389 email = git_author_email.buf;
390 else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len)
391 email = git_committer_email.buf;
392 }
393 if (!email) {
Jeff King862e80a2017-02-23 03:13:53 -0500394 if (strict && ident_use_config_only
395 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
Junio C Hamano9ed104e2020-08-21 13:36:28 -0700396 ident_env_hint(whose_ident);
Jeff King862e80a2017-02-23 03:13:53 -0500397 die(_("no email was given and auto-detection is disabled"));
398 }
399 email = ident_default_email();
400 if (strict && default_email_is_bogus) {
Junio C Hamano9ed104e2020-08-21 13:36:28 -0700401 ident_env_hint(whose_ident);
Jeff King862e80a2017-02-23 03:13:53 -0500402 die(_("unable to auto-detect email address (got '%s')"), email);
403 }
404 }
405
Jeff King59f92952016-02-04 11:12:38 +0200406 if (want_name) {
407 int using_default = 0;
408 if (!name) {
William Hubbs39ab4d02019-02-04 12:48:50 -0600409 if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len)
410 name = git_author_name.buf;
411 else if (whose_ident == WANT_COMMITTER_IDENT &&
412 git_committer_name.len)
413 name = git_committer_name.buf;
414 }
415 if (!name) {
Marios Titas734c7782016-03-30 22:29:42 +0300416 if (strict && ident_use_config_only
Marios Titasd3c06c12016-03-30 22:29:43 +0300417 && !(ident_config_given & IDENT_NAME_GIVEN)) {
Junio C Hamano9ed104e2020-08-21 13:36:28 -0700418 ident_env_hint(whose_ident);
Jeff Kingafb6c302017-02-23 03:12:30 -0500419 die(_("no name was given and auto-detection is disabled"));
Marios Titasd3c06c12016-03-30 22:29:43 +0300420 }
Jeff King59f92952016-02-04 11:12:38 +0200421 name = ident_default_name();
422 using_default = 1;
423 if (strict && default_name_is_bogus) {
Junio C Hamano9ed104e2020-08-21 13:36:28 -0700424 ident_env_hint(whose_ident);
Jeff Kingafb6c302017-02-23 03:12:30 -0500425 die(_("unable to auto-detect name (got '%s')"), name);
Jeff King59f92952016-02-04 11:12:38 +0200426 }
Junio C Hamano749be722006-02-18 20:31:05 -0800427 }
Jeff King59f92952016-02-04 11:12:38 +0200428 if (!*name) {
429 struct passwd *pw;
430 if (strict) {
431 if (using_default)
Junio C Hamano9ed104e2020-08-21 13:36:28 -0700432 ident_env_hint(whose_ident);
Jeff Kingafb6c302017-02-23 03:12:30 -0500433 die(_("empty ident name (for <%s>) not allowed"), email);
Jeff King59f92952016-02-04 11:12:38 +0200434 }
435 pw = xgetpwuid_self(NULL);
436 name = pw->pw_name;
437 }
Jeff King13b9a242017-02-23 03:15:55 -0500438 if (strict && !has_non_crud(name))
439 die(_("name consists only of disallowed characters: %s"), name);
Junio C Hamano749be722006-02-18 20:31:05 -0800440 }
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800441
Phillip Woode8cbe212020-08-17 18:40:01 +0100442 strbuf_reset(ident);
Jeff Kingc15e1982012-05-24 19:27:24 -0400443 if (want_name) {
Phillip Woode8cbe212020-08-17 18:40:01 +0100444 strbuf_addstr_without_crud(ident, name);
445 strbuf_addstr(ident, " <");
Jeff Kingc15e1982012-05-24 19:27:24 -0400446 }
Phillip Woode8cbe212020-08-17 18:40:01 +0100447 strbuf_addstr_without_crud(ident, email);
Jeff Kingc15e1982012-05-24 19:27:24 -0400448 if (want_name)
Phillip Woode8cbe212020-08-17 18:40:01 +0100449 strbuf_addch(ident, '>');
Jeff King359b27a2012-05-24 19:26:50 -0400450 if (want_date) {
Phillip Woode8cbe212020-08-17 18:40:01 +0100451 strbuf_addch(ident, ' ');
Jeff Kingc33ddc22014-08-27 03:57:08 -0400452 if (date_str && date_str[0]) {
Phillip Woode8cbe212020-08-17 18:40:01 +0100453 if (parse_date(date_str, ident) < 0)
Jeff Kingafb6c302017-02-23 03:12:30 -0500454 die(_("invalid date format: %s"), date_str);
Jeff Kingc33ddc22014-08-27 03:57:08 -0400455 }
456 else
Phillip Woode8cbe212020-08-17 18:40:01 +0100457 strbuf_addstr(ident, ident_default_date());
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800458 }
Jeff Kingc33ddc22014-08-27 03:57:08 -0400459
Phillip Woode8cbe212020-08-17 18:40:01 +0100460 return ident->buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700461}
Eric W. Biedermand289d132005-07-14 18:50:33 -0600462
William Hubbs39ab4d02019-02-04 12:48:50 -0600463const char *fmt_name(enum want_ident whose_ident)
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800464{
William Hubbs39ab4d02019-02-04 12:48:50 -0600465 char *name = NULL;
466 char *email = NULL;
467
468 switch (whose_ident) {
469 case WANT_BLANK_IDENT:
470 break;
471 case WANT_AUTHOR_IDENT:
472 name = getenv("GIT_AUTHOR_NAME");
473 email = getenv("GIT_AUTHOR_EMAIL");
474 break;
475 case WANT_COMMITTER_IDENT:
476 name = getenv("GIT_COMMITTER_NAME");
477 email = getenv("GIT_COMMITTER_EMAIL");
478 break;
479 }
480 return fmt_ident(name, email, whose_ident, NULL,
481 IDENT_STRICT | IDENT_NO_DATE);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800482}
483
Junio C Hamano774751a2007-12-08 17:32:08 -0800484const char *git_author_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600485{
Jeff Kingd6991ce2012-11-14 16:34:13 -0800486 if (getenv("GIT_AUTHOR_NAME"))
487 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
488 if (getenv("GIT_AUTHOR_EMAIL"))
489 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800490 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800491 getenv("GIT_AUTHOR_EMAIL"),
William Hubbs39ab4d02019-02-04 12:48:50 -0600492 WANT_AUTHOR_IDENT,
Junio C Hamano749be722006-02-18 20:31:05 -0800493 getenv("GIT_AUTHOR_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800494 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600495}
496
Junio C Hamano774751a2007-12-08 17:32:08 -0800497const char *git_committer_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600498{
Junio C Hamano91c38a22010-01-08 07:39:11 -0800499 if (getenv("GIT_COMMITTER_NAME"))
Jeff Kingd6991ce2012-11-14 16:34:13 -0800500 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
Junio C Hamano91c38a22010-01-08 07:39:11 -0800501 if (getenv("GIT_COMMITTER_EMAIL"))
Jeff Kingd6991ce2012-11-14 16:34:13 -0800502 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800503 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800504 getenv("GIT_COMMITTER_EMAIL"),
William Hubbs39ab4d02019-02-04 12:48:50 -0600505 WANT_COMMITTER_IDENT,
Junio C Hamano749be722006-02-18 20:31:05 -0800506 getenv("GIT_COMMITTER_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800507 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600508}
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800509
Jeff Kingd6991ce2012-11-14 16:34:13 -0800510static int ident_is_sufficient(int user_ident_explicitly_given)
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800511{
512#ifndef WINDOWS
513 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
514#else
515 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
516#endif
517}
Jeff King95979212012-05-21 19:09:54 -0400518
Jeff Kingd6991ce2012-11-14 16:34:13 -0800519int committer_ident_sufficiently_given(void)
520{
521 return ident_is_sufficient(committer_ident_explicitly_given);
522}
523
524int author_ident_sufficiently_given(void)
525{
526 return ident_is_sufficient(author_ident_explicitly_given);
527}
528
William Hubbs39ab4d02019-02-04 12:48:50 -0600529static int set_ident(const char *var, const char *value)
Jeff King95979212012-05-21 19:09:54 -0400530{
William Hubbs39ab4d02019-02-04 12:48:50 -0600531 if (!strcmp(var, "author.name")) {
532 if (!value)
533 return config_error_nonbool(var);
534 strbuf_reset(&git_author_name);
535 strbuf_addstr(&git_author_name, value);
536 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
537 ident_config_given |= IDENT_NAME_GIVEN;
538 return 0;
539 }
540
541 if (!strcmp(var, "author.email")) {
542 if (!value)
543 return config_error_nonbool(var);
544 strbuf_reset(&git_author_email);
545 strbuf_addstr(&git_author_email, value);
546 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
547 ident_config_given |= IDENT_MAIL_GIVEN;
548 return 0;
549 }
550
551 if (!strcmp(var, "committer.name")) {
552 if (!value)
553 return config_error_nonbool(var);
554 strbuf_reset(&git_committer_name);
555 strbuf_addstr(&git_committer_name, value);
556 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
557 ident_config_given |= IDENT_NAME_GIVEN;
558 return 0;
559 }
560
561 if (!strcmp(var, "committer.email")) {
562 if (!value)
563 return config_error_nonbool(var);
564 strbuf_reset(&git_committer_email);
565 strbuf_addstr(&git_committer_email, value);
566 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
567 ident_config_given |= IDENT_MAIL_GIVEN;
Dan Aloni4d5c2952016-02-06 08:23:36 +0200568 return 0;
569 }
570
Jeff King95979212012-05-21 19:09:54 -0400571 if (!strcmp(var, "user.name")) {
572 if (!value)
573 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400574 strbuf_reset(&git_default_name);
575 strbuf_addstr(&git_default_name, value);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800576 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
577 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
Dan Aloni4d5c2952016-02-06 08:23:36 +0200578 ident_config_given |= IDENT_NAME_GIVEN;
Jeff King95979212012-05-21 19:09:54 -0400579 return 0;
580 }
581
582 if (!strcmp(var, "user.email")) {
583 if (!value)
584 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400585 strbuf_reset(&git_default_email);
586 strbuf_addstr(&git_default_email, value);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800587 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
588 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Dan Aloni4d5c2952016-02-06 08:23:36 +0200589 ident_config_given |= IDENT_MAIL_GIVEN;
Jeff King95979212012-05-21 19:09:54 -0400590 return 0;
591 }
592
593 return 0;
594}
Jeff King662cc302013-09-20 06:16:28 -0400595
William Hubbs39ab4d02019-02-04 12:48:50 -0600596int git_ident_config(const char *var, const char *value, void *data)
597{
598 if (!strcmp(var, "user.useconfigonly")) {
599 ident_use_config_only = git_config_bool(var, value);
600 return 0;
601 }
602
603 return set_ident(var, value);
604}
605
Johannes Schindelinfd5a5842019-02-25 23:16:08 +0000606static void set_env_if(const char *key, const char *value, int *given, int bit)
607{
Thomas Gummerer06408972019-03-06 22:09:11 +0000608 if ((*given & bit) || getenv(key))
Johannes Schindelinfd5a5842019-02-25 23:16:08 +0000609 return; /* nothing to do */
610 setenv(key, value, 0);
611 *given |= bit;
612}
613
614void prepare_fallback_ident(const char *name, const char *email)
615{
616 set_env_if("GIT_AUTHOR_NAME", name,
617 &author_ident_explicitly_given, IDENT_NAME_GIVEN);
618 set_env_if("GIT_AUTHOR_EMAIL", email,
619 &author_ident_explicitly_given, IDENT_MAIL_GIVEN);
620 set_env_if("GIT_COMMITTER_NAME", name,
621 &committer_ident_explicitly_given, IDENT_NAME_GIVEN);
622 set_env_if("GIT_COMMITTER_EMAIL", email,
623 &committer_ident_explicitly_given, IDENT_MAIL_GIVEN);
624}
625
Jeff King662cc302013-09-20 06:16:28 -0400626static int buf_cmp(const char *a_begin, const char *a_end,
627 const char *b_begin, const char *b_end)
628{
629 int a_len = a_end - a_begin;
630 int b_len = b_end - b_begin;
631 int min = a_len < b_len ? a_len : b_len;
632 int cmp;
633
634 cmp = memcmp(a_begin, b_begin, min);
635 if (cmp)
636 return cmp;
637
638 return a_len - b_len;
639}
640
641int ident_cmp(const struct ident_split *a,
642 const struct ident_split *b)
643{
644 int cmp;
645
646 cmp = buf_cmp(a->mail_begin, a->mail_end,
647 b->mail_begin, b->mail_end);
648 if (cmp)
649 return cmp;
650
651 return buf_cmp(a->name_begin, a->name_end,
652 b->name_begin, b->name_end);
653}