Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ident.c |
| 3 | * |
| 4 | * create git identifier lines of the form "name <email> date" |
| 5 | * |
| 6 | * Copyright (C) 2005 Linus Torvalds |
| 7 | */ |
Elijah Newren | b5fa608 | 2023-02-24 00:09:29 +0000 | [diff] [blame] | 8 | #include "git-compat-util.h" |
| 9 | #include "ident.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 10 | #include "config.h" |
Ævar Arnfjörð Bjarmason | 88c7b4c | 2022-02-16 09:14:02 +0100 | [diff] [blame] | 11 | #include "date.h" |
Elijah Newren | b5fa608 | 2023-02-24 00:09:29 +0000 | [diff] [blame] | 12 | #include "gettext.h" |
Siddharth Asthana | dc88e34 | 2022-07-19 01:21:00 +0530 | [diff] [blame] | 13 | #include "mailmap.h" |
Elijah Newren | b5fa608 | 2023-02-24 00:09:29 +0000 | [diff] [blame] | 14 | #include "strbuf.h" |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 15 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 16 | static struct strbuf git_default_name = STRBUF_INIT; |
| 17 | static struct strbuf git_default_email = STRBUF_INIT; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 18 | static struct strbuf git_default_date = STRBUF_INIT; |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 19 | static struct strbuf git_author_name = STRBUF_INIT; |
| 20 | static struct strbuf git_author_email = STRBUF_INIT; |
| 21 | static struct strbuf git_committer_name = STRBUF_INIT; |
| 22 | static struct strbuf git_committer_email = STRBUF_INIT; |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 23 | static int default_email_is_bogus; |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 24 | static int default_name_is_bogus; |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 25 | |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 26 | static int ident_use_config_only; |
| 27 | |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 28 | #define IDENT_NAME_GIVEN 01 |
| 29 | #define IDENT_MAIL_GIVEN 02 |
| 30 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 31 | static int committer_ident_explicitly_given; |
| 32 | static int author_ident_explicitly_given; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 33 | static int ident_config_given; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 34 | |
Rafael Gieschke | 590e081 | 2011-05-19 13:37:55 +0200 | [diff] [blame] | 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 | |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 41 | static struct passwd *xgetpwuid_self(int *is_bogus) |
Jeff King | e850194 | 2015-12-10 16:33:05 -0500 | [diff] [blame] | 42 | { |
| 43 | struct passwd *pw; |
| 44 | |
| 45 | errno = 0; |
| 46 | pw = getpwuid(getuid()); |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 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 | } |
Jeff King | e850194 | 2015-12-10 16:33:05 -0500 | [diff] [blame] | 57 | return pw; |
| 58 | } |
| 59 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 60 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 61 | { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 62 | char *src; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 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 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 68 | for (src = get_gecos(w); *src && *src != ','; src++) { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 69 | int ch = *src; |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 70 | if (ch != '&') |
| 71 | strbuf_addch(name, ch); |
| 72 | else { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 73 | /* Sorry, Mr. McDonald... */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 74 | strbuf_addch(name, toupper(*w->pw_name)); |
| 75 | strbuf_addstr(name, w->pw_name + 1); |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 80 | static int add_mailname_host(struct strbuf *buf) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 81 | { |
| 82 | FILE *mailname; |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 83 | struct strbuf mailnamebuf = STRBUF_INIT; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 84 | |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 85 | mailname = fopen_or_warn("/etc/mailname", "r"); |
| 86 | if (!mailname) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 87 | return -1; |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 88 | |
Junio C Hamano | 1f3b1ef | 2015-10-28 13:24:41 -0700 | [diff] [blame] | 89 | if (strbuf_getline(&mailnamebuf, mailname) == EOF) { |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 90 | if (ferror(mailname)) |
Nguyễn Thái Ngọc Duy | a26f4ed | 2016-05-08 16:47:49 +0700 | [diff] [blame] | 91 | warning_errno("cannot read /etc/mailname"); |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 92 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 93 | fclose(mailname); |
| 94 | return -1; |
| 95 | } |
| 96 | /* success! */ |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 97 | strbuf_addbuf(buf, &mailnamebuf); |
| 98 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 99 | fclose(mailname); |
| 100 | return 0; |
| 101 | } |
| 102 | |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 103 | static 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)) { |
Jeff King | c375a7e | 2016-09-23 00:37:53 -0400 | [diff] [blame] | 112 | if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) { |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 113 | strbuf_addstr(out, ai->ai_canonname); |
| 114 | status = 0; |
| 115 | } |
| 116 | freeaddrinfo(ai); |
| 117 | } |
| 118 | #else |
Jeff King | 58d29ec | 2015-12-14 15:52:41 -0500 | [diff] [blame] | 119 | struct hostent *he = gethostbyname(host); |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 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 | |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 129 | static void add_domainname(struct strbuf *out, int *is_bogus) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 130 | { |
René Scharfe | da25bdb | 2017-04-18 17:57:42 -0400 | [diff] [blame] | 131 | char buf[HOST_NAME_MAX + 1]; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 132 | |
David Turner | 5781a9a | 2017-04-18 17:57:43 -0400 | [diff] [blame] | 133 | if (xgethostname(buf, sizeof(buf))) { |
Nguyễn Thái Ngọc Duy | a26f4ed | 2016-05-08 16:47:49 +0700 | [diff] [blame] | 134 | warning_errno("cannot get host name"); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 135 | strbuf_addstr(out, "(none)"); |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 136 | *is_bogus = 1; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 137 | return; |
| 138 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 139 | if (strchr(buf, '.')) |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 140 | strbuf_addstr(out, buf); |
Junio C Hamano | 5498c57 | 2015-12-21 10:59:07 -0800 | [diff] [blame] | 141 | else if (canonical_name(buf, out) < 0) { |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 142 | strbuf_addf(out, "%s.(none)", buf); |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 143 | *is_bogus = 1; |
| 144 | } |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 147 | static void copy_email(const struct passwd *pw, struct strbuf *email, |
| 148 | int *is_bogus) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 149 | { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 150 | /* |
| 151 | * Make up a fake email address |
| 152 | * (name + '@' + hostname [+ '.' + domainname]) |
| 153 | */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 154 | strbuf_addstr(email, pw->pw_name); |
| 155 | strbuf_addch(email, '@'); |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 156 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 157 | if (!add_mailname_host(email)) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 158 | return; /* read from "/etc/mailname" (Debian) */ |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 159 | add_domainname(email, is_bogus); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 162 | const char *ident_default_name(void) |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 163 | { |
Jeff King | 9442555 | 2017-02-23 03:17:08 -0500 | [diff] [blame] | 164 | if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) { |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 165 | copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 166 | strbuf_trim(&git_default_name); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 167 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 168 | return git_default_name.buf; |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 169 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 170 | |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 171 | const char *ident_default_email(void) |
| 172 | { |
Jeff King | 9442555 | 2017-02-23 03:17:08 -0500 | [diff] [blame] | 173 | if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 174 | const char *email = getenv("EMAIL"); |
| 175 | |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 176 | if (email && email[0]) { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 177 | strbuf_addstr(&git_default_email, email); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 178 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 179 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Johannes Schindelin | 501afcb | 2018-10-15 02:47:08 -0700 | [diff] [blame] | 180 | } else if ((email = query_user_email()) && email[0]) { |
| 181 | strbuf_addstr(&git_default_email, email); |
| 182 | free((char *)email); |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 183 | } else |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 184 | copy_email(xgetpwuid_self(&default_email_is_bogus), |
| 185 | &git_default_email, &default_email_is_bogus); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 186 | strbuf_trim(&git_default_email); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 187 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 188 | return git_default_email.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Junio C Hamano | dad148c | 2012-09-15 22:50:09 -0700 | [diff] [blame] | 191 | static const char *ident_default_date(void) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 192 | { |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 193 | if (!git_default_date.len) |
| 194 | datestamp(&git_default_date); |
| 195 | return git_default_date.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Jeff King | 4d9c7e6 | 2016-08-01 15:37:00 -0400 | [diff] [blame] | 198 | void reset_ident_date(void) |
| 199 | { |
| 200 | strbuf_reset(&git_default_date); |
| 201 | } |
| 202 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 203 | static int crud(unsigned char c) |
| 204 | { |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 205 | return c <= 32 || |
| 206 | c == '.' || |
| 207 | c == ',' || |
| 208 | c == ':' || |
| 209 | c == ';' || |
| 210 | c == '<' || |
| 211 | c == '>' || |
| 212 | c == '"' || |
Linus Torvalds | d404bf0 | 2008-12-01 08:41:50 -0800 | [diff] [blame] | 213 | c == '\\' || |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 214 | c == '\''; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Jeff King | 13b9a24 | 2017-02-23 03:15:55 -0500 | [diff] [blame] | 217 | static 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 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 226 | /* |
| 227 | * Copy over a string to the destination, but avoid special |
| 228 | * characters ('\n', '<' and '>') and remove crud at the end |
| 229 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 230 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 231 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 232 | size_t i, len; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 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 |
Junio C Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 253 | * characters '\n' '<' and '>' that act as delimiters on |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 254 | * an identification line. We can only remove crud, never add it, |
| 255 | * so 'len' is our maximum. |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 256 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 257 | strbuf_grow(sb, len); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 258 | for (i = 0; i < len; i++) { |
| 259 | c = *src++; |
| 260 | switch (c) { |
| 261 | case '\n': case '<': case '>': |
| 262 | continue; |
| 263 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 264 | sb->buf[sb->len++] = c; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 265 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 266 | sb->buf[sb->len] = '\0'; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 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 | */ |
| 276 | int 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 | |
Jeff King | d9955fd | 2012-05-22 02:12:20 -0400 | [diff] [blame] | 293 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 294 | if (!isspace(*cp)) { |
| 295 | split->name_end = cp + 1; |
| 296 | break; |
| 297 | } |
Junio C Hamano | e27ddb6 | 2012-08-31 14:54:18 -0700 | [diff] [blame] | 298 | if (!split->name_end) { |
| 299 | /* no human readable name */ |
| 300 | split->name_end = split->name_begin; |
| 301 | } |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 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 | |
Jeff King | 03818a4 | 2013-10-14 18:45:00 -0400 | [diff] [blame] | 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++) |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 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 | |
| 345 | person_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 | |
Siddharth Asthana | dc88e34 | 2022-07-19 01:21:00 +0530 | [diff] [blame] | 353 | /* |
| 354 | * Returns the difference between the new and old length of the ident line. |
| 355 | */ |
| 356 | static 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 | |
Siddharth Asthana | 66a8a95 | 2022-07-19 01:21:01 +0530 | [diff] [blame] | 393 | void apply_mailmap_to_header(struct strbuf *buf, const char **header, |
| 394 | struct string_list *mailmap) |
Siddharth Asthana | dc88e34 | 2022-07-19 01:21:00 +0530 | [diff] [blame] | 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 | } |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 426 | |
| 427 | static 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 | } |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 452 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 453 | const char *fmt_ident(const char *name, const char *email, |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 454 | enum want_ident whose_ident, const char *date_str, int flag) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 455 | { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 456 | static int index; |
| 457 | static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT }; |
Jeff King | f9bc573 | 2012-05-24 19:28:40 -0400 | [diff] [blame] | 458 | int strict = (flag & IDENT_STRICT); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 459 | int want_date = !(flag & IDENT_NO_DATE); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 460 | int want_name = !(flag & IDENT_NO_NAME); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 461 | |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 462 | struct strbuf *ident = &ident_pool[index]; |
| 463 | index = (index + 1) % ARRAY_SIZE(ident_pool); |
| 464 | |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 465 | if (!email) { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 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 | } |
| 471 | if (!email) { |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 472 | if (strict && ident_use_config_only |
| 473 | && !(ident_config_given & IDENT_MAIL_GIVEN)) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 474 | ident_env_hint(whose_ident); |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 475 | die(_("no email was given and auto-detection is disabled")); |
| 476 | } |
| 477 | email = ident_default_email(); |
| 478 | if (strict && default_email_is_bogus) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 479 | ident_env_hint(whose_ident); |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 480 | die(_("unable to auto-detect email address (got '%s')"), email); |
| 481 | } |
| 482 | } |
| 483 | |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 484 | if (want_name) { |
| 485 | int using_default = 0; |
| 486 | if (!name) { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 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 | } |
| 493 | if (!name) { |
Marios Titas | 734c778 | 2016-03-30 22:29:42 +0300 | [diff] [blame] | 494 | if (strict && ident_use_config_only |
Marios Titas | d3c06c1 | 2016-03-30 22:29:43 +0300 | [diff] [blame] | 495 | && !(ident_config_given & IDENT_NAME_GIVEN)) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 496 | ident_env_hint(whose_ident); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 497 | die(_("no name was given and auto-detection is disabled")); |
Marios Titas | d3c06c1 | 2016-03-30 22:29:43 +0300 | [diff] [blame] | 498 | } |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 499 | name = ident_default_name(); |
| 500 | using_default = 1; |
| 501 | if (strict && default_name_is_bogus) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 502 | ident_env_hint(whose_ident); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 503 | die(_("unable to auto-detect name (got '%s')"), name); |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 504 | } |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 505 | } |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 506 | if (!*name) { |
| 507 | struct passwd *pw; |
| 508 | if (strict) { |
| 509 | if (using_default) |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 510 | ident_env_hint(whose_ident); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 511 | die(_("empty ident name (for <%s>) not allowed"), email); |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 512 | } |
| 513 | pw = xgetpwuid_self(NULL); |
| 514 | name = pw->pw_name; |
| 515 | } |
Jeff King | 13b9a24 | 2017-02-23 03:15:55 -0500 | [diff] [blame] | 516 | if (strict && !has_non_crud(name)) |
| 517 | die(_("name consists only of disallowed characters: %s"), name); |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 518 | } |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 519 | |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 520 | strbuf_reset(ident); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 521 | if (want_name) { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 522 | strbuf_addstr_without_crud(ident, name); |
| 523 | strbuf_addstr(ident, " <"); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 524 | } |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 525 | strbuf_addstr_without_crud(ident, email); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 526 | if (want_name) |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 527 | strbuf_addch(ident, '>'); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 528 | if (want_date) { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 529 | strbuf_addch(ident, ' '); |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 530 | if (date_str && date_str[0]) { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 531 | if (parse_date(date_str, ident) < 0) |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 532 | die(_("invalid date format: %s"), date_str); |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 533 | } |
| 534 | else |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 535 | strbuf_addstr(ident, ident_default_date()); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 536 | } |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 537 | |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 538 | return ident->buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 539 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 540 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 541 | const char *fmt_name(enum want_ident whose_ident) |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 542 | { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 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); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 562 | const char *git_author_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 563 | { |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 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; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 568 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 569 | getenv("GIT_AUTHOR_EMAIL"), |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 570 | WANT_AUTHOR_IDENT, |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 571 | getenv("GIT_AUTHOR_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 572 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 573 | } |
| 574 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 575 | const char *git_committer_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 576 | { |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 577 | if (getenv("GIT_COMMITTER_NAME")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 578 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 579 | if (getenv("GIT_COMMITTER_EMAIL")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 580 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 581 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 582 | getenv("GIT_COMMITTER_EMAIL"), |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 583 | WANT_COMMITTER_IDENT, |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 584 | getenv("GIT_COMMITTER_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 585 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 586 | } |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 587 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 588 | static int ident_is_sufficient(int user_ident_explicitly_given) |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 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 | } |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 596 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 597 | int committer_ident_sufficiently_given(void) |
| 598 | { |
| 599 | return ident_is_sufficient(committer_ident_explicitly_given); |
| 600 | } |
| 601 | |
| 602 | int author_ident_sufficiently_given(void) |
| 603 | { |
| 604 | return ident_is_sufficient(author_ident_explicitly_given); |
| 605 | } |
| 606 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 607 | static int set_ident(const char *var, const char *value) |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 608 | { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 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; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 649 | if (!strcmp(var, "user.name")) { |
| 650 | if (!value) |
| 651 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 652 | strbuf_reset(&git_default_name); |
| 653 | strbuf_addstr(&git_default_name, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 654 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 655 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 656 | ident_config_given |= IDENT_NAME_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | if (!strcmp(var, "user.email")) { |
| 661 | if (!value) |
| 662 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 663 | strbuf_reset(&git_default_email); |
| 664 | strbuf_addstr(&git_default_email, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 665 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 666 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 667 | ident_config_given |= IDENT_MAIL_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 673 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 674 | int git_ident_config(const char *var, const char *value, void *data UNUSED) |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 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 | |
Johannes Schindelin | fd5a584 | 2019-02-25 23:16:08 +0000 | [diff] [blame] | 684 | static void set_env_if(const char *key, const char *value, int *given, int bit) |
| 685 | { |
Thomas Gummerer | 0640897 | 2019-03-06 22:09:11 +0000 | [diff] [blame] | 686 | if ((*given & bit) || getenv(key)) |
Johannes Schindelin | fd5a584 | 2019-02-25 23:16:08 +0000 | [diff] [blame] | 687 | return; /* nothing to do */ |
| 688 | setenv(key, value, 0); |
| 689 | *given |= bit; |
| 690 | } |
| 691 | |
| 692 | void 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 | |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 704 | static 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 | |
| 719 | int 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 | } |