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 | */ |
| 8 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Ævar Arnfjörð Bjarmason | 88c7b4c | 2022-02-16 09:14:02 +0100 | [diff] [blame] | 10 | #include "date.h" |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 11 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 12 | static struct strbuf git_default_name = STRBUF_INIT; |
| 13 | static struct strbuf git_default_email = STRBUF_INIT; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 14 | static struct strbuf git_default_date = STRBUF_INIT; |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 15 | static struct strbuf git_author_name = STRBUF_INIT; |
| 16 | static struct strbuf git_author_email = STRBUF_INIT; |
| 17 | static struct strbuf git_committer_name = STRBUF_INIT; |
| 18 | static struct strbuf git_committer_email = STRBUF_INIT; |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 19 | static int default_email_is_bogus; |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 20 | static int default_name_is_bogus; |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 21 | |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 22 | static int ident_use_config_only; |
| 23 | |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 24 | #define IDENT_NAME_GIVEN 01 |
| 25 | #define IDENT_MAIL_GIVEN 02 |
| 26 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 27 | static int committer_ident_explicitly_given; |
| 28 | static int author_ident_explicitly_given; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 29 | static int ident_config_given; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 30 | |
Rafael Gieschke | 590e081 | 2011-05-19 13:37:55 +0200 | [diff] [blame] | 31 | #ifdef NO_GECOS_IN_PWENT |
| 32 | #define get_gecos(ignored) "&" |
| 33 | #else |
| 34 | #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) |
| 35 | #endif |
| 36 | |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 37 | static struct passwd *xgetpwuid_self(int *is_bogus) |
Jeff King | e850194 | 2015-12-10 16:33:05 -0500 | [diff] [blame] | 38 | { |
| 39 | struct passwd *pw; |
| 40 | |
| 41 | errno = 0; |
| 42 | pw = getpwuid(getuid()); |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 43 | if (!pw) { |
| 44 | static struct passwd fallback; |
| 45 | fallback.pw_name = "unknown"; |
| 46 | #ifndef NO_GECOS_IN_PWENT |
| 47 | fallback.pw_gecos = "Unknown"; |
| 48 | #endif |
| 49 | pw = &fallback; |
| 50 | if (is_bogus) |
| 51 | *is_bogus = 1; |
| 52 | } |
Jeff King | e850194 | 2015-12-10 16:33:05 -0500 | [diff] [blame] | 53 | return pw; |
| 54 | } |
| 55 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 56 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 57 | { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 58 | char *src; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 59 | |
| 60 | /* Traditionally GECOS field had office phone numbers etc, separated |
| 61 | * with commas. Also & stands for capitalized form of the login name. |
| 62 | */ |
| 63 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 64 | for (src = get_gecos(w); *src && *src != ','; src++) { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 65 | int ch = *src; |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 66 | if (ch != '&') |
| 67 | strbuf_addch(name, ch); |
| 68 | else { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 69 | /* Sorry, Mr. McDonald... */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 70 | strbuf_addch(name, toupper(*w->pw_name)); |
| 71 | strbuf_addstr(name, w->pw_name + 1); |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 76 | static int add_mailname_host(struct strbuf *buf) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 77 | { |
| 78 | FILE *mailname; |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 79 | struct strbuf mailnamebuf = STRBUF_INIT; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 80 | |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 81 | mailname = fopen_or_warn("/etc/mailname", "r"); |
| 82 | if (!mailname) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 83 | return -1; |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 84 | |
Junio C Hamano | 1f3b1ef | 2015-10-28 13:24:41 -0700 | [diff] [blame] | 85 | if (strbuf_getline(&mailnamebuf, mailname) == EOF) { |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 86 | if (ferror(mailname)) |
Nguyễn Thái Ngọc Duy | a26f4ed | 2016-05-08 16:47:49 +0700 | [diff] [blame] | 87 | warning_errno("cannot read /etc/mailname"); |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 88 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 89 | fclose(mailname); |
| 90 | return -1; |
| 91 | } |
| 92 | /* success! */ |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 93 | strbuf_addbuf(buf, &mailnamebuf); |
| 94 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 95 | fclose(mailname); |
| 96 | return 0; |
| 97 | } |
| 98 | |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 99 | static int canonical_name(const char *host, struct strbuf *out) |
| 100 | { |
| 101 | int status = -1; |
| 102 | |
| 103 | #ifndef NO_IPV6 |
| 104 | struct addrinfo hints, *ai; |
| 105 | memset (&hints, '\0', sizeof (hints)); |
| 106 | hints.ai_flags = AI_CANONNAME; |
| 107 | if (!getaddrinfo(host, NULL, &hints, &ai)) { |
Jeff King | c375a7e | 2016-09-23 00:37:53 -0400 | [diff] [blame] | 108 | if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) { |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 109 | strbuf_addstr(out, ai->ai_canonname); |
| 110 | status = 0; |
| 111 | } |
| 112 | freeaddrinfo(ai); |
| 113 | } |
| 114 | #else |
Jeff King | 58d29ec | 2015-12-14 15:52:41 -0500 | [diff] [blame] | 115 | struct hostent *he = gethostbyname(host); |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 116 | if (he && strchr(he->h_name, '.')) { |
| 117 | strbuf_addstr(out, he->h_name); |
| 118 | status = 0; |
| 119 | } |
| 120 | #endif /* NO_IPV6 */ |
| 121 | |
| 122 | return status; |
| 123 | } |
| 124 | |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 125 | static void add_domainname(struct strbuf *out, int *is_bogus) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 126 | { |
René Scharfe | da25bdb | 2017-04-18 17:57:42 -0400 | [diff] [blame] | 127 | char buf[HOST_NAME_MAX + 1]; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 128 | |
David Turner | 5781a9a | 2017-04-18 17:57:43 -0400 | [diff] [blame] | 129 | if (xgethostname(buf, sizeof(buf))) { |
Nguyễn Thái Ngọc Duy | a26f4ed | 2016-05-08 16:47:49 +0700 | [diff] [blame] | 130 | warning_errno("cannot get host name"); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 131 | strbuf_addstr(out, "(none)"); |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 132 | *is_bogus = 1; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 133 | return; |
| 134 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 135 | if (strchr(buf, '.')) |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 136 | strbuf_addstr(out, buf); |
Junio C Hamano | 5498c57 | 2015-12-21 10:59:07 -0800 | [diff] [blame] | 137 | else if (canonical_name(buf, out) < 0) { |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 138 | strbuf_addf(out, "%s.(none)", buf); |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 139 | *is_bogus = 1; |
| 140 | } |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 143 | static void copy_email(const struct passwd *pw, struct strbuf *email, |
| 144 | int *is_bogus) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 145 | { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 146 | /* |
| 147 | * Make up a fake email address |
| 148 | * (name + '@' + hostname [+ '.' + domainname]) |
| 149 | */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 150 | strbuf_addstr(email, pw->pw_name); |
| 151 | strbuf_addch(email, '@'); |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 152 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 153 | if (!add_mailname_host(email)) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 154 | return; /* read from "/etc/mailname" (Debian) */ |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 155 | add_domainname(email, is_bogus); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 158 | const char *ident_default_name(void) |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 159 | { |
Jeff King | 9442555 | 2017-02-23 03:17:08 -0500 | [diff] [blame] | 160 | if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) { |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 161 | copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 162 | strbuf_trim(&git_default_name); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 163 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 164 | return git_default_name.buf; |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 165 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 166 | |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 167 | const char *ident_default_email(void) |
| 168 | { |
Jeff King | 9442555 | 2017-02-23 03:17:08 -0500 | [diff] [blame] | 169 | if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 170 | const char *email = getenv("EMAIL"); |
| 171 | |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 172 | if (email && email[0]) { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 173 | strbuf_addstr(&git_default_email, email); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 174 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 175 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Johannes Schindelin | 501afcb | 2018-10-15 02:47:08 -0700 | [diff] [blame] | 176 | } else if ((email = query_user_email()) && email[0]) { |
| 177 | strbuf_addstr(&git_default_email, email); |
| 178 | free((char *)email); |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 179 | } else |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 180 | copy_email(xgetpwuid_self(&default_email_is_bogus), |
| 181 | &git_default_email, &default_email_is_bogus); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 182 | strbuf_trim(&git_default_email); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 183 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 184 | return git_default_email.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Junio C Hamano | dad148c | 2012-09-15 22:50:09 -0700 | [diff] [blame] | 187 | static const char *ident_default_date(void) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 188 | { |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 189 | if (!git_default_date.len) |
| 190 | datestamp(&git_default_date); |
| 191 | return git_default_date.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jeff King | 4d9c7e6 | 2016-08-01 15:37:00 -0400 | [diff] [blame] | 194 | void reset_ident_date(void) |
| 195 | { |
| 196 | strbuf_reset(&git_default_date); |
| 197 | } |
| 198 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 199 | static int crud(unsigned char c) |
| 200 | { |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 201 | return c <= 32 || |
| 202 | c == '.' || |
| 203 | c == ',' || |
| 204 | c == ':' || |
| 205 | c == ';' || |
| 206 | c == '<' || |
| 207 | c == '>' || |
| 208 | c == '"' || |
Linus Torvalds | d404bf0 | 2008-12-01 08:41:50 -0800 | [diff] [blame] | 209 | c == '\\' || |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 210 | c == '\''; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jeff King | 13b9a24 | 2017-02-23 03:15:55 -0500 | [diff] [blame] | 213 | static int has_non_crud(const char *str) |
| 214 | { |
| 215 | for (; *str; str++) { |
| 216 | if (!crud(*str)) |
| 217 | return 1; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 222 | /* |
| 223 | * Copy over a string to the destination, but avoid special |
| 224 | * characters ('\n', '<' and '>') and remove crud at the end |
| 225 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 226 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 227 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 228 | size_t i, len; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 229 | unsigned char c; |
| 230 | |
| 231 | /* Remove crud from the beginning.. */ |
| 232 | while ((c = *src) != 0) { |
| 233 | if (!crud(c)) |
| 234 | break; |
| 235 | src++; |
| 236 | } |
| 237 | |
| 238 | /* Remove crud from the end.. */ |
| 239 | len = strlen(src); |
| 240 | while (len > 0) { |
| 241 | c = src[len-1]; |
| 242 | if (!crud(c)) |
| 243 | break; |
| 244 | --len; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Copy the rest to the buffer, but avoid the special |
Junio C Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 249 | * characters '\n' '<' and '>' that act as delimiters on |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 250 | * an identification line. We can only remove crud, never add it, |
| 251 | * so 'len' is our maximum. |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 252 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 253 | strbuf_grow(sb, len); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 254 | for (i = 0; i < len; i++) { |
| 255 | c = *src++; |
| 256 | switch (c) { |
| 257 | case '\n': case '<': case '>': |
| 258 | continue; |
| 259 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 260 | sb->buf[sb->len++] = c; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 261 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 262 | sb->buf[sb->len] = '\0'; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 265 | /* |
| 266 | * Reverse of fmt_ident(); given an ident line, split the fields |
| 267 | * to allow the caller to parse it. |
| 268 | * Signal a success by returning 0, but date/tz fields of the result |
| 269 | * can still be NULL if the input line only has the name/email part |
| 270 | * (e.g. reading from a reflog entry). |
| 271 | */ |
| 272 | int split_ident_line(struct ident_split *split, const char *line, int len) |
| 273 | { |
| 274 | const char *cp; |
| 275 | size_t span; |
| 276 | int status = -1; |
| 277 | |
| 278 | memset(split, 0, sizeof(*split)); |
| 279 | |
| 280 | split->name_begin = line; |
| 281 | for (cp = line; *cp && cp < line + len; cp++) |
| 282 | if (*cp == '<') { |
| 283 | split->mail_begin = cp + 1; |
| 284 | break; |
| 285 | } |
| 286 | if (!split->mail_begin) |
| 287 | return status; |
| 288 | |
Jeff King | d9955fd | 2012-05-22 02:12:20 -0400 | [diff] [blame] | 289 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 290 | if (!isspace(*cp)) { |
| 291 | split->name_end = cp + 1; |
| 292 | break; |
| 293 | } |
Junio C Hamano | e27ddb6 | 2012-08-31 14:54:18 -0700 | [diff] [blame] | 294 | if (!split->name_end) { |
| 295 | /* no human readable name */ |
| 296 | split->name_end = split->name_begin; |
| 297 | } |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 298 | |
| 299 | for (cp = split->mail_begin; cp < line + len; cp++) |
| 300 | if (*cp == '>') { |
| 301 | split->mail_end = cp; |
| 302 | break; |
| 303 | } |
| 304 | if (!split->mail_end) |
| 305 | return status; |
| 306 | |
Jeff King | 03818a4 | 2013-10-14 18:45:00 -0400 | [diff] [blame] | 307 | /* |
| 308 | * Look from the end-of-line to find the trailing ">" of the mail |
| 309 | * address, even though we should already know it as split->mail_end. |
| 310 | * This can help in cases of broken idents with an extra ">" somewhere |
| 311 | * in the email address. Note that we are assuming the timestamp will |
| 312 | * never have a ">" in it. |
| 313 | * |
| 314 | * Note that we will always find some ">" before going off the front of |
| 315 | * the string, because will always hit the split->mail_end closing |
| 316 | * bracket. |
| 317 | */ |
| 318 | for (cp = line + len - 1; *cp != '>'; cp--) |
| 319 | ; |
| 320 | |
| 321 | for (cp = cp + 1; cp < line + len && isspace(*cp); cp++) |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 322 | ; |
| 323 | if (line + len <= cp) |
| 324 | goto person_only; |
| 325 | split->date_begin = cp; |
| 326 | span = strspn(cp, "0123456789"); |
| 327 | if (!span) |
| 328 | goto person_only; |
| 329 | split->date_end = split->date_begin + span; |
| 330 | for (cp = split->date_end; cp < line + len && isspace(*cp); cp++) |
| 331 | ; |
| 332 | if (line + len <= cp || (*cp != '+' && *cp != '-')) |
| 333 | goto person_only; |
| 334 | split->tz_begin = cp; |
| 335 | span = strspn(cp + 1, "0123456789"); |
| 336 | if (!span) |
| 337 | goto person_only; |
| 338 | split->tz_end = split->tz_begin + 1 + span; |
| 339 | return 0; |
| 340 | |
| 341 | person_only: |
| 342 | split->date_begin = NULL; |
| 343 | split->date_end = NULL; |
| 344 | split->tz_begin = NULL; |
| 345 | split->tz_end = NULL; |
| 346 | return 0; |
| 347 | } |
| 348 | |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 349 | |
| 350 | static void ident_env_hint(enum want_ident whose_ident) |
| 351 | { |
| 352 | switch (whose_ident) { |
| 353 | case WANT_AUTHOR_IDENT: |
| 354 | fputs(_("Author identity unknown\n"), stderr); |
| 355 | break; |
| 356 | case WANT_COMMITTER_IDENT: |
| 357 | fputs(_("Committer identity unknown\n"), stderr); |
| 358 | break; |
| 359 | default: |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | fputs(_("\n" |
| 364 | "*** Please tell me who you are.\n" |
| 365 | "\n" |
| 366 | "Run\n" |
| 367 | "\n" |
| 368 | " git config --global user.email \"you@example.com\"\n" |
| 369 | " git config --global user.name \"Your Name\"\n" |
| 370 | "\n" |
| 371 | "to set your account\'s default identity.\n" |
| 372 | "Omit --global to set the identity only in this repository.\n" |
| 373 | "\n"), stderr); |
| 374 | } |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 375 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 376 | const char *fmt_ident(const char *name, const char *email, |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 377 | enum want_ident whose_ident, const char *date_str, int flag) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 378 | { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 379 | static int index; |
| 380 | static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT }; |
Jeff King | f9bc573 | 2012-05-24 19:28:40 -0400 | [diff] [blame] | 381 | int strict = (flag & IDENT_STRICT); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 382 | int want_date = !(flag & IDENT_NO_DATE); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 383 | int want_name = !(flag & IDENT_NO_NAME); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 384 | |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 385 | struct strbuf *ident = &ident_pool[index]; |
| 386 | index = (index + 1) % ARRAY_SIZE(ident_pool); |
| 387 | |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 388 | if (!email) { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 389 | if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len) |
| 390 | email = git_author_email.buf; |
| 391 | else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len) |
| 392 | email = git_committer_email.buf; |
| 393 | } |
| 394 | if (!email) { |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 395 | if (strict && ident_use_config_only |
| 396 | && !(ident_config_given & IDENT_MAIL_GIVEN)) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 397 | ident_env_hint(whose_ident); |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 398 | die(_("no email was given and auto-detection is disabled")); |
| 399 | } |
| 400 | email = ident_default_email(); |
| 401 | if (strict && default_email_is_bogus) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 402 | ident_env_hint(whose_ident); |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 403 | die(_("unable to auto-detect email address (got '%s')"), email); |
| 404 | } |
| 405 | } |
| 406 | |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 407 | if (want_name) { |
| 408 | int using_default = 0; |
| 409 | if (!name) { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 410 | if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len) |
| 411 | name = git_author_name.buf; |
| 412 | else if (whose_ident == WANT_COMMITTER_IDENT && |
| 413 | git_committer_name.len) |
| 414 | name = git_committer_name.buf; |
| 415 | } |
| 416 | if (!name) { |
Marios Titas | 734c778 | 2016-03-30 22:29:42 +0300 | [diff] [blame] | 417 | if (strict && ident_use_config_only |
Marios Titas | d3c06c1 | 2016-03-30 22:29:43 +0300 | [diff] [blame] | 418 | && !(ident_config_given & IDENT_NAME_GIVEN)) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 419 | ident_env_hint(whose_ident); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 420 | die(_("no name was given and auto-detection is disabled")); |
Marios Titas | d3c06c1 | 2016-03-30 22:29:43 +0300 | [diff] [blame] | 421 | } |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 422 | name = ident_default_name(); |
| 423 | using_default = 1; |
| 424 | if (strict && default_name_is_bogus) { |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 425 | ident_env_hint(whose_ident); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 426 | die(_("unable to auto-detect name (got '%s')"), name); |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 427 | } |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 428 | } |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 429 | if (!*name) { |
| 430 | struct passwd *pw; |
| 431 | if (strict) { |
| 432 | if (using_default) |
Junio C Hamano | 9ed104e | 2020-08-21 13:36:28 -0700 | [diff] [blame] | 433 | ident_env_hint(whose_ident); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 434 | die(_("empty ident name (for <%s>) not allowed"), email); |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 435 | } |
| 436 | pw = xgetpwuid_self(NULL); |
| 437 | name = pw->pw_name; |
| 438 | } |
Jeff King | 13b9a24 | 2017-02-23 03:15:55 -0500 | [diff] [blame] | 439 | if (strict && !has_non_crud(name)) |
| 440 | die(_("name consists only of disallowed characters: %s"), name); |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 441 | } |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 442 | |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 443 | strbuf_reset(ident); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 444 | if (want_name) { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 445 | strbuf_addstr_without_crud(ident, name); |
| 446 | strbuf_addstr(ident, " <"); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 447 | } |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 448 | strbuf_addstr_without_crud(ident, email); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 449 | if (want_name) |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 450 | strbuf_addch(ident, '>'); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 451 | if (want_date) { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 452 | strbuf_addch(ident, ' '); |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 453 | if (date_str && date_str[0]) { |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 454 | if (parse_date(date_str, ident) < 0) |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 455 | die(_("invalid date format: %s"), date_str); |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 456 | } |
| 457 | else |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 458 | strbuf_addstr(ident, ident_default_date()); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 459 | } |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 460 | |
Phillip Wood | e8cbe21 | 2020-08-17 18:40:01 +0100 | [diff] [blame] | 461 | return ident->buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 462 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 463 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 464 | const char *fmt_name(enum want_ident whose_ident) |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 465 | { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 466 | char *name = NULL; |
| 467 | char *email = NULL; |
| 468 | |
| 469 | switch (whose_ident) { |
| 470 | case WANT_BLANK_IDENT: |
| 471 | break; |
| 472 | case WANT_AUTHOR_IDENT: |
| 473 | name = getenv("GIT_AUTHOR_NAME"); |
| 474 | email = getenv("GIT_AUTHOR_EMAIL"); |
| 475 | break; |
| 476 | case WANT_COMMITTER_IDENT: |
| 477 | name = getenv("GIT_COMMITTER_NAME"); |
| 478 | email = getenv("GIT_COMMITTER_EMAIL"); |
| 479 | break; |
| 480 | } |
| 481 | return fmt_ident(name, email, whose_ident, NULL, |
| 482 | IDENT_STRICT | IDENT_NO_DATE); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 485 | const char *git_author_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 486 | { |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 487 | if (getenv("GIT_AUTHOR_NAME")) |
| 488 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 489 | if (getenv("GIT_AUTHOR_EMAIL")) |
| 490 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 491 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 492 | getenv("GIT_AUTHOR_EMAIL"), |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 493 | WANT_AUTHOR_IDENT, |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 494 | getenv("GIT_AUTHOR_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 495 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 496 | } |
| 497 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 498 | const char *git_committer_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 499 | { |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 500 | if (getenv("GIT_COMMITTER_NAME")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 501 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 502 | if (getenv("GIT_COMMITTER_EMAIL")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 503 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 504 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 505 | getenv("GIT_COMMITTER_EMAIL"), |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 506 | WANT_COMMITTER_IDENT, |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 507 | getenv("GIT_COMMITTER_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 508 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 509 | } |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 510 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 511 | static int ident_is_sufficient(int user_ident_explicitly_given) |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 512 | { |
| 513 | #ifndef WINDOWS |
| 514 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); |
| 515 | #else |
| 516 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); |
| 517 | #endif |
| 518 | } |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 519 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 520 | int committer_ident_sufficiently_given(void) |
| 521 | { |
| 522 | return ident_is_sufficient(committer_ident_explicitly_given); |
| 523 | } |
| 524 | |
| 525 | int author_ident_sufficiently_given(void) |
| 526 | { |
| 527 | return ident_is_sufficient(author_ident_explicitly_given); |
| 528 | } |
| 529 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 530 | static int set_ident(const char *var, const char *value) |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 531 | { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 532 | if (!strcmp(var, "author.name")) { |
| 533 | if (!value) |
| 534 | return config_error_nonbool(var); |
| 535 | strbuf_reset(&git_author_name); |
| 536 | strbuf_addstr(&git_author_name, value); |
| 537 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 538 | ident_config_given |= IDENT_NAME_GIVEN; |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | if (!strcmp(var, "author.email")) { |
| 543 | if (!value) |
| 544 | return config_error_nonbool(var); |
| 545 | strbuf_reset(&git_author_email); |
| 546 | strbuf_addstr(&git_author_email, value); |
| 547 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 548 | ident_config_given |= IDENT_MAIL_GIVEN; |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | if (!strcmp(var, "committer.name")) { |
| 553 | if (!value) |
| 554 | return config_error_nonbool(var); |
| 555 | strbuf_reset(&git_committer_name); |
| 556 | strbuf_addstr(&git_committer_name, value); |
| 557 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 558 | ident_config_given |= IDENT_NAME_GIVEN; |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | if (!strcmp(var, "committer.email")) { |
| 563 | if (!value) |
| 564 | return config_error_nonbool(var); |
| 565 | strbuf_reset(&git_committer_email); |
| 566 | strbuf_addstr(&git_committer_email, value); |
| 567 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 568 | ident_config_given |= IDENT_MAIL_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 572 | if (!strcmp(var, "user.name")) { |
| 573 | if (!value) |
| 574 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 575 | strbuf_reset(&git_default_name); |
| 576 | strbuf_addstr(&git_default_name, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 577 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 578 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 579 | ident_config_given |= IDENT_NAME_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | if (!strcmp(var, "user.email")) { |
| 584 | if (!value) |
| 585 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 586 | strbuf_reset(&git_default_email); |
| 587 | strbuf_addstr(&git_default_email, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 588 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 589 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 590 | ident_config_given |= IDENT_MAIL_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 596 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 597 | int git_ident_config(const char *var, const char *value, void *data) |
| 598 | { |
| 599 | if (!strcmp(var, "user.useconfigonly")) { |
| 600 | ident_use_config_only = git_config_bool(var, value); |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | return set_ident(var, value); |
| 605 | } |
| 606 | |
Johannes Schindelin | fd5a584 | 2019-02-25 23:16:08 +0000 | [diff] [blame] | 607 | static void set_env_if(const char *key, const char *value, int *given, int bit) |
| 608 | { |
Thomas Gummerer | 0640897 | 2019-03-06 22:09:11 +0000 | [diff] [blame] | 609 | if ((*given & bit) || getenv(key)) |
Johannes Schindelin | fd5a584 | 2019-02-25 23:16:08 +0000 | [diff] [blame] | 610 | return; /* nothing to do */ |
| 611 | setenv(key, value, 0); |
| 612 | *given |= bit; |
| 613 | } |
| 614 | |
| 615 | void prepare_fallback_ident(const char *name, const char *email) |
| 616 | { |
| 617 | set_env_if("GIT_AUTHOR_NAME", name, |
| 618 | &author_ident_explicitly_given, IDENT_NAME_GIVEN); |
| 619 | set_env_if("GIT_AUTHOR_EMAIL", email, |
| 620 | &author_ident_explicitly_given, IDENT_MAIL_GIVEN); |
| 621 | set_env_if("GIT_COMMITTER_NAME", name, |
| 622 | &committer_ident_explicitly_given, IDENT_NAME_GIVEN); |
| 623 | set_env_if("GIT_COMMITTER_EMAIL", email, |
| 624 | &committer_ident_explicitly_given, IDENT_MAIL_GIVEN); |
| 625 | } |
| 626 | |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 627 | static int buf_cmp(const char *a_begin, const char *a_end, |
| 628 | const char *b_begin, const char *b_end) |
| 629 | { |
| 630 | int a_len = a_end - a_begin; |
| 631 | int b_len = b_end - b_begin; |
| 632 | int min = a_len < b_len ? a_len : b_len; |
| 633 | int cmp; |
| 634 | |
| 635 | cmp = memcmp(a_begin, b_begin, min); |
| 636 | if (cmp) |
| 637 | return cmp; |
| 638 | |
| 639 | return a_len - b_len; |
| 640 | } |
| 641 | |
| 642 | int ident_cmp(const struct ident_split *a, |
| 643 | const struct ident_split *b) |
| 644 | { |
| 645 | int cmp; |
| 646 | |
| 647 | cmp = buf_cmp(a->mail_begin, a->mail_end, |
| 648 | b->mail_begin, b->mail_end); |
| 649 | if (cmp) |
| 650 | return cmp; |
| 651 | |
| 652 | return buf_cmp(a->name_begin, a->name_end, |
| 653 | b->name_begin, b->name_end); |
| 654 | } |