Skip to content

Commit 319c3ab

Browse files
committed
Merge branch 'sa/cat-file-mailmap--batch-check'
'cat-file' gains mailmap support for its '--batch-check' and '-s' options. * sa/cat-file-mailmap--batch-check: cat-file: add mailmap support to --batch-check option cat-file: add mailmap support to -s option
2 parents 2b4f5a4 + a797c0e commit 319c3ab

File tree

3 files changed

+132
-14
lines changed

3 files changed

+132
-14
lines changed

Documentation/git-cat-file.txt

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ OPTIONS
4545

4646
-s::
4747
Instead of the content, show the object size identified by
48-
`<object>`.
48+
`<object>`. If used with `--use-mailmap` option, will show
49+
the size of updated object after replacing idents using the
50+
mailmap mechanism.
4951

5052
-e::
5153
Exit with zero status if `<object>` exists and is a valid
@@ -89,26 +91,49 @@ OPTIONS
8991
--batch::
9092
--batch=<format>::
9193
Print object information and contents for each object provided
92-
on stdin. May not be combined with any other options or arguments
93-
except `--textconv` or `--filters`, in which case the input lines
94-
also need to specify the path, separated by whitespace. See the
95-
section `BATCH OUTPUT` below for details.
94+
on stdin. May not be combined with any other options or arguments
95+
except `--textconv`, `--filters`, or `--use-mailmap`.
96+
+
97+
* When used with `--textconv` or `--filters`, the input lines
98+
must specify the path, separated by whitespace. See the section
99+
`BATCH OUTPUT` below for details.
100+
+
101+
* When used with `--use-mailmap`, for commit and tag objects, the
102+
contents part of the output shows the identities replaced using the
103+
mailmap mechanism, while the information part of the output shows
104+
the size of the object as if it actually recorded the replacement
105+
identities.
96106

97107
--batch-check::
98108
--batch-check=<format>::
99-
Print object information for each object provided on stdin. May
100-
not be combined with any other options or arguments except
101-
`--textconv` or `--filters`, in which case the input lines also
102-
need to specify the path, separated by whitespace. See the
103-
section `BATCH OUTPUT` below for details.
109+
Print object information for each object provided on stdin. May not be
110+
combined with any other options or arguments except `--textconv`, `--filters`
111+
or `--use-mailmap`.
112+
+
113+
* When used with `--textconv` or `--filters`, the input lines must
114+
specify the path, separated by whitespace. See the section
115+
`BATCH OUTPUT` below for details.
116+
+
117+
* When used with `--use-mailmap`, for commit and tag objects, the
118+
printed object information shows the size of the object as if the
119+
identities recorded in it were replaced by the mailmap mechanism.
104120

105121
--batch-command::
106122
--batch-command=<format>::
107123
Enter a command mode that reads commands and arguments from stdin. May
108-
only be combined with `--buffer`, `--textconv` or `--filters`. In the
109-
case of `--textconv` or `--filters`, the input lines also need to specify
110-
the path, separated by whitespace. See the section `BATCH OUTPUT` below
111-
for details.
124+
only be combined with `--buffer`, `--textconv`, `--use-mailmap` or
125+
`--filters`.
126+
+
127+
* When used with `--textconv` or `--filters`, the input lines must
128+
specify the path, separated by whitespace. See the section
129+
`BATCH OUTPUT` below for details.
130+
+
131+
* When used with `--use-mailmap`, for commit and tag objects, the
132+
`contents` command shows the identities replaced using the
133+
mailmap mechanism, while the `info` command shows the size
134+
of the object as if it actually recorded the replacement
135+
identities.
136+
112137
+
113138
`--batch-command` recognizes the following commands:
114139
+

builtin/cat-file.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,21 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
132132

