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 struct strbuf git_default_date
= STRBUF_INIT
;
13 static int default_email_is_bogus
;
14 static int default_name_is_bogus
;
16 #define IDENT_NAME_GIVEN 01
17 #define IDENT_MAIL_GIVEN 02
18 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
19 static int committer_ident_explicitly_given
;
20 static int author_ident_explicitly_given
;
22 #ifdef NO_GECOS_IN_PWENT
23 #define get_gecos(ignored) "&"
25 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
28 static struct passwd
*xgetpwuid_self(int *is_bogus
)
33 pw
= getpwuid(getuid());
35 static struct passwd fallback
;
36 fallback
.pw_name
= "unknown";
37 #ifndef NO_GECOS_IN_PWENT
38 fallback
.pw_gecos
= "Unknown";
47 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
51 /* Traditionally GECOS field had office phone numbers etc, separated
52 * with commas. Also & stands for capitalized form of the login name.
55 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
58 strbuf_addch(name
, ch
);
60 /* Sorry, Mr. McDonald... */
61 strbuf_addch(name
, toupper(*w
->pw_name
));
62 strbuf_addstr(name
, w
->pw_name
+ 1);
67 static int add_mailname_host(struct strbuf
*buf
)
70 struct strbuf mailnamebuf
= STRBUF_INIT
;
72 mailname
= fopen("/etc/mailname", "r");
75 warning("cannot open /etc/mailname: %s",
79 if (strbuf_getline(&mailnamebuf
, mailname
, '\n') == EOF
) {
81 warning("cannot read /etc/mailname: %s",
83 strbuf_release(&mailnamebuf
);
88 strbuf_addbuf(buf
, &mailnamebuf
);
89 strbuf_release(&mailnamebuf
);
94 static void add_domainname(struct strbuf
*out
, int *is_bogus
)
99 if (gethostname(buf
, sizeof(buf
))) {
100 warning("cannot get host name: %s", strerror(errno
));
101 strbuf_addstr(out
, "(none)");
105 if (strchr(buf
, '.'))
106 strbuf_addstr(out
, buf
);
107 else if ((he
= gethostbyname(buf
)) && strchr(he
->h_name
, '.'))
108 strbuf_addstr(out
, he
->h_name
);
110 strbuf_addf(out
, "%s.(none)", buf
);
115 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
,
119 * Make up a fake email address
120 * (name + '@' + hostname [+ '.' + domainname])
122 strbuf_addstr(email
, pw
->pw_name
);
123 strbuf_addch(email
, '@');
125 if (!add_mailname_host(email
))
126 return; /* read from "/etc/mailname" (Debian) */
127 add_domainname(email
, is_bogus
);
130 const char *ident_default_name(void)
132 if (!git_default_name
.len
) {
133 copy_gecos(xgetpwuid_self(&default_name_is_bogus
), &git_default_name
);
134 strbuf_trim(&git_default_name
);
136 return git_default_name
.buf
;
139 const char *ident_default_email(void)
141 if (!git_default_email
.len
) {
142 const char *email
= getenv("EMAIL");
144 if (email
&& email
[0]) {
145 strbuf_addstr(&git_default_email
, email
);
146 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
147 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
149 copy_email(xgetpwuid_self(&default_email_is_bogus
),
150 &git_default_email
, &default_email_is_bogus
);
151 strbuf_trim(&git_default_email
);
153 return git_default_email
.buf
;
156 static const char *ident_default_date(void)
158 if (!git_default_date
.len
)
159 datestamp(&git_default_date
);
160 return git_default_date
.buf
;
163 static int crud(unsigned char c
)
178 * Copy over a string to the destination, but avoid special
179 * characters ('\n', '<' and '>') and remove crud at the end
181 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
186 /* Remove crud from the beginning.. */
187 while ((c
= *src
) != 0) {
193 /* Remove crud from the end.. */
203 * Copy the rest to the buffer, but avoid the special
204 * characters '\n' '<' and '>' that act as delimiters on
205 * an identification line. We can only remove crud, never add it,
206 * so 'len' is our maximum.
208 strbuf_grow(sb
, len
);
209 for (i
= 0; i
< len
; i
++) {
212 case '\n': case '<': case '>':
215 sb
->buf
[sb
->len
++] = c
;
217 sb
->buf
[sb
->len
] = '\0';
221 * Reverse of fmt_ident(); given an ident line, split the fields
222 * to allow the caller to parse it.
223 * Signal a success by returning 0, but date/tz fields of the result
224 * can still be NULL if the input line only has the name/email part
225 * (e.g. reading from a reflog entry).
227 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
233 memset(split
, 0, sizeof(*split
));
235 split
->name_begin
= line
;
236 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
238 split
->mail_begin
= cp
+ 1;
241 if (!split
->mail_begin
)
244 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
246 split
->name_end
= cp
+ 1;
249 if (!split
->name_end
) {
250 /* no human readable name */
251 split
->name_end
= split
->name_begin
;
254 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
256 split
->mail_end
= cp
;
259 if (!split
->mail_end
)
263 * Look from the end-of-line to find the trailing ">" of the mail
264 * address, even though we should already know it as split->mail_end.
265 * This can help in cases of broken idents with an extra ">" somewhere
266 * in the email address. Note that we are assuming the timestamp will
267 * never have a ">" in it.
269 * Note that we will always find some ">" before going off the front of
270 * the string, because will always hit the split->mail_end closing
273 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
276 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
278 if (line
+ len
<= cp
)
280 split
->date_begin
= cp
;
281 span
= strspn(cp
, "0123456789");
284 split
->date_end
= split
->date_begin
+ span
;
285 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
287 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
289 split
->tz_begin
= cp
;
290 span
= strspn(cp
+ 1, "0123456789");
293 split
->tz_end
= split
->tz_begin
+ 1 + span
;
297 split
->date_begin
= NULL
;
298 split
->date_end
= NULL
;
299 split
->tz_begin
= NULL
;
300 split
->tz_end
= NULL
;
304 static const char *env_hint
=
306 "*** Please tell me who you are.\n"
310 " git config --global user.email \"you@example.com\"\n"
311 " git config --global user.name \"Your Name\"\n"
313 "to set your account\'s default identity.\n"
314 "Omit --global to set the identity only in this repository.\n"
317 const char *fmt_ident(const char *name
, const char *email
,
318 const char *date_str
, int flag
)
320 static struct strbuf ident
= STRBUF_INIT
;
321 int strict
= (flag
& IDENT_STRICT
);
322 int want_date
= !(flag
& IDENT_NO_DATE
);
323 int want_name
= !(flag
& IDENT_NO_NAME
);
325 if (want_name
&& !name
)
326 name
= ident_default_name();
328 email
= ident_default_email();
330 if (want_name
&& !*name
) {
334 if (name
== git_default_name
.buf
)
335 fputs(env_hint
, stderr
);
336 die("empty ident name (for <%s>) not allowed", email
);
338 pw
= xgetpwuid_self(NULL
);
342 if (want_name
&& strict
&&
343 name
== git_default_name
.buf
&& default_name_is_bogus
) {
344 fputs(env_hint
, stderr
);
345 die("unable to auto-detect name (got '%s')", name
);
348 if (strict
&& email
== git_default_email
.buf
&& default_email_is_bogus
) {
349 fputs(env_hint
, stderr
);
350 die("unable to auto-detect email address (got '%s')", email
);
353 strbuf_reset(&ident
);
355 strbuf_addstr_without_crud(&ident
, name
);
356 strbuf_addstr(&ident
, " <");
358 strbuf_addstr_without_crud(&ident
, email
);
360 strbuf_addch(&ident
, '>');
362 strbuf_addch(&ident
, ' ');
363 if (date_str
&& date_str
[0]) {
364 if (parse_date(date_str
, &ident
) < 0)
365 die("invalid date format: %s", date_str
);
368 strbuf_addstr(&ident
, ident_default_date());
374 const char *fmt_name(const char *name
, const char *email
)
376 return fmt_ident(name
, email
, NULL
, IDENT_STRICT
| IDENT_NO_DATE
);
379 const char *git_author_info(int flag
)
381 if (getenv("GIT_AUTHOR_NAME"))
382 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
383 if (getenv("GIT_AUTHOR_EMAIL"))
384 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
385 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
386 getenv("GIT_AUTHOR_EMAIL"),
387 getenv("GIT_AUTHOR_DATE"),
391 const char *git_committer_info(int flag
)
393 if (getenv("GIT_COMMITTER_NAME"))
394 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
395 if (getenv("GIT_COMMITTER_EMAIL"))
396 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
397 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
398 getenv("GIT_COMMITTER_EMAIL"),
399 getenv("GIT_COMMITTER_DATE"),
403 static int ident_is_sufficient(int user_ident_explicitly_given
)
406 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
408 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
412 int committer_ident_sufficiently_given(void)
414 return ident_is_sufficient(committer_ident_explicitly_given
);
417 int author_ident_sufficiently_given(void)
419 return ident_is_sufficient(author_ident_explicitly_given
);
422 int git_ident_config(const char *var
, const char *value
, void *data
)
424 if (!strcmp(var
, "user.name")) {
426 return config_error_nonbool(var
);
427 strbuf_reset(&git_default_name
);
428 strbuf_addstr(&git_default_name
, value
);
429 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
430 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
434 if (!strcmp(var
, "user.email")) {
436 return config_error_nonbool(var
);
437 strbuf_reset(&git_default_email
);
438 strbuf_addstr(&git_default_email
, value
);
439 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
440 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
447 static int buf_cmp(const char *a_begin
, const char *a_end
,
448 const char *b_begin
, const char *b_end
)
450 int a_len
= a_end
- a_begin
;
451 int b_len
= b_end
- b_begin
;
452 int min
= a_len
< b_len
? a_len
: b_len
;
455 cmp
= memcmp(a_begin
, b_begin
, min
);
459 return a_len
- b_len
;
462 int ident_cmp(const struct ident_split
*a
,
463 const struct ident_split
*b
)
467 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
468 b
->mail_begin
, b
->mail_end
);
472 return buf_cmp(a
->name_begin
, a
->name_end
,
473 b
->name_begin
, b
->name_end
);