Merge branch 'as/check-ignore'
[git/jrn.git] / ident.c
blobac9672f607909111167559e3aa58e1585f05d0d8
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];
14 #define IDENT_NAME_GIVEN 01
15 #define IDENT_MAIL_GIVEN 02
16 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
17 static int committer_ident_explicitly_given;
18 static int author_ident_explicitly_given;
20 #ifdef NO_GECOS_IN_PWENT
21 #define get_gecos(ignored) "&"
22 #else
23 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
24 #endif
26 static void copy_gecos(const struct passwd *w, struct strbuf *name)
28 char *src;
30 /* Traditionally GECOS field had office phone numbers etc, separated
31 * with commas. Also & stands for capitalized form of the login name.
34 for (src = get_gecos(w); *src && *src != ','; src++) {
35 int ch = *src;
36 if (ch != '&')
37 strbuf_addch(name, ch);
38 else {
39 /* Sorry, Mr. McDonald... */
40 strbuf_addch(name, toupper(*w->pw_name));
41 strbuf_addstr(name, w->pw_name + 1);
46 static int add_mailname_host(struct strbuf *buf)
48 FILE *mailname;
50 mailname = fopen("/etc/mailname", "r");
51 if (!mailname) {
52 if (errno != ENOENT)
53 warning("cannot open /etc/mailname: %s",
54 strerror(errno));
55 return -1;
57 if (strbuf_getline(buf, mailname, '\n') == EOF) {
58 if (ferror(mailname))
59 warning("cannot read /etc/mailname: %s",
60 strerror(errno));
61 fclose(mailname);
62 return -1;
64 /* success! */
65 fclose(mailname);
66 return 0;
69 static void add_domainname(struct strbuf *out)
71 char buf[1024];
72 struct hostent *he;
74 if (gethostname(buf, sizeof(buf))) {
75 warning("cannot get host name: %s", strerror(errno));
76 strbuf_addstr(out, "(none)");
77 return;
79 if (strchr(buf, '.'))
80 strbuf_addstr(out, buf);
81 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
82 strbuf_addstr(out, he->h_name);
83 else
84 strbuf_addf(out, "%s.(none)", buf);
87 static void copy_email(const struct passwd *pw, struct strbuf *email)
90 * Make up a fake email address
91 * (name + '@' + hostname [+ '.' + domainname])
93 strbuf_addstr(email, pw->pw_name);
94 strbuf_addch(email, '@');
96 if (!add_mailname_host(email))
97 return; /* read from "/etc/mailname" (Debian) */
98 add_domainname(email);
101 static const char *ident_default_name(void)
103 if (!git_default_name.len) {
104 copy_gecos(xgetpwuid_self(), &git_default_name);
105 strbuf_trim(&git_default_name);
107 return git_default_name.buf;
110 const char *ident_default_email(void)
112 if (!git_default_email.len) {
113 const char *email = getenv("EMAIL");
115 if (email && email[0]) {
116 strbuf_addstr(&git_default_email, email);
117 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
118 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
119 } else
120 copy_email(xgetpwuid_self(), &git_default_email);
121 strbuf_trim(&git_default_email);
123 return git_default_email.buf;
126 static const char *ident_default_date(void)
128 if (!git_default_date[0])
129 datestamp(git_default_date, sizeof(git_default_date));
130 return git_default_date;
133 static int crud(unsigned char c)
135 return c <= 32 ||
136 c == '.' ||
137 c == ',' ||
138 c == ':' ||
139 c == ';' ||
140 c == '<' ||
141 c == '>' ||
142 c == '"' ||
143 c == '\\' ||
144 c == '\'';
148 * Copy over a string to the destination, but avoid special
149 * characters ('\n', '<' and '>') and remove crud at the end
151 static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src)
153 size_t i, len;
154 unsigned char c;
156 /* Remove crud from the beginning.. */
157 while ((c = *src) != 0) {
158 if (!crud(c))
159 break;
160 src++;
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;
173 * Copy the rest to the buffer, but avoid the special
174 * characters '\n' '<' and '>' that act as delimiters on
175 * an identification line. We can only remove crud, never add it,
176 * so 'len' is our maximum.
178 strbuf_grow(sb, len);
179 for (i = 0; i < len; i++) {
180 c = *src++;
181 switch (c) {
182 case '\n': case '<': case '>':
183 continue;
185 sb->buf[sb->len++] = c;
187 sb->buf[sb->len] = '\0';
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).
197 int split_ident_line(struct ident_split *split, const char *line, int len)
199 const char *cp;
200 size_t span;
201 int status = -1;
203 memset(split, 0, sizeof(*split));
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;
211 if (!split->mail_begin)
212 return status;
214 for (cp = split->mail_begin - 2; line <= cp; cp--)
215 if (!isspace(*cp)) {
216 split->name_end = cp + 1;
217 break;
219 if (!split->name_end) {
220 /* no human readable name */
221 split->name_end = split->name_begin;
224 for (cp = split->mail_begin; cp < line + len; cp++)
225 if (*cp == '>') {
226 split->mail_end = cp;
227 break;
229 if (!split->mail_end)
230 return status;
232 for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
234 if (line + len <= cp)
235 goto person_only;
236 split->date_begin = cp;
237 span = strspn(cp, "0123456789");
238 if (!span)
239 goto person_only;
240 split->date_end = split->date_begin + span;
241 for (cp = split->date_end; cp < line + len && isspace(*cp); cp++)
243 if (line + len <= cp || (*cp != '+' && *cp != '-'))
244 goto person_only;
245 split->tz_begin = cp;
246 span = strspn(cp + 1, "0123456789");
247 if (!span)
248 goto person_only;
249 split->tz_end = split->tz_begin + 1 + span;
250 return 0;
252 person_only:
253 split->date_begin = NULL;
254 split->date_end = NULL;
255 split->tz_begin = NULL;
256 split->tz_end = NULL;
257 return 0;
260 static const char *env_hint =
261 "\n"
262 "*** Please tell me who you are.\n"
263 "\n"
264 "Run\n"
265 "\n"
266 " git config --global user.email \"you@example.com\"\n"
267 " git config --global user.name \"Your Name\"\n"
268 "\n"
269 "to set your account\'s default identity.\n"
270 "Omit --global to set the identity only in this repository.\n"
271 "\n";
273 const char *fmt_ident(const char *name, const char *email,
274 const char *date_str, int flag)
276 static struct strbuf ident = STRBUF_INIT;
277 char date[50];
278 int strict = (flag & IDENT_STRICT);
279 int want_date = !(flag & IDENT_NO_DATE);
280 int want_name = !(flag & IDENT_NO_NAME);
282 if (want_name && !name)
283 name = ident_default_name();
284 if (!email)
285 email = ident_default_email();
287 if (want_name && !*name) {
288 struct passwd *pw;
290 if (strict) {
291 if (name == git_default_name.buf)
292 fputs(env_hint, stderr);
293 die("empty ident name (for <%s>) not allowed", email);
295 pw = xgetpwuid_self();
296 name = pw->pw_name;
299 if (strict && email == git_default_email.buf &&
300 strstr(email, "(none)")) {
301 fputs(env_hint, stderr);
302 die("unable to auto-detect email address (got '%s')", email);
305 if (want_date) {
306 if (date_str && date_str[0]) {
307 if (parse_date(date_str, date, sizeof(date)) < 0)
308 die("invalid date format: %s", date_str);
310 else
311 strcpy(date, ident_default_date());
314 strbuf_reset(&ident);
315 if (want_name) {
316 strbuf_addstr_without_crud(&ident, name);
317 strbuf_addstr(&ident, " <");
319 strbuf_addstr_without_crud(&ident, email);
320 if (want_name)
321 strbuf_addch(&ident, '>');
322 if (want_date) {
323 strbuf_addch(&ident, ' ');
324 strbuf_addstr_without_crud(&ident, date);
326 return ident.buf;
329 const char *fmt_name(const char *name, const char *email)
331 return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE);
334 const char *git_author_info(int flag)
336 if (getenv("GIT_AUTHOR_NAME"))
337 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
338 if (getenv("GIT_AUTHOR_EMAIL"))
339 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
340 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
341 getenv("GIT_AUTHOR_EMAIL"),
342 getenv("GIT_AUTHOR_DATE"),
343 flag);
346 const char *git_committer_info(int flag)
348 if (getenv("GIT_COMMITTER_NAME"))
349 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
350 if (getenv("GIT_COMMITTER_EMAIL"))
351 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
352 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
353 getenv("GIT_COMMITTER_EMAIL"),
354 getenv("GIT_COMMITTER_DATE"),
355 flag);
358 static int ident_is_sufficient(int user_ident_explicitly_given)
360 #ifndef WINDOWS
361 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
362 #else
363 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
364 #endif
367 int committer_ident_sufficiently_given(void)
369 return ident_is_sufficient(committer_ident_explicitly_given);
372 int author_ident_sufficiently_given(void)
374 return ident_is_sufficient(author_ident_explicitly_given);
377 int git_ident_config(const char *var, const char *value, void *data)
379 if (!strcmp(var, "user.name")) {
380 if (!value)
381 return config_error_nonbool(var);
382 strbuf_reset(&git_default_name);
383 strbuf_addstr(&git_default_name, value);
384 committer_ident_explicitly_given |= IDENT_NAME_GIVEN;
385 author_ident_explicitly_given |= IDENT_NAME_GIVEN;
386 return 0;
389 if (!strcmp(var, "user.email")) {
390 if (!value)
391 return config_error_nonbool(var);
392 strbuf_reset(&git_default_email);
393 strbuf_addstr(&git_default_email, value);
394 committer_ident_explicitly_given |= IDENT_MAIL_GIVEN;
395 author_ident_explicitly_given |= IDENT_MAIL_GIVEN;
396 return 0;
399 return 0;