Skip to content

Commit 49050a0

Browse files
edith007gitster
authored andcommitted
cat-file: add mailmap support to -s option
Even though the cat-file command with `-s` option does not complain when `--use-mailmap` option is given, the latter option is ignored. Compute the size of the object after replacing the idents and report it instead. In order to make `-s` option honour the mailmap mechanism we have to read the contents of the commit/tag object. Make use of the call to `oid_object_info_extended()` to get the contents of the object and store in `buf`. `buf` is later freed in the function. Mentored-by: Christian Couder <[email protected]> Mentored-by: John Cai <[email protected]> Helped-by: Taylor Blau <[email protected]> Helped-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Siddharth Asthana <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 63bba4f commit 49050a0

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

Documentation/git-cat-file.txt

Lines changed: 3 additions & 1 deletion
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

builtin/cat-file.c

Lines changed: 13 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;

t/t4203-mailmap.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,4 +1022,33 @@ 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+
10251054
test_done

0 commit comments

Comments
 (0)