]> Pileus Git - ~andy/git/blob - ident.c
ident: use full dns names to generate email addresses
[~andy/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 char git_default_date[50];
13 int user_ident_explicitly_given;
14
15 #ifdef NO_GECOS_IN_PWENT
16 #define get_gecos(ignored) "&"
17 #else
18 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
19 #endif
20
21 static void copy_gecos(const struct passwd *w, struct strbuf *name)
22 {
23         char *src;
24
25         /* Traditionally GECOS field had office phone numbers etc, separated
26          * with commas.  Also & stands for capitalized form of the login name.
27          */
28
29         for (src = get_gecos(w); *src && *src != ','; src++) {
30                 int ch = *src;
31                 if (ch != '&')
32                         strbuf_addch(name, ch);
33                 else {
34                         /* Sorry, Mr. McDonald... */
35                         strbuf_addch(name, toupper(*w->pw_name));
36                         strbuf_addstr(name, w->pw_name + 1);
37                 }
38         }
39 }
40
41 static int add_mailname_host(struct strbuf *buf)
42 {
43         FILE *mailname;
44
45         mailname = fopen("/etc/mailname", "r");
46         if (!mailname) {
47                 if (errno != ENOENT)
48                         warning("cannot open /etc/mailname: %s",
49                                 strerror(errno));
50                 return -1;
51         }
52         if (strbuf_getline(buf, mailname, '\n') == EOF) {
53                 if (ferror(mailname))
54                         warning("cannot read /etc/mailname: %s",
55                                 strerror(errno));
56                 fclose(mailname);
57                 return -1;
58         }
59         /* success! */
60         fclose(mailname);
61         return 0;
62 }
63
64 static void add_domainname(struct strbuf *out)
65 {
66         char buf[1024];
67         struct hostent *he;
68
69         if (gethostname(buf, sizeof(buf))) {
70                 warning("cannot get host name: %s", strerror(errno));
71                 strbuf_addstr(out, "(none)");
72                 return;
73         }
74         if (strchr(buf, '.'))
75                 strbuf_addstr(out, buf);
76         else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
77                 strbuf_addstr(out, he->h_name);
78         else
79                 strbuf_addf(out, "%s.(none)", buf);
80 }
81
82 static void copy_email(const struct passwd *pw, struct strbuf *email)
83 {
84         /*
85          * Make up a fake email address
86          * (name + '@' + hostname [+ '.' + domainname])
87          */
88         strbuf_addstr(email, pw->pw_name);
89         strbuf_addch(email, '@');
90
91         if (!add_mailname_host(email))
92                 return; /* read from "/etc/mailname" (Debian) */
93         add_domainname(email);
94 }
95
96 const char *ident_default_name(void)
97 {
98         if (!git_default_name.len)
99                 copy_gecos(xgetpwuid_self(), &git_default_name);
100         return git_default_name.buf;
101 }
102
103 const char *ident_default_email(void)
104 {
105         if (!git_default_email.len) {
106                 const char *email = getenv("EMAIL");
107
108                 if (email && email[0]) {
109                         strbuf_addstr(&git_default_email, email);
110                         user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
111                 } else
112                         copy_email(xgetpwuid_self(), &git_default_email);
113         }
114         return git_default_email.buf;
115 }
116
117 const char *ident_default_date(void)
118 {
119         if (!git_default_date[0])
120                 datestamp(git_default_date, sizeof(git_default_date));
121         return git_default_date;
122 }
123
124 static int add_raw(char *buf, size_t size, int offset, const char *str)
125 {
126         size_t len = strlen(str);
127         if (offset + len > size)
128                 return size;
129         memcpy(buf + offset, str, len);
130         return offset + len;
131 }
132
133 static int crud(unsigned char c)
134 {
135         return  c <= 32  ||
136                 c == '.' ||
137                 c == ',' ||
138                 c == ':' ||
139                 c == ';' ||
140                 c == '<' ||
141                 c == '>' ||
142                 c == '"' ||
143                 c == '\\' ||
144                 c == '\'';
145 }
146
147 /*
148  * Copy over a string to the destination, but avoid special
149  * characters ('\n', '<' and '>') and remove crud at the end
150  */
151 static int copy(char *buf, size_t size, int offset, const char *src)
152 {
153         size_t i, len;
154         unsigned char c;
155
156         /* Remove crud from the beginning.. */
157         while ((c = *src) != 0) {
158                 if (!crud(c))
159                         break;
160                 src++;
161         }
162
163         /* Remove crud from the end.. */
164         len = strlen(src);
165         while (len > 0) {
166                 c = src[len-1];
167                 if (!crud(c))
168                         break;
169                 --len;
170         }
171
172         /*
173          * Copy the rest to the buffer, but avoid the special
174          * characters '\n' '<' and '>' that act as delimiters on
175          * an identification line
176          */
177         for (i = 0; i < len; i++) {
178                 c = *src++;
179                 switch (c) {
180                 case '\n': case '<': case '>':
181                         continue;
182                 }
183                 if (offset >= size)
184                         return size;
185                 buf[offset++] = c;
186         }
187         return offset;
188 }
189
190 /*
191  * Reverse of fmt_ident(); given an ident line, split the fields
192  * to allow the caller to parse it.
193  * Signal a success by returning 0, but date/tz fields of the result
194  * can still be NULL if the input line only has the name/email part
195  * (e.g. reading from a reflog entry).
196  */
197 int split_ident_line(struct ident_split *split, const char *line, int len)
198 {
199         const char *cp;
200         size_t span;
201         int status = -1;
202
203         memset(split, 0, sizeof(*split));
204
205         split->name_begin = line;
206         for (cp = line; *cp && cp < line + len; cp++)
207                 if (*cp == '<') {
208                         split->mail_begin = cp + 1;
209                         break;
210                 }
211         if (!split->mail_begin)
212                 return status;
213
214         for (cp = split->mail_begin - 2; line < cp; cp--)
215                 if (!isspace(*cp)) {
216                         split->name_end = cp + 1;
217                         break;
218                 }
219         if (!split->name_end)
220                 return status;
221
222         for (cp = split->mail_begin; cp < line + len; cp++)
223                 if (*cp == '>') {
224                         split->mail_end = cp;
225                         break;
226                 }
227         if (!split->mail_end)
228                 return status;
229
230         for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
231                 ;
232         if (line + len <= cp)
233                 goto person_only;
234         split->date_begin = cp;
235         span = strspn(cp, "0123456789");
236         if (!span)
237                 goto person_only;
238         split->date_end = split->date_begin + span;
239         for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
240                 ;
241         if (line + len <= cp || (*cp != '+' && *cp != '-'))
242                 goto person_only;
243         split->tz_begin = cp;
244         span = strspn(cp + 1, "0123456789");
245         if (!span)
246                 goto person_only;
247         split->tz_end = split->tz_begin + 1 + span;
248         return 0;
249
250 person_only:
251         split->date_begin = NULL;
252         split->date_end = NULL;
253         split->tz_begin = NULL;
254         split->tz_end = NULL;
255         return 0;
256 }
257
258 static const char *env_hint =
259 "\n"
260 "*** Please tell me who you are.\n"
261 "\n"
262 "Run\n"
263 "\n"
264 "  git config --global user.email \"you@example.com\"\n"
265 "  git config --global user.name \"Your Name\"\n"
266 "\n"
267 "to set your account\'s default identity.\n"
268 "Omit --global to set the identity only in this repository.\n"
269 "\n";
270
271 const char *fmt_ident(const char *name, const char *email,
272                       const char *date_str, int flag)
273 {
274         static char buffer[1000];
275         char date[50];
276         int i;
277         int error_on_no_name = (flag & IDENT_ERROR_ON_NO_NAME);
278         int name_addr_only = (flag & IDENT_NO_DATE);
279
280         if (!name)
281                 name = ident_default_name();
282         if (!email)
283                 email = ident_default_email();
284
285         if (!*name) {
286                 struct passwd *pw;
287
288                 if (error_on_no_name) {
289                         if (name == git_default_name.buf)
290                                 fputs(env_hint, stderr);
291                         die("empty ident %s <%s> not allowed", name, email);
292                 }
293                 pw = xgetpwuid_self();
294                 name = pw->pw_name;
295         }
296
297         strcpy(date, ident_default_date());
298         if (!name_addr_only && date_str && date_str[0]) {
299                 if (parse_date(date_str, date, sizeof(date)) < 0)
300                         die("invalid date format: %s", date_str);
301         }
302
303         i = copy(buffer, sizeof(buffer), 0, name);
304         i = add_raw(buffer, sizeof(buffer), i, " <");
305         i = copy(buffer, sizeof(buffer), i, email);
306         if (!name_addr_only) {
307                 i = add_raw(buffer, sizeof(buffer), i,  "> ");
308                 i = copy(buffer, sizeof(buffer), i, date);
309         } else {
310                 i = add_raw(buffer, sizeof(buffer), i, ">");
311         }
312         if (i >= sizeof(buffer))
313                 die("Impossibly long personal identifier");
314         buffer[i] = 0;
315         return buffer;
316 }
317
318 const char *fmt_name(const char *name, const char *email)
319 {
320         return fmt_ident(name, email, NULL, IDENT_ERROR_ON_NO_NAME | IDENT_NO_DATE);
321 }
322
323 const char *git_author_info(int flag)
324 {
325         return fmt_ident(getenv("GIT_AUTHOR_NAME"),
326                          getenv("GIT_AUTHOR_EMAIL"),
327                          getenv("GIT_AUTHOR_DATE"),
328                          flag);
329 }
330
331 const char *git_committer_info(int flag)
332 {
333         if (getenv("GIT_COMMITTER_NAME"))
334                 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
335         if (getenv("GIT_COMMITTER_EMAIL"))
336                 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
337         return fmt_ident(getenv("GIT_COMMITTER_NAME"),
338                          getenv("GIT_COMMITTER_EMAIL"),
339                          getenv("GIT_COMMITTER_DATE"),
340                          flag);
341 }
342
343 int user_ident_sufficiently_given(void)
344 {
345 #ifndef WINDOWS
346         return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
347 #else
348         return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
349 #endif
350 }
351
352 int git_ident_config(const char *var, const char *value, void *data)
353 {
354         if (!strcmp(var, "user.name")) {
355                 if (!value)
356                         return config_error_nonbool(var);
357                 strbuf_reset(&git_default_name);
358                 strbuf_addstr(&git_default_name, value);
359                 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
360                 return 0;
361         }
362
363         if (!strcmp(var, "user.email")) {
364                 if (!value)
365                         return config_error_nonbool(var);
366                 strbuf_reset(&git_default_email);
367                 strbuf_addstr(&git_default_email, value);
368                 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
369                 return 0;
370         }
371
372         return 0;
373 }