setup: teach discover_git_directory to respect the commondir
[git/debian.git] / ident.c
blobd41fc91192097bbaa00de4367b1e2c7bda31440a
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"
9 #include "config.h"
11 static struct strbuf git_default_name = STRBUF_INIT;
12 static struct strbuf git_default_email = STRBUF_INIT;
13 static struct strbuf git_default_date = STRBUF_INIT;
14 static int default_email_is_bogus;
15 static int default_name_is_bogus;
17 static int ident_use_config_only;
19 #define IDENT_NAME_GIVEN 01
20 #define IDENT_MAIL_GIVEN 02
21 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
22 static int committer_ident_explicitly_given;
23 static int author_ident_explicitly_given;
24 static int ident_config_given;
26 #ifdef NO_GECOS_IN_PWENT
27 #define get_gecos(ignored) "&"
28 #else
29 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
30 #endif
32 static struct passwd *xgetpwuid_self(int *is_bogus)
34 struct passwd *pw;
36 errno = 0;
37 pw = getpwuid(getuid());
38 if (!pw) {
39 static struct passwd fallback;
40 fallback.pw_name = "unknown";
41 #ifndef NO_GECOS_IN_PWENT
42 fallback.pw_gecos = "Unknown";
43 #endif
44 pw = &fallback;
45 if (is_bogus)
46 *is_bogus = 1;
48 return pw;
51 static void copy_gecos(const struct passwd *w, struct strbuf *name)
53 char *src;
55 /* Traditionally GECOS field had office phone numbers etc, separated
56 * with commas. Also & stands for capitalized form of the login name.
59 for (src = get_gecos(w); *src && *src != ','; src++) {
60 int ch = *src;
61 if (ch != '&')
62 strbuf_addch(name, ch);
63 else {
64 /* Sorry, Mr. McDonald... */
65 strbuf_addch(name, toupper(*w->pw_name));
66 strbuf_addstr(name, w->pw_name + 1);
71 static int add_mailname_host(struct strbuf *buf)
73 FILE *mailname;
74 struct strbuf mailnamebuf = STRBUF_INIT;
76 mailname = fopen("/etc/mailname", "r");
77 if (!mailname) {
78 if (errno != ENOENT)
79 warning_errno("cannot open /etc/mailname");
80 return -1;
82 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
83 if (ferror(mailname))
84 warning_errno("cannot read /etc/mailname");
85 strbuf_release(&mailnamebuf);
86 fclose(mailname);
87 return -1;
89 /* success! */
90 strbuf_addbuf(buf, &mailnamebuf);
91 strbuf_release(&mailnamebuf);
92 fclose(mailname);
93 return 0;
96 static int canonical_name(const char *host, struct strbuf *out)
98 int status = -1;
100 #ifndef NO_IPV6
101 struct addrinfo hints, *ai;
102 memset (&hints, '\0', sizeof (hints));
103 hints.ai_flags = AI_CANONNAME;
104 if (!getaddrinfo(host, NULL, &hints, &ai)) {
105 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
106 strbuf_addstr(out, ai->ai_canonname);
107 status = 0;
109 freeaddrinfo(ai);
111 #else
112 struct hostent *he = gethostbyname(host);
113 if (he && strchr(he->h_name, '.')) {
114 strbuf_addstr(out, he->h_name);
115 status = 0;
117 #endif /* NO_IPV6 */
119 return status;
122 static void add_domainname(struct strbuf *out, int *is_bogus)
124 char buf[HOST_NAME_MAX + 1];
126 if (xgethostname(buf, sizeof(buf))) {
127 warning_errno("cannot get host name");
128 strbuf_addstr(out, "(none)");
129 *is_bogus = 1;
130 return;
132 if (strchr(buf, '.'))
133 strbuf_addstr(out, buf);
134 else if (canonical_name(buf, out) < 0) {
135 strbuf_addf(out, "%s.(none)", buf);
136 *is_bogus = 1;
140 static void copy_email(const struct passwd *pw, struct strbuf *email,
141 int *is_bogus)
144 * Make up a fake email address
145 * (name + '@' + hostname [+ '.' + domainname])
147 strbuf_addstr(email, pw->pw_name);
148 strbuf_addch(email, '@');
150 if (!add_mailname_host(email))
151 return; /* read from "/etc/mailname" (Debian) */
152 add_domainname(email, is_bogus);
155 const char *ident_default_name(void)
157 if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) {
158 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
159 strbuf_trim(&git_default_name);
161 return git_default_name.buf;
164 const char *ident_default_email(void)
166 if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) {
167 const char *email = getenv("EMAIL");
169 if (email && email[0]) {
170 strbuf_addstr(&git_default_email, email);
171 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
172 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
173 } else
174 copy_email(xgetpwuid_self(&default_email_is_bogus),
175 &git_default_email, &default_email_is_bogus);
176 strbuf_trim(&git_default_email);
178 return git_default_email.buf;
181 static const char *ident_default_date(void)
183 if (!git_default_date.len)
184 datestamp(&git_default_date);
185 return git_default_date.buf;
188 void reset_ident_date(void)
190 strbuf_reset(&git_default_date);
193 static int crud(unsigned char c)
195 return c <= 32 ||
196 c == '.' ||
197 c == ',' ||
198 c == ':' ||
199 c == ';' ||
200 c == '<' ||
201 c == '>' ||
202 c == '"' ||
203 c == '\\' ||
204 c == '\'';
207 static int has_non_crud(const char *str)
209 for (; *str; str++) {
210 if (!crud(*str))
211 return 1;
213 return 0;
217 * Copy over a string to the destination, but avoid special
218 * characters ('\n', '<' and '>') and remove crud at the end
220 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
222 size_t i, len;
223 unsigned char c;
225 /* Remove crud from the beginning.. */
226 while ((c = *src) != 0) {
227 if (!crud(c))
228 break;
229 src++;
232 /* Remove crud from the end.. */
233 len = strlen(src);
234 while (len > 0) {
235 c = src[len-1];
236 if (!crud(c))
237 break;
238 --len;
242 * Copy the rest to the buffer, but avoid the special
243 * characters '\n' '<' and '>' that act as delimiters on
244 * an identification line. We can only remove crud, never add it,
245 * so 'len' is our maximum.
247 strbuf_grow(sb, len);
248 for (i = 0; i < len; i++) {
249 c = *src++;
250 switch (c) {
251 case '\n': case '<': case '>':
252 continue;
254 sb->buf[sb->len++] = c;
256 sb->buf[sb->len] = '\0';
260 * Reverse of fmt_ident(); given an ident line, split the fields
261 * to allow the caller to parse it.
262 * Signal a success by returning 0, but date/tz fields of the result
263 * can still be NULL if the input line only has the name/email part
264 * (e.g. reading from a reflog entry).
266 int split_ident_line(struct ident_split *split, const char *line, int len)
268 const char *cp;
269 size_t span;
270 int status = -1;
272 memset(split, 0, sizeof(*split));
274 split->name_begin = line;
275 for (cp = line; *cp && cp < line + len; cp++)
276 if (*cp == '<') {
277 split->mail_begin = cp + 1;
278 break;
280 if (!split->mail_begin)
281 return status;
283 for (cp = split->mail_begin - 2; line <= cp; cp--)
284 if (!isspace(*cp)) {
285 split->name_end = cp + 1;
286 break;
288 if (!split->name_end) {
289 /* no human readable name */
290 split->name_end = split->name_begin;
293 for (cp = split->mail_begin; cp < line + len; cp++)
294 if (*cp == '>') {
295 split->mail_end = cp;
296 break;
298 if (!split->mail_end)
299 return status;
302 * Look from the end-of-line to find the trailing ">" of the mail
303 * address, even though we should already know it as split->mail_end.
304 * This can help in cases of broken idents with an extra ">" somewhere
305 * in the email address. Note that we are assuming the timestamp will
306 * never have a ">" in it.
308 * Note that we will always find some ">" before going off the front of
309 * the string, because will always hit the split->mail_end closing
310 * bracket.
312 for (cp = line + len - 1; *cp != '>'; cp--)
315 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
317 if (line + len <= cp)
318 goto person_only;
319 split->date_begin = cp;
320 span = strspn(cp, "0123456789");
321 if (!span)
322 goto person_only;
323 split->date_end = split->date_begin + span;
324 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
326 if (line + len <= cp || (*cp != '+' && *cp != '-'))
327 goto person_only;
328 split->tz_begin = cp;
329 span = strspn(cp + 1, "0123456789");
330 if (!span)
331 goto person_only;
332 split->tz_end = split->tz_begin + 1 + span;
333 return 0;
335 person_only:
336 split->date_begin = NULL;
337 split->date_end = NULL;
338 split->tz_begin = NULL;
339 split->tz_end = NULL;
340 return 0;
343 static const char *env_hint =
344 N_("\n"
345 "*** Please tell me who you are.\n"
346 "\n"
347 "Run\n"
348 "\n"
349 " git config --global user.email \"you@example.com\"\n"
350 " git config --global user.name \"Your Name\"\n"
351 "\n"
352 "to set your account\'s default identity.\n"
353 "Omit --global to set the identity only in this repository.\n"
354 "\n");
356 const char *fmt_ident(const char *name, const char *email,
357 const char *date_str, int flag)
359 static struct strbuf ident = STRBUF_INIT;
360 int strict = (flag & IDENT_STRICT);
361 int want_date = !(flag & IDENT_NO_DATE);
362 int want_name = !(flag & IDENT_NO_NAME);
364 if (!email) {
365 if (strict && ident_use_config_only
366 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
367 fputs(_(env_hint), stderr);
368 die(_("no email was given and auto-detection is disabled"));
370 email = ident_default_email();
371 if (strict && default_email_is_bogus) {
372 fputs(_(env_hint), stderr);
373 die(_("unable to auto-detect email address (got '%s')"), email);
377 if (want_name) {
378 int using_default = 0;
379 if (!name) {
380 if (strict && ident_use_config_only
381 && !(ident_config_given & IDENT_NAME_GIVEN)) {
382 fputs(_(env_hint), stderr);
383 die(_("no name was given and auto-detection is disabled"));
385 name = ident_default_name();
386 using_default = 1;
387 if (strict && default_name_is_bogus) {
388 fputs(_(env_hint), stderr);
389 die(_("unable to auto-detect name (got '%s')"), name);
392 if (!*name) {
393 struct passwd *pw;
394 if (strict) {
395 if (using_default)
396 fputs(_(env_hint), stderr);
397 die(_("empty ident name (for <%s>) not allowed"), email);
399 pw = xgetpwuid_self(NULL);
400 name = pw->pw_name;
402 if (strict && !has_non_crud(name))
403 die(_("name consists only of disallowed characters: %s"), name);
406 strbuf_reset(&ident);
407 if (want_name) {
408 strbuf_addstr_without_crud(&ident, name);
409 strbuf_addstr(&ident, " <");
411 strbuf_addstr_without_crud(&ident, email);
412 if (want_name)
413 strbuf_addch(&ident, '>');
414 if (want_date) {
415 strbuf_addch(&ident, ' ');
416 if (date_str && date_str[0]) {
417 if (parse_date(date_str, &ident) < 0)
418 die(_("invalid date format: %s"), date_str);
420 else
421 strbuf_addstr(&ident, ident_default_date());
424 return ident.buf;
427 const char *fmt_name(const char *name, const char *email)
429 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
432 const char *git_author_info(int flag)
434 if (getenv("GIT_AUTHOR_NAME"))
435 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
436 if (getenv("GIT_AUTHOR_EMAIL"))
437 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
438 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
439 getenv("GIT_AUTHOR_EMAIL"),
440 getenv("GIT_AUTHOR_DATE"),
441 flag);
444 const char *git_committer_info(int flag)
446 if (getenv("GIT_COMMITTER_NAME"))
447 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
448 if (getenv("GIT_COMMITTER_EMAIL"))
449 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
450 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
451 getenv("GIT_COMMITTER_EMAIL"),
452 getenv("GIT_COMMITTER_DATE"),
453 flag);
456 static int ident_is_sufficient(int user_ident_explicitly_given)
458 #ifndef WINDOWS
459 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
460 #else
461 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
462 #endif
465 int committer_ident_sufficiently_given(void)
467 return ident_is_sufficient(committer_ident_explicitly_given);
470 int author_ident_sufficiently_given(void)
472 return ident_is_sufficient(author_ident_explicitly_given);
475 int git_ident_config(const char *var, const char *value, void *data)
477 if (!strcmp(var, "user.useconfigonly")) {
478 ident_use_config_only = git_config_bool(var, value);
479 return 0;
482 if (!strcmp(var, "user.name")) {
483 if (!value)
484 return config_error_nonbool(var);
485 strbuf_reset(&git_default_name);
486 strbuf_addstr(&git_default_name, value);
487 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
488 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
489 ident_config_given |= IDENT_NAME_GIVEN;
490 return 0;
493 if (!strcmp(var, "user.email")) {
494 if (!value)
495 return config_error_nonbool(var);
496 strbuf_reset(&git_default_email);
497 strbuf_addstr(&git_default_email, value);
498 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
499 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
500 ident_config_given |= IDENT_MAIL_GIVEN;
501 return 0;
504 return 0;
507 static int buf_cmp(const char *a_begin, const char *a_end,
508 const char *b_begin, const char *b_end)
510 int a_len = a_end - a_begin;
511 int b_len = b_end - b_begin;
512 int min = a_len < b_len ? a_len : b_len;
513 int cmp;
515 cmp = memcmp(a_begin, b_begin, min);
516 if (cmp)
517 return cmp;
519 return a_len - b_len;
522 int ident_cmp(const struct ident_split *a,
523 const struct ident_split *b)
525 int cmp;
527 cmp = buf_cmp(a->mail_begin, a->mail_end,
528 b->mail_begin, b->mail_end);
529 if (cmp)
530 return cmp;
532 return buf_cmp(a->name_begin, a->name_end,
533 b->name_begin, b->name_end);