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 <christian.couder@gmail.com> Mentored-by: John Cai <johncai86@gmail.com> Helped-by: Taylor Blau <me@ttaylorr.com> Helped-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Siddharth Asthana <siddharthasthana31@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Siddharth Asthana committed Dec 20, 2022 at 11:31 UTC 49050a043bf767d370e7288cb6cca0ca21f13404
3 files changed +45 -1
Documentation/git-cat-file.txt
+3 -1
@@ -45,7 +45,9 @@ OPTIONS
45
46 -s::
47 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.
51
52 -e::
53 Exit with zero status if `<object>` exists and is a valid
builtin/cat-file.c
+13
@@ -132,8 +132,21 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
132
133 case 's':
134 oi.sizep = &size;
135 +
136 + if (use_mailmap) {
137 + oi.typep = &type;
138 + oi.contentp = (void**)&buf;
139 + }
140 +
141 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
142 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 +
150 printf("%"PRIuMAX"\n", (uintmax_t)size);
151 ret = 0;
152 goto cleanup;
t/t4203-mailmap.sh
+29
@@ -1022,4 +1022,33 @@ test_expect_success '--mailmap enables mailmap in cat-file for annotated tag obj
1022 test_cmp expect actual
1023 '
1024
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 + C O Mitter <committer@example.com> Orig <orig@example.com>
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 + Orig <orig@example.com> C O Mitter <committer@example.com>
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_done