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