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 user_ident_explicitly_given
;
19 #ifdef NO_GECOS_IN_PWENT
20 #define get_gecos(ignored) "&"
22 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
25 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
29 /* Traditionally GECOS field had office phone numbers etc, separated
30 * with commas. Also & stands for capitalized form of the login name.
33 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
36 strbuf_addch(name
, ch
);
38 /* Sorry, Mr. McDonald... */
39 strbuf_addch(name
, toupper(*w
->pw_name
));
40 strbuf_addstr(name
, w
->pw_name
+ 1);
45 static int add_mailname_host(struct strbuf
*buf
)
49 mailname
= fopen("/etc/mailname", "r");
52 warning("cannot open /etc/mailname: %s",
56 if (strbuf_getline(buf
, mailname
, '\n') == EOF
) {
58 warning("cannot read /etc/mailname: %s",
68 static void add_domainname(struct strbuf
*out
)
73 if (gethostname(buf
, sizeof(buf
))) {
74 warning("cannot get host name: %s", strerror(errno
));
75 strbuf_addstr(out
, "(none)");
79 strbuf_addstr(out
, buf
);
80 else if ((he
= gethostbyname(buf
)) && strchr(he
->h_name
, '.'))
81 strbuf_addstr(out
, he
->h_name
);
83 strbuf_addf(out
, "%s.(none)", buf
);
86 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
)
89 * Make up a fake email address
90 * (name + '@' + hostname [+ '.' + domainname])
92 strbuf_addstr(email
, pw
->pw_name
);
93 strbuf_addch(email
, '@');
95 if (!add_mailname_host(email
))
96 return; /* read from "/etc/mailname" (Debian) */
97 add_domainname(email
);
100 static const char *ident_default_name(void)
102 if (!git_default_name
.len
) {
103 copy_gecos(xgetpwuid_self(), &git_default_name
);
104 strbuf_trim(&git_default_name
);
106 return git_default_name
.buf
;
109 const char *ident_default_email(void)
111 if (!git_default_email
.len
) {
112 const char *email
= getenv("EMAIL");
114 if (email
&& email
[0]) {
115 strbuf_addstr(&git_default_email
, email
);
116 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
118 copy_email(xgetpwuid_self(), &git_default_email
);
119 strbuf_trim(&git_default_email
);
121 return git_default_email
.buf
;
124 static const char *ident_default_date(void)
126 if (!git_default_date
[0])
127 datestamp(git_default_date
, sizeof(git_default_date
));
128 return git_default_date
;
131 static int crud(unsigned char c
)
146 * Copy over a string to the destination, but avoid special
147 * characters ('\n', '<' and '>') and remove crud at the end
149 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
154 /* Remove crud from the beginning.. */
155 while ((c
= *src
) != 0) {
161 /* Remove crud from the end.. */
171 * Copy the rest to the buffer, but avoid the special
172 * characters '\n' '<' and '>' that act as delimiters on
173 * an identification line. We can only remove crud, never add it,
174 * so 'len' is our maximum.
176 strbuf_grow(sb
, len
);
177 for (i
= 0; i
< len
; i
++) {
180 case '\n': case '<': case '>':
183 sb
->buf
[sb
->len
++] = c
;
185 sb
->buf
[sb
->len
] = '\0';
189 * Reverse of fmt_ident(); given an ident line, split the fields
190 * to allow the caller to parse it.
191 * Signal a success by returning 0, but date/tz fields of the result
192 * can still be NULL if the input line only has the name/email part
193 * (e.g. reading from a reflog entry).
195 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
201 memset(split
, 0, sizeof(*split
));
203 split
->name_begin
= line
;
204 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
206 split
->mail_begin
= cp
+ 1;
209 if (!split
->mail_begin
)
212 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
214 split
->name_end
= cp
+ 1;
217 if (!split
->name_end
) {
218 /* no human readable name */
219 split
->name_end
= split
->name_begin
;
222 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
224 split
->mail_end
= cp
;
227 if (!split
->mail_end
)
230 for (cp
= split
->mail_end
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
232 if (line
+ len
<= cp
)
234 split
->date_begin
= cp
;
235 span
= strspn(cp
, "0123456789");
238 split
->date_end
= split
->date_begin
+ span
;
239 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
241 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
243 split
->tz_begin
= cp
;
244 span
= strspn(cp
+ 1, "0123456789");
247 split
->tz_end
= split
->tz_begin
+ 1 + span
;
251 split
->date_begin
= NULL
;
252 split
->date_end
= NULL
;
253 split
->tz_begin
= NULL
;
254 split
->tz_end
= NULL
;
258 static const char *env_hint
=
260 "*** Please tell me who you are.\n"
264 " git config --global user.email \"you@example.com\"\n"
265 " git config --global user.name \"Your Name\"\n"
267 "to set your account\'s default identity.\n"
268 "Omit --global to set the identity only in this repository.\n"
271 const char *fmt_ident(const char *name
, const char *email
,
272 const char *date_str
, int flag
)
274 static struct strbuf ident
= STRBUF_INIT
;
276 int strict
= (flag
& IDENT_STRICT
);
277 int want_date
= !(flag
& IDENT_NO_DATE
);
278 int want_name
= !(flag
& IDENT_NO_NAME
);
280 if (want_name
&& !name
)
281 name
= ident_default_name();
283 email
= ident_default_email();
285 if (want_name
&& !*name
) {
289 if (name
== git_default_name
.buf
)
290 fputs(env_hint
, stderr
);
291 die("empty ident name (for <%s>) not allowed", email
);
293 pw
= xgetpwuid_self();
297 if (strict
&& email
== git_default_email
.buf
&&
298 strstr(email
, "(none)")) {
299 fputs(env_hint
, stderr
);
300 die("unable to auto-detect email address (got '%s')", email
);
304 if (date_str
&& date_str
[0]) {
305 if (parse_date(date_str
, date
, sizeof(date
)) < 0)
306 die("invalid date format: %s", date_str
);
309 strcpy(date
, ident_default_date());
312 strbuf_reset(&ident
);
314 strbuf_addstr_without_crud(&ident
, name
);
315 strbuf_addstr(&ident
, " <");
317 strbuf_addstr_without_crud(&ident
, email
);
319 strbuf_addch(&ident
, '>');
321 strbuf_addch(&ident
, ' ');
322 strbuf_addstr_without_crud(&ident
, date
);
327 const char *fmt_name(const char *name
, const char *email
)
329 return fmt_ident(name
, email
, NULL
, IDENT_STRICT
| IDENT_NO_DATE
);
332 const char *git_author_info(int flag
)
334 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
335 getenv("GIT_AUTHOR_EMAIL"),
336 getenv("GIT_AUTHOR_DATE"),
340 const char *git_committer_info(int flag
)
342 if (getenv("GIT_COMMITTER_NAME"))
343 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
344 if (getenv("GIT_COMMITTER_EMAIL"))
345 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
346 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
347 getenv("GIT_COMMITTER_EMAIL"),
348 getenv("GIT_COMMITTER_DATE"),
352 int user_ident_sufficiently_given(void)
355 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
357 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
361 int git_ident_config(const char *var
, const char *value
, void *data
)
363 if (!strcmp(var
, "user.name")) {
365 return config_error_nonbool(var
);
366 strbuf_reset(&git_default_name
);
367 strbuf_addstr(&git_default_name
, value
);
368 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
372 if (!strcmp(var
, "user.email")) {
374 return config_error_nonbool(var
);
375 strbuf_reset(&git_default_email
);
376 strbuf_addstr(&git_default_email
, value
);
377 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;