ident: keep a flag for bogus default_email
[git] / ident.c
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
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
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;
20
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
26
27 static struct passwd *xgetpwuid_self(void)
28 {
29         struct passwd *pw;
30
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;
37 }
38
39 static void copy_gecos(const struct passwd *w, struct strbuf *name)
40 {
41         char *src;
42
43         /* Traditionally GECOS field had office phone numbers etc, separated
44          * with commas.  Also & stands for capitalized form of the login name.
45          */
46
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);
55                 }
56         }
57 }
58
59 static int add_mailname_host(struct strbuf *buf)
60 {
61         FILE *mailname;
62         struct strbuf mailnamebuf = STRBUF_INIT;
63
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;
70         }
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;
78         }
79         /* success! */
80         strbuf_addbuf(buf, &mailnamebuf);
81         strbuf_release(&mailnamebuf);
82         fclose(mailname);
83         return 0;
84 }
85
86 static void add_domainname(struct strbuf *out, int *is_bogus)
87 {
88         char buf[1024];
89         struct hostent *he;
90
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;
96         }
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;
104         }
105 }
106
107 static void copy_email(const struct passwd *pw, struct strbuf *email,
108                        int *is_bogus)
109 {
110         /*
111          * Make up a fake email address
112          * (name + '@' + hostname [+ '.' + domainname])
113          */
114         strbuf_addstr(email, pw->pw_name);
115         strbuf_addch(email, '@');
116
117         if (!add_mailname_host(email))
118                 return; /* read from "/etc/mailname" (Debian) */
119         add_domainname(email, is_bogus);
120 }
121
122 const char *ident_default_name(void)
123 {
124         if (!git_default_name.len) {
125                 copy_gecos(xgetpwuid_self(), &git_default_name);
126                 strbuf_trim(&git_default_name);
127         }
128         return git_default_name.buf;
129 }
130
131 const char *ident_default_email(void)
132 {
133         if (!git_default_email.len) {
134                 const char *email = getenv("EMAIL");
135
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);
144         }
145         return git_default_email.buf;
146 }
147
148 static const char *ident_default_date(void)
149 {
150         if (!git_default_date.len)
151                 datestamp(&git_default_date);
152         return git_default_date.buf;
153 }
154
155 static int crud(unsigned char c)
156 {
157         return  c <= 32  ||
158                 c == '.' ||
159                 c == ',' ||
160                 c == ':' ||
161                 c == ';' ||
162                 c == '<' ||
163                 c == '>' ||
164                 c == '"' ||
165                 c == '\\' ||
166                 c == '\'';
167 }
168
169 /*
170  * Copy over a string to the destination, but avoid special
171  * characters ('\n', '<' and '>') and remove crud at the end
172  */
173 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
174 {
175         size_t i, len;
176         unsigned char c;
177
178         /* Remove crud from the beginning.. */
179         while ((c = *src) != 0) {
180                 if (!crud(c))
181                         break;
182                 src++;
183         }
184
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;
192         }
193
194         /*
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.
199          */
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;
206                 }
207                 sb->buf[sb->len++] = c;
208         }
209         sb->buf[sb->len] = '\0';
210 }
211
212 /*
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).
218  */
219 int split_ident_line(struct ident_split *split, const char *line, int len)
220 {
221         const char *cp;
222         size_t span;
223         int status = -1;
224
225         memset(split, 0, sizeof(*split));
226
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;
232                 }
233         if (!split->mail_begin)
234                 return status;
235
236         for (cp = split->mail_begin - 2; line <= cp; cp--)
237                 if (!isspace(*cp)) {
238                         split->name_end = cp + 1;
239                         break;
240                 }
241         if (!split->name_end) {
242                 /* no human readable name */
243                 split->name_end = split->name_begin;
244         }
245
246         for (cp = split->mail_begin; cp < line + len; cp++)
247                 if (*cp == '>') {
248                         split->mail_end = cp;
249                         break;
250                 }
251         if (!split->mail_end)
252                 return status;
253
254         /*
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.
260          *
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.
264          */
265         for (cp = line + len - 1; *cp != '>'; cp--)
266                 ;
267
268         for (cp = cp + 1; cp < line + len && isspace(*cp); cp++)
269                 ;
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++)
278                 ;
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;
287
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;
294 }
295
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";
308
309 const char *fmt_ident(const char *name, const char *email,
310                       const char *date_str, int flag)
311 {
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);
316
317         if (want_name && !name)
318                 name = ident_default_name();
319         if (!email)
320                 email = ident_default_email();
321
322         if (want_name && !*name) {
323                 struct passwd *pw;
324
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);
329                 }
330                 pw = xgetpwuid_self();
331                 name = pw->pw_name;
332         }
333
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);
337         }
338
339         strbuf_reset(&ident);
340         if (want_name) {
341                 strbuf_addstr_without_crud(&ident, name);
342                 strbuf_addstr(&ident, " <");
343         }
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);
352                 }
353                 else
354                         strbuf_addstr(&ident, ident_default_date());
355         }
356
357         return ident.buf;
358 }
359
360 const char *fmt_name(const char *name, const char *email)
361 {
362         return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
363 }
364
365 const char *git_author_info(int flag)
366 {
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);
375 }
376
377 const char *git_committer_info(int flag)
378 {
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);
387 }
388
389 static int ident_is_sufficient(int user_ident_explicitly_given)
390 {
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
396 }
397
398 int committer_ident_sufficiently_given(void)
399 {
400         return ident_is_sufficient(committer_ident_explicitly_given);
401 }
402
403 int author_ident_sufficiently_given(void)
404 {
405         return ident_is_sufficient(author_ident_explicitly_given);
406 }
407
408 int git_ident_config(const char *var, const char *value, void *data)
409 {
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;
418         }
419
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;
428         }
429
430         return 0;
431 }
432
433 static int buf_cmp(const char *a_begin, const char *a_end,
434                    const char *b_begin, const char *b_end)
435 {
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;
440
441         cmp = memcmp(a_begin, b_begin, min);
442         if (cmp)
443                 return cmp;
444
445         return a_len - b_len;
446 }
447
448 int ident_cmp(const struct ident_split *a,
449               const struct ident_split *b)
450 {
451         int cmp;
452
453         cmp = buf_cmp(a->mail_begin, a->mail_end,
454                       b->mail_begin, b->mail_end);
455         if (cmp)
456                 return cmp;
457
458         return buf_cmp(a->name_begin, a->name_end,
459                        b->name_begin, b->name_end);
460 }