]>
Commit | Line | Data |
---|---|---|
6aa33f40 LT |
1 | /* |
2 | * ident.c | |
3 | * | |
4 | * create git identifier lines of the form "name <email> date" | |
5 | * | |
6 | * Copyright (C) 2005 Linus Torvalds | |
7 | */ | |
8 | #include "cache.h" | |
9 | ||
8587ead7 JK |
10 | static struct strbuf git_default_name = STRBUF_INIT; |
11 | static struct strbuf git_default_email = STRBUF_INIT; | |
c33ddc2e | 12 | static struct strbuf git_default_date = STRBUF_INIT; |
19ce497c | 13 | static int default_email_is_bogus; |
92bcbb9b | 14 | static int default_name_is_bogus; |
45280230 | 15 | |
4d5c2956 DA |
16 | static int ident_use_config_only; |
17 | ||
45280230 JK |
18 | #define IDENT_NAME_GIVEN 01 |
19 | #define IDENT_MAIL_GIVEN 02 | |
20 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) | |
d6991cee JK |
21 | static int committer_ident_explicitly_given; |
22 | static int author_ident_explicitly_given; | |
4d5c2956 | 23 | static int ident_config_given; |
6aa33f40 | 24 | |
590e081d RG |
25 | #ifdef NO_GECOS_IN_PWENT |
26 | #define get_gecos(ignored) "&" | |
27 | #else | |
28 | #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) | |
29 | #endif | |
30 | ||
92bcbb9b | 31 | static struct passwd *xgetpwuid_self(int *is_bogus) |
e850194c JK |
32 | { |
33 | struct passwd *pw; | |
34 | ||
35 | errno = 0; | |
36 | pw = getpwuid(getuid()); | |
92bcbb9b JK |
37 | if (!pw) { |
38 | static struct passwd fallback; | |
39 | fallback.pw_name = "unknown"; | |
40 | #ifndef NO_GECOS_IN_PWENT | |
41 | fallback.pw_gecos = "Unknown"; | |
42 | #endif | |
43 | pw = &fallback; | |
44 | if (is_bogus) | |
45 | *is_bogus = 1; | |
46 | } | |
e850194c JK |
47 | return pw; |
48 | } | |
49 | ||
8587ead7 | 50 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
e9bacb4f | 51 | { |
8587ead7 | 52 | char *src; |
e9bacb4f JH |
53 | |
54 | /* Traditionally GECOS field had office phone numbers etc, separated | |
55 | * with commas. Also & stands for capitalized form of the login name. | |
56 | */ | |
57 | ||
8587ead7 | 58 | for (src = get_gecos(w); *src && *src != ','; src++) { |
e9bacb4f | 59 | int ch = *src; |
8587ead7 JK |
60 | if (ch != '&') |
61 | strbuf_addch(name, ch); | |
62 | else { | |
e9bacb4f | 63 | /* Sorry, Mr. McDonald... */ |
8587ead7 JK |
64 | strbuf_addch(name, toupper(*w->pw_name)); |
65 | strbuf_addstr(name, w->pw_name + 1); | |
e9bacb4f JH |
66 | } |
67 | } | |
e9bacb4f JH |
68 | } |
69 | ||
8587ead7 | 70 | static int add_mailname_host(struct strbuf *buf) |
8a55caa8 JN |
71 | { |
72 | FILE *mailname; | |
dc342a25 | 73 | struct strbuf mailnamebuf = STRBUF_INIT; |
8a55caa8 JN |
74 | |
75 | mailname = fopen("/etc/mailname", "r"); | |
76 | if (!mailname) { | |
77 | if (errno != ENOENT) | |
a26f4ed6 | 78 | warning_errno("cannot open /etc/mailname"); |
8a55caa8 JN |
79 | return -1; |
80 | } | |
1f3b1efd | 81 | if (strbuf_getline(&mailnamebuf, mailname) == EOF) { |
8a55caa8 | 82 | if (ferror(mailname)) |
a26f4ed6 | 83 | warning_errno("cannot read /etc/mailname"); |
dc342a25 | 84 | strbuf_release(&mailnamebuf); |
8a55caa8 JN |
85 | fclose(mailname); |
86 | return -1; | |
87 | } | |
88 | /* success! */ | |
dc342a25 JN |
89 | strbuf_addbuf(buf, &mailnamebuf); |
90 | strbuf_release(&mailnamebuf); | |
8a55caa8 JN |
91 | fclose(mailname); |
92 | return 0; | |
93 | } | |
94 | ||
00bce77f EP |
95 | static int canonical_name(const char *host, struct strbuf *out) |
96 | { | |
97 | int status = -1; | |
98 | ||
99 | #ifndef NO_IPV6 | |
100 | struct addrinfo hints, *ai; | |
101 | memset (&hints, '\0', sizeof (hints)); | |
102 | hints.ai_flags = AI_CANONNAME; | |
103 | if (!getaddrinfo(host, NULL, &hints, &ai)) { | |
104 | if (ai && strchr(ai->ai_canonname, '.')) { | |
105 | strbuf_addstr(out, ai->ai_canonname); | |
106 | status = 0; | |
107 | } | |
108 | freeaddrinfo(ai); | |
109 | } | |
110 | #else | |
58d29ece | 111 | struct hostent *he = gethostbyname(host); |
00bce77f EP |
112 | if (he && strchr(he->h_name, '.')) { |
113 | strbuf_addstr(out, he->h_name); | |
114 | status = 0; | |
115 | } | |
116 | #endif /* NO_IPV6 */ | |
117 | ||
118 | return status; | |
119 | } | |
120 | ||
19ce497c | 121 | static void add_domainname(struct strbuf *out, int *is_bogus) |
8a55caa8 | 122 | { |
8587ead7 | 123 | char buf[1024]; |
8a55caa8 | 124 | |
8587ead7 | 125 | if (gethostname(buf, sizeof(buf))) { |
a26f4ed6 | 126 | warning_errno("cannot get host name"); |
8587ead7 | 127 | strbuf_addstr(out, "(none)"); |
19ce497c | 128 | *is_bogus = 1; |
8a55caa8 JN |
129 | return; |
130 | } | |
8587ead7 | 131 | if (strchr(buf, '.')) |
f8254d32 | 132 | strbuf_addstr(out, buf); |
5498c57c | 133 | else if (canonical_name(buf, out) < 0) { |
f8254d32 | 134 | strbuf_addf(out, "%s.(none)", buf); |
19ce497c JK |
135 | *is_bogus = 1; |
136 | } | |
8a55caa8 JN |
137 | } |
138 | ||
19ce497c JK |
139 | static void copy_email(const struct passwd *pw, struct strbuf *email, |
140 | int *is_bogus) | |
6aa33f40 | 141 | { |
01754769 JH |
142 | /* |
143 | * Make up a fake email address | |
144 | * (name + '@' + hostname [+ '.' + domainname]) | |
145 | */ | |
8587ead7 JK |
146 | strbuf_addstr(email, pw->pw_name); |
147 | strbuf_addch(email, '@'); | |
148 | ||
149 | if (!add_mailname_host(email)) | |
8a55caa8 | 150 | return; /* read from "/etc/mailname" (Debian) */ |
19ce497c | 151 | add_domainname(email, is_bogus); |
01754769 JH |
152 | } |
153 | ||
9830534e | 154 | const char *ident_default_name(void) |
01754769 | 155 | { |
be641abd | 156 | if (!git_default_name.len) { |
92bcbb9b | 157 | copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); |
be641abd | 158 | strbuf_trim(&git_default_name); |
01754769 | 159 | } |
8587ead7 | 160 | return git_default_name.buf; |
bcb2b004 | 161 | } |
01754769 | 162 | |
bcb2b004 JK |
163 | const char *ident_default_email(void) |
164 | { | |
8587ead7 | 165 | if (!git_default_email.len) { |
46f74f00 MK |
166 | const char *email = getenv("EMAIL"); |
167 | ||
99178c83 | 168 | if (email && email[0]) { |
8587ead7 | 169 | strbuf_addstr(&git_default_email, email); |
d6991cee JK |
170 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
171 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
2f705875 | 172 | } else |
92bcbb9b JK |
173 | copy_email(xgetpwuid_self(&default_email_is_bogus), |
174 | &git_default_email, &default_email_is_bogus); | |
be641abd | 175 | strbuf_trim(&git_default_email); |
01754769 | 176 | } |
8587ead7 | 177 | return git_default_email.buf; |
6aa33f40 LT |
178 | } |
179 | ||
dad148c3 | 180 | static const char *ident_default_date(void) |
6aa33f40 | 181 | { |
c33ddc2e JK |
182 | if (!git_default_date.len) |
183 | datestamp(&git_default_date); | |
184 | return git_default_date.buf; | |
6aa33f40 LT |
185 | } |
186 | ||
187 | static int crud(unsigned char c) | |
188 | { | |
f64c81d4 AR |
189 | return c <= 32 || |
190 | c == '.' || | |
191 | c == ',' || | |
192 | c == ':' || | |
193 | c == ';' || | |
194 | c == '<' || | |
195 | c == '>' || | |
196 | c == '"' || | |
d404bf02 | 197 | c == '\\' || |
f64c81d4 | 198 | c == '\''; |
6aa33f40 LT |
199 | } |
200 | ||
201 | /* | |
202 | * Copy over a string to the destination, but avoid special | |
203 | * characters ('\n', '<' and '>') and remove crud at the end | |
204 | */ | |
c96f0c8d | 205 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
6aa33f40 | 206 | { |
b0732115 | 207 | size_t i, len; |
6aa33f40 LT |
208 | unsigned char c; |
209 | ||
210 | /* Remove crud from the beginning.. */ | |
211 | while ((c = *src) != 0) { | |
212 | if (!crud(c)) | |
213 | break; | |
214 | src++; | |
215 | } | |
216 | ||
217 | /* Remove crud from the end.. */ | |
218 | len = strlen(src); | |
219 | while (len > 0) { | |
220 | c = src[len-1]; | |
221 | if (!crud(c)) | |
222 | break; | |
223 | --len; | |
224 | } | |
225 | ||
226 | /* | |
227 | * Copy the rest to the buffer, but avoid the special | |
82f9d58a | 228 | * characters '\n' '<' and '>' that act as delimiters on |
c96f0c8d JK |
229 | * an identification line. We can only remove crud, never add it, |
230 | * so 'len' is our maximum. | |
6aa33f40 | 231 | */ |
c96f0c8d | 232 | strbuf_grow(sb, len); |
6aa33f40 LT |
233 | for (i = 0; i < len; i++) { |
234 | c = *src++; | |
235 | switch (c) { | |
236 | case '\n': case '<': case '>': | |
237 | continue; | |
238 | } | |
c96f0c8d | 239 | sb->buf[sb->len++] = c; |
6aa33f40 | 240 | } |
c96f0c8d | 241 | sb->buf[sb->len] = '\0'; |
6aa33f40 LT |
242 | } |
243 | ||
4b340cfa JH |
244 | /* |
245 | * Reverse of fmt_ident(); given an ident line, split the fields | |
246 | * to allow the caller to parse it. | |
247 | * Signal a success by returning 0, but date/tz fields of the result | |
248 | * can still be NULL if the input line only has the name/email part | |
249 | * (e.g. reading from a reflog entry). | |
250 | */ | |
251 | int split_ident_line(struct ident_split *split, const char *line, int len) | |
252 | { | |
253 | const char *cp; | |
254 | size_t span; | |
255 | int status = -1; | |
256 | ||
257 | memset(split, 0, sizeof(*split)); | |
258 | ||
259 | split->name_begin = line; | |
260 | for (cp = line; *cp && cp < line + len; cp++) | |
261 | if (*cp == '<') { | |
262 | split->mail_begin = cp + 1; | |
263 | break; | |
264 | } | |
265 | if (!split->mail_begin) | |
266 | return status; | |
267 | ||
d9955fd6 | 268 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
4b340cfa JH |
269 | if (!isspace(*cp)) { |
270 | split->name_end = cp + 1; | |
271 | break; | |
272 | } | |
e27ddb64 JH |
273 | if (!split->name_end) { |
274 | /* no human readable name */ | |
275 | split->name_end = split->name_begin; | |
276 | } | |
4b340cfa JH |
277 | |
278 | for (cp = split->mail_begin; cp < line + len; cp++) | |
279 | if (*cp == '>') { | |
280 | split->mail_end = cp; | |
281 | break; | |
282 | } | |
283 | if (!split->mail_end) | |
284 | return status; | |
285 | ||
03818a4a JK |
286 | /* |
287 | * Look from the end-of-line to find the trailing ">" of the mail | |
288 | * address, even though we should already know it as split->mail_end. | |
289 | * This can help in cases of broken idents with an extra ">" somewhere | |
290 | * in the email address. Note that we are assuming the timestamp will | |
291 | * never have a ">" in it. | |
292 | * | |
293 | * Note that we will always find some ">" before going off the front of | |
294 | * the string, because will always hit the split->mail_end closing | |
295 | * bracket. | |
296 | */ | |
297 | for (cp = line + len - 1; *cp != '>'; cp--) | |
298 | ; | |
299 | ||
300 | for (cp = cp + 1; cp < line + len && isspace(*cp); cp++) | |
4b340cfa JH |
301 | ; |
302 | if (line + len <= cp) | |
303 | goto person_only; | |
304 | split->date_begin = cp; | |
305 | span = strspn(cp, "0123456789"); | |
306 | if (!span) | |
307 | goto person_only; | |
308 | split->date_end = split->date_begin + span; | |
309 | for (cp = split->date_end; cp < line + len && isspace(*cp); cp++) | |
310 | ; | |
311 | if (line + len <= cp || (*cp != '+' && *cp != '-')) | |
312 | goto person_only; | |
313 | split->tz_begin = cp; | |
314 | span = strspn(cp + 1, "0123456789"); | |
315 | if (!span) | |
316 | goto person_only; | |
317 | split->tz_end = split->tz_begin + 1 + span; | |
318 | return 0; | |
319 | ||
320 | person_only: | |
321 | split->date_begin = NULL; | |
322 | split->date_end = NULL; | |
323 | split->tz_begin = NULL; | |
324 | split->tz_end = NULL; | |
325 | return 0; | |
326 | } | |
327 | ||
749be728 | 328 | static const char *env_hint = |
d5cc2de9 | 329 | "\n" |
6c293d40 | 330 | "*** Please tell me who you are.\n" |
d5cc2de9 HWN |
331 | "\n" |
332 | "Run\n" | |
333 | "\n" | |
8e7425da | 334 | " git config --global user.email \"you@example.com\"\n" |
180787c4 | 335 | " git config --global user.name \"Your Name\"\n" |
d5cc2de9 | 336 | "\n" |
180787c4 SP |
337 | "to set your account\'s default identity.\n" |
338 | "Omit --global to set the identity only in this repository.\n" | |
d5cc2de9 | 339 | "\n"; |
749be728 | 340 | |
774751a8 JH |
341 | const char *fmt_ident(const char *name, const char *email, |
342 | const char *date_str, int flag) | |
6aa33f40 | 343 | { |
c96f0c8d | 344 | static struct strbuf ident = STRBUF_INIT; |
f9bc573f | 345 | int strict = (flag & IDENT_STRICT); |
359b27ad | 346 | int want_date = !(flag & IDENT_NO_DATE); |
c15e1987 | 347 | int want_name = !(flag & IDENT_NO_NAME); |
6aa33f40 | 348 | |
59f92959 JK |
349 | if (want_name) { |
350 | int using_default = 0; | |
351 | if (!name) { | |
734c7789 | 352 | if (strict && ident_use_config_only |
d3c06c19 MT |
353 | && !(ident_config_given & IDENT_NAME_GIVEN)) { |
354 | fputs(env_hint, stderr); | |
355 | die("no name was given and auto-detection is disabled"); | |
356 | } | |
59f92959 JK |
357 | name = ident_default_name(); |
358 | using_default = 1; | |
359 | if (strict && default_name_is_bogus) { | |
b9f0ac17 | 360 | fputs(env_hint, stderr); |
59f92959 JK |
361 | die("unable to auto-detect name (got '%s')", name); |
362 | } | |
363 | } | |
364 | if (!*name) { | |
365 | struct passwd *pw; | |
366 | if (strict) { | |
367 | if (using_default) | |
368 | fputs(env_hint, stderr); | |
369 | die("empty ident name (for <%s>) not allowed", email); | |
370 | } | |
371 | pw = xgetpwuid_self(NULL); | |
372 | name = pw->pw_name; | |
749be728 | 373 | } |
92bcbb9b JK |
374 | } |
375 | ||
59f92959 | 376 | if (!email) { |
734c7789 | 377 | if (strict && ident_use_config_only |
d3c06c19 MT |
378 | && !(ident_config_given & IDENT_MAIL_GIVEN)) { |
379 | fputs(env_hint, stderr); | |
380 | die("no email was given and auto-detection is disabled"); | |
381 | } | |
59f92959 JK |
382 | email = ident_default_email(); |
383 | if (strict && default_email_is_bogus) { | |
384 | fputs(env_hint, stderr); | |
385 | die("unable to auto-detect email address (got '%s')", email); | |
386 | } | |
749be728 | 387 | } |
dfdd309e | 388 | |
c96f0c8d | 389 | strbuf_reset(&ident); |
c15e1987 JK |
390 | if (want_name) { |
391 | strbuf_addstr_without_crud(&ident, name); | |
392 | strbuf_addstr(&ident, " <"); | |
d9ccfe77 | 393 | } |
c96f0c8d | 394 | strbuf_addstr_without_crud(&ident, email); |
c15e1987 JK |
395 | if (want_name) |
396 | strbuf_addch(&ident, '>'); | |
359b27ad | 397 | if (want_date) { |
c96f0c8d | 398 | strbuf_addch(&ident, ' '); |
c33ddc2e JK |
399 | if (date_str && date_str[0]) { |
400 | if (parse_date(date_str, &ident) < 0) | |
401 | die("invalid date format: %s", date_str); | |
402 | } | |
403 | else | |
404 | strbuf_addstr(&ident, ident_default_date()); | |
d9ccfe77 | 405 | } |
c33ddc2e | 406 | |
c96f0c8d | 407 | return ident.buf; |
6aa33f40 | 408 | } |
d289d136 | 409 | |
d9ccfe77 JH |
410 | const char *fmt_name(const char *name, const char *email) |
411 | { | |
f9bc573f | 412 | return fmt_ident(name, email, NULL, IDENT_STRICT | IDENT_NO_DATE); |
d9ccfe77 JH |
413 | } |
414 | ||
774751a8 | 415 | const char *git_author_info(int flag) |
d289d136 | 416 | { |
d6991cee JK |
417 | if (getenv("GIT_AUTHOR_NAME")) |
418 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; | |
419 | if (getenv("GIT_AUTHOR_EMAIL")) | |
420 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
798123af | 421 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
c7d77dab | 422 | getenv("GIT_AUTHOR_EMAIL"), |
749be728 | 423 | getenv("GIT_AUTHOR_DATE"), |
774751a8 | 424 | flag); |
d289d136 EB |
425 | } |
426 | ||
774751a8 | 427 | const char *git_committer_info(int flag) |
d289d136 | 428 | { |
91c38a21 | 429 | if (getenv("GIT_COMMITTER_NAME")) |
d6991cee | 430 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
91c38a21 | 431 | if (getenv("GIT_COMMITTER_EMAIL")) |
d6991cee | 432 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
798123af | 433 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
c7d77dab | 434 | getenv("GIT_COMMITTER_EMAIL"), |
749be728 | 435 | getenv("GIT_COMMITTER_DATE"), |
774751a8 | 436 | flag); |
d289d136 | 437 | } |
5aeb3a3a | 438 | |
d6991cee | 439 | static int ident_is_sufficient(int user_ident_explicitly_given) |
5aeb3a3a JH |
440 | { |
441 | #ifndef WINDOWS | |
442 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); | |
443 | #else | |
444 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); | |
445 | #endif | |
446 | } | |
9597921b | 447 | |
d6991cee JK |
448 | int committer_ident_sufficiently_given(void) |
449 | { | |
450 | return ident_is_sufficient(committer_ident_explicitly_given); | |
451 | } | |
452 | ||
453 | int author_ident_sufficiently_given(void) | |
454 | { | |
455 | return ident_is_sufficient(author_ident_explicitly_given); | |
456 | } | |
457 | ||
9597921b JK |
458 | int git_ident_config(const char *var, const char *value, void *data) |
459 | { | |
4d5c2956 DA |
460 | if (!strcmp(var, "user.useconfigonly")) { |
461 | ident_use_config_only = git_config_bool(var, value); | |
462 | return 0; | |
463 | } | |
464 | ||
9597921b JK |
465 | if (!strcmp(var, "user.name")) { |
466 | if (!value) | |
467 | return config_error_nonbool(var); | |
8587ead7 JK |
468 | strbuf_reset(&git_default_name); |
469 | strbuf_addstr(&git_default_name, value); | |
d6991cee JK |
470 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
471 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; | |
4d5c2956 | 472 | ident_config_given |= IDENT_NAME_GIVEN; |
9597921b JK |
473 | return 0; |
474 | } | |
475 | ||
476 | if (!strcmp(var, "user.email")) { | |
477 | if (!value) | |
478 | return config_error_nonbool(var); | |
8587ead7 JK |
479 | strbuf_reset(&git_default_email); |
480 | strbuf_addstr(&git_default_email, value); | |
d6991cee JK |
481 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
482 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; | |
4d5c2956 | 483 | ident_config_given |= IDENT_MAIL_GIVEN; |
9597921b JK |
484 | return 0; |
485 | } | |
486 | ||
487 | return 0; | |
488 | } | |
662cc30c JK |
489 | |
490 | static int buf_cmp(const char *a_begin, const char *a_end, | |
491 | const char *b_begin, const char *b_end) | |
492 | { | |
493 | int a_len = a_end - a_begin; | |
494 | int b_len = b_end - b_begin; | |
495 | int min = a_len < b_len ? a_len : b_len; | |
496 | int cmp; | |
497 | ||
498 | cmp = memcmp(a_begin, b_begin, min); | |
499 | if (cmp) | |
500 | return cmp; | |
501 | ||
502 | return a_len - b_len; | |
503 | } | |
504 | ||
505 | int ident_cmp(const struct ident_split *a, | |
506 | const struct ident_split *b) | |
507 | { | |
508 | int cmp; | |
509 | ||
510 | cmp = buf_cmp(a->mail_begin, a->mail_end, | |
511 | b->mail_begin, b->mail_end); | |
512 | if (cmp) | |
513 | return cmp; | |
514 | ||
515 | return buf_cmp(a->name_begin, a->name_end, | |
516 | b->name_begin, b->name_end); | |
517 | } |