git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 4/1] t3920: replace two cats with a tee

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Dec 3, 2022, 12:53 UTC
Message-ID
<221203.86lenoeipb.gmgdl@evledraar.gmail.com>
In-Reply-To
<4611a23a-e7e9-6039-5d54-c2f8cadc2f73@web.de>
On Sat, Dec 03 2022, René Scharfe wrote:
Show 27 quoted lines
> Am 03.12.22 um 06:09 schrieb Eric Sunshine:
>> On Fri, Dec 2, 2022 at 11:51 AM René Scharfe <l.s.r@web.de> wrote:
>>> Use tee(1) to replace two calls of cat(1) for writing files with
>>> different line endings.  That's shorter and spawns less processes.
>>> [...]
>>> Signed-off-by: René Scharfe <l.s.r@web.de>
>>> ---
>>> diff --git a/t/t3920-crlf-messages.sh b/t/t3920-crlf-messages.sh
>>> @@ -9,8 +9,7 @@ LIB_CRLF_BRANCHES=""
>>>  create_crlf_ref () {
>>> -       cat >.crlf-orig-$branch.txt &&
>>> -       cat .crlf-orig-$branch.txt | append_cr >.crlf-message-$branch.txt &&
>>> +       tee .crlf-orig-$branch.txt | append_cr >.crlf-message-$branch.txt &&
>>
>> This feels slightly magical and more difficult to reason about than
>> using simple redirection to eliminate the second `cat`. Wouldn't this
>> work just as well?
>>
>>     cat >.crlf-orig-$branch.txt &&
>>     append_cr <.crlf-orig-$branch.txt >.crlf-message-$branch.txt &&
>
> It would work, of course, but this is the exact use case for tee(1).  No
> repetition, no extra redirection symbols, just an nicely fitting piece
> of pipework.  Don't fear the tee! ;-)
>
> (I'm delighted to learn from https://en.wikipedia.org/wiki/Tee_(command)
> that PowerShell has a tee command as well.)

I don't really care, but I must say I agree with Eric here. Not having surprising patterns in the test suite has a value of its own.

In this case I wonder if you want to optimize this whether we couldn't do much better with "test_commit_bulk", maybe by teaching it a small set of new tricks.

I.e. if I do:
	git fast-export --all

At the end of the setup test it seems we just end up with refs with names that correspond to their contents, and with double newlines in them or whatever. This is a lot of "grep", "sed", "tr" etc. just to end up with that.

So maybe we can create them as a patch, possibly with some slight "sed" munging on the input stream, just just teach it to accept a "ref prefix" and "commit message contents". That could just be an argument that you "$(printf "...")", so we don't even need a sub-process....

Also this:
     perl -wE 'say for 1..1024*100' | tee /tmp/x | perl -nE 'print "in: $_"; exit 1 if $_ == 512'; tail -n 1 /tmp/x

Isn't deterministic. Now, in this case I doubt it matters, but it's nice to have intermediate files in the test suite be determanistic, i.e. to always have the full content be in the file at the top after the "top".

With a "tee" you need to worry about the "append_cr" function it's being piped in stopping the stdin.

I don't think it matters in this case, but in general as a pattern: I do fear the "tee" a bit :)

Previous: René ScharfeNext: René Scharfe
Message 22 of 25 in “t3920: don't ignore errors of more than one command with `|| true`”
  1. t3920: don't ignore errors of more than one command with `|| true`Johannes Sixt, Nov 21, 2022
  2. René ScharfeNov 21, 2022
  3. Junio C HamanoNov 22, 2022
  4. Philippe BlainNov 22, 2022
  5. Ævar Arnfjörð BjarmasonNov 22, 2022
  6. Johannes SixtNov 22, 2022
  7. Ævar Arnfjörð BjarmasonNov 22, 2022
  8. Junio C HamanoNov 23, 2022
  9. 2/1 t3920: support CR-eating grepRené Scharfe, Dec 2, 2022
  10. Philippe BlainDec 2, 2022
  11. René ScharfeDec 3, 2022
  12. Eric SunshineDec 2, 2022
  13. René ScharfeDec 3, 2022
  14. Junio C HamanoDec 5, 2022
  15. René ScharfeDec 5, 2022
  16. Junio C HamanoDec 5, 2022
  17. René ScharfeDec 5, 2022
  18. 3/1 t3920: simplify redirection of loop outputRené Scharfe, Dec 2, 2022
  19. 4/1 t3920: replace two cats with a teeRené Scharfe, Dec 2, 2022
  20. Eric SunshineDec 3, 2022
  21. René ScharfeDec 3, 2022
  22. Ævar Arnfjörð BjarmasonDec 3, 2022
  23. René ScharfeDec 3, 2022
  24. Ævar Arnfjörð BjarmasonDec 4, 2022
  25. Eric SunshineDec 4, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.