Skip to content

Commit 7c105ef

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. Comments? * sa/cat-file-batch-mailmap-switch: cat-file: add mailmap subcommand to --batch-command
2 parents c409626 + 80b171a commit 7c105ef

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

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 <yes|no>::
179+
Enable or disable mailmap for subsequent `contents` and `info`
180+
commands. When `yes` is given, mailmap data is loaded from disk on
181+
first use and kept in memory; passing `yes` again does not reload it.
182+
When `no` is given, mailmap is disabled but the data stays in memory
183+
so that a later `mailmap yes` does not need to reload it from disk.
177184
--
178185
+
179186

builtin/cat-file.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static const char *force_path;
5454

5555
static struct string_list mailmap = STRING_LIST_INIT_NODUP;
5656
static int use_mailmap;
57+
static int mailmap_loaded;
5758

5859
static char *replace_idents_using_mailmap(char *, size_t *);
5960

@@ -692,6 +693,24 @@ static void parse_cmd_info(struct batch_options *opt,
692693
batch_one_object(line, output, opt, data);
693694
}
694695

696+
static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
697+
const char *line,
698+
struct strbuf *output UNUSED,
699+
struct expand_data *data UNUSED)
700+
{
701+
if (!strcmp(line, "yes")) {
702+
if (!mailmap_loaded) {
703+
read_mailmap(the_repository, &mailmap);
704+
mailmap_loaded = 1;
705+
}
706+
use_mailmap = 1;
707+
} else if (!strcmp(line, "no")) {
708+
use_mailmap = 0;
709+
} else {
710+
die(_("mailmap: unknown argument '%s', expected 'yes' or 'no'"), line);
711+
}
712+
}
713+
695714
static void dispatch_calls(struct batch_options *opt,
696715
struct strbuf *output,
697716
struct expand_data *data,
@@ -725,9 +744,10 @@ static const struct parse_cmd {
725744
parse_cmd_fn_t fn;
726745
unsigned takes_args;
727746
} commands[] = {
728-
{ "contents", parse_cmd_contents, 1},
729-
{ "info", parse_cmd_info, 1},
730-
{ "flush", NULL, 0},
747+
{ "contents", parse_cmd_contents, 1 },
748+
{ "info", parse_cmd_info, 1 },
749+
{ "flush", NULL, 0 },
750+
{ "mailmap", parse_cmd_mailmap, 1 },
731751
};
732752

733753
static void batch_objects_command(struct batch_options *opt,
@@ -1130,8 +1150,10 @@ int cmd_cat_file(int argc,
11301150
opt_cw = (opt == 'c' || opt == 'w');
11311151
opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
11321152

1133-
if (use_mailmap)
1153+
if (use_mailmap) {
11341154
read_mailmap(the_repository, &mailmap);
1155+
mailmap_loaded = 1;
1156+
}
11351157

11361158
switch (batch.objects_filter.choice) {
11371159
case LOFC_DISABLED:

t/t4203-mailmap.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,54 @@ 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+
11361184
test_expect_success 'git cat-file --mailmap works with different author and committer' '
11371185
test_when_finished "rm .mailmap" &&
11381186
cat >.mailmap <<-\EOF &&

0 commit comments

Comments
 (0)