builtin: add git-check-mailmap command
authorEric Sunshine <sunshine@sunshineco.com>
Sat, 13 Jul 2013 00:53:10 +0000 (20:53 -0400)
committerJunio C Hamano <gitster@pobox.com>
Sat, 13 Jul 2013 17:19:37 +0000 (10:19 -0700)
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>
.gitignore
Documentation/git-check-mailmap.txt [new file with mode: 0644]
Makefile
builtin.h
builtin/check-mailmap.c [new file with mode: 0644]
command-list.txt
contrib/completion/git-completion.bash
git.c
index c0e00eb37bb370ce9deee72468fb419bc25cfd62..62300976ff2470e38f6fcf0794ed793211bbcb38 100644 (file)
@@ -23,6 +23,7 @@
 /git-cat-file
 /git-check-attr
 /git-check-ignore
+/git-check-mailmap
 /git-check-ref-format
 /git-checkout
 /git-checkout-index
diff --git a/Documentation/git-check-mailmap.txt b/Documentation/git-check-mailmap.txt
new file mode 100644 (file)
index 0000000..39028ee
--- /dev/null
@@ -0,0 +1,47 @@
+git-check-mailmap(1)
+====================
+
+NAME
+----
+git-check-mailmap - Show canonical names and email addresses of contacts
+
+
+SYNOPSIS
+--------
+[verse]
+'git check-mailmap' [options] <contact>...
+
+
+DESCRIPTION
+-----------
+
+For each ``Name $$<user@host>$$'' or ``$$<user@host>$$'' from the command-line
+or standard input (when using `--stdin`), look up the person's canonical name
+and email address (see "Mapping Authors" below). If found, print them;
+otherwise print the input as-is.
+
+
+OPTIONS
+-------
+--stdin::
+       Read contacts, one per line, from the standard input after exhausting
+       contacts provided on the command-line.
+
+
+OUTPUT
+------
+
+For each contact, a single line is output, terminated by a newline.  If the
+name is provided or known to the 'mailmap', ``Name $$<user@host>$$'' is
+printed; otherwise only ``$$<user@host>$$'' is printed.
+
+
+MAPPING AUTHORS
+---------------
+
+include::mailmap.txt[]
+
+
+GIT
+---
+Part of the linkgit:git[1] suite
index e1583761dfa58c3ede93b2076e4aa0963f2aa379..18f1c448b97a070d943a5e74c9bc30301170f9c9 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -909,6 +909,7 @@ BUILTIN_OBJS += builtin/bundle.o
 BUILTIN_OBJS += builtin/cat-file.o
 BUILTIN_OBJS += builtin/check-attr.o
 BUILTIN_OBJS += builtin/check-ignore.o
+BUILTIN_OBJS += builtin/check-mailmap.o
 BUILTIN_OBJS += builtin/check-ref-format.o
 BUILTIN_OBJS += builtin/checkout-index.o
 BUILTIN_OBJS += builtin/checkout.o
index 1ed8edb0cbd83ef1819c0d56defe97c10ade9c0d..8afa2def491ac08d6c0a2962bee152631fb956ca 100644 (file)
--- a/builtin.h
+++ b/builtin.h
@@ -40,6 +40,7 @@ extern int cmd_checkout(int argc, const char **argv, const char *prefix);
 extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
 extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ignore(int argc, const char **argv, const char *prefix);
+extern int cmd_check_mailmap(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
diff --git a/builtin/check-mailmap.c b/builtin/check-mailmap.c
new file mode 100644 (file)
index 0000000..8f4d809
--- /dev/null
@@ -0,0 +1,66 @@
+#include "builtin.h"
+#include "mailmap.h"
+#include "parse-options.h"
+#include "string-list.h"
+
+static int use_stdin;
+static const char * const check_mailmap_usage[] = {
+N_("git check-mailmap [options] <contact>..."),
+NULL
+};
+
+static const struct option check_mailmap_options[] = {
+       OPT_BOOL(0, "stdin", &use_stdin, N_("also read contacts from stdin")),
+       OPT_END()
+};
+
+static void check_mailmap(struct string_list *mailmap, const char *contact)
+{
+       const char *name, *mail;
+       size_t namelen, maillen;
+       struct ident_split ident;
+
+       if (split_ident_line(&ident, contact, strlen(contact)))
+               die(_("unable to parse contact: %s"), contact);
+
+       name = ident.name_begin;
+       namelen = ident.name_end - ident.name_begin;
+       mail = ident.mail_begin;
+       maillen = ident.mail_end - ident.mail_begin;
+
+       map_user(mailmap, &mail, &maillen, &name, &namelen);
+
+       if (namelen)
+               printf("%.*s ", (int)namelen, name);
+       printf("<%.*s>\n", (int)maillen, mail);
+}
+
+int cmd_check_mailmap(int argc, const char **argv, const char *prefix)
+{
+       int i;
+       struct string_list mailmap = STRING_LIST_INIT_NODUP;
+
+       git_config(git_default_config, NULL);
+       argc = parse_options(argc, argv, prefix, check_mailmap_options,
+                            check_mailmap_usage, 0);
+       if (argc == 0 && !use_stdin)
+               die(_("no contacts specified"));
+
+       read_mailmap(&mailmap, NULL);
+
+       for (i = 0; i < argc; ++i)
+               check_mailmap(&mailmap, argv[i]);
+       maybe_flush_or_die(stdout, "stdout");
+
+       if (use_stdin) {
+               struct strbuf buf = STRBUF_INIT;
+               while (strbuf_getline(&buf, stdin, '\n') != EOF) {
+                       check_mailmap(&mailmap, buf.buf);
+                       maybe_flush_or_die(stdout, "stdout");
+               }
+               strbuf_release(&buf);
+       }
+
+       clear_mailmap(&mailmap);
+       return 0;
+}
index bf83303c48dc5cfbccfc4a31b347dcdec197d7d4..08b04e2c66429a05f6fc5ac1f5b2aa94160a5c37 100644 (file)
@@ -13,6 +13,7 @@ git-bundle                              mainporcelain
 git-cat-file                            plumbinginterrogators
 git-check-attr                          purehelpers
 git-check-ignore                        purehelpers
+git-check-mailmap                       purehelpers
 git-checkout                            mainporcelain common
 git-checkout-index                      plumbingmanipulators
 git-check-ref-format                    purehelpers
index 6c3bafeea569601c4b55de165eb7f56ac3be1d31..28f50d14b036073e240f6a3e4dea85d6aadca4bc 100644 (file)
@@ -650,6 +650,7 @@ __git_list_porcelain_commands ()
                cat-file)         : plumbing;;
                check-attr)       : plumbing;;
                check-ignore)     : plumbing;;
+               check-mailmap)    : plumbing;;
                check-ref-format) : plumbing;;
                checkout-index)   : plumbing;;
                commit-tree)      : plumbing;;
diff --git a/git.c b/git.c
index 4359086fd6c47f1bfc3fc408b7bc986517eea9a0..02c3035d02d2156ace33c100ebffea75f532106b 100644 (file)
--- a/git.c
+++ b/git.c
@@ -324,6 +324,7 @@ static void handle_internal_command(int argc, const char **argv)
                { "cat-file", cmd_cat_file, RUN_SETUP },
                { "check-attr", cmd_check_attr, RUN_SETUP },
                { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
+               { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
                { "check-ref-format", cmd_check_ref_format },
                { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
                { "checkout-index", cmd_checkout_index,