133133
case 's':
134134
oi.sizep = &size;
135+
136+
if (use_mailmap) {
137+
oi.typep = &type;
138+
oi.contentp = (void**)&buf;
139+
}
140+
135141
if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
136142
die("git cat-file: could not get object info");
143+
144+
if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) {
145+
size_t s = size;
146+
buf = replace_idents_using_mailmap(buf, &s);
147+
size = cast_size_t_to_ulong(s);
148+
}
149+
137150
printf("%"PRIuMAX"\n", (uintmax_t)size);
138151
ret = 0;
139152
goto cleanup;
@@ -431,6 +444,9 @@ static void batch_object_write(const char *obj_name,
431444
if (!data->skip_object_info) {
432445
int ret;
433446

447+
if (use_mailmap)
448+
data->info.typep = &data->type;
449+
434450
if (pack)
435451
ret = packed_object_info(the_repository, pack, offset,
436452
&data->info);
@@ -444,6 +460,18 @@ static void batch_object_write(const char *obj_name,
444460
fflush(stdout);
445461
return;
446462
}
463+
464+
if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) {
465+
size_t s = data->size;
466+
char *buf = NULL;
467+
468+
buf = repo_read_object_file(the_repository, &data->oid, &data->type,
469+
&data->size);
470+
buf = replace_idents_using_mailmap(buf, &s);
471+
data->size = cast_size_t_to_ulong(s);
472+
473+
free(buf);
474+
}
447475
}
448476

449477
strbuf_reset(scratch);

t/t4203-mailmap.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,69 @@ test_expect_success '--mailmap enables mailmap in cat-file for annotated tag obj
10221022
test_cmp expect actual
10231023
'
10241024

1025+
test_expect_success 'git cat-file -s returns correct size with --use-mailmap' '
1026+
test_when_finished "rm .mailmap" &&
1027+
cat >.mailmap <<-\EOF &&
1028+
1029+
EOF
1030+
git cat-file commit HEAD >commit.out &&
1031+
echo $(wc -c <commit.out) >expect &&
1032+
git cat-file --use-mailmap commit HEAD >commit.out &&
1033+
echo $(wc -c <commit.out) >>expect &&
1034+
git cat-file -s HEAD >actual &&
1035+
git cat-file --use-mailmap -s HEAD >>actual &&
1036+
test_cmp expect actual
1037+
'
1038+
1039+
test_expect_success 'git cat-file -s returns correct size with --use-mailmap for tag objects' '
1040+
test_when_finished "rm .mailmap" &&
1041+
cat >.mailmap <<-\EOF &&
1042+
1043+
EOF
1044+
git tag -a -m "annotated tag" v3 &&
1045+
git cat-file tag v3 >tag.out &&
1046+
echo $(wc -c <tag.out) >expect &&
1047+
git cat-file --use-mailmap tag v3 >tag.out &&
1048+
echo $(wc -c <tag.out) >>expect &&
1049+
git cat-file -s v3 >actual &&
1050+
git cat-file --use-mailmap -s v3 >>actual &&
1051+
test_cmp expect actual
1052+
'
1053+
1054+
test_expect_success 'git cat-file --batch-check returns correct size with --use-mailmap' '
1055+
test_when_finished "rm .mailmap" &&
1056+
cat >.mailmap <<-\EOF &&
1057+
1058+
EOF
1059+
git cat-file commit HEAD >commit.out &&
1060+
commit_size=$(wc -c <commit.out) &&
1061+
commit_sha=$(git rev-parse HEAD) &&
1062+
echo $commit_sha commit $commit_size >expect &&
1063+
git cat-file --use-mailmap commit HEAD >commit.out &&
1064+
commit_size=$(wc -c <commit.out) &&
1065+
echo $commit_sha commit $commit_size >>expect &&
1066+
echo "HEAD" >in &&
1067+
git cat-file --batch-check <in >actual &&
1068+
git cat-file --use-mailmap --batch-check <in >>actual &&
1069+
test_cmp expect actual
1070+
'
1071+
1072+
test_expect_success 'git cat-file --batch-command returns correct size with --use-mailmap' '
1073+
test_when_finished "rm .mailmap" &&
1074+
cat >.mailmap <<-\EOF &&
1075+
1076+
EOF
1077+
git cat-file commit HEAD >commit.out &&
1078+
commit_size=$(wc -c <commit.out) &&
1079+
commit_sha=$(git rev-parse HEAD) &&
1080+
echo $commit_sha commit $commit_size >expect &&
1081+
git cat-file --use-mailmap commit HEAD >commit.out &&
1082+
commit_size=$(wc -c <commit.out) &&
1083+
echo $commit_sha commit $commit_size >>expect &&
1084+
echo "info HEAD" >in &&
1085+
git cat-file --batch-command <in >actual &&
1086+
git cat-file --use-mailmap --batch-command <in >>actual &&
1087+
test_cmp expect actual
1088+
'
1089+
10251090
test_done

0 commit comments

Comments
 (0)