Git 1.8.4.1
[git/git.git] / ident.c
CommitLineData
6aa33f40
LT
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"
9
8587ead7
JK
10static struct strbuf git_default_name = STRBUF_INIT;
11static struct strbuf git_default_email = STRBUF_INIT;
e1b10391 12static char git_default_date[50];
45280230
JK
13
14#define IDENT_NAME_GIVEN 01
15#define IDENT_MAIL_GIVEN 02
16#define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
d6991cee
JK
17static int committer_ident_explicitly_given;
18static int author_ident_explicitly_given;
6aa33f40 19
590e081d
RG
20#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
8587ead7 26static void copy_gecos(const struct passwd *w, struct strbuf *name)
e9bacb4f 27{
8587ead7 28 char *src;
e9bacb4f
JH
29
30 /* Traditionally GECOS field had office phone numbers etc, separated
31 * with commas. Also & stands for capitalized form of the login name.
32 */
33
8587ead7 34 for (src = get_gecos(w); *src && *src != ','; src++) {
e9bacb4f 35 int ch = *src;
8587ead7
JK
36 if (ch != '&')
37 strbuf_addch(name, ch);
38 else {
e9bacb4f 39 /* Sorry, Mr. McDonald... */
8587ead7
JK
40 strbuf_addch(name, toupper(*w->pw_name));
41 strbuf_addstr(name, w->pw_name + 1);
e9bacb4f
JH
42 }
43 }
e9bacb4f
JH
44}
45
8587ead7 46static int add_mailname_host(struct strbuf *buf)
8a55caa8
JN
47{
48 FILE *mailname;
dc342a25 49 struct strbuf mailnamebuf = STRBUF_INIT;
8a55caa8
JN
50
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 }
dc342a25 58 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
8a55caa8
JN
59 if (ferror(mailname))
60 warning("cannot read /etc/mailname: %s",
61 strerror(errno));
dc342a25 62 strbuf_release(&mailnamebuf);
8a55caa8
JN
63 fclose(mailname);
64 return -1;
65 }
66 /* success! */
dc342a25
JN
67 strbuf_addbuf(buf, &mailnamebuf);
68 strbuf_release(&mailnamebuf);
8a55caa8
JN
69 fclose(mailname);
70 return 0;
71}
72
8587ead7 73static void add_domainname(struct strbuf *out)
8a55caa8 74{
8587ead7 75 char buf[1024];
8a55caa8 76 struct hostent *he;
8a55caa8 77
8587ead7 78 if (gethostname(buf, sizeof(buf))) {
8a55caa8 79 warning("cannot get host name: %s", strerror(errno));
8587ead7 80 strbuf_addstr(out, "(none)");
8a55caa8
JN
81 return;
82 }
8587ead7 83 if (strchr(buf, '.'))
f8254d32
JK
84 strbuf_addstr(out, buf);
85 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
86 strbuf_addstr(out, he->h_name);
8a55caa8 87 else
f8254d32 88 strbuf_addf(out, "%s.(none)", buf);
8a55caa8
JN
89}
90
8587ead7 91static void copy_email(const struct passwd *pw, struct strbuf *email)
6aa33f40 92{
01754769
JH
93 /*
94 * Make up a fake email address
95 * (name + '@' + hostname [+ '.' + domainname])
96 */
8587ead7
JK
97 strbuf_addstr(email, pw->pw_name);
98 strbuf_addch(email, '@');
99
100 if (!add_mailname_host(email))
8a55caa8 101 return; /* read from "/etc/mailname" (Debian) */
8587ead7 102 add_domainname(email);
01754769
JH
103}
104
dad148c3 105static const char *ident_default_name(void)
01754769 106{
be641abd 107 if (!git_default_name.len) {
2f705875 108 copy_gecos(xgetpwuid_self(), &git_default_name);
be641abd 109 strbuf_trim(&git_default_name);
01754769 110 }
8587ead7 111 return git_default_name.buf;
bcb2b004 112}
01754769 113
bcb2b004
JK
114const char *ident_default_email(void)
115{
8587ead7 116 if (!git_default_email.len) {
46f74f00
MK
117 const char *email = getenv("EMAIL");
118
99178c83 119 if (email && email[0]) {
8587ead7 120 strbuf_addstr(&git_default_email, email);
d6991cee
JK
121 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
122 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
2f705875
JK
123 } else
124 copy_email(xgetpwuid_self(), &git_default_email);
be641abd 125 strbuf_trim(&git_default_email);
01754769 126 }
8587ead7 127 return git_default_email.buf;
6aa33f40
LT
128}
129
dad148c3 130static const char *ident_default_date(void)
6aa33f40 131{
01754769
JH
132 if (!git_default_date[0])
133 datestamp(git_default_date, sizeof(git_default_date));
bcb2b004 134 return git_default_date;
6aa33f40
LT
135}
136
137static int crud(unsigned char c)
138{
f64c81d4
AR
139 return c <= 32 ||
140 c == '.' ||
141 c == ',' ||
142 c == ':' ||
143 c == ';' ||
144 c == '<' ||
145 c == '>' ||
146 c == '"' ||
d404bf02 147 c == '\\' ||
f64c81d4 148 c == '\'';
6aa33f40
LT
149}
150
151/*
152 * Copy over a string to the destination, but avoid special
153 * characters ('\n', '<' and '>') and remove crud at the end
154 */
c96f0c8d 155static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 156{
b0732115 157 size_t i, len;
6aa33f40
LT
158 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
82f9d58a 178 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
179 * an identification line. We can only remove crud, never add it,
180 * so 'len' is our maximum.
6aa33f40 181 */
c96f0c8d 182 strbuf_grow(sb, len);
6aa33f40
LT
183 for (i = 0; i < len; i++) {
184 c = *src++;
185 switch (c) {
186 case '\n': case '<': case '>':
187 continue;
188 }
c96f0c8d 189 sb->buf[sb->len++] = c;
6aa33f40 190 }
c96f0c8d 191 sb->buf[sb->len] = '\0';
6aa33f40
LT
192}
193
4b340cfa
JH
194/*
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
d9955fd6 218 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
219 if (!isspace(*cp)) {
220 split->name_end = cp + 1;
221 break;
222 }
e27ddb64
JH
223 if (!split->name_end) {
224 /* no human readable name */
225 split->name_end = split->name_begin;
226 }
4b340cfa
JH
227
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
749be728 264static const char *env_hint =
d5cc2de9 265"\n"
6c293d40 266"*** Please tell me who you are.\n"
d5cc2de9
HWN
267"\n"
268"Run\n"
269"\n"
8e7425da 270" git config --global user.email \"you@example.com\"\n"
180787c4 271" git config --global user.name \"Your Name\"\n"
d5cc2de9 272"\n"
180787c4
SP
273"to set your account\'s default identity.\n"
274"Omit --global to set the identity only in this repository.\n"
d5cc2de9 275"\n";
749be728 276
774751a8
JH
277const char *fmt_ident(const char *name, const char *email,
278 const char *date_str, int flag)
6aa33f40 279{
c96f0c8d 280 static struct strbuf ident = STRBUF_INIT;
6aa33f40 281 char date[50];
f9bc573f 282 int strict = (flag & IDENT_STRICT);
359b27ad 283 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 284 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 285
c15e1987 286 if (want_name && !name)
bcb2b004
JK
287 name = ident_default_name();
288 if (!email)
289 email = ident_default_email();
dfdd309e 290
c15e1987 291 if (want_name && !*name) {
cb280e10
JH
292 struct passwd *pw;
293
f9bc573f 294 if (strict) {
8587ead7 295 if (name == git_default_name.buf)
b9f0ac17 296 fputs(env_hint, stderr);
b00f6cfc 297 die("empty ident name (for <%s>) not allowed", email);
749be728 298 }
2f705875 299 pw = xgetpwuid_self();
060d4bb3 300 name = pw->pw_name;
749be728 301 }
dfdd309e 302
8c5b1ae1
JK
303 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);
749be728 307 }
dfdd309e 308
359b27ad
JK
309 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());
4579bb41 316 }
6aa33f40 317
c96f0c8d 318 strbuf_reset(&ident);
c15e1987
JK
319 if (want_name) {
320 strbuf_addstr_without_crud(&ident, name);
321 strbuf_addstr(&ident, " <");
d9ccfe77 322 }
c96f0c8d 323 strbuf_addstr_without_crud(&ident, email);
c15e1987
JK
324 if (want_name)
325 strbuf_addch(&ident, '>');
359b27ad 326 if (want_date) {
c96f0c8d
JK
327 strbuf_addch(&ident, ' ');
328 strbuf_addstr_without_crud(&ident, date);
d9ccfe77 329 }
c96f0c8d 330 return ident.buf;
6aa33f40 331}
d289d136 332
d9ccfe77
JH
333const char *fmt_name(const char *name, const char *email)
334{
f9bc573f 335 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
336}
337
774751a8 338const char *git_author_info(int flag)
d289d136 339{
d6991cee
JK
340 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;
798123af 344 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 345 getenv("GIT_AUTHOR_EMAIL"),
749be728 346 getenv("GIT_AUTHOR_DATE"),
774751a8 347 flag);
d289d136
EB
348}
349
774751a8 350const char *git_committer_info(int flag)
d289d136 351{
91c38a21 352 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 353 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 354 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 355 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 356 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 357 getenv("GIT_COMMITTER_EMAIL"),
749be728 358 getenv("GIT_COMMITTER_DATE"),
774751a8 359 flag);
d289d136 360}
5aeb3a3a 361
d6991cee 362static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
363{
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}
9597921b 370
d6991cee
JK
371int 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
9597921b
JK
381int 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);
8587ead7
JK
386 strbuf_reset(&git_default_name);
387 strbuf_addstr(&git_default_name, value);
d6991cee
JK
388 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
389 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
9597921b
JK
390 return 0;
391 }
392
393 if (!strcmp(var, "user.email")) {
394 if (!value)
395 return config_error_nonbool(var);
8587ead7
JK
396 strbuf_reset(&git_default_email);
397 strbuf_addstr(&git_default_email, value);
d6991cee
JK
398 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
399 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
9597921b
JK
400 return 0;
401 }
402
403 return 0;
404}