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];
13 int user_ident_explicitly_given
;
15 #ifdef NO_GECOS_IN_PWENT
16 #define get_gecos(ignored) "&"
18 #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos)
21 static void copy_gecos(const struct passwd
*w
, struct strbuf
*name
)
25 /* Traditionally GECOS field had office phone numbers etc, separated
26 * with commas. Also & stands for capitalized form of the login name.
29 for (src
= get_gecos(w
); *src
&& *src
!= ','; src
++) {
32 strbuf_addch(name
, ch
);
34 /* Sorry, Mr. McDonald... */
35 strbuf_addch(name
, toupper(*w
->pw_name
));
36 strbuf_addstr(name
, w
->pw_name
+ 1);
41 static int add_mailname_host(struct strbuf
*buf
)
45 mailname
= fopen("/etc/mailname", "r");
48 warning("cannot open /etc/mailname: %s",
52 if (strbuf_getline(buf
, mailname
, '\n') == EOF
) {
54 warning("cannot read /etc/mailname: %s",
64 static void add_domainname(struct strbuf
*out
)
69 if (gethostname(buf
, sizeof(buf
))) {
70 warning("cannot get host name: %s", strerror(errno
));
71 strbuf_addstr(out
, "(none)");
75 strbuf_addstr(out
, buf
);
76 else if ((he
= gethostbyname(buf
)) && strchr(he
->h_name
, '.'))
77 strbuf_addstr(out
, he
->h_name
);
79 strbuf_addf(out
, "%s.(none)", buf
);
82 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
)
85 * Make up a fake email address
86 * (name + '@' + hostname [+ '.' + domainname])
88 strbuf_addstr(email
, pw
->pw_name
);
89 strbuf_addch(email
, '@');
91 if (!add_mailname_host(email
))
92 return; /* read from "/etc/mailname" (Debian) */
93 add_domainname(email
);
96 const char *ident_default_name(void)
98 if (!git_default_name
.len
) {
99 copy_gecos(xgetpwuid_self(), &git_default_name
);
100 strbuf_trim(&git_default_name
);
102 return git_default_name
.buf
;
105 const char *ident_default_email(void)
107 if (!git_default_email
.len
) {
108 const char *email
= getenv("EMAIL");
110 if (email
&& email
[0]) {
111 strbuf_addstr(&git_default_email
, email
);
112 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
114 copy_email(xgetpwuid_self(), &git_default_email
);
115 strbuf_trim(&git_default_email
);
117 return git_default_email
.buf
;
120 const char *ident_default_date(void)
122 if (!git_default_date
[0])
123 datestamp(git_default_date
, sizeof(git_default_date
));
124 return git_default_date
;
127 static int crud(unsigned char c
)
142 * Copy over a string to the destination, but avoid special
143 * characters ('\n', '<' and '>') and remove crud at the end
145 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
150 /* Remove crud from the beginning.. */
151 while ((c
= *src
) != 0) {
157 /* Remove crud from the end.. */
167 * Copy the rest to the buffer, but avoid the special
168 * characters '\n' '<' and '>' that act as delimiters on
169 * an identification line. We can only remove crud, never add it,
170 * so 'len' is our maximum.
172 strbuf_grow(sb
, len
);
173 for (i
= 0; i
< len
; i
++) {
176 case '\n': case '<': case '>':
179 sb
->buf
[sb
->len
++] = c
;
181 sb
->buf
[sb
->len
] = '\0';
185 * Reverse of fmt_ident(); given an ident line, split the fields
186 * to allow the caller to parse it.
187 * Signal a success by returning 0, but date/tz fields of the result
188 * can still be NULL if the input line only has the name/email part
189 * (e.g. reading from a reflog entry).
191 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
197 memset(split
, 0, sizeof(*split
));
199 split
->name_begin
= line
;
200 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
202 split
->mail_begin
= cp
+ 1;
205 if (!split
->mail_begin
)
208 for (cp
= split
->mail_begin
- 2; line
<= cp
; cp
--)
210 split
->name_end
= cp
+ 1;
213 if (!split
->name_end
)
216 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
218 split
->mail_end
= cp
;
221 if (!split
->mail_end
)
224 for (cp
= split
->mail_end
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
226 if (line
+ len
<= cp
)
228 split
->date_begin
= cp
;
229 span
= strspn(cp
, "0123456789");
232 split
->date_end
= split
->date_begin
+ span
;
233 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
235 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
237 split
->tz_begin
= cp
;
238 span
= strspn(cp
+ 1, "0123456789");
241 split
->tz_end
= split
->tz_begin
+ 1 + span
;
245 split
->date_begin
= NULL
;
246 split
->date_end
= NULL
;
247 split
->tz_begin
= NULL
;
248 split
->tz_end
= NULL
;
252 static const char *env_hint
=
254 "*** Please tell me who you are.\n"
258 " git config --global user.email \"you@example.com\"\n"
259 " git config --global user.name \"Your Name\"\n"
261 "to set your account\'s default identity.\n"
262 "Omit --global to set the identity only in this repository.\n"
265 const char *fmt_ident(const char *name
, const char *email
,
266 const char *date_str
, int flag
)
268 static struct strbuf ident
= STRBUF_INIT
;
270 int strict
= (flag
& IDENT_STRICT
);
271 int want_date
= !(flag
& IDENT_NO_DATE
);
272 int want_name
= !(flag
& IDENT_NO_NAME
);
274 if (want_name
&& !name
)
275 name
= ident_default_name();
277 email
= ident_default_email();
279 if (want_name
&& !*name
) {
283 if (name
== git_default_name
.buf
)
284 fputs(env_hint
, stderr
);
285 die("empty ident name (for <%s>) not allowed", email
);
287 pw
= xgetpwuid_self();
291 if (strict
&& email
== git_default_email
.buf
&&
292 strstr(email
, "(none)")) {
293 fputs(env_hint
, stderr
);
294 die("unable to auto-detect email address (got '%s')", email
);
298 if (date_str
&& date_str
[0]) {
299 if (parse_date(date_str
, date
, sizeof(date
)) < 0)
300 die("invalid date format: %s", date_str
);
303 strcpy(date
, ident_default_date());
306 strbuf_reset(&ident
);
308 strbuf_addstr_without_crud(&ident
, name
);
309 strbuf_addstr(&ident
, " <");
311 strbuf_addstr_without_crud(&ident
, email
);
313 strbuf_addch(&ident
, '>');
315 strbuf_addch(&ident
, ' ');
316 strbuf_addstr_without_crud(&ident
, date
);
321 const char *fmt_name(const char *name
, const char *email
)
323 return fmt_ident(name
, email
, NULL
, IDENT_STRICT
| IDENT_NO_DATE
);
326 const char *git_author_info(int flag
)
328 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
329 getenv("GIT_AUTHOR_EMAIL"),
330 getenv("GIT_AUTHOR_DATE"),
334 const char *git_committer_info(int flag
)
336 if (getenv("GIT_COMMITTER_NAME"))
337 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
338 if (getenv("GIT_COMMITTER_EMAIL"))
339 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
340 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
341 getenv("GIT_COMMITTER_EMAIL"),
342 getenv("GIT_COMMITTER_DATE"),
346 int user_ident_sufficiently_given(void)
349 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
351 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
355 int git_ident_config(const char *var
, const char *value
, void *data
)
357 if (!strcmp(var
, "user.name")) {
359 return config_error_nonbool(var
);
360 strbuf_reset(&git_default_name
);
361 strbuf_addstr(&git_default_name
, value
);
362 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
366 if (!strcmp(var
, "user.email")) {
368 return config_error_nonbool(var
);
369 strbuf_reset(&git_default_email
);
370 strbuf_addstr(&git_default_email
, value
);
371 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;