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 return git_default_name
.buf
;
103 const char *ident_default_email(void)
105 if (!git_default_email
.len
) {
106 const char *email
= getenv("EMAIL");
108 if (email
&& email
[0]) {
109 strbuf_addstr(&git_default_email
, email
);
110 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
112 copy_email(xgetpwuid_self(), &git_default_email
);
114 return git_default_email
.buf
;
117 const char *ident_default_date(void)
119 if (!git_default_date
[0])
120 datestamp(git_default_date
, sizeof(git_default_date
));
121 return git_default_date
;
124 static int crud(unsigned char c
)
139 * Copy over a string to the destination, but avoid special
140 * characters ('\n', '<' and '>') and remove crud at the end
142 static void strbuf_addstr_without_crud(struct strbuf
*sb
, const char *src
)
147 /* Remove crud from the beginning.. */
148 while ((c
= *src
) != 0) {
154 /* Remove crud from the end.. */
164 * Copy the rest to the buffer, but avoid the special
165 * characters '\n' '<' and '>' that act as delimiters on
166 * an identification line. We can only remove crud, never add it,
167 * so 'len' is our maximum.
169 strbuf_grow(sb
, len
);
170 for (i
= 0; i
< len
; i
++) {
173 case '\n': case '<': case '>':
176 sb
->buf
[sb
->len
++] = c
;
178 sb
->buf
[sb
->len
] = '\0';
182 * Reverse of fmt_ident(); given an ident line, split the fields
183 * to allow the caller to parse it.
184 * Signal a success by returning 0, but date/tz fields of the result
185 * can still be NULL if the input line only has the name/email part
186 * (e.g. reading from a reflog entry).
188 int split_ident_line(struct ident_split
*split
, const char *line
, int len
)
194 memset(split
, 0, sizeof(*split
));
196 split
->name_begin
= line
;
197 for (cp
= line
; *cp
&& cp
< line
+ len
; cp
++)
199 split
->mail_begin
= cp
+ 1;
202 if (!split
->mail_begin
)
205 for (cp
= split
->mail_begin
- 2; line
< cp
; cp
--)
207 split
->name_end
= cp
+ 1;
210 if (!split
->name_end
)
213 for (cp
= split
->mail_begin
; cp
< line
+ len
; cp
++)
215 split
->mail_end
= cp
;
218 if (!split
->mail_end
)
221 for (cp
= split
->mail_end
+ 1; cp
< line
+ len
&& isspace(*cp
); cp
++)
223 if (line
+ len
<= cp
)
225 split
->date_begin
= cp
;
226 span
= strspn(cp
, "0123456789");
229 split
->date_end
= split
->date_begin
+ span
;
230 for (cp
= split
->date_end
; cp
< line
+ len
&& isspace(*cp
); cp
++)
232 if (line
+ len
<= cp
|| (*cp
!= '+' && *cp
!= '-'))
234 split
->tz_begin
= cp
;
235 span
= strspn(cp
+ 1, "0123456789");
238 split
->tz_end
= split
->tz_begin
+ 1 + span
;
242 split
->date_begin
= NULL
;
243 split
->date_end
= NULL
;
244 split
->tz_begin
= NULL
;
245 split
->tz_end
= NULL
;
249 static const char *env_hint
=
251 "*** Please tell me who you are.\n"
255 " git config --global user.email \"you@example.com\"\n"
256 " git config --global user.name \"Your Name\"\n"
258 "to set your account\'s default identity.\n"
259 "Omit --global to set the identity only in this repository.\n"
262 const char *fmt_ident(const char *name
, const char *email
,
263 const char *date_str
, int flag
)
265 static struct strbuf ident
= STRBUF_INIT
;
267 int error_on_no_name
= (flag
& IDENT_ERROR_ON_NO_NAME
);
268 int name_addr_only
= (flag
& IDENT_NO_DATE
);
271 name
= ident_default_name();
273 email
= ident_default_email();
278 if (error_on_no_name
) {
279 if (name
== git_default_name
.buf
)
280 fputs(env_hint
, stderr
);
281 die("empty ident %s <%s> not allowed", name
, email
);
283 pw
= xgetpwuid_self();
287 strcpy(date
, ident_default_date());
288 if (!name_addr_only
&& date_str
&& date_str
[0]) {
289 if (parse_date(date_str
, date
, sizeof(date
)) < 0)
290 die("invalid date format: %s", date_str
);
293 strbuf_reset(&ident
);
294 strbuf_addstr_without_crud(&ident
, name
);
295 strbuf_addstr(&ident
, " <");
296 strbuf_addstr_without_crud(&ident
, email
);
297 strbuf_addch(&ident
, '>');
298 if (!name_addr_only
) {
299 strbuf_addch(&ident
, ' ');
300 strbuf_addstr_without_crud(&ident
, date
);
305 const char *fmt_name(const char *name
, const char *email
)
307 return fmt_ident(name
, email
, NULL
, IDENT_ERROR_ON_NO_NAME
| IDENT_NO_DATE
);
310 const char *git_author_info(int flag
)
312 return fmt_ident(getenv("GIT_AUTHOR_NAME"),
313 getenv("GIT_AUTHOR_EMAIL"),
314 getenv("GIT_AUTHOR_DATE"),
318 const char *git_committer_info(int flag
)
320 if (getenv("GIT_COMMITTER_NAME"))
321 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
322 if (getenv("GIT_COMMITTER_EMAIL"))
323 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;
324 return fmt_ident(getenv("GIT_COMMITTER_NAME"),
325 getenv("GIT_COMMITTER_EMAIL"),
326 getenv("GIT_COMMITTER_DATE"),
330 int user_ident_sufficiently_given(void)
333 return (user_ident_explicitly_given
& IDENT_MAIL_GIVEN
);
335 return (user_ident_explicitly_given
== IDENT_ALL_GIVEN
);
339 int git_ident_config(const char *var
, const char *value
, void *data
)
341 if (!strcmp(var
, "user.name")) {
343 return config_error_nonbool(var
);
344 strbuf_reset(&git_default_name
);
345 strbuf_addstr(&git_default_name
, value
);
346 user_ident_explicitly_given
|= IDENT_NAME_GIVEN
;
350 if (!strcmp(var
, "user.email")) {
352 return config_error_nonbool(var
);
353 strbuf_reset(&git_default_email
);
354 strbuf_addstr(&git_default_email
, value
);
355 user_ident_explicitly_given
|= IDENT_MAIL_GIVEN
;