| 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 "git-compat-util.h" |
| 9 | #include "ident.h" |
| 10 | #include "config.h" |
| 11 | #include "date.h" |
| 12 | #include "gettext.h" |
| 13 | #include "mailmap.h" |
| 14 | #include "strbuf.h" |
| 15 | |
| 16 | static struct strbuf git_default_name = STRBUF_INIT; |
| 17 | static struct strbuf git_default_email = STRBUF_INIT; |
| 18 | static struct strbuf git_default_date = STRBUF_INIT; |
| 19 | static struct strbuf git_author_name = STRBUF_INIT; |
| 20 | static struct strbuf git_author_email = STRBUF_INIT; |
| 21 | static struct strbuf git_committer_name = STRBUF_INIT; |
| 22 | static struct strbuf git_committer_email = STRBUF_INIT; |
| 23 | static int default_email_is_bogus; |
| 24 | static int default_name_is_bogus; |
| 25 | |
| 26 | static int ident_use_config_only; |
| 27 | |
| 28 | #define IDENT_NAME_GIVEN 01 |
| 29 | #define IDENT_MAIL_GIVEN 02 |
| 30 | #define IDENT_ALL_GIVEN (IDENT_NAME_GIVEN|IDENT_MAIL_GIVEN) |
| 31 | static int committer_ident_explicitly_given; |
| 32 | static int author_ident_explicitly_given; |
| 33 | static int ident_config_given; |
| 34 | |
| 35 | #ifdef NO_GECOS_IN_PWENT |
| 36 | #define get_gecos(ignored) "&" |
| 37 | #else |
| 38 | #define get_gecos(struct_passwd) ((struct_passwd)->pw_gecos) |
| 39 | #endif |
| 40 | |
| 41 | static struct passwd *xgetpwuid_self(int *is_bogus) |
| 42 | { |
| 43 | struct passwd *pw; |
| 44 | |
| 45 | errno = 0; |
| 46 | pw = getpwuid(getuid()); |
| 47 | if (!pw) { |
| 48 | static struct passwd fallback; |
| 49 | fallback.pw_name = (char *) "unknown"; |
| 50 | #ifndef NO_GECOS_IN_PWENT |
| 51 | fallback.pw_gecos = (char *) "Unknown"; |
| 52 | #endif |
| 53 | pw = &fallback; |
| 54 | if (is_bogus) |
| 55 | *is_bogus = 1; |
| 56 | } |
| 57 | return pw; |
| 58 | } |
| 59 | |
| 60 | static void copy_gecos(const struct passwd *w, struct strbuf *name) |
| 61 | { |
| 62 | const char *src; |
| 63 | |
| 64 | /* Traditionally GECOS field had office phone numbers etc, separated |
| 65 | * with commas. Also & stands for capitalized form of the login name. |
| 66 | */ |
| 67 | |
| 68 | for (src = get_gecos(w); *src && *src != ','; src++) { |
| 69 | int ch = *src; |
| 70 | if (ch != '&') |
| 71 | strbuf_addch(name, ch); |
| 72 | else { |
| 73 | /* Sorry, Mr. McDonald... */ |
| 74 | strbuf_addch(name, toupper(*w->pw_name)); |
| 75 | strbuf_addstr(name, w->pw_name + 1); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static int add_mailname_host(struct strbuf *buf) |
| 81 | { |
| 82 | FILE *mailname; |
| 83 | struct strbuf mailnamebuf = STRBUF_INIT; |
| 84 | |
| 85 | mailname = fopen_or_warn("/etc/mailname", "r"); |
| 86 | if (!mailname) |
| 87 | return -1; |
| 88 | |
| 89 | if (strbuf_getline(&mailnamebuf, mailname) == EOF) { |
| 90 | if (ferror(mailname)) |
| 91 | warning_errno("cannot read /etc/mailname"); |
| 92 | strbuf_release(&mailnamebuf); |
| 93 | fclose(mailname); |
| 94 | return -1; |
| 95 | } |
| 96 | /* success! */ |
| 97 | strbuf_addbuf(buf, &mailnamebuf); |
| 98 | strbuf_release(&mailnamebuf); |
| 99 | fclose(mailname); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int canonical_name(const char *host, struct strbuf *out) |
| 104 | { |
| 105 | int status = -1; |
| 106 | |
| 107 | #ifndef NO_IPV6 |
| 108 | struct addrinfo hints, *ai; |
| 109 | memset (&hints, '\0', sizeof (hints)); |
| 110 | hints.ai_flags = AI_CANONNAME; |
| 111 | if (!getaddrinfo(host, NULL, &hints, &ai)) { |
| 112 | if (ai && ai->ai_canonname && strchr(ai->ai_canonname, '.')) { |
| 113 | strbuf_addstr(out, ai->ai_canonname); |
| 114 | status = 0; |
| 115 | } |
| 116 | freeaddrinfo(ai); |
| 117 | } |
| 118 | #else |
| 119 | struct hostent *he = gethostbyname(host); |
| 120 | if (he && strchr(he->h_name, '.')) { |
| 121 | strbuf_addstr(out, he->h_name); |
| 122 | status = 0; |
| 123 | } |
| 124 | #endif /* NO_IPV6 */ |
| 125 | |
| 126 | return status; |
| 127 | } |
| 128 | |
| 129 | static void add_domainname(struct strbuf *out, int *is_bogus) |
| 130 | { |
| 131 | char buf[HOST_NAME_MAX + 1]; |
| 132 | |
| 133 | if (xgethostname(buf, sizeof(buf))) { |
| 134 | warning_errno("cannot get host name"); |
| 135 | strbuf_addstr(out, "(none)"); |
| 136 | *is_bogus = 1; |
| 137 | return; |
| 138 | } |
| 139 | if (strchr(buf, '.')) |
| 140 | strbuf_addstr(out, buf); |
| 141 | else if (canonical_name(buf, out) < 0) { |
| 142 | strbuf_addf(out, "%s.(none)", buf); |
| 143 | *is_bogus = 1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void copy_email(const struct passwd *pw, struct strbuf *email, |
| 148 | int *is_bogus) |
| 149 | { |
| 150 | /* |
| 151 | * Make up a fake email address |
| 152 | * (name + '@' + hostname [+ '.' + domainname]) |
| 153 | */ |
| 154 | strbuf_addstr(email, pw->pw_name); |
| 155 | strbuf_addch(email, '@'); |
| 156 | |
| 157 | if (!add_mailname_host(email)) |
| 158 | return; /* read from "/etc/mailname" (Debian) */ |
| 159 | add_domainname(email, is_bogus); |
| 160 | } |
| 161 | |
| 162 | const char *ident_default_name(void) |
| 163 | { |
| 164 | if (!(ident_config_given & IDENT_NAME_GIVEN) && !git_default_name.len) { |
| 165 | copy_gecos(xgetpwuid_self(&default_name_is_bogus), &git_default_name); |
| 166 | strbuf_trim(&git_default_name); |
| 167 | } |
| 168 | return git_default_name.buf; |
| 169 | } |
| 170 | |
| 171 | const char *ident_default_email(void) |
| 172 | { |
| 173 | if (!(ident_config_given & IDENT_MAIL_GIVEN) && !git_default_email.len) { |
| 174 | const char *email = getenv("EMAIL"); |
| 175 | |
| 176 | if (email && email[0]) { |
| 177 | strbuf_addstr(&git_default_email, email); |
| 178 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 179 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 180 | } else if ((email = query_user_email()) && email[0]) { |
| 181 | strbuf_addstr(&git_default_email, email); |
| 182 | free((char *)email); |
| 183 | } else |
| 184 | copy_email(xgetpwuid_self(&default_email_is_bogus), |
| 185 | &git_default_email, &default_email_is_bogus); |
| 186 | strbuf_trim(&git_default_email); |
| 187 | } |
| 188 | return git_default_email.buf; |
| 189 | } |
| 190 | |
| 191 | static const char *ident_default_date(void) |
| 192 | { |
| 193 | if (!git_default_date.len) |
| 194 | datestamp(&git_default_date); |
| 195 | return git_default_date.buf; |
| 196 | } |
| 197 | |
| 198 | void reset_ident_date(void) |
| 199 | { |
| 200 | strbuf_reset(&git_default_date); |
| 201 | } |
| 202 | |
| 203 | static int crud(unsigned char c) |
| 204 | { |
| 205 | return c <= 32 || |
| 206 | c == ',' || |
| 207 | c == ':' || |
| 208 | c == ';' || |
| 209 | c == '<' || |
| 210 | c == '>' || |
| 211 | c == '"' || |
| 212 | c == '\\' || |
| 213 | c == '\''; |
| 214 | } |
| 215 | |
| 216 | static int has_non_crud(const char *str) |
| 217 | { |
| 218 | for (; *str; str++) { |
| 219 | if (!crud(*str)) |
| 220 | return 1; |
| 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Copy over a string to the destination, but avoid special |
| 227 | * characters ('\n', '<' and '>') and remove crud at the end |
| 228 | */ |
| 229 | static void strbuf_addstr_without_crud(struct strbuf *sb, const char *src) |
| 230 | { |
| 231 | size_t i, len; |
| 232 | unsigned char c; |
| 233 | |
| 234 | /* Remove crud from the beginning.. */ |
| 235 | while ((c = *src) != 0) { |
| 236 | if (!crud(c)) |
| 237 | break; |
| 238 | src++; |
| 239 | } |
| 240 | |
| 241 | /* Remove crud from the end.. */ |
| 242 | len = strlen(src); |
| 243 | while (len > 0) { |
| 244 | c = src[len-1]; |
| 245 | if (!crud(c)) |
| 246 | break; |
| 247 | --len; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Copy the rest to the buffer, but avoid the special |
| 252 | * characters '\n' '<' and '>' that act as delimiters on |
| 253 | * an identification line. We can only remove crud, never add it, |
| 254 | * so 'len' is our maximum. |
| 255 | */ |
| 256 | strbuf_grow(sb, len); |
| 257 | for (i = 0; i < len; i++) { |
| 258 | c = *src++; |
| 259 | switch (c) { |
| 260 | case '\n': case '<': case '>': |
| 261 | continue; |
| 262 | } |
| 263 | sb->buf[sb->len++] = c; |
| 264 | } |
| 265 | sb->buf[sb->len] = '\0'; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Reverse of fmt_ident(); given an ident line, split the fields |
| 270 | * to allow the caller to parse it. |
| 271 | * Signal a success by returning 0, but date/tz fields of the result |
| 272 | * can still be NULL if the input line only has the name/email part |
| 273 | * (e.g. reading from a reflog entry). |
| 274 | */ |
| 275 | int split_ident_line(struct ident_split *split, const char *line, size_t len) |
| 276 | { |
| 277 | const char *cp; |
| 278 | size_t span; |
| 279 | int status = -1; |
| 280 | |
| 281 | memset(split, 0, sizeof(*split)); |
| 282 | |
| 283 | split->name_begin = line; |
| 284 | for (cp = line; *cp && cp < line + len; cp++) |
| 285 | if (*cp == '<') { |
| 286 | split->mail_begin = cp + 1; |
| 287 | break; |
| 288 | } |
| 289 | if (!split->mail_begin) |
| 290 | return status; |
| 291 | |
| 292 | for (cp = split->mail_begin - 2; line <= cp; cp--) |
| 293 | if (!isspace(*cp)) { |
| 294 | split->name_end = cp + 1; |
| 295 | break; |
| 296 | } |
| 297 | if (!split->name_end) { |
| 298 | /* no human readable name */ |
| 299 | split->name_end = split->name_begin; |
| 300 | } |
| 301 | |
| 302 | for (cp = split->mail_begin; cp < line + len; cp++) |
| 303 | if (*cp == '>') { |
| 304 | split->mail_end = cp; |
| 305 | break; |
| 306 | } |
| 307 | if (!split->mail_end) |
| 308 | return status; |
| 309 | |
| 310 | /* |
| 311 | * Look from the end-of-line to find the trailing ">" of the mail |
| 312 | * address, even though we should already know it as split->mail_end. |
| 313 | * This can help in cases of broken idents with an extra ">" somewhere |
| 314 | * in the email address. Note that we are assuming the timestamp will |
| 315 | * never have a ">" in it. |
| 316 | * |
| 317 | * Note that we will always find some ">" before going off the front of |
| 318 | * the string, because will always hit the split->mail_end closing |
| 319 | * bracket. |
| 320 | */ |
| 321 | for (cp = line + len - 1; *cp != '>'; cp--) |
| 322 | ; |
| 323 | |
| 324 | for (cp = cp + 1; cp < line + len && isspace(*cp); cp++) |
| 325 | ; |
| 326 | if (line + len <= cp) |
| 327 | goto person_only; |
| 328 | split->date_begin = cp; |
| 329 | span = strspn(cp, "0123456789"); |
| 330 | if (!span) |
| 331 | goto person_only; |
| 332 | split->date_end = split->date_begin + span; |
| 333 | for (cp = split->date_end; cp < line + len && isspace(*cp); cp++) |
| 334 | ; |
| 335 | if (line + len <= cp || (*cp != '+' && *cp != '-')) |
| 336 | goto person_only; |
| 337 | split->tz_begin = cp; |
| 338 | span = strspn(cp + 1, "0123456789"); |
| 339 | if (!span) |
| 340 | goto person_only; |
| 341 | split->tz_end = split->tz_begin + 1 + span; |
| 342 | return 0; |
| 343 | |
| 344 | person_only: |
| 345 | split->date_begin = NULL; |
| 346 | split->date_end = NULL; |
| 347 | split->tz_begin = NULL; |
| 348 | split->tz_end = NULL; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * Returns the difference between the new and old length of the ident line. |
| 354 | */ |
| 355 | static ssize_t rewrite_ident_line(const char *person, size_t len, |
| 356 | struct strbuf *buf, |
| 357 | struct string_list *mailmap) |
| 358 | { |
| 359 | size_t namelen, maillen; |
| 360 | const char *name; |
| 361 | const char *mail; |
| 362 | struct ident_split ident; |
| 363 | |
| 364 | if (split_ident_line(&ident, person, len)) |
| 365 | return 0; |
| 366 | |
| 367 | mail = ident.mail_begin; |
| 368 | maillen = ident.mail_end - ident.mail_begin; |
| 369 | name = ident.name_begin; |
| 370 | namelen = ident.name_end - ident.name_begin; |
| 371 | |
| 372 | if (map_user(mailmap, &mail, &maillen, &name, &namelen)) { |
| 373 | struct strbuf namemail = STRBUF_INIT; |
| 374 | size_t newlen; |
| 375 | |
| 376 | strbuf_addf(&namemail, "%.*s <%.*s>", |
| 377 | (int)namelen, name, (int)maillen, mail); |
| 378 | |
| 379 | strbuf_splice(buf, ident.name_begin - buf->buf, |
| 380 | ident.mail_end - ident.name_begin + 1, |
| 381 | namemail.buf, namemail.len); |
| 382 | newlen = namemail.len; |
| 383 | |
| 384 | strbuf_release(&namemail); |
| 385 | |
| 386 | return newlen - (ident.mail_end - ident.name_begin); |
| 387 | } |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | void apply_mailmap_to_header(struct strbuf *buf, const char **header, |
| 393 | struct string_list *mailmap) |
| 394 | { |
| 395 | size_t buf_offset = 0; |
| 396 | |
| 397 | if (!mailmap) |
| 398 | return; |
| 399 | |
| 400 | for (;;) { |
| 401 | const char *person, *line; |
| 402 | size_t i; |
| 403 | int found_header = 0; |
| 404 | |
| 405 | line = buf->buf + buf_offset; |
| 406 | if (!*line || *line == '\n') |
| 407 | return; /* End of headers */ |
| 408 | |
| 409 | for (i = 0; header[i]; i++) |
| 410 | if (skip_prefix(line, header[i], &person)) { |
| 411 | const char *endp = strchrnul(person, '\n'); |
| 412 | found_header = 1; |
| 413 | buf_offset += endp - line; |
| 414 | buf_offset += rewrite_ident_line(person, endp - person, buf, mailmap); |
| 415 | /* Recompute endp after potential buffer reallocation */ |
| 416 | endp = buf->buf + buf_offset; |
| 417 | if (*endp == '\n') |
| 418 | buf_offset++; |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if (!found_header) { |
| 423 | buf_offset = strchrnul(line, '\n') - buf->buf; |
| 424 | if (buf->buf[buf_offset] == '\n') |
| 425 | buf_offset++; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | static void ident_env_hint(enum want_ident whose_ident) |
| 431 | { |
| 432 | switch (whose_ident) { |
| 433 | case WANT_AUTHOR_IDENT: |
| 434 | fputs(_("Author identity unknown\n"), stderr); |
| 435 | break; |
| 436 | case WANT_COMMITTER_IDENT: |
| 437 | fputs(_("Committer identity unknown\n"), stderr); |
| 438 | break; |
| 439 | default: |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | fputs(_("\n" |
| 444 | "*** Please tell me who you are.\n" |
| 445 | "\n" |
| 446 | "Run\n" |
| 447 | "\n" |
| 448 | " git config --global user.email \"you@example.com\"\n" |
| 449 | " git config --global user.name \"Your Name\"\n" |
| 450 | "\n" |
| 451 | "to set your account\'s default identity.\n" |
| 452 | "Omit --global to set the identity only in this repository.\n" |
| 453 | "\n"), stderr); |
| 454 | } |
| 455 | |
| 456 | const char *fmt_ident(const char *name, const char *email, |
| 457 | enum want_ident whose_ident, const char *date_str, int flag) |
| 458 | { |
| 459 | static int index; |
| 460 | static struct strbuf ident_pool[2] = { STRBUF_INIT, STRBUF_INIT }; |
| 461 | int strict = (flag & IDENT_STRICT); |
| 462 | int want_date = !(flag & IDENT_NO_DATE); |
| 463 | int want_name = !(flag & IDENT_NO_NAME); |
| 464 | |
| 465 | struct strbuf *ident = &ident_pool[index]; |
| 466 | index = (index + 1) % ARRAY_SIZE(ident_pool); |
| 467 | |
| 468 | if (!email) { |
| 469 | if (whose_ident == WANT_AUTHOR_IDENT && git_author_email.len) |
| 470 | email = git_author_email.buf; |
| 471 | else if (whose_ident == WANT_COMMITTER_IDENT && git_committer_email.len) |
| 472 | email = git_committer_email.buf; |
| 473 | } |
| 474 | if (!email) { |
| 475 | if (strict && ident_use_config_only |
| 476 | && !(ident_config_given & IDENT_MAIL_GIVEN)) { |
| 477 | ident_env_hint(whose_ident); |
| 478 | die(_("no email was given and auto-detection is disabled")); |
| 479 | } |
| 480 | email = ident_default_email(); |
| 481 | if (strict && default_email_is_bogus) { |
| 482 | ident_env_hint(whose_ident); |
| 483 | die(_("unable to auto-detect email address (got '%s')"), email); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (want_name) { |
| 488 | int using_default = 0; |
| 489 | if (!name) { |
| 490 | if (whose_ident == WANT_AUTHOR_IDENT && git_author_name.len) |
| 491 | name = git_author_name.buf; |
| 492 | else if (whose_ident == WANT_COMMITTER_IDENT && |
| 493 | git_committer_name.len) |
| 494 | name = git_committer_name.buf; |
| 495 | } |
| 496 | if (!name) { |
| 497 | if (strict && ident_use_config_only |
| 498 | && !(ident_config_given & IDENT_NAME_GIVEN)) { |
| 499 | ident_env_hint(whose_ident); |
| 500 | die(_("no name was given and auto-detection is disabled")); |
| 501 | } |
| 502 | name = ident_default_name(); |
| 503 | using_default = 1; |
| 504 | if (strict && default_name_is_bogus) { |
| 505 | ident_env_hint(whose_ident); |
| 506 | die(_("unable to auto-detect name (got '%s')"), name); |
| 507 | } |
| 508 | } |
| 509 | if (!*name) { |
| 510 | struct passwd *pw; |
| 511 | if (strict) { |
| 512 | if (using_default) |
| 513 | ident_env_hint(whose_ident); |
| 514 | die(_("empty ident name (for <%s>) not allowed"), email); |
| 515 | } |
| 516 | pw = xgetpwuid_self(NULL); |
| 517 | name = pw->pw_name; |
| 518 | } |
| 519 | if (strict && !has_non_crud(name)) |
| 520 | die(_("name consists only of disallowed characters: %s"), name); |
| 521 | } |
| 522 | |
| 523 | strbuf_reset(ident); |
| 524 | if (want_name) { |
| 525 | strbuf_addstr_without_crud(ident, name); |
| 526 | strbuf_addstr(ident, " <"); |
| 527 | } |
| 528 | strbuf_addstr_without_crud(ident, email); |
| 529 | if (want_name) |
| 530 | strbuf_addch(ident, '>'); |
| 531 | if (want_date) { |
| 532 | strbuf_addch(ident, ' '); |
| 533 | if (date_str && date_str[0]) { |
| 534 | if (parse_date(date_str, ident) < 0) |
| 535 | die(_("invalid date format: %s"), date_str); |
| 536 | } |
| 537 | else |
| 538 | strbuf_addstr(ident, ident_default_date()); |
| 539 | } |
| 540 | |
| 541 | return ident->buf; |
| 542 | } |
| 543 | |
| 544 | const char *fmt_name(enum want_ident whose_ident) |
| 545 | { |
| 546 | char *name = NULL; |
| 547 | char *email = NULL; |
| 548 | |
| 549 | switch (whose_ident) { |
| 550 | case WANT_BLANK_IDENT: |
| 551 | break; |
| 552 | case WANT_AUTHOR_IDENT: |
| 553 | name = getenv("GIT_AUTHOR_NAME"); |
| 554 | email = getenv("GIT_AUTHOR_EMAIL"); |
| 555 | break; |
| 556 | case WANT_COMMITTER_IDENT: |
| 557 | name = getenv("GIT_COMMITTER_NAME"); |
| 558 | email = getenv("GIT_COMMITTER_EMAIL"); |
| 559 | break; |
| 560 | } |
| 561 | return fmt_ident(name, email, whose_ident, NULL, |
| 562 | IDENT_STRICT | IDENT_NO_DATE); |
| 563 | } |
| 564 | |
| 565 | const char *git_author_info(int flag) |
| 566 | { |
| 567 | if (getenv("GIT_AUTHOR_NAME")) |
| 568 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 569 | if (getenv("GIT_AUTHOR_EMAIL")) |
| 570 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 571 | return fmt_ident(getenv("GIT_AUTHOR_NAME"), |
| 572 | getenv("GIT_AUTHOR_EMAIL"), |
| 573 | WANT_AUTHOR_IDENT, |
| 574 | getenv("GIT_AUTHOR_DATE"), |
| 575 | flag); |
| 576 | } |
| 577 | |
| 578 | const char *git_committer_info(int flag) |
| 579 | { |
| 580 | if (getenv("GIT_COMMITTER_NAME")) |
| 581 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 582 | if (getenv("GIT_COMMITTER_EMAIL")) |
| 583 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 584 | return fmt_ident(getenv("GIT_COMMITTER_NAME"), |
| 585 | getenv("GIT_COMMITTER_EMAIL"), |
| 586 | WANT_COMMITTER_IDENT, |
| 587 | getenv("GIT_COMMITTER_DATE"), |
| 588 | flag); |
| 589 | } |
| 590 | |
| 591 | static int ident_is_sufficient(int user_ident_explicitly_given) |
| 592 | { |
| 593 | #ifndef WINDOWS |
| 594 | return (user_ident_explicitly_given & IDENT_MAIL_GIVEN); |
| 595 | #else |
| 596 | return (user_ident_explicitly_given == IDENT_ALL_GIVEN); |
| 597 | #endif |
| 598 | } |
| 599 | |
| 600 | int committer_ident_sufficiently_given(void) |
| 601 | { |
| 602 | return ident_is_sufficient(committer_ident_explicitly_given); |
| 603 | } |
| 604 | |
| 605 | int author_ident_sufficiently_given(void) |
| 606 | { |
| 607 | return ident_is_sufficient(author_ident_explicitly_given); |
| 608 | } |
| 609 | |
| 610 | static int set_ident(const char *var, const char *value) |
| 611 | { |
| 612 | if (!strcmp(var, "author.name")) { |
| 613 | if (!value) |
| 614 | return config_error_nonbool(var); |
| 615 | strbuf_reset(&git_author_name); |
| 616 | strbuf_addstr(&git_author_name, value); |
| 617 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 618 | ident_config_given |= IDENT_NAME_GIVEN; |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | if (!strcmp(var, "author.email")) { |
| 623 | if (!value) |
| 624 | return config_error_nonbool(var); |
| 625 | strbuf_reset(&git_author_email); |
| 626 | strbuf_addstr(&git_author_email, value); |
| 627 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 628 | ident_config_given |= IDENT_MAIL_GIVEN; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | if (!strcmp(var, "committer.name")) { |
| 633 | if (!value) |
| 634 | return config_error_nonbool(var); |
| 635 | strbuf_reset(&git_committer_name); |
| 636 | strbuf_addstr(&git_committer_name, value); |
| 637 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 638 | ident_config_given |= IDENT_NAME_GIVEN; |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | if (!strcmp(var, "committer.email")) { |
| 643 | if (!value) |
| 644 | return config_error_nonbool(var); |
| 645 | strbuf_reset(&git_committer_email); |
| 646 | strbuf_addstr(&git_committer_email, value); |
| 647 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 648 | ident_config_given |= IDENT_MAIL_GIVEN; |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | if (!strcmp(var, "user.name")) { |
| 653 | if (!value) |
| 654 | return config_error_nonbool(var); |
| 655 | strbuf_reset(&git_default_name); |
| 656 | strbuf_addstr(&git_default_name, value); |
| 657 | committer_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 658 | author_ident_explicitly_given |= IDENT_NAME_GIVEN; |
| 659 | ident_config_given |= IDENT_NAME_GIVEN; |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | if (!strcmp(var, "user.email")) { |
| 664 | if (!value) |
| 665 | return config_error_nonbool(var); |
| 666 | strbuf_reset(&git_default_email); |
| 667 | strbuf_addstr(&git_default_email, value); |
| 668 | committer_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 669 | author_ident_explicitly_given |= IDENT_MAIL_GIVEN; |
| 670 | ident_config_given |= IDENT_MAIL_GIVEN; |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | int git_ident_config(const char *var, const char *value, |
| 678 | const struct config_context *ctx UNUSED, |
| 679 | void *data UNUSED) |
| 680 | { |
| 681 | if (!strcmp(var, "user.useconfigonly")) { |
| 682 | ident_use_config_only = git_config_bool(var, value); |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | return set_ident(var, value); |
| 687 | } |
| 688 | |
| 689 | static void set_env_if(const char *key, const char *value, int *given, int bit) |
| 690 | { |
| 691 | if ((*given & bit) || getenv(key)) |
| 692 | return; /* nothing to do */ |
| 693 | setenv(key, value, 0); |
| 694 | *given |= bit; |
| 695 | } |
| 696 | |
| 697 | void prepare_fallback_ident(const char *name, const char *email) |
| 698 | { |
| 699 | set_env_if("GIT_AUTHOR_NAME", name, |
| 700 | &author_ident_explicitly_given, IDENT_NAME_GIVEN); |
| 701 | set_env_if("GIT_AUTHOR_EMAIL", email, |
| 702 | &author_ident_explicitly_given, IDENT_MAIL_GIVEN); |
| 703 | set_env_if("GIT_COMMITTER_NAME", name, |
| 704 | &committer_ident_explicitly_given, IDENT_NAME_GIVEN); |
| 705 | set_env_if("GIT_COMMITTER_EMAIL", email, |
| 706 | &committer_ident_explicitly_given, IDENT_MAIL_GIVEN); |
| 707 | } |
| 708 | |
| 709 | static int buf_cmp(const char *a_begin, const char *a_end, |
| 710 | const char *b_begin, const char *b_end) |
| 711 | { |
| 712 | int a_len = a_end - a_begin; |
| 713 | int b_len = b_end - b_begin; |
| 714 | int min = a_len < b_len ? a_len : b_len; |
| 715 | int cmp; |
| 716 | |
| 717 | cmp = memcmp(a_begin, b_begin, min); |
| 718 | if (cmp) |
| 719 | return cmp; |
| 720 | |
| 721 | return a_len - b_len; |
| 722 | } |
| 723 | |
| 724 | int ident_cmp(const struct ident_split *a, |
| 725 | const struct ident_split *b) |
| 726 | { |
| 727 | int cmp; |
| 728 | |
| 729 | cmp = buf_cmp(a->mail_begin, a->mail_end, |
| 730 | b->mail_begin, b->mail_end); |
| 731 | if (cmp) |
| 732 | return cmp; |
| 733 | |
| 734 | return buf_cmp(a->name_begin, a->name_end, |
| 735 | b->name_begin, b->name_end); |
| 736 | } |