ident: keep a flag for bogus default_email
[git/debian.git] / ident.c
blob085cfbe2319451c2711cd5430c3becbeb94c5eab
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;
15 #define IDENT_NAME_GIVEN 01
16 #define IDENT_MAIL_GIVEN 02
17 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
18 static int committer_ident_explicitly_given;
19 static int author_ident_explicitly_given;
21 #ifdef NO_GECOS_IN_PWENT
22 #define get_gecos(ignored) "&"
23 #else
24 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
25 #endif
27 static struct passwd *xgetpwuid_self(void)
29 struct passwd *pw;
31 errno = 0;
32 pw = getpwuid(getuid());
33 if (!pw)
34 die(_("unable to look up current user in the passwd file: %s"),
35 errno ? strerror(errno) : _("no such user"));
36 return pw;
39 static void copy_gecos(const struct passwd *w, struct strbuf *name)
41 char *src;
43 /* Traditionally GECOS field had office phone numbers etc, separated
44 * with commas. Also & stands for capitalized form of the login name.
47 for (src = get_gecos(w); *src && *src != ','; src++) {
48 int ch = *src;
49 if (ch != '&')
50 strbuf_addch(name, ch);
51 else {
52 /* Sorry, Mr. McDonald... */
53 strbuf_addch(name, toupper(*w->pw_name));
54 strbuf_addstr(name, w->pw_name + 1);
59 static int add_mailname_host(struct strbuf *buf)
61 FILE *mailname;
62 struct strbuf mailnamebuf = STRBUF_INIT;
64 mailname = fopen("/etc/mailname", "r");
65 if (!mailname) {
66 if (errno != ENOENT)
67 warning("cannot open /etc/mailname: %s",
68 strerror(errno));
69 return -1;
71 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
72 if (ferror(mailname))
73 warning("cannot read /etc/mailname: %s",
74 strerror(errno));
75 strbuf_release(&mailnamebuf);
76 fclose(mailname);
77 return -1;
79 /* success! */
80 strbuf_addbuf(buf, &mailnamebuf);
81 strbuf_release(&mailnamebuf);
82 fclose(mailname);
83 return 0;
86 static void add_domainname(struct strbuf *out, int *is_bogus)
88 char buf[1024];
89 struct hostent *he;
91 if (gethostname(buf, sizeof(buf))) {
92 warning("cannot get host name: %s", strerror(errno));
93 strbuf_addstr(out, "(none)");
94 *is_bogus = 1;
95 return;
97 if (strchr(buf, '.'))
98 strbuf_addstr(out, buf);
99 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
100 strbuf_addstr(out, he->h_name);
101 else {
102 strbuf_addf(out, "%s.(none)", buf);
103 *is_bogus = 1;
107 static void copy_email(const struct passwd *pw, struct strbuf *email,
108 int *is_bogus)
111 * Make up a fake email address
112 * (name + '@' + hostname [+ '.' + domainname])
114 strbuf_addstr(email, pw->pw_name);
115 strbuf_addch(email, '@');
117 if (!add_mailname_host(email))
118 return; /* read from "/etc/mailname" (Debian) */
119 add_domainname(email, is_bogus);
122 const char *ident_default_name(void)
124 if (!git_default_name.len) {
125 copy_gecos(xgetpwuid_self(), &git_default_name);
126 strbuf_trim(&git_default_name);
128 return git_default_name.buf;
131 const char *ident_default_email(void)
133 if (!git_default_email.len) {
134 const char *email = getenv("EMAIL");
136 if (email && email[0]) {
137 strbuf_addstr(&git_default_email, email);
138 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
139 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
140 } else
141 copy_email(xgetpwuid_self(), &git_default_email,
142 &default_email_is_bogus);
143 strbuf_trim(&git_default_email);
145 return git_default_email.buf;
148 static const char *ident_default_date(void)
150 if (!git_default_date.len)
151 datestamp(&git_default_date);
152 return git_default_date.buf;
155 static int crud(unsigned char c)
157 return c <= 32 ||
158 c == '.' ||
159 c == ',' ||
160 c == ':' ||
161 c == ';' ||
162 c == '<' ||
163 c == '>' ||
164 c == '"' ||
165 c == '\\' ||
166 c == '\'';
170 * Copy over a string to the destination, but avoid special
171 * characters ('\n', '<' and '>') and remove crud at the end
173 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
175 size_t i, len;
176 unsigned char c;
178 /* Remove crud from the beginning.. */
179 while ((c = *src) != 0) {
180 if (!crud(c))
181 break;
182 src++;
185 /* Remove crud from the end.. */
186 len = strlen(src);
187 while (len > 0) {
188 c = src[len-1];
189 if (!crud(c))
190 break;
191 --len;
195 * Copy the rest to the buffer, but avoid the special
196 * characters '\n' '<' and '>' that act as delimiters on
197 * an identification line. We can only remove crud, never add it,
198 * so 'len' is our maximum.
200 strbuf_grow(sb, len);
201 for (i = 0; i < len; i++) {
202 c = *src++;
203 switch (c) {
204 case '\n': case '<': case '>':
205 continue;
207 sb->buf[sb->len++] = c;
209 sb->buf[sb->len] = '\0';
213 * Reverse of fmt_ident(); given an ident line, split the fields
214 * to allow the caller to parse it.
215 * Signal a success by returning 0, but date/tz fields of the result
216 * can still be NULL if the input line only has the name/email part
217 * (e.g. reading from a reflog entry).
219 int split_ident_line(struct ident_split *split, const char *line, int len)
221 const char *cp;
222 size_t span;
223 int status = -1;
225 memset(split, 0, sizeof(*split));
227 split->name_begin = line;
228 for (cp = line; *cp && cp < line + len; cp++)
229 if (*cp == '<') {
230 split->mail_begin = cp + 1;
231 break;
233 if (!split->mail_begin)
234 return status;
236 for (cp = split->mail_begin - 2; line <= cp; cp--)
237 if (!isspace(*cp)) {
238 split->name_end = cp + 1;
239 break;
241 if (!split->name_end) {
242 /* no human readable name */
243 split->name_end = split->name_begin;
246 for (cp = split->mail_begin; cp < line + len; cp++)
247 if (*cp == '>') {
248 split->mail_end = cp;
249 break;
251 if (!split->mail_end)
252 return status;
255 * Look from the end-of-line to find the trailing ">" of the mail
256 * address, even though we should already know it as split->mail_end.
257 * This can help in cases of broken idents with an extra ">" somewhere
258 * in the email address. Note that we are assuming the timestamp will
259 * never have a ">" in it.
261 * Note that we will always find some ">" before going off the front of
262 * the string, because will always hit the split->mail_end closing
263 * bracket.
265 for (cp = line + len - 1; *cp != '>'; cp--)
268 for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
270 if (line + len <= cp)
271 goto person_only;
272 split->date_begin = cp;
273 span = strspn(cp, "0123456789");
274 if (!span)
275 goto person_only;
276 split->date_end = split->date_begin + span;
277 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
279 if (line + len <= cp || (*cp != '+' && *cp != '-'))
280 goto person_only;
281 split->tz_begin = cp;
282 span = strspn(cp + 1, "0123456789");
283 if (!span)
284 goto person_only;
285 split->tz_end = split->tz_begin + 1 + span;
286 return 0;
288 person_only:
289 split->date_begin = NULL;
290 split->date_end = NULL;
291 split->tz_begin = NULL;
292 split->tz_end = NULL;
293 return 0;
296 static const char *env_hint =
297 "\n"
298 "*** Please tell me who you are.\n"
299 "\n"
300 "Run\n"
301 "\n"
302 " git config --global user.email \"you@example.com\"\n"
303 " git config --global user.name \"Your Name\"\n"
304 "\n"
305 "to set your account\'s default identity.\n"
306 "Omit --global to set the identity only in this repository.\n"
307 "\n";
309 const char *fmt_ident(const char *name, const char *email,
310 const char *date_str, int flag)
312 static struct strbuf ident = STRBUF_INIT;
313 int strict = (flag & IDENT_STRICT);
314 int want_date = !(flag & IDENT_NO_DATE);
315 int want_name = !(flag & IDENT_NO_NAME);
317 if (want_name && !name)
318 name = ident_default_name();
319 if (!email)
320 email = ident_default_email();
322 if (want_name && !*name) {
323 struct passwd *pw;
325 if (strict) {
326 if (name == git_default_name.buf)
327 fputs(env_hint, stderr);
328 die("empty ident name (for <%s>) not allowed", email);
330 pw = xgetpwuid_self();
331 name = pw->pw_name;
334 if (strict && email == git_default_email.buf && default_email_is_bogus) {
335 fputs(env_hint, stderr);
336 die("unable to auto-detect email address (got '%s')", email);
339 strbuf_reset(&ident);
340 if (want_name) {
341 strbuf_addstr_without_crud(&ident, name);
342 strbuf_addstr(&ident, " <");
344 strbuf_addstr_without_crud(&ident, email);
345 if (want_name)
346 strbuf_addch(&ident, '>');
347 if (want_date) {
348 strbuf_addch(&ident, ' ');
349 if (date_str && date_str[0]) {
350 if (parse_date(date_str, &ident) < 0)
351 die("invalid date format: %s", date_str);
353 else
354 strbuf_addstr(&ident, ident_default_date());
357 return ident.buf;
360 const char *fmt_name(const char *name, const char *email)
362 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
365 const char *git_author_info(int flag)
367 if (getenv("GIT_AUTHOR_NAME"))
368 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
369 if (getenv("GIT_AUTHOR_EMAIL"))
370 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
371 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
372 getenv("GIT_AUTHOR_EMAIL"),
373 getenv("GIT_AUTHOR_DATE"),
374 flag);
377 const char *git_committer_info(int flag)
379 if (getenv("GIT_COMMITTER_NAME"))
380 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
381 if (getenv("GIT_COMMITTER_EMAIL"))
382 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
383 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
384 getenv("GIT_COMMITTER_EMAIL"),
385 getenv("GIT_COMMITTER_DATE"),
386 flag);
389 static int ident_is_sufficient(int user_ident_explicitly_given)
391 #ifndef WINDOWS
392 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
393 #else
394 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
395 #endif
398 int committer_ident_sufficiently_given(void)
400 return ident_is_sufficient(committer_ident_explicitly_given);
403 int author_ident_sufficiently_given(void)
405 return ident_is_sufficient(author_ident_explicitly_given);
408 int git_ident_config(const char *var, const char *value, void *data)
410 if (!strcmp(var, "user.name")) {
411 if (!value)
412 return config_error_nonbool(var);
413 strbuf_reset(&git_default_name);
414 strbuf_addstr(&git_default_name, value);
415 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
416 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
417 return 0;
420 if (!strcmp(var, "user.email")) {
421 if (!value)
422 return config_error_nonbool(var);
423 strbuf_reset(&git_default_email);
424 strbuf_addstr(&git_default_email, value);
425 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
426 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
427 return 0;
430 return 0;
433 static int buf_cmp(const char *a_begin, const char *a_end,
434 const char *b_begin, const char *b_end)
436 int a_len = a_end - a_begin;
437 int b_len = b_end - b_begin;
438 int min = a_len < b_len ? a_len : b_len;
439 int cmp;
441 cmp = memcmp(a_begin, b_begin, min);
442 if (cmp)
443 return cmp;
445 return a_len - b_len;
448 int ident_cmp(const struct ident_split *a,
449 const struct ident_split *b)
451 int cmp;
453 cmp = buf_cmp(a->mail_begin, a->mail_end,
454 b->mail_begin, b->mail_end);
455 if (cmp)
456 return cmp;
458 return buf_cmp(a->name_begin, a->name_end,
459 b->name_begin, b->name_end);