Message ID | 20221120074852.121346-2-siddharthasthana31@gmail.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | Add mailmap mechanism in cat-file options | expand |
Siddharth Asthana <siddharthasthana31@gmail.com> writes: > +test_expect_success 'git cat-file -s returns correct size with --use-mailmap' ' > + test_when_finished "rm .mailmap" && > + cat >.mailmap <<-\EOF && > + C O Mitter <committer@example.com> Orig <orig@example.com> > + EOF > + git cat-file commit HEAD | wc -c >expect && > + git cat-file --use-mailmap commit HEAD | wc -c >>expect && > + git cat-file -s HEAD >actual && > + git cat-file --use-mailmap -s HEAD >>actual && Doesn't this break under macOS where wc output tends to be padded with SP on the right? We used to often see test breakage when a carelessly written test like test "$(wc -l <outout)" = 2 which expects the output file to have exactly two files (the solution in this sample case is to lose the double quotes around the command substitution). Besides, having "cat-file" on the upstream side of a pipe is a bad practice.
On Mon, Nov 21, 2022 at 8:27 AM Junio C Hamano <gitster@pobox.com> wrote: > > Siddharth Asthana <siddharthasthana31@gmail.com> writes: > > > +test_expect_success 'git cat-file -s returns correct size with --use-mailmap' ' > > + test_when_finished "rm .mailmap" && > > + cat >.mailmap <<-\EOF && > > + C O Mitter <committer@example.com> Orig <orig@example.com> > > + EOF > > + git cat-file commit HEAD | wc -c >expect && > > + git cat-file --use-mailmap commit HEAD | wc -c >>expect && > > + git cat-file -s HEAD >actual && > > + git cat-file --use-mailmap -s HEAD >>actual && > > Doesn't this break under macOS where wc output tends to be padded > with SP on the right? We used to often see test breakage when a > carelessly written test like > > test "$(wc -l <outout)" = 2 > > which expects the output file to have exactly two files (the > solution in this sample case is to lose the double quotes around the > command substitution). I guess that's the reason why `wc -c | sed -e 's/^ *//'` is used in the strlen() function in t1006-cat-file.sh. There are a number of places in the tests where wc -c or wc -l are used without piping the result into sed -e 's/^ *//' though. So it's not easy to understand why it's sometimes needed. > Besides, having "cat-file" on the upstream side of a pipe is a bad > practice. Yeah, right. So I would suggest the following: git cat-file commit HEAD >commit.out && wc -c <commit.out | sed -e 's/^ *//' >expect && git cat-file --use-mailmap commit HEAD >commit.out && wc -c <commit.out | sed -e 's/^ *//' >>expect && Thanks!
Christian Couder <christian.couder@gmail.com> writes: > Yeah, right. So I would suggest the following: > > git cat-file commit HEAD >commit.out && > wc -c <commit.out | sed -e 's/^ *//' >expect && You should not use sed for things like that. echo $(wc -c <file) should suffice to strip away unwanted $IFS around the output (Note. lack of any quotihg around $() is the key).
On Mon, Nov 21 2022, Christian Couder wrote: > On Mon, Nov 21, 2022 at 8:27 AM Junio C Hamano <gitster@pobox.com> wrote: >> >> Siddharth Asthana <siddharthasthana31@gmail.com> writes: >> >> > +test_expect_success 'git cat-file -s returns correct size with --use-mailmap' ' >> > + test_when_finished "rm .mailmap" && >> > + cat >.mailmap <<-\EOF && >> > + C O Mitter <committer@example.com> Orig <orig@example.com> >> > + EOF >> > + git cat-file commit HEAD | wc -c >expect && >> > + git cat-file --use-mailmap commit HEAD | wc -c >>expect && >> > + git cat-file -s HEAD >actual && >> > + git cat-file --use-mailmap -s HEAD >>actual && >> >> Doesn't this break under macOS where wc output tends to be padded >> with SP on the right? We used to often see test breakage when a >> carelessly written test like >> >> test "$(wc -l <outout)" = 2 >> >> which expects the output file to have exactly two files (the >> solution in this sample case is to lose the double quotes around the >> command substitution). > > I guess that's the reason why `wc -c | sed -e 's/^ *//'` is used in > the strlen() function in t1006-cat-file.sh. There are a number of > places in the tests where wc -c or wc -l are used without piping the > result into sed -e 's/^ *//' though. So it's not easy to understand > why it's sometimes needed. It's because in "t1006-cat-file.sh" we're assigning the "wc -c" to a variable, because it's used to "test_cmp" the number of bytes in some free-form text. It would be nicer to split "test_line_count" into some utility function that knew how to parse out "wc -l", "wc -c" etc. for a given input file, and return that as a string. In that case the "sed" isn't needed, and we're just (ab)using it to do things we can do with whitespace managent + shell built-ins. E.g. this works too (the "echo; echo; echo" showing that we're stripping out whitespace "wc -c" might emit: diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 23b8942edba..9ae4b534421 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -106,7 +106,7 @@ echo_without_newline_nul () { } strlen () { - echo_without_newline "$1" | wc -c | sed -e 's/^ *//' + printf "%s" $(printf "%s" "$1" | (echo ; echo ; echo ; wc -c)) } maybe_remove_timestamp () {
diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt index ec30b5c574..f82d702d6b 100644 --- a/Documentation/git-cat-file.txt +++ b/Documentation/git-cat-file.txt @@ -45,7 +45,9 @@ OPTIONS -s:: Instead of the content, show the object size identified by - `<object>`. + `<object>`. If used with `--use-mailmap` option, will show + the size of updated object after replacing idents using the + mailmap mechanism. -e:: Exit with zero status if `<object>` exists and is a valid diff --git a/builtin/cat-file.c b/builtin/cat-file.c index fa7bd89169..8a6e2343ec 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -132,8 +132,21 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name, case 's': oi.sizep = &size; + + if (use_mailmap) { + oi.typep = &type; + oi.contentp = (void**)&buf; + } + if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0) die("git cat-file: could not get object info"); + + if (use_mailmap && (type == OBJ_COMMIT || type == OBJ_TAG)) { + size_t s = size; + buf = replace_idents_using_mailmap(buf, &s); + size = cast_size_t_to_ulong(s); + } + printf("%"PRIuMAX"\n", (uintmax_t)size); ret = 0; goto cleanup; diff --git a/t/t4203-mailmap.sh b/t/t4203-mailmap.sh index cd1cab3e54..87b77fc5c9 100755 --- a/t/t4203-mailmap.sh +++ b/t/t4203-mailmap.sh @@ -1022,4 +1022,29 @@ test_expect_success '--mailmap enables mailmap in cat-file for annotated tag obj test_cmp expect actual ' +test_expect_success 'git cat-file -s returns correct size with --use-mailmap' ' + test_when_finished "rm .mailmap" && + cat >.mailmap <<-\EOF && + C O Mitter <committer@example.com> Orig <orig@example.com> + EOF + git cat-file commit HEAD | wc -c >expect && + git cat-file --use-mailmap commit HEAD | wc -c >>expect && + git cat-file -s HEAD >actual && + git cat-file --use-mailmap -s HEAD >>actual && + test_cmp expect actual +' + +test_expect_success 'git cat-file -s returns correct size with --use-mailmap for tag objects' ' + test_when_finished "rm .mailmap" && + cat >.mailmap <<-\EOF && + Orig <orig@example.com> C O Mitter <committer@example.com> + EOF + git tag -a -m "annotated tag" v3 && + git cat-file tag v3 | wc -c >expect && + git cat-file --use-mailmap tag v3 | wc -c >>expect && + git cat-file -s v3 >actual && + git cat-file --use-mailmap -s v3 >>actual && + test_cmp expect actual +' + test_done
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> --- Documentation/git-cat-file.txt | 4 +++- builtin/cat-file.c | 13 +++++++++++++ t/t4203-mailmap.sh | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-)