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