builtin: add git-check-mailmap command

Introduce command check-mailmap, similar to check-attr and check-ignore, which allows direct testing of .mailmap configuration. As plumbing accessible to scripts and other porcelain, check-mailmap publishes the stable, well-tested .mailmap functionality employed by built-in Git commands. Consequently, script authors need not re-implement .mailmap functionality manually, thus avoiding potential quirks and behavioral differences. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 12, 2013 at 20:53 UTC 226ad3482ac506af0eadd04e732dda556fda80e4
8 files changed +119
.gitignore
+1
@@ -23,6 +23,7 @@
23 /git-cat-file
24 /git-check-attr
25 /git-check-ignore
26 +/git-check-mailmap
27 /git-check-ref-format
28 /git-checkout
29 /git-checkout-index
Documentation/git-check-mailmap.txt new
+47
@@ -0,0 +1,47 @@
1 +git-check-mailmap(1)
2 +====================
3 +
4 +NAME
5 +----
6 +git-check-mailmap - Show canonical names and email addresses of contacts
7 +
8 +
9 +SYNOPSIS
10 +--------
11 +[verse]
12 +'git check-mailmap' [options] <contact>...
13 +
14 +
15 +DESCRIPTION
16 +-----------
17 +
18 +For each ``Name $$<user@host>$$'' or ``$$<user@host>$$'' from the command-line
19 +or standard input (when using `--stdin`), look up the person's canonical name
20 +and email address (see "Mapping Authors" below). If found, print them;
21 +otherwise print the input as-is.
22 +
23 +
24 +OPTIONS
25 +-------
26 +--stdin::
27 + Read contacts, one per line, from the standard input after exhausting
28 + contacts provided on the command-line.
29 +
30 +
31 +OUTPUT
32 +------
33 +
34 +For each contact, a single line is output, terminated by a newline. If the
35 +name is provided or known to the 'mailmap', ``Name $$<user@host>$$'' is
36 +printed; otherwise only ``$$<user@host>$$'' is printed.
37 +
38 +
39 +MAPPING AUTHORS
40 +---------------
41 +
42 +include::mailmap.txt[]
43 +
44 +
45 +GIT
46 +---
47 +Part of the linkgit:git[1] suite
Makefile
+1
@@ -909,6 +909,7 @@ BUILTIN_OBJS += builtin/bundle.o
909 BUILTIN_OBJS += builtin/cat-file.o
910 BUILTIN_OBJS += builtin/check-attr.o
911 BUILTIN_OBJS += builtin/check-ignore.o
912 +BUILTIN_OBJS += builtin/check-mailmap.o
913 BUILTIN_OBJS += builtin/check-ref-format.o
914 BUILTIN_OBJS += builtin/checkout-index.o
915 BUILTIN_OBJS += builtin/checkout.o
builtin.h
+1
@@ -40,6 +40,7 @@ extern int cmd_checkout(int argc, const char **argv, const char *prefix);
40 extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
41 extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
42 extern int cmd_check_ignore(int argc, const char **argv, const char *prefix);
43 +extern int cmd_check_mailmap(int argc, const char **argv, const char *prefix);
44 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
45 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
46 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
builtin/check-mailmap.c new
+66
@@ -0,0 +1,66 @@
1 +#include "builtin.h"
2 +#include "mailmap.h"
3 +#include "parse-options.h"
4 +#include "string-list.h"
5 +
6 +static int use_stdin;
7 +static const char * const check_mailmap_usage[] = {
8 +N_("git check-mailmap [options] <contact>..."),
9 +NULL
10 +};
11 +
12 +static const struct option check_mailmap_options[] = {
13 + OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
14 + OPT_END()
15 +};
16 +
17 +static void check_mailmap(struct string_list *mailmap, const char *contact)
18 +{
19 + const char *name, *mail;
20 + size_t namelen, maillen;
21 + struct ident_split ident;
22 +
23 + if (split_ident_line(&ident, contact, strlen(contact)))
24 + die(_("unable to parse contact: %s"), contact);
25 +
26 + name = ident.name_begin;
27 + namelen = ident.name_end - ident.name_begin;
28 + mail = ident.mail_begin;
29 + maillen = ident.mail_end - ident.mail_begin;
30 +
31 + map_user(mailmap, &mail, &maillen, &name, &namelen);
32 +
33 + if (namelen)
34 + printf("%.*s ", (int)namelen, name);
35 + printf("<%.*s>\n", (int)maillen, mail);
36 +}
37 +
38 +int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
39 +{
40 + int i;
41 + struct string_list mailmap = STRING_LIST_INIT_NODUP;
42 +
43 + git_config(git_default_config, NULL);
44 + argc = parse_options(argc, argv, prefix, check_mailmap_options,
45 + check_mailmap_usage, 0);
46 + if (argc == 0 && !use_stdin)
47 + die(_("no contacts specified"));
48 +
49 + read_mailmap(&mailmap, NULL);
50 +
51 + for (i = 0; i < argc; ++i)
52 + check_mailmap(&mailmap, argv[i]);
53 + maybe_flush_or_die(stdout, "stdout");
54 +
55 + if (use_stdin) {
56 + struct strbuf buf = STRBUF_INIT;
57 + while (strbuf_getline(&buf, stdin, '\n') != EOF) {
58 + check_mailmap(&mailmap, buf.buf);
59 + maybe_flush_or_die(stdout, "stdout");
60 + }
61 + strbuf_release(&buf);
62 + }
63 +
64 + clear_mailmap(&mailmap);
65 + return 0;
66 +}
command-list.txt
+1
@@ -13,6 +13,7 @@ git-bundle mainporcelain
13 git-cat-file plumbinginterrogators
14 git-check-attr purehelpers
15 git-check-ignore purehelpers
16 +git-check-mailmap purehelpers
17 git-checkout mainporcelain common
18 git-checkout-index plumbingmanipulators
19 git-check-ref-format purehelpers
contrib/completion/git-completion.bash
+1
@@ -650,6 +650,7 @@ __git_list_porcelain_commands ()
650 cat-file) : plumbing;;
651 check-attr) : plumbing;;
652 check-ignore) : plumbing;;
653 + check-mailmap) : plumbing;;
654 check-ref-format) : plumbing;;
655 checkout-index) : plumbing;;
656 commit-tree) : plumbing;;
git.c
+1
@@ -324,6 +324,7 @@ static void handle_internal_command(int argc, const char **argv)
324 { "cat-file", cmd_cat_file, RUN_SETUP },
325 { "check-attr", cmd_check_attr, RUN_SETUP },
326 { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
327 + { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
328 { "check-ref-format", cmd_check_ref_format },
329 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
330 { "checkout-index", cmd_checkout_index,