| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | #include "builtin.h" |
| 3 | #include "config.h" |
| 4 | #include "environment.h" |
| 5 | #include "gettext.h" |
| 6 | #include "ident.h" |
| 7 | #include "mailmap.h" |
| 8 | #include "parse-options.h" |
| 9 | #include "strbuf.h" |
| 10 | #include "string-list.h" |
| 11 | #include "write-or-die.h" |
| 12 | |
| 13 | static int use_stdin; |
| 14 | static const char *mailmap_file, *mailmap_blob; |
| 15 | static const char * const check_mailmap_usage[] = { |
| 16 | N_("git check-mailmap [<options>] <contact>..."), |
| 17 | NULL |
| 18 | }; |
| 19 | |
| 20 | static const struct option check_mailmap_options[] = { |
| 21 | OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")), |
| 22 | OPT_FILENAME(0, "mailmap-file", &mailmap_file, N_("read additional mailmap entries from file")), |
| 23 | OPT_STRING(0, "mailmap-blob", &mailmap_blob, N_("blob"), N_("read additional mailmap entries from blob")), |
| 24 | OPT_END() |
| 25 | }; |
| 26 | |
| 27 | static void check_mailmap(struct string_list *mailmap, const char *contact) |
| 28 | { |
| 29 | const char *name, *mail; |
| 30 | size_t namelen, maillen; |
| 31 | struct ident_split ident; |
| 32 | |
| 33 | if (!split_ident_line(&ident, contact, strlen(contact))) { |
| 34 | name = ident.name_begin; |
| 35 | namelen = ident.name_end - ident.name_begin; |
| 36 | mail = ident.mail_begin; |
| 37 | maillen = ident.mail_end - ident.mail_begin; |
| 38 | } else { |
| 39 | name = ""; |
| 40 | namelen = 0; |
| 41 | mail = contact; |
| 42 | maillen = strlen(contact); |
| 43 | } |
| 44 | |
| 45 | map_user(mailmap, &mail, &maillen, &name, &namelen); |
| 46 | |
| 47 | if (namelen) |
| 48 | printf("%.*s ", (int)namelen, name); |
| 49 | printf("<%.*s>\n", (int)maillen, mail); |
| 50 | } |
| 51 | |
| 52 | int cmd_check_mailmap(int argc, |
| 53 | const char **argv, |
| 54 | const char *prefix, |
| 55 | struct repository *repo UNUSED) |
| 56 | { |
| 57 | int i; |
| 58 | struct string_list mailmap = STRING_LIST_INIT_NODUP; |
| 59 | |
| 60 | repo_config(the_repository, git_default_config, NULL); |
| 61 | argc = parse_options(argc, argv, prefix, check_mailmap_options, |
| 62 | check_mailmap_usage, 0); |
| 63 | if (argc == 0 && !use_stdin) |
| 64 | die(_("no contacts specified")); |
| 65 | |
| 66 | read_mailmap(the_repository, &mailmap); |
| 67 | if (mailmap_blob) |
| 68 | read_mailmap_blob(the_repository, &mailmap, mailmap_blob); |
| 69 | if (mailmap_file) |
| 70 | read_mailmap_file(&mailmap, mailmap_file, 0); |
| 71 | |
| 72 | for (i = 0; i < argc; ++i) |
| 73 | check_mailmap(&mailmap, argv[i]); |
| 74 | maybe_flush_or_die(stdout, "stdout"); |
| 75 | |
| 76 | if (use_stdin) { |
| 77 | struct strbuf buf = STRBUF_INIT; |
| 78 | while (strbuf_getline_lf(&buf, stdin) != EOF) { |
| 79 | check_mailmap(&mailmap, buf.buf); |
| 80 | maybe_flush_or_die(stdout, "stdout"); |
| 81 | } |
| 82 | strbuf_release(&buf); |
| 83 | } |
| 84 | |
| 85 | clear_mailmap(&mailmap); |
| 86 | return 0; |
| 87 | } |