Skip to content

Commit 9e25c34

Browse files
committed
Merge branch 'sa/cat-file-batch-mailmap-switch' into jch
"git cat-file --batch" learns an in-line command "mailmap" that lets the user toggle use of mailmap. * sa/cat-file-batch-mailmap-switch: cat-file: add mailmap subcommand to --batch-command
2 parents 4d496d3 + c878e03 commit 9e25c34

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

Documentation/git-cat-file.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ flush::
174174
since the beginning or since the last flush was issued. When `--buffer`
175175
is used, no output will come until a `flush` is issued. When `--buffer`
176176
is not used, commands are flushed each time without issuing `flush`.
177+
178+
mailmap <bool>::
179+
Enable or disable mailmap for subsequent commands.
180+
+
181+
The `<bool>` argument accepts the same boolean values as
182+
linkgit:git-config[1]. When enabled, mailmap data is loaded on first
183+
use and kept in memory until the process exits.
177184
--
178185
+
179186

builtin/cat-file.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ static int use_mailmap;
5757

5858
static char *replace_idents_using_mailmap(char *, size_t *);
5959

60+
static void load_mailmap(void)
61+
{
62+
if (mailmap.strdup_strings)
63+
return;
64+
65+
read_mailmap(the_repository, &mailmap);
66+
}
67+
6068
static char *replace_idents_using_mailmap(char *object_buf, size_t *size)
6169
{
6270
struct strbuf sb = STRBUF_INIT;
@@ -692,6 +700,21 @@ static void parse_cmd_info(struct batch_options *opt,
692700
batch_one_object(line, output, opt, data);
693701
}
694702

703+
static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
704+
const char *line,
705+
struct strbuf *output UNUSED,
706+
struct expand_data *data UNUSED)
707+
{
708+
int value = git_parse_maybe_bool(line);
709+
710+
if (value < 0)
711+
die(_("mailmap: invalid boolean '%s'"), line);
712+
713+
if (value > 0)
714+
load_mailmap();
715+
use_mailmap = value;
716+
}
717+
695718
static void dispatch_calls(struct batch_options *opt,
696719
struct strbuf *output,
697720
struct expand_data *data,
@@ -725,9 +748,10 @@ static const struct parse_cmd {
725748
parse_cmd_fn_t fn;
726749
unsigned takes_args;
727750
} commands[] = {
728-
{ "contents", parse_cmd_contents, 1},
729-
{ "info", parse_cmd_info, 1},
730-
{ "flush", NULL, 0},
751+
{ "contents", parse_cmd_contents, 1 },
752+
{ "info", parse_cmd_info, 1 },
753+
{ "flush", NULL, 0 },
754+
{ "mailmap", parse_cmd_mailmap, 1 },
731755
};
732756

733757
static void batch_objects_command(struct batch_options *opt,
@@ -1131,7 +1155,7 @@ int cmd_cat_file(int argc,
11311155
opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
11321156

11331157
if (use_mailmap)
1134-
read_mailmap(the_repository, &mailmap);
1158+
load_mailmap();
11351159

11361160
switch (batch.objects_filter.choice) {
11371161
case LOFC_DISABLED:

t/t4203-mailmap.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,111 @@ test_expect_success 'git cat-file --batch-command returns correct size with --us
11331133
test_cmp expect actual
11341134
'
11351135

1136+
test_expect_success 'git cat-file --batch-command mailmap yes enables mailmap mid-stream' '
1137+
test_when_finished "rm .mailmap" &&
1138+
cat >.mailmap <<-\EOF &&
1139+
C O Mitter <committer@example.com> Orig <orig@example.com>
1140+
EOF
1141+
commit_sha=$(git rev-parse HEAD) &&
1142+
git cat-file commit HEAD >commit_no_mailmap.out &&
1143+
git cat-file --use-mailmap commit HEAD >commit_mailmap.out &&
1144+
size_no_mailmap=$(wc -c <commit_no_mailmap.out) &&
1145+
size_mailmap=$(wc -c <commit_mailmap.out) &&
1146+
printf "info HEAD\nmailmap yes\ninfo HEAD\n" | git cat-file --batch-command >actual &&
1147+
echo $commit_sha commit $size_no_mailmap >expect &&
1148+
echo $commit_sha commit $size_mailmap >>expect &&
1149+
test_cmp expect actual
1150+
'
1151+
1152+
test_expect_success 'git cat-file --batch-command mailmap no disables mailmap mid-stream' '
1153+
test_when_finished "rm .mailmap" &&
1154+
cat >.mailmap <<-\EOF &&
1155+
C O Mitter <committer@example.com> Orig <orig@example.com>
1156+
EOF
1157+
commit_sha=$(git rev-parse HEAD) &&
1158+
git cat-file commit HEAD >commit_no_mailmap.out &&
1159+
git cat-file --use-mailmap commit HEAD >commit_mailmap.out &&
1160+
size_no_mailmap=$(wc -c <commit_no_mailmap.out) &&
1161+
size_mailmap=$(wc -c <commit_mailmap.out) &&
1162+
printf "mailmap yes\ninfo HEAD\nmailmap no\ninfo HEAD\n" | git cat-file --batch-command >actual &&
1163+
echo $commit_sha commit $size_mailmap >expect &&
1164+
echo $commit_sha commit $size_no_mailmap >>expect &&
1165+
test_cmp expect actual
1166+
'
1167+
1168+
test_expect_success 'git cat-file --batch-command mailmap works in --buffer mode' '
1169+
test_when_finished "rm .mailmap" &&
1170+
cat >.mailmap <<-\EOF &&
1171+
C O Mitter <committer@example.com> Orig <orig@example.com>
1172+
EOF
1173+
commit_sha=$(git rev-parse HEAD) &&
1174+
git cat-file commit HEAD >commit_no_mailmap.out &&
1175+
git cat-file --use-mailmap commit HEAD >commit_mailmap.out &&
1176+
size_no_mailmap=$(wc -c <commit_no_mailmap.out) &&
1177+
size_mailmap=$(wc -c <commit_mailmap.out) &&
1178+
printf "mailmap yes\ninfo HEAD\nmailmap no\ninfo HEAD\nflush\n" | git cat-file --batch-command --buffer >actual &&
1179+
echo $commit_sha commit $size_mailmap >expect &&
1180+
echo $commit_sha commit $size_no_mailmap >>expect &&
1181+
test_cmp expect actual
1182+
'
1183+
1184+
test_expect_success 'git cat-file --batch-command mailmap no overrides startup --mailmap' '
1185+
test_when_finished "rm .mailmap" &&
1186+
cat >.mailmap <<-\EOF &&
1187+
C O Mitter <committer@example.com> Orig <orig@example.com>
1188+
EOF
1189+
commit_sha=$(git rev-parse HEAD) &&
1190+
git cat-file --use-mailmap commit HEAD >commit_mailmap.out &&
1191+
size_mailmap=$(wc -c <commit_mailmap.out) &&
1192+
git cat-file commit HEAD >commit_no_mailmap.out &&
1193+
size_no_mailmap=$(wc -c <commit_no_mailmap.out) &&
1194+
printf "info HEAD\nmailmap no\ninfo HEAD\n" | \
1195+
git cat-file --mailmap --batch-command >actual &&
1196+
echo $commit_sha commit $size_mailmap >expect &&
1197+
echo $commit_sha commit $size_no_mailmap >>expect &&
1198+
test_cmp expect actual
1199+
'
1200+
1201+
test_expect_success 'git cat-file --batch-command mailmap yes overrides startup --no-mailmap' '
1202+
test_when_finished "rm .mailmap" &&
1203+
cat >.mailmap <<-\EOF &&
1204+
C O Mitter <committer@example.com> Orig <orig@example.com>
1205+
EOF
1206+
commit_sha=$(git rev-parse HEAD) &&
1207+
git cat-file commit HEAD >commit_no_mailmap.out &&
1208+
size_no_mailmap=$(wc -c <commit_no_mailmap.out) &&
1209+
git cat-file --use-mailmap commit HEAD >commit_mailmap.out &&
1210+
size_mailmap=$(wc -c <commit_mailmap.out) &&
1211+
printf "info HEAD\nmailmap yes\ninfo HEAD\n" | \
1212+
git cat-file --no-mailmap --batch-command >actual &&
1213+
echo $commit_sha commit $size_no_mailmap >expect &&
1214+
echo $commit_sha commit $size_mailmap >>expect &&
1215+
test_cmp expect actual
1216+
'
1217+
1218+
test_expect_success 'git cat-file --batch-command mailmap accepts true/false' '
1219+
test_when_finished "rm .mailmap" &&
1220+
cat >.mailmap <<-\EOF &&
1221+
C O Mitter <committer@example.com> Orig <orig@example.com>
1222+
EOF
1223+
commit_sha=$(git rev-parse HEAD) &&
1224+
git cat-file commit HEAD >commit_no_mailmap.out &&
1225+
size_no_mailmap=$(wc -c <commit_no_mailmap.out) &&
1226+
git cat-file --use-mailmap commit HEAD >commit_mailmap.out &&
1227+
size_mailmap=$(wc -c <commit_mailmap.out) &&
1228+
printf "mailmap true\ninfo HEAD\nmailmap false\ninfo HEAD\n" | \
1229+
git cat-file --batch-command >actual &&
1230+
echo $commit_sha commit $size_mailmap >expect &&
1231+
echo $commit_sha commit $size_no_mailmap >>expect &&
1232+
test_cmp expect actual
1233+
'
1234+
1235+
test_expect_success 'git cat-file --batch-command mailmap rejects invalid boolean' '
1236+
echo "mailmap maybe" >in &&
1237+
test_must_fail git cat-file --batch-command <in 2>err &&
1238+
test_grep "mailmap: invalid boolean .*maybe" err
1239+
'
1240+
11361241
test_expect_success 'git cat-file --mailmap works with different author and committer' '
11371242
test_when_finished "rm .mailmap" &&
11381243
cat >.mailmap <<-\EOF &&

0 commit comments

Comments
 (0)