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
;
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 struct passwd
*xgetpwuid_self(void)
31 pw
= getpwuid(getuid());
33 die(_("unable to look up current user in the passwd file: %s"),
34 errno
? strerror(errno
) : _("no such user"));
38 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
42 /* Traditionally GECOS field had office phone numbers etc, separated
43 * with commas. Also & stands for capitalized form of the login name.
46 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
49 strbuf_addch(name
, ch
);
51 /* Sorry, Mr. McDonald... */
52 strbuf_addch(name
, toupper(*w
->pw_name
));
53 strbuf_addstr(name
, w
->pw_name
+ 1);
58 static int add_mailname_host(struct strbuf
*buf
)
61 struct strbuf mailnamebuf
= STRBUF_INIT
;
63 mailname
= fopen("/etc/mailname", "r");
66 warning("cannot open /etc/mailname: %s",
70 if (strbuf_getline(&mailnamebuf
, mailname
, '\n') == EOF
) {
72 warning("cannot read /etc/mailname: %s",
74 strbuf_release(&mailnamebuf
);
79 strbuf_addbuf(buf
, &mailnamebuf
);
80 strbuf_release(&mailnamebuf
);
85 static void add_domainname(struct strbuf
*out
)
90 if (gethostname(buf
, sizeof(buf
))) {
91 warning("cannot get host name: %s", strerror(errno
));
92 strbuf_addstr(out
, "(none)");
96 strbuf_addstr(out
, buf
);
97 else if ((he
= gethostbyname(buf
)) && strchr(he
->h_name
, '.'))
98 strbuf_addstr(out
, he
->h_name
);
100 strbuf_addf(out
, "%s.(none)", buf
);
103 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
)
106 * Make up a fake email address
107 * (name + '@' + hostname [+ '.' + domainname])
109 strbuf_addstr(email
, pw
->pw_name
);
110 strbuf_addch(email
, '@');
112 if (!add_mailname_host(email
))
113 return; /* read from "/etc/mailname" (Debian) */
114 add_domainname(email
);
117 const char *ident_default_name(void)
119 if (!git_default_name
.len
) {
120 copy_gecos(xgetpwuid_self(), &git_default_name
);
121 strbuf_trim(&git_default_name
);
123 return git_default_name
.buf
;
126 const char *ident_default_email(void)
128 if (!git_default_email
.len
) {
129 const char *email
= getenv("EMAIL");
131 if (email
&& email
[0]) {
132 strbuf_addstr(&git_default_email
, email
);
133 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
134 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
136 copy_email(xgetpwuid_self(), &git_default_email
);
137 strbuf_trim(&git_default_email
);
139 return git_default_email
.buf
;
142 static const char *ident_default_date(void)
144 if (!git_default_date
.len
)
145 datestamp(&git_default_date
);
146 return git_default_date
.buf
;
149 static int crud(unsigned char c
)
164 * Copy over a string to the destination, but avoid special
165 * characters ('\n', '<' and '>') and remove crud at the end
167 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
172 /* Remove crud from the beginning.. */
173 while ((c
= *src
) != 0) {
179 /* Remove crud from the end.. */
189 * Copy the rest to the buffer, but avoid the special
190 * characters '\n' '<' and '>' that act as delimiters on
191 * an identification line. We can only remove crud, never add it,
192 * so 'len' is our maximum.
194 strbuf_grow(sb
, len
);
195 for (i
= 0; i
< len
; i
++) {
198 case '\n': case '<': case '>':
201 sb
->buf
[sb
->len
++] = c
;
203 sb
->buf
[sb
->len
] = '\0';
207 * Reverse of fmt_ident(); given an ident line, split the fields
208 * to allow the caller to parse it.
209 * Signal a success by returning 0, but date/tz fields of the result
210 * can still be NULL if the input line only has the name/email part
211 * (e.g. reading from a reflog entry).
213 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
219 memset(split
, 0, sizeof(*split
));
221 split
->name_begin
= line
;
222 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
224 split
->mail_begin
= cp
+ 1;
227 if (!split
->mail_begin
)
230 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
232 split
->name_end
= cp
+ 1;
235 if (!split
->name_end
) {
236 /* no human readable name */
237 split
->name_end
= split
->name_begin
;
240 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
242 split
->mail_end
= cp
;
245 if (!split
->mail_end
)
249 * Look from the end-of-line to find the trailing ">" of the mail
250 * address, even though we should already know it as split->mail_end.
251 * This can help in cases of broken idents with an extra ">" somewhere
252 * in the email address. Note that we are assuming the timestamp will
253 * never have a ">" in it.
255 * Note that we will always find some ">" before going off the front of
256 * the string, because will always hit the split->mail_end closing
259 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
262 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
264 if (line
+ len
<= cp
)
266 split
->date_begin
= cp
;
267 span
= strspn(cp
, "0123456789");
270 split
->date_end
= split
->date_begin
+ span
;
271 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
273 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
275 split
->tz_begin
= cp
;
276 span
= strspn(cp
+ 1, "0123456789");
279 split
->tz_end
= split
->tz_begin
+ 1 + span
;
283 split
->date_begin
= NULL
;
284 split
->date_end
= NULL
;
285 split
->tz_begin
= NULL
;
286 split
->tz_end
= NULL
;
290 static const char *env_hint
=
292 "*** Please tell me who you are.\n"
296 " git config --global user.email \"you@example.com\"\n"
297 " git config --global user.name \"Your Name\"\n"
299 "to set your account\'s default identity.\n"
300 "Omit --global to set the identity only in this repository.\n"
303 const char *fmt_ident(const char *name
, const char *email
,
304 const char *date_str
, int flag
)
306 static struct strbuf ident
= STRBUF_INIT
;
307 int strict
= (flag
& IDENT_STRICT
);
308 int want_date
= !(flag
& IDENT_NO_DATE
);
309 int want_name
= !(flag
& IDENT_NO_NAME
);
311 if (want_name
&& !name
)
312 name
= ident_default_name();
314 email
= ident_default_email();
316 if (want_name
&& !*name
) {
320 if (name
== git_default_name
.buf
)
321 fputs(env_hint
, stderr
);
322 die("empty ident name (for <%s>) not allowed", email
);
324 pw
= xgetpwuid_self();
328 if (strict
&& email
== git_default_email
.buf
&&
329 strstr(email
, "(none)")) {
330 fputs(env_hint
, stderr
);
331 die("unable to auto-detect email address (got '%s')", email
);
334 strbuf_reset(&ident
);
336 strbuf_addstr_without_crud(&ident
, name
);
337 strbuf_addstr(&ident
, " <");
339 strbuf_addstr_without_crud(&ident
, email
);
341 strbuf_addch(&ident
, '>');
343 strbuf_addch(&ident
, ' ');
344 if (date_str
&& date_str
[0]) {
345 if (parse_date(date_str
, &ident
) < 0)
346 die("invalid date format: %s", date_str
);
349 strbuf_addstr(&ident
, ident_default_date());
355 const char *fmt_name(const char *name
, const char *email
)
357 return fmt_ident(name
, email
, NULL
, IDENT_STRICT
| IDENT_NO_DATE
);
360 const char *git_author_info(int flag
)
362 if (getenv("GIT_AUTHOR_NAME"))
363 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
364 if (getenv("GIT_AUTHOR_EMAIL"))
365 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
366 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
367 getenv("GIT_AUTHOR_EMAIL"),
368 getenv("GIT_AUTHOR_DATE"),
372 const char *git_committer_info(int flag
)
374 if (getenv("GIT_COMMITTER_NAME"))
375 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
376 if (getenv("GIT_COMMITTER_EMAIL"))
377 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
378 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
379 getenv("GIT_COMMITTER_EMAIL"),
380 getenv("GIT_COMMITTER_DATE"),
384 static int ident_is_sufficient(int user_ident_explicitly_given
)
387 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
389 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
393 int committer_ident_sufficiently_given(void)
395 return ident_is_sufficient(committer_ident_explicitly_given
);
398 int author_ident_sufficiently_given(void)
400 return ident_is_sufficient(author_ident_explicitly_given
);
403 int git_ident_config(const char *var
, const char *value
, void *data
)
405 if (!strcmp(var
, "user.name")) {
407 return config_error_nonbool(var
);
408 strbuf_reset(&git_default_name
);
409 strbuf_addstr(&git_default_name
, value
);
410 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
411 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
415 if (!strcmp(var
, "user.email")) {
417 return config_error_nonbool(var
);
418 strbuf_reset(&git_default_email
);
419 strbuf_addstr(&git_default_email
, value
);
420 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
421 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
428 static int buf_cmp(const char *a_begin
, const char *a_end
,
429 const char *b_begin
, const char *b_end
)
431 int a_len
= a_end
- a_begin
;
432 int b_len
= b_end
- b_begin
;
433 int min
= a_len
< b_len
? a_len
: b_len
;
436 cmp
= memcmp(a_begin
, b_begin
, min
);
440 return a_len
- b_len
;
443 int ident_cmp(const struct ident_split
*a
,
444 const struct ident_split
*b
)
448 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
449 b
->mail_begin
, b
->mail_end
);
453 return buf_cmp(a
->name_begin
, a
->name_end
,
454 b
->name_begin
, b
->name_end
);