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