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
;
15 #define IDENT_NAME_GIVEN 01
16 #define IDENT_MAIL_GIVEN 02
17 #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN)
18 static int committer_ident_explicitly_given
;
19 static int author_ident_explicitly_given
;
21 #ifdef NO_GECOS_IN_PWENT
22 #define get_gecos(ignored) "&"
24 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
27 static struct passwd
*xgetpwuid_self(void)
32 pw
= getpwuid(getuid());
34 die(_("unable to look up current user in the passwd file: %s"),
35 errno
? strerror(errno
) : _("no such user"));
39 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
43 /* Traditionally GECOS field had office phone numbers etc, separated
44 * with commas. Also & stands for capitalized form of the login name.
47 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
50 strbuf_addch(name
, ch
);
52 /* Sorry, Mr. McDonald... */
53 strbuf_addch(name
, toupper(*w
->pw_name
));
54 strbuf_addstr(name
, w
->pw_name
+ 1);
59 static int add_mailname_host(struct strbuf
*buf
)
62 struct strbuf mailnamebuf
= STRBUF_INIT
;
64 mailname
= fopen("/etc/mailname", "r");
67 warning("cannot open /etc/mailname: %s",
71 if (strbuf_getline(&mailnamebuf
, mailname
, '\n') == EOF
) {
73 warning("cannot read /etc/mailname: %s",
75 strbuf_release(&mailnamebuf
);
80 strbuf_addbuf(buf
, &mailnamebuf
);
81 strbuf_release(&mailnamebuf
);
86 static void add_domainname(struct strbuf
*out
, int *is_bogus
)
91 if (gethostname(buf
, sizeof(buf
))) {
92 warning("cannot get host name: %s", strerror(errno
));
93 strbuf_addstr(out
, "(none)");
98 strbuf_addstr(out
, buf
);
99 else if ((he
= gethostbyname(buf
)) && strchr(he
->h_name
, '.'))
100 strbuf_addstr(out
, he
->h_name
);
102 strbuf_addf(out
, "%s.(none)", buf
);
107 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
,
111 * Make up a fake email address
112 * (name + '@' + hostname [+ '.' + domainname])
114 strbuf_addstr(email
, pw
->pw_name
);
115 strbuf_addch(email
, '@');
117 if (!add_mailname_host(email
))
118 return; /* read from "/etc/mailname" (Debian) */
119 add_domainname(email
, is_bogus
);
122 const char *ident_default_name(void)
124 if (!git_default_name
.len
) {
125 copy_gecos(xgetpwuid_self(), &git_default_name
);
126 strbuf_trim(&git_default_name
);
128 return git_default_name
.buf
;
131 const char *ident_default_email(void)
133 if (!git_default_email
.len
) {
134 const char *email
= getenv("EMAIL");
136 if (email
&& email
[0]) {
137 strbuf_addstr(&git_default_email
, email
);
138 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
139 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
141 copy_email(xgetpwuid_self(), &git_default_email
,
142 &default_email_is_bogus
);
143 strbuf_trim(&git_default_email
);
145 return git_default_email
.buf
;
148 static const char *ident_default_date(void)
150 if (!git_default_date
.len
)
151 datestamp(&git_default_date
);
152 return git_default_date
.buf
;
155 static int crud(unsigned char c
)
170 * Copy over a string to the destination, but avoid special
171 * characters ('\n', '<' and '>') and remove crud at the end
173 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
178 /* Remove crud from the beginning.. */
179 while ((c
= *src
) != 0) {
185 /* Remove crud from the end.. */
195 * Copy the rest to the buffer, but avoid the special
196 * characters '\n' '<' and '>' that act as delimiters on
197 * an identification line. We can only remove crud, never add it,
198 * so 'len' is our maximum.
200 strbuf_grow(sb
, len
);
201 for (i
= 0; i
< len
; i
++) {
204 case '\n': case '<': case '>':
207 sb
->buf
[sb
->len
++] = c
;
209 sb
->buf
[sb
->len
] = '\0';
213 * Reverse of fmt_ident(); given an ident line, split the fields
214 * to allow the caller to parse it.
215 * Signal a success by returning 0, but date/tz fields of the result
216 * can still be NULL if the input line only has the name/email part
217 * (e.g. reading from a reflog entry).
219 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
225 memset(split
, 0, sizeof(*split
));
227 split
->name_begin
= line
;
228 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
230 split
->mail_begin
= cp
+ 1;
233 if (!split
->mail_begin
)
236 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
238 split
->name_end
= cp
+ 1;
241 if (!split
->name_end
) {
242 /* no human readable name */
243 split
->name_end
= split
->name_begin
;
246 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
248 split
->mail_end
= cp
;
251 if (!split
->mail_end
)
255 * Look from the end-of-line to find the trailing ">" of the mail
256 * address, even though we should already know it as split->mail_end.
257 * This can help in cases of broken idents with an extra ">" somewhere
258 * in the email address. Note that we are assuming the timestamp will
259 * never have a ">" in it.
261 * Note that we will always find some ">" before going off the front of
262 * the string, because will always hit the split->mail_end closing
265 for (cp
= line
+ len
- 1; *cp
!= '>'; cp
--)
268 for (cp
= cp
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
270 if (line
+ len
<= cp
)
272 split
->date_begin
= cp
;
273 span
= strspn(cp
, "0123456789");
276 split
->date_end
= split
->date_begin
+ span
;
277 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
279 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
281 split
->tz_begin
= cp
;
282 span
= strspn(cp
+ 1, "0123456789");
285 split
->tz_end
= split
->tz_begin
+ 1 + span
;
289 split
->date_begin
= NULL
;
290 split
->date_end
= NULL
;
291 split
->tz_begin
= NULL
;
292 split
->tz_end
= NULL
;
296 static const char *env_hint
=
298 "*** Please tell me who you are.\n"
302 " git config --global user.email \"you@example.com\"\n"
303 " git config --global user.name \"Your Name\"\n"
305 "to set your account\'s default identity.\n"
306 "Omit --global to set the identity only in this repository.\n"
309 const char *fmt_ident(const char *name
, const char *email
,
310 const char *date_str
, int flag
)
312 static struct strbuf ident
= STRBUF_INIT
;
313 int strict
= (flag
& IDENT_STRICT
);
314 int want_date
= !(flag
& IDENT_NO_DATE
);
315 int want_name
= !(flag
& IDENT_NO_NAME
);
317 if (want_name
&& !name
)
318 name
= ident_default_name();
320 email
= ident_default_email();
322 if (want_name
&& !*name
) {
326 if (name
== git_default_name
.buf
)
327 fputs(env_hint
, stderr
);
328 die("empty ident name (for <%s>) not allowed", email
);
330 pw
= xgetpwuid_self();
334 if (strict
&& email
== git_default_email
.buf
&& default_email_is_bogus
) {
335 fputs(env_hint
, stderr
);
336 die("unable to auto-detect email address (got '%s')", email
);
339 strbuf_reset(&ident
);
341 strbuf_addstr_without_crud(&ident
, name
);
342 strbuf_addstr(&ident
, " <");
344 strbuf_addstr_without_crud(&ident
, email
);
346 strbuf_addch(&ident
, '>');
348 strbuf_addch(&ident
, ' ');
349 if (date_str
&& date_str
[0]) {
350 if (parse_date(date_str
, &ident
) < 0)
351 die("invalid date format: %s", date_str
);
354 strbuf_addstr(&ident
, ident_default_date());
360 const char *fmt_name(const char *name
, const char *email
)
362 return fmt_ident(name
, email
, NULL
, IDENT_STRICT
| IDENT_NO_DATE
);
365 const char *git_author_info(int flag
)
367 if (getenv("GIT_AUTHOR_NAME"))
368 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
369 if (getenv("GIT_AUTHOR_EMAIL"))
370 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
371 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
372 getenv("GIT_AUTHOR_EMAIL"),
373 getenv("GIT_AUTHOR_DATE"),
377 const char *git_committer_info(int flag
)
379 if (getenv("GIT_COMMITTER_NAME"))
380 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
381 if (getenv("GIT_COMMITTER_EMAIL"))
382 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
383 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
384 getenv("GIT_COMMITTER_EMAIL"),
385 getenv("GIT_COMMITTER_DATE"),
389 static int ident_is_sufficient(int user_ident_explicitly_given
)
392 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
394 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
398 int committer_ident_sufficiently_given(void)
400 return ident_is_sufficient(committer_ident_explicitly_given
);
403 int author_ident_sufficiently_given(void)
405 return ident_is_sufficient(author_ident_explicitly_given
);
408 int git_ident_config(const char *var
, const char *value
, void *data
)
410 if (!strcmp(var
, "user.name")) {
412 return config_error_nonbool(var
);
413 strbuf_reset(&git_default_name
);
414 strbuf_addstr(&git_default_name
, value
);
415 committer_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
416 author_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
420 if (!strcmp(var
, "user.email")) {
422 return config_error_nonbool(var
);
423 strbuf_reset(&git_default_email
);
424 strbuf_addstr(&git_default_email
, value
);
425 committer_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
426 author_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
433 static int buf_cmp(const char *a_begin
, const char *a_end
,
434 const char *b_begin
, const char *b_end
)
436 int a_len
= a_end
- a_begin
;
437 int b_len
= b_end
- b_begin
;
438 int min
= a_len
< b_len
? a_len
: b_len
;
441 cmp
= memcmp(a_begin
, b_begin
, min
);
445 return a_len
- b_len
;
448 int ident_cmp(const struct ident_split
*a
,
449 const struct ident_split
*b
)
453 cmp
= buf_cmp(a
->mail_begin
, a
->mail_end
,
454 b
->mail_begin
, b
->mail_end
);
458 return buf_cmp(a
->name_begin
, a
->name_end
,
459 b
->name_begin
, b
->name_end
);