Skip to content

Commit 226ad34

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7a3187e commit 226ad34

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/git-cat-file
2424
/git-check-attr
2525
/git-check-ignore
26+
/git-check-mailmap
2627
/git-check-ref-format
2728
/git-checkout
2829
/git-checkout-index

Documentation/git-check-mailmap.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ BUILTIN_OBJS += builtin/bundle.o
909909
BUILTIN_OBJS += builtin/cat-file.o
910910
BUILTIN_OBJS += builtin/check-attr.o
911911
BUILTIN_OBJS += builtin/check-ignore.o
912+
BUILTIN_OBJS += builtin/check-mailmap.o
912913
BUILTIN_OBJS += builtin/check-ref-format.o
913914
BUILTIN_OBJS += builtin/checkout-index.o
914915
BUILTIN_OBJS += builtin/checkout.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern int cmd_checkout(int argc, const char **argv, const char *prefix);
4040
extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
4141
extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
4242
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);
4344
extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
4445
extern int cmd_cherry(int argc, const char **argv, const char *prefix);
4546
extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);

builtin/check-mailmap.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ git-bundle mainporcelain
1313
git-cat-file plumbinginterrogators
1414
git-check-attr purehelpers
1515
git-check-ignore purehelpers
16+
git-check-mailmap purehelpers
1617
git-checkout mainporcelain common
1718
git-checkout-index plumbingmanipulators
1819
git-check-ref-format purehelpers

contrib/completion/git-completion.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ __git_list_porcelain_commands ()
650650
cat-file) : plumbing;;
651651
check-attr) : plumbing;;
652652
check-ignore) : plumbing;;
653+
check-mailmap) : plumbing;;
653654
check-ref-format) : plumbing;;
654655
checkout-index) : plumbing;;
655656
commit-tree) : plumbing;;

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ static void handle_internal_command(int argc, const char **argv)
324324
{ "cat-file", cmd_cat_file, RUN_SETUP },
325325
{ "check-attr", cmd_check_attr, RUN_SETUP },
326326
{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
327+
{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
327328
{ "check-ref-format", cmd_check_ref_format },
328329
{ "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
329330
{ "checkout-index", cmd_checkout_index,

0 commit comments

Comments
 (0)