pathspec: create parse_long_magic function
[alt-git.git] / ident.c
blobac4ae02b485637002869b3fc7a78033d7a5b6aec
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_errno("cannot open /etc/mailname");
79 return -1;
81 if (strbuf_getline(&mailnamebuf, mailname) == EOF) {
82 if (ferror(mailname))
83 warning_errno("cannot read /etc/mailname");
84 strbuf_release(&mailnamebuf);
85 fclose(mailname);
86 return -1;
88 /* success! */
89 strbuf_addbuf(buf, &mailnamebuf);
90 strbuf_release(&mailnamebuf);
91 fclose(mailname);
92 return 0;
95 static int canonical_name(const char *host, struct strbuf *out)
97 int status = -1;
99 #ifndef NO_IPV6
100 struct addrinfo hints, *ai;
101 memset (&hints, '\0', sizeof (hints));
102 hints.ai_flags = AI_CANONNAME;
103 if (!getaddrinfo(host, NULL, &hints, &ai)) {
104 if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) {
105 strbuf_addstr(out, ai->ai_canonname);
106 status = 0;
108 freeaddrinfo(ai);
110 #else
111 struct hostent *he = gethostbyname(host);
112 if (he && strchr(he->h_name, '.')) {
113 strbuf_addstr(out, he->h_name);
114 status = 0;
116 #endif /* NO_IPV6 */
118 return status;
121 static void add_domainname(struct strbuf *out, int *is_bogus)
123 char buf[1024];
125 if (gethostname(buf, sizeof(buf))) {
126 warning_errno("cannot get host name");
127 strbuf_addstr(out, "(none)");
128 *is_bogus = 1;
129 return;
131 if (strchr(buf, '.'))
132 strbuf_addstr(out, buf);
133 else if (canonical_name(buf, out) < 0) {
134 strbuf_addf(out, "%s.(none)", buf);
135 *is_bogus = 1;
139 static void copy_email(const struct passwd *pw, struct strbuf *email,
140 int *is_bogus)
143 * Make up a fake email address
144 * (name + '@' + hostname [+ '.' + domainname])
146 strbuf_addstr(email, pw->pw_name);
147 strbuf_addch(email, '@');
149 if (!add_mailname_host(email))
150 return; /* read from "/etc/mailname" (Debian) */
151 add_domainname(email, is_bogus);
154 const char *ident_default_name(void)
156 if (!git_default_name.len) {
157 copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name);
158 strbuf_trim(&git_default_name);
160 return git_default_name.buf;
163 const char *ident_default_email(void)
165 if (!git_default_email.len) {
166 const char *email = getenv("EMAIL");
168 if (email && email[0]) {
169 strbuf_addstr(&git_default_email, email);
170 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
171 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
172 } else
173 copy_email(xgetpwuid_self(&default_email_is_bogus),
174 &git_default_email, &default_email_is_bogus);
175 strbuf_trim(&git_default_email);
177 return git_default_email.buf;
180 static const char *ident_default_date(void)
182 if (!git_default_date.len)
183 datestamp(&git_default_date);
184 return git_default_date.buf;
187 void reset_ident_date(void)
189 strbuf_reset(&git_default_date);
192 static int crud(unsigned char c)
194 return c <= 32 ||
195 c == '.' ||
196 c == ',' ||
197 c == ':' ||
198 c == ';' ||
199 c == '<' ||
200 c == '>' ||
201 c == '"' ||
202 c == '\\' ||
203 c == '\'';
207 * Copy over a string to the destination, but avoid special
208 * characters ('\n', '<' and '>') and remove crud at the end
210 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
212 size_t i, len;
213 unsigned char c;
215 /* Remove crud from the beginning.. */
216 while ((c = *src) != 0) {
217 if (!crud(c))
218 break;
219 src++;
222 /* Remove crud from the end.. */
223 len = strlen(src);
224 while (len > 0) {
225 c = src[len-1];
226 if (!crud(c))
227 break;
228 --len;
232 * Copy the rest to the buffer, but avoid the special
233 * characters '\n' '<' and '>' that act as delimiters on
234 * an identification line. We can only remove crud, never add it,
235 * so 'len' is our maximum.
237 strbuf_grow(sb, len);
238 for (i = 0; i < len; i++) {
239 c = *src++;
240 switch (c) {
241 case '\n': case '<': case '>':
242 continue;
244 sb->buf[sb->len++] = c;
246 sb->buf[sb->len] = '\0';
250 * Reverse of fmt_ident(); given an ident line, split the fields
251 * to allow the caller to parse it.
252 * Signal a success by returning 0, but date/tz fields of the result
253 * can still be NULL if the input line only has the name/email part
254 * (e.g. reading from a reflog entry).
256 int split_ident_line(struct ident_split *split, const char *line, int len)
258 const char *cp;
259 size_t span;
260 int status = -1;
262 memset(split, 0, sizeof(*split));
264 split->name_begin = line;
265 for (cp = line; *cp && cp < line + len; cp++)
266 if (*cp == '<') {
267 split->mail_begin = cp + 1;
268 break;
270 if (!split->mail_begin)
271 return status;
273 for (cp = split->mail_begin - 2; line <= cp; cp--)
274 if (!isspace(*cp)) {
275 split->name_end = cp + 1;
276 break;
278 if (!split->name_end) {
279 /* no human readable name */
280 split->name_end = split->name_begin;
283 for (cp = split->mail_begin; cp < line + len; cp++)
284 if (*cp == '>') {
285 split->mail_end = cp;
286 break;
288 if (!split->mail_end)
289 return status;
292 * Look from the end-of-line to find the trailing ">" of the mail
293 * address, even though we should already know it as split->mail_end.
294 * This can help in cases of broken idents with an extra ">" somewhere
295 * in the email address. Note that we are assuming the timestamp will
296 * never have a ">" in it.
298 * Note that we will always find some ">" before going off the front of
299 * the string, because will always hit the split->mail_end closing
300 * bracket.
302 for (cp = line + len - 1; *cp != '>'; cp--)
305 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
307 if (line + len <= cp)
308 goto person_only;
309 split->date_begin = cp;
310 span = strspn(cp, "0123456789");
311 if (!span)
312 goto person_only;
313 split->date_end = split->date_begin + span;
314 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
316 if (line + len <= cp || (*cp != '+' && *cp != '-'))
317 goto person_only;
318 split->tz_begin = cp;
319 span = strspn(cp + 1, "0123456789");
320 if (!span)
321 goto person_only;
322 split->tz_end = split->tz_begin + 1 + span;
323 return 0;
325 person_only:
326 split->date_begin = NULL;
327 split->date_end = NULL;
328 split->tz_begin = NULL;
329 split->tz_end = NULL;
330 return 0;
333 static const char *env_hint =
334 N_("\n"
335 "*** Please tell me who you are.\n"
336 "\n"
337 "Run\n"
338 "\n"
339 " git config --global user.email \"you@example.com\"\n"
340 " git config --global user.name \"Your Name\"\n"
341 "\n"
342 "to set your account\'s default identity.\n"
343 "Omit --global to set the identity only in this repository.\n"
344 "\n");
346 const char *fmt_ident(const char *name, const char *email,
347 const char *date_str, int flag)
349 static struct strbuf ident = STRBUF_INIT;
350 int strict = (flag & IDENT_STRICT);
351 int want_date = !(flag & IDENT_NO_DATE);
352 int want_name = !(flag & IDENT_NO_NAME);
354 if (want_name) {
355 int using_default = 0;
356 if (!name) {
357 if (strict && ident_use_config_only
358 && !(ident_config_given & IDENT_NAME_GIVEN)) {
359 fputs(_(env_hint), stderr);
360 die("no name was given and auto-detection is disabled");
362 name = ident_default_name();
363 using_default = 1;
364 if (strict && default_name_is_bogus) {
365 fputs(_(env_hint), stderr);
366 die("unable to auto-detect name (got '%s')", name);
369 if (!*name) {
370 struct passwd *pw;
371 if (strict) {
372 if (using_default)
373 fputs(_(env_hint), stderr);
374 die("empty ident name (for <%s>) not allowed", email);
376 pw = xgetpwuid_self(NULL);
377 name = pw->pw_name;
381 if (!email) {
382 if (strict && ident_use_config_only
383 && !(ident_config_given & IDENT_MAIL_GIVEN)) {
384 fputs(_(env_hint), stderr);
385 die("no email was given and auto-detection is disabled");
387 email = ident_default_email();
388 if (strict && default_email_is_bogus) {
389 fputs(_(env_hint), stderr);
390 die("unable to auto-detect email address (got '%s')", email);
394 strbuf_reset(&ident);
395 if (want_name) {
396 strbuf_addstr_without_crud(&ident, name);
397 strbuf_addstr(&ident, " <");
399 strbuf_addstr_without_crud(&ident, email);
400 if (want_name)
401 strbuf_addch(&ident, '>');
402 if (want_date) {
403 strbuf_addch(&ident, ' ');
404 if (date_str && date_str[0]) {
405 if (parse_date(date_str, &ident) < 0)
406 die("invalid date format: %s", date_str);
408 else
409 strbuf_addstr(&ident, ident_default_date());
412 return ident.buf;
415 const char *fmt_name(const char *name, const char *email)
417 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
420 const char *git_author_info(int flag)
422 if (getenv("GIT_AUTHOR_NAME"))
423 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
424 if (getenv("GIT_AUTHOR_EMAIL"))
425 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
426 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
427 getenv("GIT_AUTHOR_EMAIL"),
428 getenv("GIT_AUTHOR_DATE"),
429 flag);
432 const char *git_committer_info(int flag)
434 if (getenv("GIT_COMMITTER_NAME"))
435 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
436 if (getenv("GIT_COMMITTER_EMAIL"))
437 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
438 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
439 getenv("GIT_COMMITTER_EMAIL"),
440 getenv("GIT_COMMITTER_DATE"),
441 flag);
444 static int ident_is_sufficient(int user_ident_explicitly_given)
446 #ifndef WINDOWS
447 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
448 #else
449 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
450 #endif
453 int committer_ident_sufficiently_given(void)
455 return ident_is_sufficient(committer_ident_explicitly_given);
458 int author_ident_sufficiently_given(void)
460 return ident_is_sufficient(author_ident_explicitly_given);
463 int git_ident_config(const char *var, const char *value, void *data)
465 if (!strcmp(var, "user.useconfigonly")) {
466 ident_use_config_only = git_config_bool(var, value);
467 return 0;
470 if (!strcmp(var, "user.name")) {
471 if (!value)
472 return config_error_nonbool(var);
473 strbuf_reset(&git_default_name);
474 strbuf_addstr(&git_default_name, value);
475 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
476 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
477 ident_config_given |= IDENT_NAME_GIVEN;
478 return 0;
481 if (!strcmp(var, "user.email")) {
482 if (!value)
483 return config_error_nonbool(var);
484 strbuf_reset(&git_default_email);
485 strbuf_addstr(&git_default_email, value);
486 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
487 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
488 ident_config_given |= IDENT_MAIL_GIVEN;
489 return 0;
492 return 0;
495 static int buf_cmp(const char *a_begin, const char *a_end,
496 const char *b_begin, const char *b_end)
498 int a_len = a_end - a_begin;
499 int b_len = b_end - b_begin;
500 int min = a_len < b_len ? a_len : b_len;
501 int cmp;
503 cmp = memcmp(a_begin, b_begin, min);
504 if (cmp)
505 return cmp;
507 return a_len - b_len;
510 int ident_cmp(const struct ident_split *a,
511 const struct ident_split *b)
513 int cmp;
515 cmp = buf_cmp(a->mail_begin, a->mail_end,
516 b->mail_begin, b->mail_end);
517 if (cmp)
518 return cmp;
520 return buf_cmp(a->name_begin, a->name_end,
521 b->name_begin, b->name_end);