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
)
68 const char *domainname
;
70 if (gethostname(buf
, sizeof(buf
))) {
71 warning("cannot get host name: %s", strerror(errno
));
72 strbuf_addstr(out
, "(none)");
75 strbuf_addstr(out
, buf
);
79 he
= gethostbyname(buf
);
80 strbuf_addch(out
, '.');
81 if (he
&& (domainname
= strchr(he
->h_name
, '.')))
82 strbuf_addstr(out
, domainname
+ 1);
84 strbuf_addstr(out
, "(none)");
87 static void copy_email(const struct passwd
*pw
, struct strbuf
*email
)
90 * Make up a fake email address
91 * (name + '@' + hostname [+ '.' + domainname])
93 strbuf_addstr(email
, pw
->pw_name
);
94 strbuf_addch(email
, '@');
96 if (!add_mailname_host(email
))
97 return; /* read from "/etc/mailname" (Debian) */
98 add_domainname(email
);
101 const char *ident_default_name(void)
103 if (!git_default_name
.len
)
104 copy_gecos(xgetpwuid_self(), &git_default_name
);
105 return git_default_name
.buf
;
108 const char *ident_default_email(void)
110 if (!git_default_email
.len
) {
111 const char *email
= getenv("EMAIL");
113 if (email
&& email
[0]) {
114 strbuf_addstr(&git_default_email
, email
);
115 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
117 copy_email(xgetpwuid_self(), &git_default_email
);
119 return git_default_email
.buf
;
122 const char *ident_default_date(void)
124 if (!git_default_date
[0])
125 datestamp(git_default_date
, sizeof(git_default_date
));
126 return git_default_date
;
129 static int add_raw(char *buf
, size_t size
, int offset
, const char *str
)
131 size_t len
= strlen(str
);
132 if (offset
+ len
> size
)
134 memcpy(buf
+ offset
, str
, len
);
138 static int crud(unsigned char c
)
153 * Copy over a string to the destination, but avoid special
154 * characters ('\n', '<' and '>') and remove crud at the end
156 static int copy(char *buf
, size_t size
, int offset
, const char *src
)
161 /* Remove crud from the beginning.. */
162 while ((c
= *src
) != 0) {
168 /* Remove crud from the end.. */
178 * Copy the rest to the buffer, but avoid the special
179 * characters '\n' '<' and '>' that act as delimiters on
180 * an identification line
182 for (i
= 0; i
< len
; i
++) {
185 case '\n': case '<': case '>':
196 * Reverse of fmt_ident(); given an ident line, split the fields
197 * to allow the caller to parse it.
198 * Signal a success by returning 0, but date/tz fields of the result
199 * can still be NULL if the input line only has the name/email part
200 * (e.g. reading from a reflog entry).
202 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
208 memset(split
, 0, sizeof(*split
));
210 split
->name_begin
= line
;
211 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
213 split
->mail_begin
= cp
+ 1;
216 if (!split
->mail_begin
)
219 for (cp
= split
->mail_begin
- 2; line
< cp
; cp
--)
221 split
->name_end
= cp
+ 1;
224 if (!split
->name_end
)
227 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
229 split
->mail_end
= cp
;
232 if (!split
->mail_end
)
235 for (cp
= split
->mail_end
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
237 if (line
+ len
<= cp
)
239 split
->date_begin
= cp
;
240 span
= strspn(cp
, "0123456789");
243 split
->date_end
= split
->date_begin
+ span
;
244 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
246 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
248 split
->tz_begin
= cp
;
249 span
= strspn(cp
+ 1, "0123456789");
252 split
->tz_end
= split
->tz_begin
+ 1 + span
;
256 split
->date_begin
= NULL
;
257 split
->date_end
= NULL
;
258 split
->tz_begin
= NULL
;
259 split
->tz_end
= NULL
;
263 static const char *env_hint
=
265 "*** Please tell me who you are.\n"
269 " git config --global user.email \"you@example.com\"\n"
270 " git config --global user.name \"Your Name\"\n"
272 "to set your account\'s default identity.\n"
273 "Omit --global to set the identity only in this repository.\n"
276 const char *fmt_ident(const char *name
, const char *email
,
277 const char *date_str
, int flag
)
279 static char buffer
[1000];
282 int error_on_no_name
= (flag
& IDENT_ERROR_ON_NO_NAME
);
283 int name_addr_only
= (flag
& IDENT_NO_DATE
);
286 name
= ident_default_name();
288 email
= ident_default_email();
293 if (error_on_no_name
) {
294 if (name
== git_default_name
.buf
)
295 fputs(env_hint
, stderr
);
296 die("empty ident %s <%s> not allowed", name
, email
);
298 pw
= xgetpwuid_self();
302 strcpy(date
, ident_default_date());
303 if (!name_addr_only
&& date_str
&& date_str
[0]) {
304 if (parse_date(date_str
, date
, sizeof(date
)) < 0)
305 die("invalid date format: %s", date_str
);
308 i
= copy(buffer
, sizeof(buffer
), 0, name
);
309 i
= add_raw(buffer
, sizeof(buffer
), i
, " <");
310 i
= copy(buffer
, sizeof(buffer
), i
, email
);
311 if (!name_addr_only
) {
312 i
= add_raw(buffer
, sizeof(buffer
), i
, "> ");
313 i
= copy(buffer
, sizeof(buffer
), i
, date
);
315 i
= add_raw(buffer
, sizeof(buffer
), i
, ">");
317 if (i
>= sizeof(buffer
))
318 die("Impossibly long personal identifier");
323 const char *fmt_name(const char *name
, const char *email
)
325 return fmt_ident(name
, email
, NULL
, IDENT_ERROR_ON_NO_NAME
| IDENT_NO_DATE
);
328 const char *git_author_info(int flag
)
330 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
331 getenv("GIT_AUTHOR_EMAIL"),
332 getenv("GIT_AUTHOR_DATE"),
336 const char *git_committer_info(int flag
)
338 if (getenv("GIT_COMMITTER_NAME"))
339 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
340 if (getenv("GIT_COMMITTER_EMAIL"))
341 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
342 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
343 getenv("GIT_COMMITTER_EMAIL"),
344 getenv("GIT_COMMITTER_DATE"),
348 int user_ident_sufficiently_given(void)
351 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
353 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
357 int git_ident_config(const char *var
, const char *value
, void *data
)
359 if (!strcmp(var
, "user.name")) {
361 return config_error_nonbool(var
);
362 strbuf_reset(&git_default_name
);
363 strbuf_addstr(&git_default_name
, value
);
364 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
368 if (!strcmp(var
, "user.email")) {
370 return config_error_nonbool(var
);
371 strbuf_reset(&git_default_email
);
372 strbuf_addstr(&git_default_email
, value
);
373 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;