builtin/apply: move 'state_linenr' global into 'struct apply_state'
[git.git] / ident.c
blob4fd82d104365c2e2c1ab7e1597e7e246c8eded24
1 /*
2 * ident.c
4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
7 */
8 #include "cache.h"
10 static struct strbuf git_default_name = STRBUF_INIT;
11 static struct strbuf git_default_email = STRBUF_INIT;
12 static struct strbuf git_default_date = STRBUF_INIT;
13 static int default_email_is_bogus;
14 static int default_name_is_bogus;
16 static int ident_use_config_only;
18 #define IDENT_NAME_GIVEN 01
19 #define IDENT_MAIL_GIVEN 02
20 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
21 static int committer_ident_explicitly_given;
22 static int author_ident_explicitly_given;
23 static int ident_config_given;
25 #ifdef NO_GECOS_IN_PWENT
26 #define get_gecos(ignored) "&"
27 #else
28 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
29 #endif
31 static struct passwd *xgetpwuid_self(int *is_bogus)
33 struct passwd *pw;
35 errno = 0;
36 pw = getpwuid(getuid());
37 if (!pw) {
38 static struct passwd fallback;
39 fallback.pw_name = "unknown";
40 #ifndef NO_GECOS_IN_PWENT
41 fallback.pw_gecos = "Unknown";
42 #endif
43 pw = &fallback;
44 if (is_bogus)
45 *is_bogus = 1;
47 return pw;
50 static void copy_gecos(const struct passwd *w, struct strbuf *name)
52 char *src;
54 /* Traditionally GECOS field had office phone numbers etc, separated
55 * with commas. Also & stands for capitalized form of the login name.
58 for (src = get_gecos(w); *src && *src != ','; src++) {
59 int ch = *src;
60 if (ch != '&')
61 strbuf_addch(name, ch);
62 else {
63 /* Sorry, Mr. McDonald... */
64 strbuf_addch(name, toupper(*w->pw_name));
65 strbuf_addstr(name, w->pw_name + 1);
70 static int add_mailname_host(struct strbuf *buf)
72 FILE *mailname;
73 struct strbuf mailnamebuf = STRBUF_INIT;
75 mailname = fopen("/etc/mailname", "r");
76 if (!mailname) {
77 if (errno != ENOENT)
78 warning("cannot open /etc/mailname: %s",
79 strerror(errno));
80 return -1;
82 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
83 if (ferror(mailname))
84 warning("cannot read /etc/mailname: %s",
85 strerror(errno));
86 strbuf_release(&mailnamebuf);
87 fclose(mailname);
88 return -1;
90 /* success! */
91 strbuf_addbuf(buf, &mailnamebuf);
92 strbuf_release(&mailnamebuf);
93 fclose(mailname);
94 return 0;
97 static int canonical_name(const char *host, struct strbuf *out)
99 int status = -1;
101 #ifndef NO_IPV6
102 struct addrinfo hints, *ai;
103 memset (&hints, '\0', sizeof (hints));
104 hints.ai_flags = AI_CANONNAME;
105 if (!getaddrinfo(host, NULL, &hints, &ai)) {
106 if (ai && strchr(ai->ai_canonname, '.')) {
107 strbuf_addstr(out, ai->ai_canonname);
108 status = 0;
110 freeaddrinfo(ai);
112 #else
113 struct hostent *he = gethostbyname(host);
114 if (he && strchr(he->h_name, '.')) {
115 strbuf_addstr(out, he->h_name);
116 status = 0;
118 #endif /* NO_IPV6 */
120 return status;
123 static void add_domainname(struct strbuf *out, int *is_bogus)
125 char buf[1024];
127 if (gethostname(buf, sizeof(buf))) {
128 warning("cannot get host name: %s", strerror(errno));
129 strbuf_addstr(out, "(none)");
130 *is_bogus = 1;
131 return;
133 if (strchr(buf, '.'))
134 strbuf_addstr(out, buf);
135 else if (canonical_name(buf, out) < 0) {
136 strbuf_addf(out, "%s.(none)", buf);
137 *is_bogus = 1;
141 static void copy_email(const struct passwd *pw, struct strbuf *email,
142 int *is_bogus)
145 * Make up a fake email address
146 * (name + '@' + hostname [+ '.' + domainname])
148 strbuf_addstr(email, pw->pw_name);
149 strbuf_addch(email, '@');
151 if (!add_mailname_host(email))
152 return; /* read from "/etc/mailname" (Debian) */
153 add_domainname(email, is_bogus);
156 const char *ident_default_name(void)
158 if (!git_default_name.len) {
159 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
160 strbuf_trim(&git_default_name);
162 return git_default_name.buf;
165 const char *ident_default_email(void)
167 if (!git_default_email.len) {
168 const char *email = getenv("EMAIL");
170 if (email && email[0]) {
171 strbuf_addstr(&git_default_email, email);
172 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
173 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
174 } else
175 copy_email(xgetpwuid_self(&default_email_is_bogus),
176 &git_default_email, &default_email_is_bogus);
177 strbuf_trim(&git_default_email);
179 return git_default_email.buf;
182 static const char *ident_default_date(void)
184 if (!git_default_date.len)
185 datestamp(&git_default_date);
186 return git_default_date.buf;
189 static int crud(unsigned char c)
191 return c <= 32 ||
192 c == '.' ||
193 c == ',' ||
194 c == ':' ||
195 c == ';' ||
196 c == '<' ||
197 c == '>' ||
198 c == '"' ||
199 c == '\\' ||
200 c == '\'';
204 * Copy over a string to the destination, but avoid special
205 * characters ('\n', '<' and '>') and remove crud at the end
207 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
209 size_t i, len;
210 unsigned char c;
212 /* Remove crud from the beginning.. */
213 while ((c = *src) != 0) {
214 if (!crud(c))
215 break;
216 src++;
219 /* Remove crud from the end.. */
220 len = strlen(src);
221 while (len > 0) {
222 c = src[len-1];
223 if (!crud(c))
224 break;
225 --len;
229 * Copy the rest to the buffer, but avoid the special
230 * characters '\n' '<' and '>' that act as delimiters on
231 * an identification line. We can only remove crud, never add it,
232 * so 'len' is our maximum.
234 strbuf_grow(sb, len);
235 for (i = 0; i < len; i++) {
236 c = *src++;
237 switch (c) {
238 case '\n': case '<': case '>':
239 continue;
241 sb->buf[sb->len++] = c;
243 sb->buf[sb->len] = '\0';
247 * Reverse of fmt_ident(); given an ident line, split the fields
248 * to allow the caller to parse it.
249 * Signal a success by returning 0, but date/tz fields of the result
250 * can still be NULL if the input line only has the name/email part
251 * (e.g. reading from a reflog entry).
253 int split_ident_line(struct ident_split *split, const char *line, int len)
255 const char *cp;
256 size_t span;
257 int status = -1;
259 memset(split, 0, sizeof(*split));
261 split->name_begin = line;
262 for (cp = line; *cp && cp < line + len; cp++)
263 if (*cp == '<') {
264 split->mail_begin = cp + 1;
265 break;
267 if (!split->mail_begin)
268 return status;
270 for (cp = split->mail_begin - 2; line <= cp; cp--)
271 if (!isspace(*cp)) {
272 split->name_end = cp + 1;
273 break;
275 if (!split->name_end) {
276 /* no human readable name */
277 split->name_end = split->name_begin;
280 for (cp = split->mail_begin; cp < line + len; cp++)
281 if (*cp == '>') {
282 split->mail_end = cp;
283 break;
285 if (!split->mail_end)
286 return status;
289 * Look from the end-of-line to find the trailing ">" of the mail
290 * address, even though we should already know it as split->mail_end.
291 * This can help in cases of broken idents with an extra ">" somewhere
292 * in the email address. Note that we are assuming the timestamp will
293 * never have a ">" in it.
295 * Note that we will always find some ">" before going off the front of
296 * the string, because will always hit the split->mail_end closing
297 * bracket.
299 for (cp = line + len - 1; *cp != '>'; cp--)
302 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
304 if (line + len <= cp)
305 goto person_only;
306 split->date_begin = cp;
307 span = strspn(cp, "0123456789");
308 if (!span)
309 goto person_only;
310 split->date_end = split->date_begin + span;
311 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
313 if (line + len <= cp || (*cp != '+' && *cp != '-'))
314 goto person_only;
315 split->tz_begin = cp;
316 span = strspn(cp + 1, "0123456789");
317 if (!span)
318 goto person_only;
319 split->tz_end = split->tz_begin + 1 + span;
320 return 0;
322 person_only:
323 split->date_begin = NULL;
324 split->date_end = NULL;
325 split->tz_begin = NULL;
326 split->tz_end = NULL;
327 return 0;
330 static const char *env_hint =
331 "\n"
332 "*** Please tell me who you are.\n"
333 "\n"
334 "Run\n"
335 "\n"
336 " git config --global user.email \"you@example.com\"\n"
337 " git config --global user.name \"Your Name\"\n"
338 "\n"
339 "to set your account\'s default identity.\n"
340 "Omit --global to set the identity only in this repository.\n"
341 "\n";
343 const char *fmt_ident(const char *name, const char *email,
344 const char *date_str, int flag)
346 static struct strbuf ident = STRBUF_INIT;
347 int strict = (flag & IDENT_STRICT);
348 int want_date = !(flag & IDENT_NO_DATE);
349 int want_name = !(flag & IDENT_NO_NAME);
351 if (want_name) {
352 int using_default = 0;
353 if (!name) {
354 if (strict && ident_use_config_only
355 && !(ident_config_given & IDENT_NAME_GIVEN)) {
356 fputs(env_hint, stderr);
357 die("no name was given and auto-detection is disabled");
359 name = ident_default_name();
360 using_default = 1;
361 if (strict && default_name_is_bogus) {
362 fputs(env_hint, stderr);
363 die("unable to auto-detect name (got '%s')", name);
366 if (!*name) {
367 struct passwd *pw;
368 if (strict) {
369 if (using_default)
370 fputs(env_hint, stderr);
371 die("empty ident name (for <%s>) not allowed", email);
373 pw = xgetpwuid_self(NULL);
374 name = pw->pw_name;
378 if (!email) {
379 if (strict && ident_use_config_only
380 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
381 fputs(env_hint, stderr);
382 die("no email was given and auto-detection is disabled");
384 email = ident_default_email();
385 if (strict && default_email_is_bogus) {
386 fputs(env_hint, stderr);
387 die("unable to auto-detect email address (got '%s')", email);
391 strbuf_reset(&ident);
392 if (want_name) {
393 strbuf_addstr_without_crud(&ident, name);
394 strbuf_addstr(&ident, " <");
396 strbuf_addstr_without_crud(&ident, email);
397 if (want_name)
398 strbuf_addch(&ident, '>');
399 if (want_date) {
400 strbuf_addch(&ident, ' ');
401 if (date_str && date_str[0]) {
402 if (parse_date(date_str, &ident) < 0)
403 die("invalid date format: %s", date_str);
405 else
406 strbuf_addstr(&ident, ident_default_date());
409 return ident.buf;
412 const char *fmt_name(const char *name, const char *email)
414 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
417 const char *git_author_info(int flag)
419 if (getenv("GIT_AUTHOR_NAME"))
420 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
421 if (getenv("GIT_AUTHOR_EMAIL"))
422 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
423 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
424 getenv("GIT_AUTHOR_EMAIL"),
425 getenv("GIT_AUTHOR_DATE"),
426 flag);
429 const char *git_committer_info(int flag)
431 if (getenv("GIT_COMMITTER_NAME"))
432 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
433 if (getenv("GIT_COMMITTER_EMAIL"))
434 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
435 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
436 getenv("GIT_COMMITTER_EMAIL"),
437 getenv("GIT_COMMITTER_DATE"),
438 flag);
441 static int ident_is_sufficient(int user_ident_explicitly_given)
443 #ifndef WINDOWS
444 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
445 #else
446 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
447 #endif
450 int committer_ident_sufficiently_given(void)
452 return ident_is_sufficient(committer_ident_explicitly_given);
455 int author_ident_sufficiently_given(void)
457 return ident_is_sufficient(author_ident_explicitly_given);
460 int git_ident_config(const char *var, const char *value, void *data)
462 if (!strcmp(var, "user.useconfigonly")) {
463 ident_use_config_only = git_config_bool(var, value);
464 return 0;
467 if (!strcmp(var, "user.name")) {
468 if (!value)
469 return config_error_nonbool(var);
470 strbuf_reset(&git_default_name);
471 strbuf_addstr(&git_default_name, value);
472 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
473 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
474 ident_config_given |= IDENT_NAME_GIVEN;
475 return 0;
478 if (!strcmp(var, "user.email")) {
479 if (!value)
480 return config_error_nonbool(var);
481 strbuf_reset(&git_default_email);
482 strbuf_addstr(&git_default_email, value);
483 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
484 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
485 ident_config_given |= IDENT_MAIL_GIVEN;
486 return 0;
489 return 0;
492 static int buf_cmp(const char *a_begin, const char *a_end,
493 const char *b_begin, const char *b_end)
495 int a_len = a_end - a_begin;
496 int b_len = b_end - b_begin;
497 int min = a_len < b_len ? a_len : b_len;
498 int cmp;
500 cmp = memcmp(a_begin, b_begin, min);
501 if (cmp)
502 return cmp;
504 return a_len - b_len;
507 int ident_cmp(const struct ident_split *a,
508 const struct ident_split *b)
510 int cmp;
512 cmp = buf_cmp(a->mail_begin, a->mail_end,
513 b->mail_begin, b->mail_end);
514 if (cmp)
515 return cmp;
517 return buf_cmp(a->name_begin, a->name_end,
518 b->name_begin, b->name_end);