ident: make xgetpwuid_self() a static local helper
[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;
c33ddc2e 12static struct strbuf git_default_date = STRBUF_INIT;
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
e850194c
JK
26static struct passwd *xgetpwuid_self(void)
27{
28 struct passwd *pw;
29
30 errno = 0;
31 pw = getpwuid(getuid());
32 if (!pw)
33 die(_("unable to look up current user in the passwd file: %s"),
34 errno ? strerror(errno) : _("no such user"));
35 return pw;
36}
37
8587ead7 38static void copy_gecos(const struct passwd *w, struct strbuf *name)
e9bacb4f 39{
8587ead7 40 char *src;
e9bacb4f
JH
41
42 /* Traditionally GECOS field had office phone numbers etc, separated
43 * with commas. Also & stands for capitalized form of the login name.
44 */
45
8587ead7 46 for (src = get_gecos(w); *src && *src != ','; src++) {
e9bacb4f 47 int ch = *src;
8587ead7
JK
48 if (ch != '&')
49 strbuf_addch(name, ch);
50 else {
e9bacb4f 51 /* Sorry, Mr. McDonald... */
8587ead7
JK
52 strbuf_addch(name, toupper(*w->pw_name));
53 strbuf_addstr(name, w->pw_name + 1);
e9bacb4f
JH
54 }
55 }
e9bacb4f
JH
56}
57
8587ead7 58static int add_mailname_host(struct strbuf *buf)
8a55caa8
JN
59{
60 FILE *mailname;
dc342a25 61 struct strbuf mailnamebuf = STRBUF_INIT;
8a55caa8
JN
62
63 mailname = fopen("/etc/mailname", "r");
64 if (!mailname) {
65 if (errno != ENOENT)
66 warning("cannot open /etc/mailname: %s",
67 strerror(errno));
68 return -1;
69 }
dc342a25 70 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
8a55caa8
JN
71 if (ferror(mailname))
72 warning("cannot read /etc/mailname: %s",
73 strerror(errno));
dc342a25 74 strbuf_release(&mailnamebuf);
8a55caa8
JN
75 fclose(mailname);
76 return -1;
77 }
78 /* success! */
dc342a25
JN
79 strbuf_addbuf(buf, &mailnamebuf);
80 strbuf_release(&mailnamebuf);
8a55caa8
JN
81 fclose(mailname);
82 return 0;
83}
84
8587ead7 85static void add_domainname(struct strbuf *out)
8a55caa8 86{
8587ead7 87 char buf[1024];
8a55caa8 88 struct hostent *he;
8a55caa8 89
8587ead7 90 if (gethostname(buf, sizeof(buf))) {
8a55caa8 91 warning("cannot get host name: %s", strerror(errno));
8587ead7 92 strbuf_addstr(out, "(none)");
8a55caa8
JN
93 return;
94 }
8587ead7 95 if (strchr(buf, '.'))
f8254d32
JK
96 strbuf_addstr(out, buf);
97 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
98 strbuf_addstr(out, he->h_name);
8a55caa8 99 else
f8254d32 100 strbuf_addf(out, "%s.(none)", buf);
8a55caa8
JN
101}
102
8587ead7 103static void copy_email(const struct passwd *pw, struct strbuf *email)
6aa33f40 104{
01754769
JH
105 /*
106 * Make up a fake email address
107 * (name + '@' + hostname [+ '.' + domainname])
108 */
8587ead7
JK
109 strbuf_addstr(email, pw->pw_name);
110 strbuf_addch(email, '@');
111
112 if (!add_mailname_host(email))
8a55caa8 113 return; /* read from "/etc/mailname" (Debian) */
8587ead7 114 add_domainname(email);
01754769
JH
115}
116
9830534e 117const char *ident_default_name(void)
01754769 118{
be641abd 119 if (!git_default_name.len) {
2f705875 120 copy_gecos(xgetpwuid_self(), &git_default_name);
be641abd 121 strbuf_trim(&git_default_name);
01754769 122 }
8587ead7 123 return git_default_name.buf;
bcb2b004 124}
01754769 125
bcb2b004
JK
126const char *ident_default_email(void)
127{
8587ead7 128 if (!git_default_email.len) {
46f74f00
MK
129 const char *email = getenv("EMAIL");
130
99178c83 131 if (email && email[0]) {
8587ead7 132 strbuf_addstr(&git_default_email, email);
d6991cee
JK
133 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
134 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
2f705875
JK
135 } else
136 copy_email(xgetpwuid_self(), &git_default_email);
be641abd 137 strbuf_trim(&git_default_email);
01754769 138 }
8587ead7 139 return git_default_email.buf;
6aa33f40
LT
140}
141
dad148c3 142static const char *ident_default_date(void)
6aa33f40 143{
c33ddc2e
JK
144 if (!git_default_date.len)
145 datestamp(&git_default_date);
146 return git_default_date.buf;
6aa33f40
LT
147}
148
149static int crud(unsigned char c)
150{
f64c81d4
AR
151 return c <= 32 ||
152 c == '.' ||
153 c == ',' ||
154 c == ':' ||
155 c == ';' ||
156 c == '<' ||
157 c == '>' ||
158 c == '"' ||
d404bf02 159 c == '\\' ||
f64c81d4 160 c == '\'';
6aa33f40
LT
161}
162
163/*
164 * Copy over a string to the destination, but avoid special
165 * characters ('\n', '<' and '>') and remove crud at the end
166 */
c96f0c8d 167static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
6aa33f40 168{
b0732115 169 size_t i, len;
6aa33f40
LT
170 unsigned char c;
171
172 /* Remove crud from the beginning.. */
173 while ((c = *src) != 0) {
174 if (!crud(c))
175 break;
176 src++;
177 }
178
179 /* Remove crud from the end.. */
180 len = strlen(src);
181 while (len > 0) {
182 c = src[len-1];
183 if (!crud(c))
184 break;
185 --len;
186 }
187
188 /*
189 * Copy the rest to the buffer, but avoid the special
82f9d58a 190 * characters '\n' '<' and '>' that act as delimiters on
c96f0c8d
JK
191 * an identification line. We can only remove crud, never add it,
192 * so 'len' is our maximum.
6aa33f40 193 */
c96f0c8d 194 strbuf_grow(sb, len);
6aa33f40
LT
195 for (i = 0; i < len; i++) {
196 c = *src++;
197 switch (c) {
198 case '\n': case '<': case '>':
199 continue;
200 }
c96f0c8d 201 sb->buf[sb->len++] = c;
6aa33f40 202 }
c96f0c8d 203 sb->buf[sb->len] = '\0';
6aa33f40
LT
204}
205
4b340cfa
JH
206/*
207 * Reverse of fmt_ident(); given an ident line, split the fields
208 * to allow the caller to parse it.
209 * Signal a success by returning 0, but date/tz fields of the result
210 * can still be NULL if the input line only has the name/email part
211 * (e.g. reading from a reflog entry).
212 */
213int split_ident_line(struct ident_split *split, const char *line, int len)
214{
215 const char *cp;
216 size_t span;
217 int status = -1;
218
219 memset(split, 0, sizeof(*split));
220
221 split->name_begin = line;
222 for (cp = line; *cp && cp < line + len; cp++)
223 if (*cp == '<') {
224 split->mail_begin = cp + 1;
225 break;
226 }
227 if (!split->mail_begin)
228 return status;
229
d9955fd6 230 for (cp = split->mail_begin - 2; line <= cp; cp--)
4b340cfa
JH
231 if (!isspace(*cp)) {
232 split->name_end = cp + 1;
233 break;
234 }
e27ddb64
JH
235 if (!split->name_end) {
236 /* no human readable name */
237 split->name_end = split->name_begin;
238 }
4b340cfa
JH
239
240 for (cp = split->mail_begin; cp < line + len; cp++)
241 if (*cp == '>') {
242 split->mail_end = cp;
243 break;
244 }
245 if (!split->mail_end)
246 return status;
247
03818a4a
JK
248 /*
249 * Look from the end-of-line to find the trailing ">" of the mail
250 * address, even though we should already know it as split->mail_end.
251 * This can help in cases of broken idents with an extra ">" somewhere
252 * in the email address. Note that we are assuming the timestamp will
253 * never have a ">" in it.
254 *
255 * Note that we will always find some ">" before going off the front of
256 * the string, because will always hit the split->mail_end closing
257 * bracket.
258 */
259 for (cp = line + len - 1; *cp != '>'; cp--)
260 ;
261
262 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
4b340cfa
JH
263 ;
264 if (line + len <= cp)
265 goto person_only;
266 split->date_begin = cp;
267 span = strspn(cp, "0123456789");
268 if (!span)
269 goto person_only;
270 split->date_end = split->date_begin + span;
271 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
272 ;
273 if (line + len <= cp || (*cp != '+' && *cp != '-'))
274 goto person_only;
275 split->tz_begin = cp;
276 span = strspn(cp + 1, "0123456789");
277 if (!span)
278 goto person_only;
279 split->tz_end = split->tz_begin + 1 + span;
280 return 0;
281
282person_only:
283 split->date_begin = NULL;
284 split->date_end = NULL;
285 split->tz_begin = NULL;
286 split->tz_end = NULL;
287 return 0;
288}
289
749be728 290static const char *env_hint =
d5cc2de9 291"\n"
6c293d40 292"*** Please tell me who you are.\n"
d5cc2de9
HWN
293"\n"
294"Run\n"
295"\n"
8e7425da 296" git config --global user.email \"you@example.com\"\n"
180787c4 297" git config --global user.name \"Your Name\"\n"
d5cc2de9 298"\n"
180787c4
SP
299"to set your account\'s default identity.\n"
300"Omit --global to set the identity only in this repository.\n"
d5cc2de9 301"\n";
749be728 302
774751a8
JH
303const char *fmt_ident(const char *name, const char *email,
304 const char *date_str, int flag)
6aa33f40 305{
c96f0c8d 306 static struct strbuf ident = STRBUF_INIT;
f9bc573f 307 int strict = (flag & IDENT_STRICT);
359b27ad 308 int want_date = !(flag & IDENT_NO_DATE);
c15e1987 309 int want_name = !(flag & IDENT_NO_NAME);
6aa33f40 310
c15e1987 311 if (want_name && !name)
bcb2b004
JK
312 name = ident_default_name();
313 if (!email)
314 email = ident_default_email();
dfdd309e 315
c15e1987 316 if (want_name && !*name) {
cb280e10
JH
317 struct passwd *pw;
318
f9bc573f 319 if (strict) {
8587ead7 320 if (name == git_default_name.buf)
b9f0ac17 321 fputs(env_hint, stderr);
b00f6cfc 322 die("empty ident name (for <%s>) not allowed", email);
749be728 323 }
2f705875 324 pw = xgetpwuid_self();
060d4bb3 325 name = pw->pw_name;
749be728 326 }
dfdd309e 327
8c5b1ae1
JK
328 if (strict && email == git_default_email.buf &&
329 strstr(email, "(none)")) {
330 fputs(env_hint, stderr);
331 die("unable to auto-detect email address (got '%s')", email);
749be728 332 }
dfdd309e 333
c96f0c8d 334 strbuf_reset(&ident);
c15e1987
JK
335 if (want_name) {
336 strbuf_addstr_without_crud(&ident, name);
337 strbuf_addstr(&ident, " <");
d9ccfe77 338 }
c96f0c8d 339 strbuf_addstr_without_crud(&ident, email);
c15e1987
JK
340 if (want_name)
341 strbuf_addch(&ident, '>');
359b27ad 342 if (want_date) {
c96f0c8d 343 strbuf_addch(&ident, ' ');
c33ddc2e
JK
344 if (date_str && date_str[0]) {
345 if (parse_date(date_str, &ident) < 0)
346 die("invalid date format: %s", date_str);
347 }
348 else
349 strbuf_addstr(&ident, ident_default_date());
d9ccfe77 350 }
c33ddc2e 351
c96f0c8d 352 return ident.buf;
6aa33f40 353}
d289d136 354
d9ccfe77
JH
355const char *fmt_name(const char *name, const char *email)
356{
f9bc573f 357 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
d9ccfe77
JH
358}
359
774751a8 360const char *git_author_info(int flag)
d289d136 361{
d6991cee
JK
362 if (getenv("GIT_AUTHOR_NAME"))
363 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
364 if (getenv("GIT_AUTHOR_EMAIL"))
365 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 366 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
c7d77dab 367 getenv("GIT_AUTHOR_EMAIL"),
749be728 368 getenv("GIT_AUTHOR_DATE"),
774751a8 369 flag);
d289d136
EB
370}
371
774751a8 372const char *git_committer_info(int flag)
d289d136 373{
91c38a21 374 if (getenv("GIT_COMMITTER_NAME"))
d6991cee 375 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
91c38a21 376 if (getenv("GIT_COMMITTER_EMAIL"))
d6991cee 377 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
798123af 378 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
c7d77dab 379 getenv("GIT_COMMITTER_EMAIL"),
749be728 380 getenv("GIT_COMMITTER_DATE"),
774751a8 381 flag);
d289d136 382}
5aeb3a3a 383
d6991cee 384static int ident_is_sufficient(int user_ident_explicitly_given)
5aeb3a3a
JH
385{
386#ifndef WINDOWS
387 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
388#else
389 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
390#endif
391}
9597921b 392
d6991cee
JK
393int committer_ident_sufficiently_given(void)
394{
395 return ident_is_sufficient(committer_ident_explicitly_given);
396}
397
398int author_ident_sufficiently_given(void)
399{
400 return ident_is_sufficient(author_ident_explicitly_given);
401}
402
9597921b
JK
403int git_ident_config(const char *var, const char *value, void *data)
404{
405 if (!strcmp(var, "user.name")) {
406 if (!value)
407 return config_error_nonbool(var);
8587ead7
JK
408 strbuf_reset(&git_default_name);
409 strbuf_addstr(&git_default_name, value);
d6991cee
JK
410 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
411 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
9597921b
JK
412 return 0;
413 }
414
415 if (!strcmp(var, "user.email")) {
416 if (!value)
417 return config_error_nonbool(var);
8587ead7
JK
418 strbuf_reset(&git_default_email);
419 strbuf_addstr(&git_default_email, value);
d6991cee
JK
420 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
421 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
9597921b
JK
422 return 0;
423 }
424
425 return 0;
426}
662cc30c
JK
427
428static int buf_cmp(const char *a_begin, const char *a_end,
429 const char *b_begin, const char *b_end)
430{
431 int a_len = a_end - a_begin;
432 int b_len = b_end - b_begin;
433 int min = a_len < b_len ? a_len : b_len;
434 int cmp;
435
436 cmp = memcmp(a_begin, b_begin, min);
437 if (cmp)
438 return cmp;
439
440 return a_len - b_len;
441}
442
443int ident_cmp(const struct ident_split *a,
444 const struct ident_split *b)
445{
446 int cmp;
447
448 cmp = buf_cmp(a->mail_begin, a->mail_end,
449 b->mail_begin, b->mail_end);
450 if (cmp)
451 return cmp;
452
453 return buf_cmp(a->name_begin, a->name_end,
454 b->name_begin, b->name_end);
455}