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" |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 10 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 11 | static struct strbuf git_default_name = STRBUF_INIT; |
| 12 | static struct strbuf git_default_email = STRBUF_INIT; |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 13 | static struct strbuf git_default_date = STRBUF_INIT; |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 14 | static struct strbuf git_author_name = STRBUF_INIT; |
| 15 | static struct strbuf git_author_email = STRBUF_INIT; |
| 16 | static struct strbuf git_committer_name = STRBUF_INIT; |
| 17 | static struct strbuf git_committer_email = STRBUF_INIT; |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 18 | static int default_email_is_bogus; |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 19 | static int default_name_is_bogus; |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 20 | |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 21 | static int ident_use_config_only; |
| 22 | |
Jeff King | 4528023 | 2012-11-14 16:34:05 -0800 | [diff] [blame] | 23 | #define IDENT_NAME_GIVEN 01 |
| 24 | #define IDENT_MAIL_GIVEN 02 |
| 25 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 26 | static int committer_ident_explicitly_given; |
| 27 | static int author_ident_explicitly_given; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 28 | static int ident_config_given; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 29 | |
Rafael Gieschke | 590e081 | 2011-05-19 13:37:55 +0200 | [diff] [blame] | 30 | #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 King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 36 | static struct passwd *xgetpwuid_self(int *is_bogus) |
Jeff King | e850194 | 2015-12-10 16:33:05 -0500 | [diff] [blame] | 37 | { |
| 38 | struct passwd *pw; |
| 39 | |
| 40 | errno = 0; |
| 41 | pw = getpwuid(getuid()); |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 42 | 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 King | e850194 | 2015-12-10 16:33:05 -0500 | [diff] [blame] | 52 | return pw; |
| 53 | } |
| 54 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 55 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 56 | { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 57 | char *src; |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 58 | |
| 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 King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 63 | for (src = get_gecos(w); *src && *src != ','; src++) { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 64 | int ch = *src; |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 65 | if (ch != '&') |
| 66 | strbuf_addch(name, ch); |
| 67 | else { |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 68 | /* Sorry, Mr. McDonald... */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 69 | strbuf_addch(name, toupper(*w->pw_name)); |
| 70 | strbuf_addstr(name, w->pw_name + 1); |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
Junio C Hamano | e9bacb4 | 2005-09-19 16:06:56 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 75 | static int add_mailname_host(struct strbuf *buf) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 76 | { |
| 77 | FILE *mailname; |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 78 | struct strbuf mailnamebuf = STRBUF_INIT; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 79 | |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 80 | mailname = fopen_or_warn("/etc/mailname", "r"); |
| 81 | if (!mailname) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 82 | return -1; |
Nguyễn Thái Ngọc Duy | e9d983f | 2017-05-03 17:16:50 +0700 | [diff] [blame] | 83 | |
Junio C Hamano | 1f3b1ef | 2015-10-28 13:24:41 -0700 | [diff] [blame] | 84 | if (strbuf_getline(&mailnamebuf, mailname) == EOF) { |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 85 | if (ferror(mailname)) |
Nguyễn Thái Ngọc Duy | a26f4ed | 2016-05-08 16:47:49 +0700 | [diff] [blame] | 86 | warning_errno("cannot read /etc/mailname"); |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 87 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 88 | fclose(mailname); |
| 89 | return -1; |
| 90 | } |
| 91 | /* success! */ |
Jonathan Nieder | dc342a2 | 2013-01-24 15:21:46 -0800 | [diff] [blame] | 92 | strbuf_addbuf(buf, &mailnamebuf); |
| 93 | strbuf_release(&mailnamebuf); |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 94 | fclose(mailname); |
| 95 | return 0; |
| 96 | } |
| 97 | |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 98 | static 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 King | c375a7e | 2016-09-23 00:37:53 -0400 | [diff] [blame] | 107 | if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) { |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 108 | strbuf_addstr(out, ai->ai_canonname); |
| 109 | status = 0; |
| 110 | } |
| 111 | freeaddrinfo(ai); |
| 112 | } |
| 113 | #else |
Jeff King | 58d29ec | 2015-12-14 15:52:41 -0500 | [diff] [blame] | 114 | struct hostent *he = gethostbyname(host); |
Elia Pinto | 00bce77 | 2015-11-27 14:08:27 +0000 | [diff] [blame] | 115 | 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 King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 124 | static void add_domainname(struct strbuf *out, int *is_bogus) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 125 | { |
René Scharfe | da25bdb | 2017-04-18 17:57:42 -0400 | [diff] [blame] | 126 | char buf[HOST_NAME_MAX + 1]; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 127 | |
David Turner | 5781a9a | 2017-04-18 17:57:43 -0400 | [diff] [blame] | 128 | if (xgethostname(buf, sizeof(buf))) { |
Nguyễn Thái Ngọc Duy | a26f4ed | 2016-05-08 16:47:49 +0700 | [diff] [blame] | 129 | warning_errno("cannot get host name"); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 130 | strbuf_addstr(out, "(none)"); |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 131 | *is_bogus = 1; |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 132 | return; |
| 133 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 134 | if (strchr(buf, '.')) |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 135 | strbuf_addstr(out, buf); |
Junio C Hamano | 5498c57 | 2015-12-21 10:59:07 -0800 | [diff] [blame] | 136 | else if (canonical_name(buf, out) < 0) { |
Jeff King | f8254d3 | 2012-05-21 19:10:23 -0400 | [diff] [blame] | 137 | strbuf_addf(out, "%s.(none)", buf); |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 138 | *is_bogus = 1; |
| 139 | } |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 140 | } |
| 141 | |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 142 | static void copy_email(const struct passwd *pw, struct strbuf *email, |
| 143 | int *is_bogus) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 144 | { |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 145 | /* |
| 146 | * Make up a fake email address |
| 147 | * (name + '@' + hostname [+ '.' + domainname]) |
| 148 | */ |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 149 | strbuf_addstr(email, pw->pw_name); |
| 150 | strbuf_addch(email, '@'); |
Petr Baudis | adc3dbc | 2005-10-21 03:57:39 +0200 | [diff] [blame] | 151 | |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 152 | if (!add_mailname_host(email)) |
Jonathan Nieder | 8a55caa | 2011-10-03 01:16:33 -0500 | [diff] [blame] | 153 | return; /* read from "/etc/mailname" (Debian) */ |
Jeff King | 19ce497 | 2015-12-10 16:35:36 -0500 | [diff] [blame] | 154 | add_domainname(email, is_bogus); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Matthieu Moy | 9830534 | 2014-07-25 21:11:34 +0200 | [diff] [blame] | 157 | const char *ident_default_name(void) |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 158 | { |
Jeff King | 9442555 | 2017-02-23 03:17:08 -0500 | [diff] [blame] | 159 | if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) { |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 160 | copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 161 | strbuf_trim(&git_default_name); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 162 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 163 | return git_default_name.buf; |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 164 | } |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 165 | |
Jeff King | bcb2b00 | 2012-05-21 19:09:43 -0400 | [diff] [blame] | 166 | const char *ident_default_email(void) |
| 167 | { |
Jeff King | 9442555 | 2017-02-23 03:17:08 -0500 | [diff] [blame] | 168 | if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) { |
Matt Kraai | 46f74f0 | 2007-07-05 17:29:41 -0700 | [diff] [blame] | 169 | const char *email = getenv("EMAIL"); |
| 170 | |
Junio C Hamano | 99178c8 | 2010-01-08 08:01:10 -0800 | [diff] [blame] | 171 | if (email && email[0]) { |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 172 | strbuf_addstr(&git_default_email, email); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 173 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 174 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Johannes Schindelin | 501afcb | 2018-10-15 02:47:08 -0700 | [diff] [blame] | 175 | } else if ((email = query_user_email()) && email[0]) { |
| 176 | strbuf_addstr(&git_default_email, email); |
| 177 | free((char *)email); |
Jeff King | 2f70587 | 2012-05-21 19:10:20 -0400 | [diff] [blame] | 178 | } else |
Jeff King | 92bcbb9 | 2015-12-10 16:41:29 -0500 | [diff] [blame] | 179 | copy_email(xgetpwuid_self(&default_email_is_bogus), |
| 180 | &git_default_email, &default_email_is_bogus); |
Jeff King | be641ab | 2012-05-21 19:10:29 -0400 | [diff] [blame] | 181 | strbuf_trim(&git_default_email); |
Junio C Hamano | 0175476 | 2007-01-28 00:50:53 -0800 | [diff] [blame] | 182 | } |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 183 | return git_default_email.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Junio C Hamano | dad148c | 2012-09-15 22:50:09 -0700 | [diff] [blame] | 186 | static const char *ident_default_date(void) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 187 | { |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 188 | if (!git_default_date.len) |
| 189 | datestamp(&git_default_date); |
| 190 | return git_default_date.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jeff King | 4d9c7e6 | 2016-08-01 15:37:00 -0400 | [diff] [blame] | 193 | void reset_ident_date(void) |
| 194 | { |
| 195 | strbuf_reset(&git_default_date); |
| 196 | } |
| 197 | |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 198 | static int crud(unsigned char c) |
| 199 | { |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 200 | return c <= 32 || |
| 201 | c == '.' || |
| 202 | c == ',' || |
| 203 | c == ':' || |
| 204 | c == ';' || |
| 205 | c == '<' || |
| 206 | c == '>' || |
| 207 | c == '"' || |
Linus Torvalds | d404bf0 | 2008-12-01 08:41:50 -0800 | [diff] [blame] | 208 | c == '\\' || |
Alex Riesen | f64c81d | 2007-12-03 20:11:43 +0100 | [diff] [blame] | 209 | c == '\''; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Jeff King | 13b9a24 | 2017-02-23 03:15:55 -0500 | [diff] [blame] | 212 | static 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 Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 221 | /* |
| 222 | * Copy over a string to the destination, but avoid special |
| 223 | * characters ('\n', '<' and '>') and remove crud at the end |
| 224 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 225 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 226 | { |
Luiz Fernando N. Capitulino | b073211 | 2007-04-15 15:51:29 -0300 | [diff] [blame] | 227 | size_t i, len; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 228 | 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 Hamano | 82f9d58 | 2005-12-29 01:30:08 -0800 | [diff] [blame] | 248 | * characters '\n' '<' and '>' that act as delimiters on |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 249 | * an identification line. We can only remove crud, never add it, |
| 250 | * so 'len' is our maximum. |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 251 | */ |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 252 | strbuf_grow(sb, len); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 253 | for (i = 0; i < len; i++) { |
| 254 | c = *src++; |
| 255 | switch (c) { |
| 256 | case '\n': case '<': case '>': |
| 257 | continue; |
| 258 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 259 | sb->buf[sb->len++] = c; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 260 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 261 | sb->buf[sb->len] = '\0'; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 264 | /* |
| 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 | */ |
| 271 | int 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 King | d9955fd | 2012-05-22 02:12:20 -0400 | [diff] [blame] | 288 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 289 | if (!isspace(*cp)) { |
| 290 | split->name_end = cp + 1; |
| 291 | break; |
| 292 | } |
Junio C Hamano | e27ddb6 | 2012-08-31 14:54:18 -0700 | [diff] [blame] | 293 | if (!split->name_end) { |
| 294 | /* no human readable name */ |
| 295 | split->name_end = split->name_begin; |
| 296 | } |
Junio C Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 297 | |
| 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 King | 03818a4 | 2013-10-14 18:45:00 -0400 | [diff] [blame] | 306 | /* |
| 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 Hamano | 4b340cf | 2012-03-11 01:25:43 -0800 | [diff] [blame] | 321 | ; |
| 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 | |
| 340 | person_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 Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 348 | static const char *env_hint = |
Vasco Almeida | 166e55e | 2016-09-19 13:08:19 +0000 | [diff] [blame] | 349 | N_("\n" |
| 350 | "*** Please tell me who you are.\n" |
| 351 | "\n" |
| 352 | "Run\n" |
| 353 | "\n" |
| 354 | " git config --global user.email \"you@example.com\"\n" |
| 355 | " git config --global user.name \"Your Name\"\n" |
| 356 | "\n" |
| 357 | "to set your account\'s default identity.\n" |
| 358 | "Omit --global to set the identity only in this repository.\n" |
| 359 | "\n"); |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 360 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 361 | const char *fmt_ident(const char *name, const char *email, |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 362 | enum want_ident whose_ident, const char *date_str, int flag) |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 363 | { |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 364 | static struct strbuf ident = STRBUF_INIT; |
Jeff King | f9bc573 | 2012-05-24 19:28:40 -0400 | [diff] [blame] | 365 | int strict = (flag & IDENT_STRICT); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 366 | int want_date = !(flag & IDENT_NO_DATE); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 367 | int want_name = !(flag & IDENT_NO_NAME); |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 368 | |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 369 | if (!email) { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 370 | if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len) |
| 371 | email = git_author_email.buf; |
| 372 | else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len) |
| 373 | email = git_committer_email.buf; |
| 374 | } |
| 375 | if (!email) { |
Jeff King | 862e80a | 2017-02-23 03:13:53 -0500 | [diff] [blame] | 376 | if (strict && ident_use_config_only |
| 377 | && !(ident_config_given & IDENT_MAIL_GIVEN)) { |
| 378 | fputs(_(env_hint), stderr); |
| 379 | die(_("no email was given and auto-detection is disabled")); |
| 380 | } |
| 381 | email = ident_default_email(); |
| 382 | if (strict && default_email_is_bogus) { |
| 383 | fputs(_(env_hint), stderr); |
| 384 | die(_("unable to auto-detect email address (got '%s')"), email); |
| 385 | } |
| 386 | } |
| 387 | |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 388 | if (want_name) { |
| 389 | int using_default = 0; |
| 390 | if (!name) { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 391 | if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len) |
| 392 | name = git_author_name.buf; |
| 393 | else if (whose_ident == WANT_COMMITTER_IDENT && |
| 394 | git_committer_name.len) |
| 395 | name = git_committer_name.buf; |
| 396 | } |
| 397 | if (!name) { |
Marios Titas | 734c778 | 2016-03-30 22:29:42 +0300 | [diff] [blame] | 398 | if (strict && ident_use_config_only |
Marios Titas | d3c06c1 | 2016-03-30 22:29:43 +0300 | [diff] [blame] | 399 | && !(ident_config_given & IDENT_NAME_GIVEN)) { |
Vasco Almeida | 166e55e | 2016-09-19 13:08:19 +0000 | [diff] [blame] | 400 | fputs(_(env_hint), stderr); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 401 | die(_("no name was given and auto-detection is disabled")); |
Marios Titas | d3c06c1 | 2016-03-30 22:29:43 +0300 | [diff] [blame] | 402 | } |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 403 | name = ident_default_name(); |
| 404 | using_default = 1; |
| 405 | if (strict && default_name_is_bogus) { |
Vasco Almeida | 166e55e | 2016-09-19 13:08:19 +0000 | [diff] [blame] | 406 | fputs(_(env_hint), stderr); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 407 | die(_("unable to auto-detect name (got '%s')"), name); |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 408 | } |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 409 | } |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 410 | if (!*name) { |
| 411 | struct passwd *pw; |
| 412 | if (strict) { |
| 413 | if (using_default) |
Vasco Almeida | 166e55e | 2016-09-19 13:08:19 +0000 | [diff] [blame] | 414 | fputs(_(env_hint), stderr); |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 415 | die(_("empty ident name (for <%s>) not allowed"), email); |
Jeff King | 59f9295 | 2016-02-04 11:12:38 +0200 | [diff] [blame] | 416 | } |
| 417 | pw = xgetpwuid_self(NULL); |
| 418 | name = pw->pw_name; |
| 419 | } |
Jeff King | 13b9a24 | 2017-02-23 03:15:55 -0500 | [diff] [blame] | 420 | if (strict && !has_non_crud(name)) |
| 421 | die(_("name consists only of disallowed characters: %s"), name); |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 422 | } |
Junio C Hamano | dfdd309 | 2006-02-07 13:19:10 -0800 | [diff] [blame] | 423 | |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 424 | strbuf_reset(&ident); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 425 | if (want_name) { |
| 426 | strbuf_addstr_without_crud(&ident, name); |
| 427 | strbuf_addstr(&ident, " <"); |
| 428 | } |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 429 | strbuf_addstr_without_crud(&ident, email); |
Jeff King | c15e198 | 2012-05-24 19:27:24 -0400 | [diff] [blame] | 430 | if (want_name) |
| 431 | strbuf_addch(&ident, '>'); |
Jeff King | 359b27a | 2012-05-24 19:26:50 -0400 | [diff] [blame] | 432 | if (want_date) { |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 433 | strbuf_addch(&ident, ' '); |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 434 | if (date_str && date_str[0]) { |
| 435 | if (parse_date(date_str, &ident) < 0) |
Jeff King | afb6c30 | 2017-02-23 03:12:30 -0500 | [diff] [blame] | 436 | die(_("invalid date format: %s"), date_str); |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 437 | } |
| 438 | else |
| 439 | strbuf_addstr(&ident, ident_default_date()); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 440 | } |
Jeff King | c33ddc2 | 2014-08-27 03:57:08 -0400 | [diff] [blame] | 441 | |
Jeff King | c96f0c8 | 2012-05-21 19:10:26 -0400 | [diff] [blame] | 442 | return ident.buf; |
Linus Torvalds | 6aa33f4 | 2005-07-12 11:49:27 -0700 | [diff] [blame] | 443 | } |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 444 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 445 | const char *fmt_name(enum want_ident whose_ident) |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 446 | { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 447 | char *name = NULL; |
| 448 | char *email = NULL; |
| 449 | |
| 450 | switch (whose_ident) { |
| 451 | case WANT_BLANK_IDENT: |
| 452 | break; |
| 453 | case WANT_AUTHOR_IDENT: |
| 454 | name = getenv("GIT_AUTHOR_NAME"); |
| 455 | email = getenv("GIT_AUTHOR_EMAIL"); |
| 456 | break; |
| 457 | case WANT_COMMITTER_IDENT: |
| 458 | name = getenv("GIT_COMMITTER_NAME"); |
| 459 | email = getenv("GIT_COMMITTER_EMAIL"); |
| 460 | break; |
| 461 | } |
| 462 | return fmt_ident(name, email, whose_ident, NULL, |
| 463 | IDENT_STRICT | IDENT_NO_DATE); |
Junio C Hamano | d9ccfe7 | 2007-12-02 13:43:34 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 466 | const char *git_author_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 467 | { |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 468 | if (getenv("GIT_AUTHOR_NAME")) |
| 469 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 470 | if (getenv("GIT_AUTHOR_EMAIL")) |
| 471 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 472 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 473 | getenv("GIT_AUTHOR_EMAIL"), |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 474 | WANT_AUTHOR_IDENT, |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 475 | getenv("GIT_AUTHOR_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 476 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 477 | } |
| 478 | |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 479 | const char *git_committer_info(int flag) |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 480 | { |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 481 | if (getenv("GIT_COMMITTER_NAME")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 482 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Junio C Hamano | 91c38a2 | 2010-01-08 07:39:11 -0800 | [diff] [blame] | 483 | if (getenv("GIT_COMMITTER_EMAIL")) |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 484 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Junio C Hamano | 798123a | 2007-02-04 17:50:14 -0800 | [diff] [blame] | 485 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
Junio C Hamano | c7d77da | 2005-11-21 23:44:35 -0800 | [diff] [blame] | 486 | getenv("GIT_COMMITTER_EMAIL"), |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 487 | WANT_COMMITTER_IDENT, |
Junio C Hamano | 749be72 | 2006-02-18 20:31:05 -0800 | [diff] [blame] | 488 | getenv("GIT_COMMITTER_DATE"), |
Junio C Hamano | 774751a | 2007-12-08 17:32:08 -0800 | [diff] [blame] | 489 | flag); |
Eric W. Biederman | d289d13 | 2005-07-14 18:50:33 -0600 | [diff] [blame] | 490 | } |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 491 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 492 | static int ident_is_sufficient(int user_ident_explicitly_given) |
Junio C Hamano | 5aeb3a3 | 2010-01-17 13:54:28 -0800 | [diff] [blame] | 493 | { |
| 494 | #ifndef WINDOWS |
| 495 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); |
| 496 | #else |
| 497 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); |
| 498 | #endif |
| 499 | } |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 500 | |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 501 | int committer_ident_sufficiently_given(void) |
| 502 | { |
| 503 | return ident_is_sufficient(committer_ident_explicitly_given); |
| 504 | } |
| 505 | |
| 506 | int author_ident_sufficiently_given(void) |
| 507 | { |
| 508 | return ident_is_sufficient(author_ident_explicitly_given); |
| 509 | } |
| 510 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 511 | static int set_ident(const char *var, const char *value) |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 512 | { |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 513 | if (!strcmp(var, "author.name")) { |
| 514 | if (!value) |
| 515 | return config_error_nonbool(var); |
| 516 | strbuf_reset(&git_author_name); |
| 517 | strbuf_addstr(&git_author_name, value); |
| 518 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 519 | ident_config_given |= IDENT_NAME_GIVEN; |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | if (!strcmp(var, "author.email")) { |
| 524 | if (!value) |
| 525 | return config_error_nonbool(var); |
| 526 | strbuf_reset(&git_author_email); |
| 527 | strbuf_addstr(&git_author_email, value); |
| 528 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 529 | ident_config_given |= IDENT_MAIL_GIVEN; |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | if (!strcmp(var, "committer.name")) { |
| 534 | if (!value) |
| 535 | return config_error_nonbool(var); |
| 536 | strbuf_reset(&git_committer_name); |
| 537 | strbuf_addstr(&git_committer_name, value); |
| 538 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 539 | ident_config_given |= IDENT_NAME_GIVEN; |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | if (!strcmp(var, "committer.email")) { |
| 544 | if (!value) |
| 545 | return config_error_nonbool(var); |
| 546 | strbuf_reset(&git_committer_email); |
| 547 | strbuf_addstr(&git_committer_email, value); |
| 548 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 549 | ident_config_given |= IDENT_MAIL_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 553 | if (!strcmp(var, "user.name")) { |
| 554 | if (!value) |
| 555 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 556 | strbuf_reset(&git_default_name); |
| 557 | strbuf_addstr(&git_default_name, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 558 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 559 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 560 | ident_config_given |= IDENT_NAME_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | if (!strcmp(var, "user.email")) { |
| 565 | if (!value) |
| 566 | return config_error_nonbool(var); |
Jeff King | 8587ead | 2012-05-21 19:10:17 -0400 | [diff] [blame] | 567 | strbuf_reset(&git_default_email); |
| 568 | strbuf_addstr(&git_default_email, value); |
Jeff King | d6991ce | 2012-11-14 16:34:13 -0800 | [diff] [blame] | 569 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 570 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
Dan Aloni | 4d5c295 | 2016-02-06 08:23:36 +0200 | [diff] [blame] | 571 | ident_config_given |= IDENT_MAIL_GIVEN; |
Jeff King | 9597921 | 2012-05-21 19:09:54 -0400 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 577 | |
William Hubbs | 39ab4d0 | 2019-02-04 12:48:50 -0600 | [diff] [blame] | 578 | int git_ident_config(const char *var, const char *value, void *data) |
| 579 | { |
| 580 | if (!strcmp(var, "user.useconfigonly")) { |
| 581 | ident_use_config_only = git_config_bool(var, value); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | return set_ident(var, value); |
| 586 | } |
| 587 | |
Johannes Schindelin | fd5a584 | 2019-02-25 23:16:08 +0000 | [diff] [blame] | 588 | static void set_env_if(const char *key, const char *value, int *given, int bit) |
| 589 | { |
Thomas Gummerer | 0640897 | 2019-03-06 22:09:11 +0000 | [diff] [blame] | 590 | if ((*given & bit) || getenv(key)) |
Johannes Schindelin | fd5a584 | 2019-02-25 23:16:08 +0000 | [diff] [blame] | 591 | return; /* nothing to do */ |
| 592 | setenv(key, value, 0); |
| 593 | *given |= bit; |
| 594 | } |
| 595 | |
| 596 | void prepare_fallback_ident(const char *name, const char *email) |
| 597 | { |
| 598 | set_env_if("GIT_AUTHOR_NAME", name, |
| 599 | &author_ident_explicitly_given, IDENT_NAME_GIVEN); |
| 600 | set_env_if("GIT_AUTHOR_EMAIL", email, |
| 601 | &author_ident_explicitly_given, IDENT_MAIL_GIVEN); |
| 602 | set_env_if("GIT_COMMITTER_NAME", name, |
| 603 | &committer_ident_explicitly_given, IDENT_NAME_GIVEN); |
| 604 | set_env_if("GIT_COMMITTER_EMAIL", email, |
| 605 | &committer_ident_explicitly_given, IDENT_MAIL_GIVEN); |
| 606 | } |
| 607 | |
Jeff King | 662cc30 | 2013-09-20 06:16:28 -0400 | [diff] [blame] | 608 | static int buf_cmp(const char *a_begin, const char *a_end, |
| 609 | const char *b_begin, const char *b_end) |
| 610 | { |
| 611 | int a_len = a_end - a_begin; |
| 612 | int b_len = b_end - b_begin; |
| 613 | int min = a_len < b_len ? a_len : b_len; |
| 614 | int cmp; |
| 615 | |
| 616 | cmp = memcmp(a_begin, b_begin, min); |
| 617 | if (cmp) |
| 618 | return cmp; |
| 619 | |
| 620 | return a_len - b_len; |
| 621 | } |
| 622 | |
| 623 | int ident_cmp(const struct ident_split *a, |
| 624 | const struct ident_split *b) |
| 625 | { |
| 626 | int cmp; |
| 627 | |
| 628 | cmp = buf_cmp(a->mail_begin, a->mail_end, |
| 629 | b->mail_begin, b->mail_end); |
| 630 | if (cmp) |
| 631 | return cmp; |
| 632 | |
| 633 | return buf_cmp(a->name_begin, a->name_end, |
| 634 | b->name_begin, b->name_end); |
| 635 | } |