blob: f5160e1f43bf06aaf2cddfc2fe66f8d463c3f6cc [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 King2d4b4fc2012-05-21 19:09:57 -040013int user_ident_explicitly_given;
Linus Torvalds6aa33f42005-07-12 11:49:27 -070014
Rafael Gieschke590e0812011-05-19 13:37:55 +020015#ifdef NO_GECOS_IN_PWENT
16#define get_gecos(ignored) "&"
17#else
18#define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
19#endif
20
Jeff King8587ead2012-05-21 19:10:17 -040021static void copy_gecos(const struct passwd *w, struct strbuf *name)
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070022{
Jeff King8587ead2012-05-21 19:10:17 -040023 char *src;
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070024
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
27 */
28
Jeff King8587ead2012-05-21 19:10:17 -040029 for (src = get_gecos(w); *src && *src != ','; src++) {
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070030 int ch = *src;
Jeff King8587ead2012-05-21 19:10:17 -040031 if (ch != '&')
32 strbuf_addch(name, ch);
33 else {
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070034 /* Sorry, Mr. McDonald... */
Jeff King8587ead2012-05-21 19:10:17 -040035 strbuf_addch(name, toupper(*w->pw_name));
36 strbuf_addstr(name, w->pw_name + 1);
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070037 }
38 }
Junio C Hamanoe9bacb42005-09-19 16:06:56 -070039}
40
Jeff King8587ead2012-05-21 19:10:17 -040041static int add_mailname_host(struct strbuf *buf)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050042{
43 FILE *mailname;
44
45 mailname = fopen("/etc/mailname", "r");
46 if (!mailname) {
47 if (errno != ENOENT)
48 warning("cannot open /etc/mailname: %s",
49 strerror(errno));
50 return -1;
51 }
Jeff King8587ead2012-05-21 19:10:17 -040052 if (strbuf_getline(buf, mailname, '\n') == EOF) {
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050053 if (ferror(mailname))
54 warning("cannot read /etc/mailname: %s",
55 strerror(errno));
56 fclose(mailname);
57 return -1;
58 }
59 /* success! */
60 fclose(mailname);
61 return 0;
62}
63
Jeff King8587ead2012-05-21 19:10:17 -040064static void add_domainname(struct strbuf *out)
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050065{
Jeff King8587ead2012-05-21 19:10:17 -040066 char buf[1024];
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050067 struct hostent *he;
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050068
Jeff King8587ead2012-05-21 19:10:17 -040069 if (gethostname(buf, sizeof(buf))) {
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050070 warning("cannot get host name: %s", strerror(errno));
Jeff King8587ead2012-05-21 19:10:17 -040071 strbuf_addstr(out, "(none)");
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050072 return;
73 }
Jeff King8587ead2012-05-21 19:10:17 -040074 if (strchr(buf, '.'))
Jeff Kingf8254d32012-05-21 19:10:23 -040075 strbuf_addstr(out, buf);
76 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
77 strbuf_addstr(out, he->h_name);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050078 else
Jeff Kingf8254d32012-05-21 19:10:23 -040079 strbuf_addf(out, "%s.(none)", buf);
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050080}
81
Jeff King8587ead2012-05-21 19:10:17 -040082static void copy_email(const struct passwd *pw, struct strbuf *email)
Linus Torvalds6aa33f42005-07-12 11:49:27 -070083{
Junio C Hamano01754762007-01-28 00:50:53 -080084 /*
85 * Make up a fake email address
86 * (name + '@' + hostname [+ '.' + domainname])
87 */
Jeff King8587ead2012-05-21 19:10:17 -040088 strbuf_addstr(email, pw->pw_name);
89 strbuf_addch(email, '@');
Petr Baudisadc3dbc2005-10-21 03:57:39 +020090
Jeff King8587ead2012-05-21 19:10:17 -040091 if (!add_mailname_host(email))
Jonathan Nieder8a55caa2011-10-03 01:16:33 -050092 return; /* read from "/etc/mailname" (Debian) */
Jeff King8587ead2012-05-21 19:10:17 -040093 add_domainname(email);
Junio C Hamano01754762007-01-28 00:50:53 -080094}
95
Jeff Kingbcb2b002012-05-21 19:09:43 -040096const char *ident_default_name(void)
Junio C Hamano01754762007-01-28 00:50:53 -080097{
Jeff Kingbe641ab2012-05-21 19:10:29 -040098 if (!git_default_name.len) {
Jeff King2f705872012-05-21 19:10:20 -040099 copy_gecos(xgetpwuid_self(), &git_default_name);
Jeff Kingbe641ab2012-05-21 19:10:29 -0400100 strbuf_trim(&git_default_name);
101 }
Jeff King8587ead2012-05-21 19:10:17 -0400102 return git_default_name.buf;
Jeff Kingbcb2b002012-05-21 19:09:43 -0400103}
Junio C Hamano01754762007-01-28 00:50:53 -0800104
Jeff Kingbcb2b002012-05-21 19:09:43 -0400105const char *ident_default_email(void)
106{
Jeff King8587ead2012-05-21 19:10:17 -0400107 if (!git_default_email.len) {
Matt Kraai46f74f02007-07-05 17:29:41 -0700108 const char *email = getenv("EMAIL");
109
Junio C Hamano99178c82010-01-08 08:01:10 -0800110 if (email && email[0]) {
Jeff King8587ead2012-05-21 19:10:17 -0400111 strbuf_addstr(&git_default_email, email);
Junio C Hamano99178c82010-01-08 08:01:10 -0800112 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Jeff King2f705872012-05-21 19:10:20 -0400113 } else
114 copy_email(xgetpwuid_self(), &git_default_email);
Jeff Kingbe641ab2012-05-21 19:10:29 -0400115 strbuf_trim(&git_default_email);
Junio C Hamano01754762007-01-28 00:50:53 -0800116 }
Jeff King8587ead2012-05-21 19:10:17 -0400117 return git_default_email.buf;
Jeff Kingbcb2b002012-05-21 19:09:43 -0400118}
Junio C Hamano01754762007-01-28 00:50:53 -0800119
Jeff Kingbcb2b002012-05-21 19:09:43 -0400120const char *ident_default_date(void)
121{
Junio C Hamano01754762007-01-28 00:50:53 -0800122 if (!git_default_date[0])
123 datestamp(git_default_date, sizeof(git_default_date));
Jeff Kingbcb2b002012-05-21 19:09:43 -0400124 return git_default_date;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700125}
126
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700127static int crud(unsigned char c)
128{
Alex Riesenf64c81d2007-12-03 20:11:43 +0100129 return c <= 32 ||
130 c == '.' ||
131 c == ',' ||
132 c == ':' ||
133 c == ';' ||
134 c == '<' ||
135 c == '>' ||
136 c == '"' ||
Linus Torvaldsd404bf02008-12-01 08:41:50 -0800137 c == '\\' ||
Alex Riesenf64c81d2007-12-03 20:11:43 +0100138 c == '\'';
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700139}
140
141/*
142 * Copy over a string to the destination, but avoid special
143 * characters ('\n', '<' and '>') and remove crud at the end
144 */
Jeff Kingc96f0c82012-05-21 19:10:26 -0400145static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700146{
Luiz Fernando N. Capitulinob0732112007-04-15 15:51:29 -0300147 size_t i, len;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700148 unsigned char c;
149
150 /* Remove crud from the beginning.. */
151 while ((c = *src) != 0) {
152 if (!crud(c))
153 break;
154 src++;
155 }
156
157 /* Remove crud from the end.. */
158 len = strlen(src);
159 while (len > 0) {
160 c = src[len-1];
161 if (!crud(c))
162 break;
163 --len;
164 }
165
166 /*
167 * Copy the rest to the buffer, but avoid the special
Junio C Hamano82f9d582005-12-29 01:30:08 -0800168 * characters '\n' '<' and '>' that act as delimiters on
Jeff Kingc96f0c82012-05-21 19:10:26 -0400169 * an identification line. We can only remove crud, never add it,
170 * so 'len' is our maximum.
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700171 */
Jeff Kingc96f0c82012-05-21 19:10:26 -0400172 strbuf_grow(sb, len);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700173 for (i = 0; i < len; i++) {
174 c = *src++;
175 switch (c) {
176 case '\n': case '<': case '>':
177 continue;
178 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400179 sb->buf[sb->len++] = c;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700180 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400181 sb->buf[sb->len] = '\0';
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700182}
183
Junio C Hamano4b340cf2012-03-11 01:25:43 -0800184/*
185 * Reverse of fmt_ident(); given an ident line, split the fields
186 * to allow the caller to parse it.
187 * Signal a success by returning 0, but date/tz fields of the result
188 * can still be NULL if the input line only has the name/email part
189 * (e.g. reading from a reflog entry).
190 */
191int split_ident_line(struct ident_split *split, const char *line, int len)
192{
193 const char *cp;
194 size_t span;
195 int status = -1;
196
197 memset(split, 0, sizeof(*split));
198
199 split->name_begin = line;
200 for (cp = line; *cp && cp < line + len; cp++)
201 if (*cp == '<') {
202 split->mail_begin = cp + 1;
203 break;
204 }
205 if (!split->mail_begin)
206 return status;
207
208 for (cp = split->mail_begin - 2; line < cp; cp--)
209 if (!isspace(*cp)) {
210 split->name_end = cp + 1;
211 break;
212 }
213 if (!split->name_end)
214 return status;
215
216 for (cp = split->mail_begin; cp < line + len; cp++)
217 if (*cp == '>') {
218 split->mail_end = cp;
219 break;
220 }
221 if (!split->mail_end)
222 return status;
223
224 for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
225 ;
226 if (line + len <= cp)
227 goto person_only;
228 split->date_begin = cp;
229 span = strspn(cp, "0123456789");
230 if (!span)
231 goto person_only;
232 split->date_end = split->date_begin + span;
233 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
234 ;
235 if (line + len <= cp || (*cp != '+' && *cp != '-'))
236 goto person_only;
237 split->tz_begin = cp;
238 span = strspn(cp + 1, "0123456789");
239 if (!span)
240 goto person_only;
241 split->tz_end = split->tz_begin + 1 + span;
242 return 0;
243
244person_only:
245 split->date_begin = NULL;
246 split->date_end = NULL;
247 split->tz_begin = NULL;
248 split->tz_end = NULL;
249 return 0;
250}
251
Junio C Hamano749be722006-02-18 20:31:05 -0800252static const char *env_hint =
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100253"\n"
Santi Béjar6c293d42008-03-08 12:30:04 +0100254"*** Please tell me who you are.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100255"\n"
256"Run\n"
257"\n"
David Symonds8e7425d2007-12-07 10:36:45 +1100258" git config --global user.email \"you@example.com\"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200259" git config --global user.name \"Your Name\"\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100260"\n"
Steffen Prohaska180787c2007-08-14 00:05:50 +0200261"to set your account\'s default identity.\n"
262"Omit --global to set the identity only in this repository.\n"
Han-Wen Nienhuysd5cc2de2006-11-28 11:27:39 +0100263"\n";
Junio C Hamano749be722006-02-18 20:31:05 -0800264
Junio C Hamano774751a2007-12-08 17:32:08 -0800265const char *fmt_ident(const char *name, const char *email,
266 const char *date_str, int flag)
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700267{
Jeff Kingc96f0c82012-05-21 19:10:26 -0400268 static struct strbuf ident = STRBUF_INIT;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700269 char date[50];
Junio C Hamano774751a2007-12-08 17:32:08 -0800270 int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
Junio C Hamano774751a2007-12-08 17:32:08 -0800271 int name_addr_only = (flag & IDENT_NO_DATE);
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700272
Jeff Kingbcb2b002012-05-21 19:09:43 -0400273 if (!name)
274 name = ident_default_name();
275 if (!email)
276 email = ident_default_email();
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800277
Junio C Hamano749be722006-02-18 20:31:05 -0800278 if (!*name) {
Junio C Hamanocb280e12007-01-25 19:05:01 -0800279 struct passwd *pw;
280
Jeff Kingb9f0ac12012-05-21 19:10:11 -0400281 if (error_on_no_name) {
Jeff King8587ead2012-05-21 19:10:17 -0400282 if (name == git_default_name.buf)
Jeff Kingb9f0ac12012-05-21 19:10:11 -0400283 fputs(env_hint, stderr);
Jeff Kingb00f6cf2012-05-24 19:26:32 -0400284 die("empty ident name (for <%s>) not allowed", email);
Jeff Kingb9f0ac12012-05-21 19:10:11 -0400285 }
Jeff King2f705872012-05-21 19:10:20 -0400286 pw = xgetpwuid_self();
Jeff King060d4bb2012-05-21 19:10:14 -0400287 name = pw->pw_name;
Junio C Hamano749be722006-02-18 20:31:05 -0800288 }
Junio C Hamanodfdd3092006-02-07 13:19:10 -0800289
Jeff Kingbcb2b002012-05-21 19:09:43 -0400290 strcpy(date, ident_default_date());
Jeff King4579bb42010-12-13 12:02:25 -0500291 if (!name_addr_only && date_str && date_str[0]) {
292 if (parse_date(date_str, date, sizeof(date)) < 0)
293 die("invalid date format: %s", date_str);
294 }
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700295
Jeff Kingc96f0c82012-05-21 19:10:26 -0400296 strbuf_reset(&ident);
297 strbuf_addstr_without_crud(&ident, name);
298 strbuf_addstr(&ident, " <");
299 strbuf_addstr_without_crud(&ident, email);
300 strbuf_addch(&ident, '>');
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800301 if (!name_addr_only) {
Jeff Kingc96f0c82012-05-21 19:10:26 -0400302 strbuf_addch(&ident, ' ');
303 strbuf_addstr_without_crud(&ident, date);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800304 }
Jeff Kingc96f0c82012-05-21 19:10:26 -0400305 return ident.buf;
Linus Torvalds6aa33f42005-07-12 11:49:27 -0700306}
Eric W. Biedermand289d132005-07-14 18:50:33 -0600307
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800308const char *fmt_name(const char *name, const char *email)
309{
Junio C Hamano774751a2007-12-08 17:32:08 -0800310 return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
Junio C Hamanod9ccfe72007-12-02 13:43:34 -0800311}
312
Junio C Hamano774751a2007-12-08 17:32:08 -0800313const char *git_author_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600314{
Junio C Hamano798123a2007-02-04 17:50:14 -0800315 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800316 getenv("GIT_AUTHOR_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800317 getenv("GIT_AUTHOR_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800318 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600319}
320
Junio C Hamano774751a2007-12-08 17:32:08 -0800321const char *git_committer_info(int flag)
Eric W. Biedermand289d132005-07-14 18:50:33 -0600322{
Junio C Hamano91c38a22010-01-08 07:39:11 -0800323 if (getenv("GIT_COMMITTER_NAME"))
324 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
325 if (getenv("GIT_COMMITTER_EMAIL"))
326 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
Junio C Hamano798123a2007-02-04 17:50:14 -0800327 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
Junio C Hamanoc7d77da2005-11-21 23:44:35 -0800328 getenv("GIT_COMMITTER_EMAIL"),
Junio C Hamano749be722006-02-18 20:31:05 -0800329 getenv("GIT_COMMITTER_DATE"),
Junio C Hamano774751a2007-12-08 17:32:08 -0800330 flag);
Eric W. Biedermand289d132005-07-14 18:50:33 -0600331}
Junio C Hamano5aeb3a32010-01-17 13:54:28 -0800332
333int user_ident_sufficiently_given(void)
334{
335#ifndef WINDOWS
336 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
337#else
338 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
339#endif
340}
Jeff King95979212012-05-21 19:09:54 -0400341
342int git_ident_config(const char *var, const char *value, void *data)
343{
344 if (!strcmp(var, "user.name")) {
345 if (!value)
346 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400347 strbuf_reset(&git_default_name);
348 strbuf_addstr(&git_default_name, value);
Jeff King95979212012-05-21 19:09:54 -0400349 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
350 return 0;
351 }
352
353 if (!strcmp(var, "user.email")) {
354 if (!value)
355 return config_error_nonbool(var);
Jeff King8587ead2012-05-21 19:10:17 -0400356 strbuf_reset(&git_default_email);
357 strbuf_addstr(&git_default_email, value);
Jeff King95979212012-05-21 19:09:54 -0400358 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
359 return 0;
360 }
361
362 return 0;
363}