4 * create git identifier lines of the form "name <email> date"
6 * Copyright (C) 2005 Linus Torvalds
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) "&"
23 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
26 static void copy_gecos(const struct passwd *w, struct strbuf *name)
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++) {
37 strbuf_addch(name, ch);
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)
50 mailname = fopen("/etc/mailname", "r");
53 warning("cannot open /etc/mailname: %s",
57 if (strbuf_getline(buf, mailname, '\n') == EOF) {
59 warning("cannot read /etc/mailname: %s",
69 static void add_domainname(struct strbuf *out)
74 if (gethostname(buf, sizeof(buf))) {
75 warning("cannot get host name: %s", strerror(errno));
76 strbuf_addstr(out, "(none)");
80 strbuf_addstr(out, buf);
81 else if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))
82 strbuf_addstr(out, he->h_name);
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;
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)
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)
156 /* Remove crud from the beginning.. */
157 while ((c = *src) != 0) {
163 /* Remove crud from the end.. */
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++) {
182 case '\n': case '<': case '>':
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)
203 memset(split, 0, sizeof(*split));
205 split->name_begin = line;
206 for (cp = line; *cp && cp < line + len; cp++)
208 split->mail_begin = cp + 1;
211 if (!split->mail_begin)
214 for (cp = split->mail_begin - 2; line <= cp; cp--)
216 split->name_end = cp + 1;
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++)
226 split->mail_end = cp;
229 if (!split->mail_end)
232 for (cp = split->mail_end + 1; cp < line + len && isspace(*cp); cp++)
234 if (line + len <= cp)
236 split->date_begin = cp;
237 span = strspn(cp, "0123456789");
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 != '-'))
245 split->tz_begin = cp;
246 span = strspn(cp + 1, "0123456789");
249 split->tz_end = split->tz_begin + 1 + span;
253 split->date_begin = NULL;
254 split->date_end = NULL;
255 split->tz_begin = NULL;
256 split->tz_end = NULL;
260 static const char *env_hint =
262 "*** Please tell me who you are.\n"
266 " git config --global user.email \"you@example.com\"\n"
267 " git config --global user.name \"Your Name\"\n"
269 "to set your account\'s default identity.\n"
270 "Omit --global to set the identity only in this repository.\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;
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();
285 email = ident_default_email();
287 if (want_name && !*name) {
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();
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);
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);
311 strcpy(date, ident_default_date());
314 strbuf_reset(&ident);
316 strbuf_addstr_without_crud(&ident, name);
317 strbuf_addstr(&ident, " <");
319 strbuf_addstr_without_crud(&ident, email);
321 strbuf_addch(&ident, '>');
323 strbuf_addch(&ident, ' ');
324 strbuf_addstr_without_crud(&ident, date);
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"),
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"),
358 static int ident_is_sufficient(int user_ident_explicitly_given)
361 return (user_ident_explicitly_given & IDENT_MAIL_GIVEN);
363 return (user_ident_explicitly_given == IDENT_ALL_GIVEN);
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")) {
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;
389 if (!strcmp(var, "user.email")) {
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;