blob: 1c123e685fbfbfcd5289958adc54a6cd48351628 [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;
Linus Torvaldse1b10392005-10-11 18:47:34 -070012static char git_default_date[50];
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
Junio C Hamanodad148c2012-09-15 22:50:09 -0700105static const 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{
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700132 if (!git_default_date[0])
133 datestamp(git_default_date, sizeof(git_default_date));
Jeff Kingbcb2b002012-05-21 19:09:43 -0400134 return git_default_date;
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
236 for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
237 ;
238 if (line + len <= cp)
239 goto person_only;
240 split->date_begin = cp;
241 span = strspn(cp, "0123456789");
242 if (!span)
243 goto person_only;
244 split->date_end = split->date_begin + span;
245 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
246 ;
247 if (line + len <= cp || (*cp != '+' && *cp != '-'))
248 goto person_only;
249 split->tz_begin = cp;
250 span = strspn(cp + 1, "0123456789");
251 if (!span)
252 goto person_only;
253 split->tz_end = split->tz_begin + 1 + span;
254 return 0;
255
256person_only:
257 split->date_begin = NULL;
258 split->date_end = NULL;
259 split->tz_begin = NULL;
260 split->tz_end = NULL;
261 return 0;
262}
263
Junio C Hamano749be722006-02-18 20:31:05 -0800264static const char *env_hint =
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100265"\n"
Santi Béjar6c293d42008-03-08 12:30:04 +0100266"*** Please tell me who you are.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100267"\n"
268"Run\n"
269"\n"
David Symonds8e7425d2007-12-07 10:36:45 +1100270" git config --global user.email \"you@example.com\"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200271" git config --global user.name \"Your Name\"\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100272"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200273"to set your account\'s default identity.\n"
274"Omit --global to set the identity only in this repository.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100275"\n";
Junio C Hamano749be722006-02-18 20:31:05 -0800276
Junio C Hamano774751a2007-12-08 17:32:08 -0800277const char *fmt_ident(const char *name, const char *email,
278 const char *date_str, int flag)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700279{
Jeff Kingc96f0c82012-05-21 19:10:26 -0400280 static struct strbuf ident = STRBUF_INIT;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700281 char date[50];
Jeff Kingf9bc5732012-05-24 19:28:40 -0400282 int strict = (flag & IDENT_STRICT);
Jeff King359b27a2012-05-24 19:26:50 -0400283 int want_date = !(flag & IDENT_NO_DATE);
Jeff Kingc15e1982012-05-24 19:27:24 -0400284 int want_name = !(flag & IDENT_NO_NAME);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700285
Jeff Kingc15e1982012-05-24 19:27:24 -0400286 if (want_name && !name)
Jeff Kingbcb2b002012-05-21 19:09:43 -0400287 name = ident_default_name();
288 if (!email)
289 email = ident_default_email();
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800290
Jeff Kingc15e1982012-05-24 19:27:24 -0400291 if (want_name && !*name) {
Junio C Hamanocb280e12007-01-25 19:05:01 -0800292 struct passwd *pw;
293
Jeff Kingf9bc5732012-05-24 19:28:40 -0400294 if (strict) {
Jeff King8587ead2012-05-21 19:10:17 -0400295 if (name == git_default_name.buf)
Jeff Kingb9f0ac12012-05-21 19:10:11 -0400296 fputs(env_hint, stderr);
Jeff Kingb00f6cf2012-05-24 19:26:32 -0400297 die("empty ident name (for <%s>) not allowed", email);
Junio C Hamano749be722006-02-18 20:31:05 -0800298 }
Jeff King2f705872012-05-21 19:10:20 -0400299 pw = xgetpwuid_self();
Jeff King060d4bb2012-05-21 19:10:14 -0400300 name = pw->pw_name;
Junio C Hamano749be722006-02-18 20:31:05 -0800301 }
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800302
Jeff King8c5b1ae2012-05-24 19:32:37 -0400303 if (strict && email == git_default_email.buf &&
304 strstr(email, "(none)")) {
305 fputs(env_hint, stderr);
306 die("unable to auto-detect email address (got '%s')", email);
Jeff King4579bb42010-12-13 12:02:25 -0500307 }
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700308
Jeff King359b27a2012-05-24 19:26:50 -0400309 if (want_date) {
310 if (date_str && date_str[0]) {
311 if (parse_date(date_str, date, sizeof(date)) < 0)
312 die("invalid date format: %s", date_str);
313 }
314 else
315 strcpy(date, ident_default_date());
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800316 }
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700317
Jeff Kingc96f0c82012-05-21 19:10:26 -0400318 strbuf_reset(&ident);
Jeff Kingc15e1982012-05-24 19:27:24 -0400319 if (want_name) {
320 strbuf_addstr_without_crud(&ident, name);
321 strbuf_addstr(&ident, " <");
322 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400323 strbuf_addstr_without_crud(&ident, email);
Jeff Kingc15e1982012-05-24 19:27:24 -0400324 if (want_name)
325 strbuf_addch(&ident, '>');
Jeff King359b27a2012-05-24 19:26:50 -0400326 if (want_date) {
Jeff Kingc96f0c82012-05-21 19:10:26 -0400327 strbuf_addch(&ident, ' ');
328 strbuf_addstr_without_crud(&ident, date);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700329 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400330 return ident.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700331}
Eric W. Biedermand289d132005-07-14 18:50:33 -0600332
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800333const char *fmt_name(const char *name, const char *email)
334{
Jeff Kingf9bc5732012-05-24 19:28:40 -0400335 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800336}
337
Junio C Hamano774751a2007-12-08 17:32:08 -0800338const char *git_author_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600339{
Jeff Kingd6991ce2012-11-14 16:34:13 -0800340 if (getenv("GIT_AUTHOR_NAME"))
341 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
342 if (getenv("GIT_AUTHOR_EMAIL"))
343 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800344 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800345 getenv("GIT_AUTHOR_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800346 getenv("GIT_AUTHOR_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800347 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600348}
349
Junio C Hamano774751a2007-12-08 17:32:08 -0800350const char *git_committer_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600351{
Junio C Hamano91c38a22010-01-08 07:39:11 -0800352 if (getenv("GIT_COMMITTER_NAME"))
Jeff Kingd6991ce2012-11-14 16:34:13 -0800353 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
Junio C Hamano91c38a22010-01-08 07:39:11 -0800354 if (getenv("GIT_COMMITTER_EMAIL"))
Jeff Kingd6991ce2012-11-14 16:34:13 -0800355 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800356 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800357 getenv("GIT_COMMITTER_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800358 getenv("GIT_COMMITTER_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800359 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600360}
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800361
Jeff Kingd6991ce2012-11-14 16:34:13 -0800362static int ident_is_sufficient(int user_ident_explicitly_given)
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800363{
364#ifndef WINDOWS
365 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
366#else
367 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
368#endif
369}
Jeff King95979212012-05-21 19:09:54 -0400370
Jeff Kingd6991ce2012-11-14 16:34:13 -0800371int committer_ident_sufficiently_given(void)
372{
373 return ident_is_sufficient(committer_ident_explicitly_given);
374}
375
376int author_ident_sufficiently_given(void)
377{
378 return ident_is_sufficient(author_ident_explicitly_given);
379}
380
Jeff King95979212012-05-21 19:09:54 -0400381int git_ident_config(const char *var, const char *value, void *data)
382{
383 if (!strcmp(var, "user.name")) {
384 if (!value)
385 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400386 strbuf_reset(&git_default_name);
387 strbuf_addstr(&git_default_name, value);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800388 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
389 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
Jeff King95979212012-05-21 19:09:54 -0400390 return 0;
391 }
392
393 if (!strcmp(var, "user.email")) {
394 if (!value)
395 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400396 strbuf_reset(&git_default_email);
397 strbuf_addstr(&git_default_email, value);
Jeff Kingd6991ce2012-11-14 16:34:13 -0800398 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
399 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Jeff King95979212012-05-21 19:09:54 -0400400 return 0;
401 }
402
403 return 0;
404}