ident: do not drop username when reading from /etc/mailname
[git.git] / ident.c
blobfb4cc722a3aa7859cbf170e0376ac16b445e6cba
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 char git_default_date[50];
13 int user_ident_explicitly_given;
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
21 static void copy_gecos(const struct passwd *w, struct strbuf *name)
23 char *src;
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
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);
41 static int add_mailname_host(struct strbuf *buf)
43 FILE *mailname;
44 struct strbuf mailnamebuf = STRBUF_INIT;
46 mailname = fopen("/etc/mailname", "r");
47 if (!mailname) {
48 if (errno != ENOENT)
49 warning("cannot open /etc/mailname: %s",
50 strerror(errno));
51 return -1;
53 if (strbuf_getline(&mailnamebuf, mailname, '\n') == EOF) {
54 if (ferror(mailname))
55 warning("cannot read /etc/mailname: %s",
56 strerror(errno));
57 strbuf_release(&mailnamebuf);
58 fclose(mailname);
59 return -1;
61 /* success! */
62 strbuf_addbuf(buf, &mailnamebuf);
63 strbuf_release(&mailnamebuf);
64 fclose(mailname);
65 return 0;
68 static void add_domainname(struct strbuf *out)
70 char buf[1024];
71 struct hostent *he;
73 if (gethostname(buf, sizeof(buf))) {
74 warning("cannot get host name: %s", strerror(errno));
75 strbuf_addstr(out, "(none)");
76 return;
78 if (strchr(buf, '.'))
79 strbuf_addstr(out, buf);
80 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
81 strbuf_addstr(out, he->h_name);
82 else
83 strbuf_addf(out, "%s.(none)", buf);
86 static void copy_email(const struct passwd *pw, struct strbuf *email)
89 * Make up a fake email address
90 * (name + '@' + hostname [+ '.' + domainname])
92 strbuf_addstr(email, pw->pw_name);
93 strbuf_addch(email, '@');
95 if (!add_mailname_host(email))
96 return; /* read from "/etc/mailname" (Debian) */
97 add_domainname(email);
100 const char *ident_default_name(void)
102 if (!git_default_name.len) {
103 copy_gecos(xgetpwuid_self(), &git_default_name);
104 strbuf_trim(&git_default_name);
106 return git_default_name.buf;
109 const char *ident_default_email(void)
111 if (!git_default_email.len) {
112 const char *email = getenv("EMAIL");
114 if (email && email[0]) {
115 strbuf_addstr(&git_default_email, email);
116 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
117 } else
118 copy_email(xgetpwuid_self(), &git_default_email);
119 strbuf_trim(&git_default_email);
121 return git_default_email.buf;
124 const char *ident_default_date(void)
126 if (!git_default_date[0])
127 datestamp(git_default_date, sizeof(git_default_date));
128 return git_default_date;
131 static int crud(unsigned char c)
133 return c <= 32 ||
134 c == '.' ||
135 c == ',' ||
136 c == ':' ||
137 c == ';' ||
138 c == '<' ||
139 c == '>' ||
140 c == '"' ||
141 c == '\\' ||
142 c == '\'';
146 * Copy over a string to the destination, but avoid special
147 * characters ('\n', '<' and '>') and remove crud at the end
149 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
151 size_t i, len;
152 unsigned char c;
154 /* Remove crud from the beginning.. */
155 while ((c = *src) != 0) {
156 if (!crud(c))
157 break;
158 src++;
161 /* Remove crud from the end.. */
162 len = strlen(src);
163 while (len > 0) {
164 c = src[len-1];
165 if (!crud(c))
166 break;
167 --len;
171 * Copy the rest to the buffer, but avoid the special
172 * characters '\n' '<' and '>' that act as delimiters on
173 * an identification line. We can only remove crud, never add it,
174 * so 'len' is our maximum.
176 strbuf_grow(sb, len);
177 for (i = 0; i < len; i++) {
178 c = *src++;
179 switch (c) {
180 case '\n': case '<': case '>':
181 continue;
183 sb->buf[sb->len++] = c;
185 sb->buf[sb->len] = '\0';
189 * Reverse of fmt_ident(); given an ident line, split the fields
190 * to allow the caller to parse it.
191 * Signal a success by returning 0, but date/tz fields of the result
192 * can still be NULL if the input line only has the name/email part
193 * (e.g. reading from a reflog entry).
195 int split_ident_line(struct ident_split *split, const char *line, int len)
197 const char *cp;
198 size_t span;
199 int status = -1;
201 memset(split, 0, sizeof(*split));
203 split->name_begin = line;
204 for (cp = line; *cp && cp < line + len; cp++)
205 if (*cp == '<') {
206 split->mail_begin = cp + 1;
207 break;
209 if (!split->mail_begin)
210 return status;
212 for (cp = split->mail_begin - 2; line <= cp; cp--)
213 if (!isspace(*cp)) {
214 split->name_end = cp + 1;
215 break;
217 if (!split->name_end) {
218 /* no human readable name */
219 split->name_end = split->name_begin;
222 for (cp = split->mail_begin; cp < line + len; cp++)
223 if (*cp == '>') {
224 split->mail_end = cp;
225 break;
227 if (!split->mail_end)
228 return status;
230 for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
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++)
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;
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;
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";
271 const char *fmt_ident(const char *name, const char *email,
272 const char *date_str, int flag)
274 static struct strbuf ident = STRBUF_INIT;
275 char date[50];
276 int strict = (flag & IDENT_STRICT);
277 int want_date = !(flag & IDENT_NO_DATE);
278 int want_name = !(flag & IDENT_NO_NAME);
280 if (want_name && !name)
281 name = ident_default_name();
282 if (!email)
283 email = ident_default_email();
285 if (want_name && !*name) {
286 struct passwd *pw;
288 if (strict) {
289 if (name == git_default_name.buf)
290 fputs(env_hint, stderr);
291 die("empty ident name (for <%s>) not allowed", email);
293 pw = xgetpwuid_self();
294 name = pw->pw_name;
297 if (strict && email == git_default_email.buf &&
298 strstr(email, "(none)")) {
299 fputs(env_hint, stderr);
300 die("unable to auto-detect email address (got '%s')", email);
303 if (want_date) {
304 if (date_str && date_str[0]) {
305 if (parse_date(date_str, date, sizeof(date)) < 0)
306 die("invalid date format: %s", date_str);
308 else
309 strcpy(date, ident_default_date());
312 strbuf_reset(&ident);
313 if (want_name) {
314 strbuf_addstr_without_crud(&ident, name);
315 strbuf_addstr(&ident, " <");
317 strbuf_addstr_without_crud(&ident, email);
318 if (want_name)
319 strbuf_addch(&ident, '>');
320 if (want_date) {
321 strbuf_addch(&ident, ' ');
322 strbuf_addstr_without_crud(&ident, date);
324 return ident.buf;
327 const char *fmt_name(const char *name, const char *email)
329 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
332 const char *git_author_info(int flag)
334 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
335 getenv("GIT_AUTHOR_EMAIL"),
336 getenv("GIT_AUTHOR_DATE"),
337 flag);
340 const char *git_committer_info(int flag)
342 if (getenv("GIT_COMMITTER_NAME"))
343 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
344 if (getenv("GIT_COMMITTER_EMAIL"))
345 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
346 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
347 getenv("GIT_COMMITTER_EMAIL"),
348 getenv("GIT_COMMITTER_DATE"),
349 flag);
352 int user_ident_sufficiently_given(void)
354 #ifndef WINDOWS
355 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
356 #else
357 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
358 #endif
361 int git_ident_config(const char *var, const char *value, void *data)
363 if (!strcmp(var, "user.name")) {
364 if (!value)
365 return config_error_nonbool(var);
366 strbuf_reset(&git_default_name);
367 strbuf_addstr(&git_default_name, value);
368 user_ident_explicitly_given |= IDENT_NAME_GIVEN;
369 return 0;
372 if (!strcmp(var, "user.email")) {
373 if (!value)
374 return config_error_nonbool(var);
375 strbuf_reset(&git_default_email);
376 strbuf_addstr(&git_default_email, value);
377 user_ident_explicitly_given |= IDENT_MAIL_GIVEN;
378 return 0;
381 return 0;