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

Re: [GSoC][PATCH v3 2/3] t0000: avoid using pipes

From
JCjonathan chang <ttjtftx@gmail.com>
Date
Mar 24, 2019, 11:26 UTC
Message-ID
<CAOAu_YJs_ZL0nAARNmdSFjmdh6mSP+e7KvzNB3wj-JGe5sHcdA@mail.gmail.com>
In-Reply-To
<87imwha1o3.fsf@evledraar.gmail.com>

On Mon, Mar 18, 2019 at 12:47 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:

Show 34 quoted lines
>
>
> On Sun, Mar 17 2019, Jonathan Chang wrote:
>
> > The exit code of the upstream in a pipe is ignored thus we should avoid
> > using it. By writing out the output of the git command to a file, we can
> > test the exit codes of both the commands.
> >
> > Signed-off-by: Jonathan Chang <ttjtftx@gmail.com>
> > ---
> >  t/t0000-basic.sh | 28 ++++++++++++++--------------
> >  1 file changed, 14 insertions(+), 14 deletions(-)
> >
> > diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
> > index 53821f5817..47666b013e 100755
> > --- a/t/t0000-basic.sh
> > +++ b/t/t0000-basic.sh
> > @@ -1118,27 +1118,25 @@ P=$(test_oid root)
> >
> >  test_expect_success 'git commit-tree records the correct tree in a commit' '
> >       commit0=$(echo NO | git commit-tree $P) &&
> > -     tree=$(git show --pretty=raw $commit0 |
> > -              sed -n -e "s/^tree //p" -e "/^author /q") &&
> > +     git show --pretty=raw $commit0 >actual &&
> > +     tree=$(sed -n -e "s/^tree //p" -e "/^author /q" actual) &&
> >       test "z$tree" = "z$P"
>
> This change is an improvement just changing the "git" invocations. But I
> wonder as we're reviewing this / churning this if we couldn't also
> modernize this style to just:
>
>     git .. >tmp &&
>     sed -n -e <tmp >actual &&
>     test_must_be_empty actual
Do you mean something like this:
-       git show --pretty=raw $commit0 >actual &&
-       tree=$(sed -n -e "s/^tree //p" -e "/^author /q" actual) &&
-       test "z$tree" = "z$P"
+       git show --pretty=raw $commit0 >tmp &&
+       sed -n -e "/$P/d" -e "s/^tree //p" -e "/^author /q" tmp >actual &&
+       test_must_be_empty actual

It works. But the semantic is different if we use test_must_be_empty. I wonder if you mean test_cmp because I found some commits[1 2 3] that changes 'test "z...' to use test_cmp with 'git log -G 'test "z' --oneline' and git-show. However, they are all around 2013, so I'm not so sure this is what you mean either.

I did found some use of sed's 'd' function in conjunction with
test_must_be_empty, using:
  git grep -A 5 'sed .*/d' | grep -B 5 'test_must_be_empty'
However, they don't use parameter expansion in sed.

There are some places where parameter expansion is used in sed, but that would require test_cmp in this case, and would need to write to another file to compare.

Maybe this 'test "z$A" = "z$B"' syntax is fine, yet most of the existing usages are added around 12 years ago according git-blame I saw on github.

[1]: 03c893cbf9 ("t1006: modernize output comparisons", 2013-07-10) [2]: 848575d833 ("push test: simplify check of push result", 2013-03-18) [3]: ed838e6615 ("t1300: style updates", 2012-10-23)

Show 18 quoted lines
> > @@ -1162,12 +1161,13 @@ test_expect_success 'very long name in the index handled sanely' '
> >       >path4 &&
> >       git update-index --add path4 &&
> >       (
> > -             git ls-files -s path4 |
> > -             sed -e "s/      .*/     /" |
> > +             git ls-files -s path4 >actual &&
> > +             sed -e "s/      .*/     /" actual |
> >               tr -d "\012" &&
> >               echo "$a"
> >       ) | git update-index --index-info &&
> > -     len=$(git ls-files "a*" | wc -c) &&
> > +     git ls-files "a*" >actual &&
> > +     len=$(wc -c <actual) &&
> >       test $len = 4098
>
> Ditto. Maybe the initial author wanted to avoid writing out 4k lines,
> but now that we're doing so anyway...
This is 'wc -c', so I think I don't have to modify it?

By the way, I found 2 lines that can be changed to use test_must_be_empty in t0000-basic.sh. I paste the diff below: ---

@@ -51,7 +51,7 @@ test_expect_success 'verify that the running shell
supports "local"' '

 test_expect_success '.git/objects should be empty after git init in
an empty repo' '
        find .git/objects -type f -print >should-be-empty &&
-       test_line_count = 0 should-be-empty
+       test_must_be_empty should-be-empty
 '

 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
@@ -1110,7 +1110,7 @@ test_expect_success 'git update-index --refresh
should succeed' '

 test_expect_success 'no diff after checkout and git update-index --refresh' '
        git diff-files >current &&
-       cmp -s current /dev/null
+       test_must_be_empty current
 '
Previous: Ævar Arnfjörð BjarmasonNext: Ævar Arnfjörð Bjarmason
Message 5 of 11 in “Avoid using pipes”
  1. Jonathan ChangMar 17, 2019
  2. [GSoC][PATCH v3 1/3] t0000: fix indentationJonathan Chang, Mar 17, 2019
  3. [GSoC][PATCH v3 2/3] t0000: avoid using pipesJonathan Chang, Mar 17, 2019
  4. Ævar Arnfjörð BjarmasonMar 17, 2019
  5. jonathan changMar 24, 2019
  6. Ævar Arnfjörð BjarmasonMar 24, 2019
  7. [GSoC][PATCH v3 3/3] t0000: use test_line_count instead of wc -lJonathan Chang, Mar 17, 2019
  8. Ævar Arnfjörð BjarmasonMar 17, 2019
  9. Thomas GummererMar 17, 2019
  10. Ævar Arnfjörð BjarmasonMar 18, 2019
  11. Junio C HamanoMar 18, 2019

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.