blob: 5ff1aadaaaa999df3bfecb07f84f259469b3a54d [file] [log] [blame]
Linus Torvalds6aa33f42005-07-12 11:49:27 -07001/*
2 * ident.c
3 *
4 * create git identifier lines of the form "name <email> date"
5 *
6 * Copyright (C) 2005 Linus Torvalds
7 */
8#include "cache.h"
9
Jeff King8587ead2012-05-21 19:10:17 -040010static struct strbuf git_default_name = STRBUF_INIT;
11static struct strbuf git_default_email = STRBUF_INIT;
Jeff Kingc33ddc22014-08-27 03:57:08 -040012static struct strbuf git_default_date = STRBUF_INIT;
Jeff King45280232012-11-14 16:34:05 -080013
14#define IDENT_NAME_GIVEN 01
15#define IDENT_MAIL_GIVEN 02
16#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
Jeff Kingd6991ce2012-11-14 16:34:13 -080017static int committer_ident_explicitly_given;
18static int author_ident_explicitly_given;
Linus Torvalds6aa33f42005-07-12 11:49:27 -070019
Rafael Gieschke590e0812011-05-19 13:37:55 +020020#ifdef NO_GECOS_IN_PWENT
21#define get_gecos(ignored) "&"
22#else
23#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
24#endif
25
Jeff King8587ead2012-05-21 19:10:17 -040026static void copy_gecos(const struct passwd *w, struct strbuf *name)
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070027{
Jeff King8587ead2012-05-21 19:10:17 -040028 char *src;
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070029
30 /* Traditionally GECOS field had office phone numbers etc, separated
31 * with commas. Also & stands for capitalized form of the login name.
32 */
33
Jeff King8587ead2012-05-21 19:10:17 -040034 for (src = get_gecos(w); *src && *src != ','; src++) {
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070035 int ch = *src;
Jeff King8587ead2012-05-21 19:10:17 -040036 if (ch != '&')
37 strbuf_addch(name, ch);
38 else {
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070039 /* Sorry, Mr. McDonald... */
Jeff King8587ead2012-05-21 19:10:17 -040040 strbuf_addch(name, toupper(*w->pw_name));
41 strbuf_addstr(name, w->pw_name + 1);
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070042 }
43 }
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070044}
45
Jeff King8587ead2012-05-21 19:10:17 -040046static int add_mailname_host(struct strbuf *buf)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050047{
48 FILE *mailname;
Jonathan Niederdc342a22013-01-24 15:21:46 -080049 struct strbuf mailnamebuf = STRBUF_INIT;
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050050
51 mailname = fopen("/etc/mailname", "r");
52 if (!mailname) {
53 if (errno != ENOENT)
54 warning("cannot open /etc/mailname: %s",
55 strerror(errno));
56 return -1;
57 }
Jonathan Niederdc342a22013-01-24 15:21:46 -080058 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050059 if (ferror(mailname))
60 warning("cannot read /etc/mailname: %s",
61 strerror(errno));
Jonathan Niederdc342a22013-01-24 15:21:46 -080062 strbuf_release(&mailnamebuf);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050063 fclose(mailname);
64 return -1;
65 }
66 /* success! */
Jonathan Niederdc342a22013-01-24 15:21:46 -080067 strbuf_addbuf(buf, &mailnamebuf);
68 strbuf_release(&mailnamebuf);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050069 fclose(mailname);
70 return 0;
71}
72
Jeff King8587ead2012-05-21 19:10:17 -040073static void add_domainname(struct strbuf *out)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050074{
Jeff King8587ead2012-05-21 19:10:17 -040075 char buf[1024];
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050076 struct hostent *he;
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050077
Jeff King8587ead2012-05-21 19:10:17 -040078 if (gethostname(buf, sizeof(buf))) {
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050079 warning("cannot get host name: %s", strerror(errno));
Jeff King8587ead2012-05-21 19:10:17 -040080 strbuf_addstr(out, "(none)");
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050081 return;
82 }
Jeff King8587ead2012-05-21 19:10:17 -040083 if (strchr(buf, '.'))
Jeff Kingf8254d32012-05-21 19:10:23 -040084 strbuf_addstr(out, buf);
85 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
86 strbuf_addstr(out, he->h_name);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050087 else
Jeff Kingf8254d32012-05-21 19:10:23 -040088 strbuf_addf(out, "%s.(none)", buf);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050089}
90
Jeff King8587ead2012-05-21 19:10:17 -040091static void copy_email(const struct passwd *pw, struct strbuf *email)
Linus Torvalds6aa33f42005-07-12 11:49:27 -070092{
Junio C Hamano01754762007-01-28 00:50:53 -080093 /*
94 * Make up a fake email address
95 * (name + '@' + hostname [+ '.' + domainname])
96 */
Jeff King8587ead2012-05-21 19:10:17 -040097 strbuf_addstr(email, pw->pw_name);
98 strbuf_addch(email, '@');
Petr Baudisadc3dbc2005-10-21 03:57:39 +020099
Jeff King8587ead2012-05-21 19:10:17 -0400100 if (!add_mailname_host(email))
Jonathan Nieder8a55caa2011-10-03 01:16:33 -0500101 return; /* read from "/etc/mailname" (Debian) */
Jeff King8587ead2012-05-21 19:10:17 -0400102 add_domainname(email);
Junio C Hamano01754762007-01-28 00:50:53 -0800103}
104
Matthieu Moy98305342014-07-25 21:11:34 +0200105const char *ident_default_name(void)
Junio C Hamano01754762007-01-28 00:50:53 -0800106{
Jeff Kingbe641ab2012-05-21 19:10:29 -0400107 if (!git_default_name.len) {
Jeff King2f705872012-05-21 19:10:20 -0400108 copy_gecos(xgetpwuid_self(), &git_default_name);
Jeff Kingbe641ab2012-05-21 19:10:29 -0400109 strbuf_trim(&git_default_name);
Junio C Hamano01754762007-01-28 00:50:53 -0800110 }
Jeff King8587ead2012-05-21 19:10:17 -0400111 return git_default_name.buf;
Jeff Kingbcb2b002012-05-21 19:09:43 -0400112}
Junio C Hamano01754762007-01-28 00:50:53 -0800113
Jeff Kingbcb2b002012-05-21 19:09:43 -0400114const char *ident_default_email(void)
115{
Jeff King8587ead2012-05-21 19:10:17 -0400116 if (!git_default_email.len) {
Matt Kraai46f74f02007-07-05 17:29:41 -0700117 const char *email = getenv("EMAIL");
118
Junio C Hamano99178c82010-01-08 08:01:10 -0800119 if (email && email[0]) {
Jeff King8587ead2012-05-21 19:10:17 -0400120 strbuf_addstr(&git_default_email, email);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800121 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
122 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Jeff King2f705872012-05-21 19:10:20 -0400123 } else
124 copy_email(xgetpwuid_self(), &git_default_email);
Jeff Kingbe641ab2012-05-21 19:10:29 -0400125 strbuf_trim(&git_default_email);
Junio C Hamano01754762007-01-28 00:50:53 -0800126 }
Jeff King8587ead2012-05-21 19:10:17 -0400127 return git_default_email.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700128}
129
Junio C Hamanodad148c2012-09-15 22:50:09 -0700130static const char *ident_default_date(void)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700131{
Jeff Kingc33ddc22014-08-27 03:57:08 -0400132 if (!git_default_date.len)
133 datestamp(&git_default_date);
134 return git_default_date.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700135}
136
137static int crud(unsigned char c)
138{
Alex Riesenf64c81d2007-12-03 20:11:43 +0100139 return c <= 32 ||
140 c == '.' ||
141 c == ',' ||
142 c == ':' ||
143 c == ';' ||
144 c == '<' ||
145 c == '>' ||
146 c == '"' ||
Linus Torvaldsd404bf02008-12-01 08:41:50 -0800147 c == '\\' ||
Alex Riesenf64c81d2007-12-03 20:11:43 +0100148 c == '\'';
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700149}
150
151/*
152 * Copy over a string to the destination, but avoid special
153 * characters ('\n', '<' and '>') and remove crud at the end
154 */
Jeff Kingc96f0c82012-05-21 19:10:26 -0400155static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700156{
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300157 size_t i, len;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700158 unsigned char c;
159
160 /* Remove crud from the beginning.. */
161 while ((c = *src) != 0) {
162 if (!crud(c))
163 break;
164 src++;
165 }
166
167 /* Remove crud from the end.. */
168 len = strlen(src);
169 while (len > 0) {
170 c = src[len-1];
171 if (!crud(c))
172 break;
173 --len;
174 }
175
176 /*
177 * Copy the rest to the buffer, but avoid the special
Junio C Hamano82f9d582005-12-29 01:30:08 -0800178 * characters '\n' '<' and '>' that act as delimiters on
Jeff Kingc96f0c82012-05-21 19:10:26 -0400179 * an identification line. We can only remove crud, never add it,
180 * so 'len' is our maximum.
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700181 */
Jeff Kingc96f0c82012-05-21 19:10:26 -0400182 strbuf_grow(sb, len);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700183 for (i = 0; i < len; i++) {
184 c = *src++;
185 switch (c) {
186 case '\n': case '<': case '>':
187 continue;
188 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400189 sb->buf[sb->len++] = c;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700190 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400191 sb->buf[sb->len] = '\0';
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700192}
193
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800194/*
195 * Reverse of fmt_ident(); given an ident line, split the fields
196 * to allow the caller to parse it.
197 * Signal a success by returning 0, but date/tz fields of the result
198 * can still be NULL if the input line only has the name/email part
199 * (e.g. reading from a reflog entry).
200 */
201int split_ident_line(struct ident_split *split, const char *line, int len)
202{
203 const char *cp;
204 size_t span;
205 int status = -1;
206
207 memset(split, 0, sizeof(*split));
208
209 split->name_begin = line;
210 for (cp = line; *cp && cp < line + len; cp++)
211 if (*cp == '<') {
212 split->mail_begin = cp + 1;
213 break;
214 }
215 if (!split->mail_begin)
216 return status;
217
Jeff Kingd9955fd2012-05-22 02:12:20 -0400218 for (cp = split->mail_begin - 2; line <= cp; cp--)
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800219 if (!isspace(*cp)) {
220 split->name_end = cp + 1;
221 break;
222 }
Junio C Hamanoe27ddb62012-08-31 14:54:18 -0700223 if (!split->name_end) {
224 /* no human readable name */
225 split->name_end = split->name_begin;
226 }
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800227
228 for (cp = split->mail_begin; cp < line + len; cp++)
229 if (*cp == '>') {
230 split->mail_end = cp;
231 break;
232 }
233 if (!split->mail_end)
234 return status;
235
Jeff King03818a42013-10-14 18:45:00 -0400236 /*
237 * Look from the end-of-line to find the trailing ">" of the mail
238 * address, even though we should already know it as split->mail_end.
239 * This can help in cases of broken idents with an extra ">" somewhere
240 * in the email address. Note that we are assuming the timestamp will
241 * never have a ">" in it.
242 *
243 * Note that we will always find some ">" before going off the front of
244 * the string, because will always hit the split->mail_end closing
245 * bracket.
246 */
247 for (cp = line + len - 1; *cp != '>'; cp--)
248 ;
249
250 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800251 ;
252 if (line + len <= cp)
253 goto person_only;
254 split->date_begin = cp;
255 span = strspn(cp, "0123456789");
256 if (!span)
257 goto person_only;
258 split->date_end = split->date_begin + span;
259 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
260 ;
261 if (line + len <= cp || (*cp != '+' && *cp != '-'))
262 goto person_only;
263 split->tz_begin = cp;
264 span = strspn(cp + 1, "0123456789");
265 if (!span)
266 goto person_only;
267 split->tz_end = split->tz_begin + 1 + span;
268 return 0;
269
270person_only:
271 split->date_begin = NULL;
272 split->date_end = NULL;
273 split->tz_begin = NULL;
274 split->tz_end = NULL;
275 return 0;
276}
277
Junio C Hamano749be722006-02-18 20:31:05 -0800278static const char *env_hint =
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100279"\n"
Santi Béjar6c293d42008-03-08 12:30:04 +0100280"*** Please tell me who you are.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100281"\n"
282"Run\n"
283"\n"
David Symonds8e7425d2007-12-07 10:36:45 +1100284" git config --global user.email \"you@example.com\"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200285" git config --global user.name \"Your Name\"\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100286"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200287"to set your account\'s default identity.\n"
288"Omit --global to set the identity only in this repository.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100289"\n";
Junio C Hamano749be722006-02-18 20:31:05 -0800290
Junio C Hamano774751a2007-12-08 17:32:08 -0800291const char *fmt_ident(const char *name, const char *email,
292 const char *date_str, int flag)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700293{
Jeff Kingc96f0c82012-05-21 19:10:26 -0400294 static struct strbuf ident = STRBUF_INIT;
Jeff Kingf9bc5732012-05-24 19:28:40 -0400295 int strict = (flag & IDENT_STRICT);
Jeff King359b27a2012-05-24 19:26:50 -0400296 int want_date = !(flag & IDENT_NO_DATE);
Jeff Kingc15e1982012-05-24 19:27:24 -0400297 int want_name = !(flag & IDENT_NO_NAME);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700298
Jeff Kingc15e1982012-05-24 19:27:24 -0400299 if (want_name && !name)
Jeff Kingbcb2b002012-05-21 19:09:43 -0400300 name = ident_default_name();
301 if (!email)
302 email = ident_default_email();
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800303
Jeff Kingc15e1982012-05-24 19:27:24 -0400304 if (want_name && !*name) {
Junio C Hamanocb280e12007-01-25 19:05:01 -0800305 struct passwd *pw;
306
Jeff Kingf9bc5732012-05-24 19:28:40 -0400307 if (strict) {
Jeff King8587ead2012-05-21 19:10:17 -0400308 if (name == git_default_name.buf)
Jeff Kingb9f0ac12012-05-21 19:10:11 -0400309 fputs(env_hint, stderr);
Jeff Kingb00f6cf2012-05-24 19:26:32 -0400310 die("empty ident name (for <%s>) not allowed", email);
Junio C Hamano749be722006-02-18 20:31:05 -0800311 }
Jeff King2f705872012-05-21 19:10:20 -0400312 pw = xgetpwuid_self();
Jeff King060d4bb2012-05-21 19:10:14 -0400313 name = pw->pw_name;
Junio C Hamano749be722006-02-18 20:31:05 -0800314 }
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800315
Jeff King8c5b1ae2012-05-24 19:32:37 -0400316 if (strict && email == git_default_email.buf &&
317 strstr(email, "(none)")) {
318 fputs(env_hint, stderr);
319 die("unable to auto-detect email address (got '%s')", email);
Jeff King4579bb42010-12-13 12:02:25 -0500320 }
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700321
Jeff Kingc96f0c82012-05-21 19:10:26 -0400322 strbuf_reset(&ident);
Jeff Kingc15e1982012-05-24 19:27:24 -0400323 if (want_name) {
324 strbuf_addstr_without_crud(&ident, name);
325 strbuf_addstr(&ident, " <");
326 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400327 strbuf_addstr_without_crud(&ident, email);
Jeff Kingc15e1982012-05-24 19:27:24 -0400328 if (want_name)
329 strbuf_addch(&ident, '>');
Jeff King359b27a2012-05-24 19:26:50 -0400330 if (want_date) {
Jeff Kingc96f0c82012-05-21 19:10:26 -0400331 strbuf_addch(&ident, ' ');
Jeff Kingc33ddc22014-08-27 03:57:08 -0400332 if (date_str && date_str[0]) {
333 if (parse_date(date_str, &ident) < 0)
334 die("invalid date format: %s", date_str);
335 }
336 else
337 strbuf_addstr(&ident, ident_default_date());
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800338 }
Jeff Kingc33ddc22014-08-27 03:57:08 -0400339
Jeff Kingc96f0c82012-05-21 19:10:26 -0400340 return ident.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700341}
Eric W. Biedermand289d132005-07-14 18:50:33 -0600342
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800343const char *fmt_name(const char *name, const char *email)
344{
Jeff Kingf9bc5732012-05-24 19:28:40 -0400345 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800346}
347
Junio C Hamano774751a2007-12-08 17:32:08 -0800348const char *git_author_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600349{
Jeff Kingd6991ce2012-11-14 16:34:13 -0800350 if (getenv("GIT_AUTHOR_NAME"))
351 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
352 if (getenv("GIT_AUTHOR_EMAIL"))
353 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800354 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800355 getenv("GIT_AUTHOR_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800356 getenv("GIT_AUTHOR_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800357 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600358}
359
Junio C Hamano774751a2007-12-08 17:32:08 -0800360const char *git_committer_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600361{
Junio C Hamano91c38a22010-01-08 07:39:11 -0800362 if (getenv("GIT_COMMITTER_NAME"))
Jeff Kingd6991ce2012-11-14 16:34:13 -0800363 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
Junio C Hamano91c38a22010-01-08 07:39:11 -0800364 if (getenv("GIT_COMMITTER_EMAIL"))
Jeff Kingd6991ce2012-11-14 16:34:13 -0800365 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800366 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800367 getenv("GIT_COMMITTER_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800368 getenv("GIT_COMMITTER_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800369 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600370}
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800371
Jeff Kingd6991ce2012-11-14 16:34:13 -0800372static int ident_is_sufficient(int user_ident_explicitly_given)
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800373{
374#ifndef WINDOWS
375 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
376#else
377 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
378#endif
379}
Jeff King95979212012-05-21 19:09:54 -0400380
Jeff Kingd6991ce2012-11-14 16:34:13 -0800381int committer_ident_sufficiently_given(void)
382{
383 return ident_is_sufficient(committer_ident_explicitly_given);
384}
385
386int author_ident_sufficiently_given(void)
387{
388 return ident_is_sufficient(author_ident_explicitly_given);
389}
390
Jeff King95979212012-05-21 19:09:54 -0400391int git_ident_config(const char *var, const char *value, void *data)
392{
393 if (!strcmp(var, "user.name")) {
394 if (!value)
395 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400396 strbuf_reset(&git_default_name);
397 strbuf_addstr(&git_default_name, value);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800398 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
399 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
Jeff King95979212012-05-21 19:09:54 -0400400 return 0;
401 }
402
403 if (!strcmp(var, "user.email")) {
404 if (!value)
405 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400406 strbuf_reset(&git_default_email);
407 strbuf_addstr(&git_default_email, value);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800408 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
409 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Jeff King95979212012-05-21 19:09:54 -0400410 return 0;
411 }
412
413 return 0;
414}
Jeff King662cc302013-09-20 06:16:28 -0400415
416static int buf_cmp(const char *a_begin, const char *a_end,
417 const char *b_begin, const char *b_end)
418{
419 int a_len = a_end - a_begin;
420 int b_len = b_end - b_begin;
421 int min = a_len < b_len ? a_len : b_len;
422 int cmp;
423
424 cmp = memcmp(a_begin, b_begin, min);
425 if (cmp)
426 return cmp;
427
428 return a_len - b_len;
429}
430
431int ident_cmp(const struct ident_split *a,
432 const struct ident_split *b)
433{
434 int cmp;
435
436 cmp = buf_cmp(a->mail_begin, a->mail_end,
437 b->mail_begin, b->mail_end);
438 if (cmp)
439 return cmp;
440
441 return buf_cmp(a->name_begin, a->name_end,
442 b->name_begin, b->name_end);
443}