Eric Sunshine | 226ad34 | 2013-07-12 20:53:10 -0400 | [diff] [blame] | 1 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Eric Sunshine | 226ad34 | 2013-07-12 20:53:10 -0400 | [diff] [blame] | 3 | #include "mailmap.h" |
| 4 | #include "parse-options.h" |
| 5 | #include "string-list.h" |
| 6 | |
| 7 | static int use_stdin; |
| 8 | static const char * const check_mailmap_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 9 | N_("git check-mailmap [<options>] <contact>..."), |
Eric Sunshine | 226ad34 | 2013-07-12 20:53:10 -0400 | [diff] [blame] | 10 | NULL |
| 11 | }; |
| 12 | |
| 13 | static const struct option check_mailmap_options[] = { |
| 14 | OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")), |
| 15 | OPT_END() |
| 16 | }; |
| 17 | |
| 18 | static void check_mailmap(struct string_list *mailmap, const char *contact) |
| 19 | { |
| 20 | const char *name, *mail; |
| 21 | size_t namelen, maillen; |
| 22 | struct ident_split ident; |
| 23 | |
| 24 | if (split_ident_line(&ident, contact, strlen(contact))) |
| 25 | die(_("unable to parse contact: %s"), contact); |
| 26 | |
| 27 | name = ident.name_begin; |
| 28 | namelen = ident.name_end - ident.name_begin; |
| 29 | mail = ident.mail_begin; |
| 30 | maillen = ident.mail_end - ident.mail_begin; |
| 31 | |
| 32 | map_user(mailmap, &mail, &maillen, &name, &namelen); |
| 33 | |
| 34 | if (namelen) |
| 35 | printf("%.*s ", (int)namelen, name); |
| 36 | printf("<%.*s>\n", (int)maillen, mail); |
| 37 | } |
| 38 | |
| 39 | int cmd_check_mailmap(int argc, const char **argv, const char *prefix) |
| 40 | { |
| 41 | int i; |
| 42 | struct string_list mailmap = STRING_LIST_INIT_NODUP; |
| 43 | |
| 44 | git_config(git_default_config, NULL); |
| 45 | argc = parse_options(argc, argv, prefix, check_mailmap_options, |
| 46 | check_mailmap_usage, 0); |
| 47 | if (argc == 0 && !use_stdin) |
| 48 | die(_("no contacts specified")); |
| 49 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 50 | read_mailmap(&mailmap); |
Eric Sunshine | 226ad34 | 2013-07-12 20:53:10 -0400 | [diff] [blame] | 51 | |
| 52 | for (i = 0; i < argc; ++i) |
| 53 | check_mailmap(&mailmap, argv[i]); |
| 54 | maybe_flush_or_die(stdout, "stdout"); |
| 55 | |
| 56 | if (use_stdin) { |
| 57 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 8f309ae | 2016-01-13 15:31:17 -0800 | [diff] [blame] | 58 | while (strbuf_getline_lf(&buf, stdin) != EOF) { |
Eric Sunshine | 226ad34 | 2013-07-12 20:53:10 -0400 | [diff] [blame] | 59 | check_mailmap(&mailmap, buf.buf); |
| 60 | maybe_flush_or_die(stdout, "stdout"); |
| 61 | } |
| 62 | strbuf_release(&buf); |
| 63 | } |
| 64 | |
| 65 | clear_mailmap(&mailmap); |
| 66 | return 0; |
| 67 | } |