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

Re: [PATCH v3 1/5] CodingGuidelines: add shell piping guidelines

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Sep 24, 2018, 21:03 UTC
Message-ID
<20180924210314.GE27036@localhost>
In-Reply-To
<7dd9a1120726dee34aeebdfa4bf45a232c185500.1537493611.git.matvore@google.com>
On Thu, Sep 20, 2018 at 06:43:27PM -0700, Matthew DeVore wrote:
Show 36 quoted lines
> Add two guidelines:
> 
>  - pipe characters should appear at the end of lines, and not cause
>    indentation
>  - pipes should be avoided when they swallow exit codes that can
>    potentially fail
> ---
>  Documentation/CodingGuidelines | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines
> index 48aa4edfb..6d265327c 100644
> --- a/Documentation/CodingGuidelines
> +++ b/Documentation/CodingGuidelines
> @@ -118,6 +118,24 @@ For shell scripts specifically (not exhaustive):
>  		do this
>  	fi
>  
> + - If a command sequence joined with && or || or | spans multiple
> +   lines, put each command on a separate line and put && and || and |
> +   operators at the end of each line, rather than the start. This
> +   means you don't need to use \ to join lines, since the above
> +   operators imply the sequence isn't finished.
> +
> +	(incorrect)
> +	grep blob verify_pack_result \
> +	| awk -f print_1.awk \
> +	| sort >actual &&
> +	...
> +
> +	(correct)
> +	grep blob verify_pack_result |
> +	awk -f print_1.awk |
> +	sort >actual &&
> +	...
> +

The above are general shell coding style guidelines, so it makes sense to include them in 'Documentation/CodingGuidelines'.

Show 8 quoted lines
>   - We prefer "test" over "[ ... ]".
>  
>   - We do not write the noiseword "function" in front of shell
> @@ -163,6 +181,15 @@ For shell scripts specifically (not exhaustive):
>  
>     does not have such a problem.
>  
> + - In a piped chain such as "grep blob objects | sort", the exit codes
Let's make an example with git in it, e.g. something like this:
  git cmd | grep important | sort
since just two lines below the new text mentions git crashing.
Show 6 quoted lines
> +   returned by processes besides the last are ignored. This means that
> +   if git crashes at the beginning or middle of a chain, it may go
> +   undetected. Prefer writing the output of that command to a
> +   temporary file with '>' rather than pipe it.
> +
> + - The $(git ...) construct also discards git's exit code, so if the

This contruct is called command substitution, and it does preserve the command's exit code, when the expanded text is assigned to a variable:

  $ var=$(exit 42) ; echo $?
  42

Note, however, that even in that case only the exit code of the last command substitution is preserved:

  $ var=$(exit 1)foo$(exit 2)bar$(exit 3) ; echo $?
  3
> +   goal is to test that particular command, redirect its output to a
> +   temporary file rather than wrap it with $( ).

I find this a bit vague, and to me it implies that ignoring the exit code of a git command that is not the main focus of the given test is acceptable, e.g. (made up pseudo example):

  test_expect_success 'fetch gets what it should' '
    git fetch $remote &&
    test "$(git rev-parse just-fetched)" = $expected_oid
  '

In my opinion no tests should ignore the exit code of any git command, ever.

These last two points, however, are specific to test scripts, therefore I think they would be better placed in 't/README', where the rest of the test-specific guidelines are.

Show 5 quoted lines
>  For C programs:
>  
> -- 
> 2.19.0.444.g18242da7ef-goog
> 
Previous: Matthew DeVoreNext: Matthew DeVore
Message 16 of 66 in “Cleanup tests for test_cmp argument ordering and "|" placement”
  1. 0/2 Cleanup tests for test_cmp argument ordering and "|" placementMatthew DeVore, Sep 15, 2018
  2. 1/2 t/*: fix pipe placement and remove \'sMatthew DeVore, Sep 15, 2018
  3. Jonathan NiederSep 17, 2018
  4. Matthew DeVoreSep 17, 2018
  5. 2/2 t/*: fix ordering of expected/observed argumentsMatthew DeVore, Sep 15, 2018
  6. Matthew DeVoreSep 17, 2018
  7. Junio C HamanoSep 15, 2018
  8. 0/6 Clean up tests for test_cmp arg ordering and pipe placementMatthew DeVore, Sep 17, 2018
  9. 4/6 tests: Add linter check for pipe placement styleMatthew DeVore, Sep 17, 2018
  10. Eric SunshineSep 18, 2018
  11. Matthew DeVoreSep 19, 2018
  12. 0/5 Clean up tests for test_cmp arg ordering and pipe placementMatthew DeVore, Sep 21, 2018
  13. 1/5 CodingGuidelines: add shell piping guidelinesMatthew DeVore, Sep 21, 2018
  14. Eric SunshineSep 21, 2018
  15. Matthew DeVoreSep 21, 2018
  16. SZEDER GáborSep 24, 2018
  17. Matthew DeVoreSep 25, 2018
  18. SZEDER GáborSep 27, 2018
  19. Matthew DeVoreOct 1, 2018
  20. 2/5 tests: standardize pipe placementMatthew DeVore, Sep 21, 2018
  21. 3/5 t/*: fix ordering of expected/observed argumentsMatthew DeVore, Sep 21, 2018
  22. 4/5 tests: don't swallow Git errors upstream of pipesMatthew DeVore, Sep 21, 2018
  23. 5/5 t9109: don't swallow Git errors upstream of pipesMatthew DeVore, Sep 21, 2018
  24. 0/7 Clean up tests for test_cmp arg ordering and pipe placementMatthew DeVore, Oct 3, 2018
  25. 1/7 t/README: reformat Do, Don't, Keep in mind listsMatthew DeVore, Oct 3, 2018
  26. Junio C HamanoOct 5, 2018
  27. Matthew DeVoreOct 5, 2018
  28. 2/7 Documentation: add shell guidelinesMatthew DeVore, Oct 3, 2018
  29. Junio C HamanoOct 5, 2018
  30. Matthew DeVoreOct 5, 2018
  31. 3/7 tests: standardize pipe placementMatthew DeVore, Oct 3, 2018
  32. 4/7 t/*: fix ordering of expected/observed argumentsMatthew DeVore, Oct 3, 2018
  33. 5/7 tests: don't swallow Git errors upstream of pipesMatthew DeVore, Oct 3, 2018
  34. Junio C HamanoOct 5, 2018
  35. Matthew DeVoreOct 5, 2018
  36. Matthew DeVoreOct 5, 2018
  37. 6/7 t9109: don't swallow Git errors upstream of pipesMatthew DeVore, Oct 3, 2018
  38. 7/7 tests: order arguments to git-rev-list properlyMatthew DeVore, Oct 3, 2018
  39. Matthew DeVoreOct 3, 2018
  40. Junio C HamanoOct 5, 2018
  41. 0/7 subject: Clean up tests for test_cmp arg ordering and pipe placementMatthew DeVore, Oct 5, 2018
  42. 1/7 t/README: reformat Do, Don't, Keep in mind listsMatthew DeVore, Oct 5, 2018
  43. 2/7 Documentation: add shell guidelinesMatthew DeVore, Oct 5, 2018
  44. 3/7 tests: standardize pipe placementMatthew DeVore, Oct 5, 2018
  45. 4/7 t/*: fix ordering of expected/observed argumentsMatthew DeVore, Oct 5, 2018
  46. 5/7 tests: don't swallow Git errors upstream of pipesMatthew DeVore, Oct 5, 2018
  47. 6/7 t9109: don't swallow Git errors upstream of pipesMatthew DeVore, Oct 5, 2018
  48. 7/7 tests: order arguments to git-rev-list properlyMatthew DeVore, Oct 5, 2018
  49. Junio C HamanoOct 6, 2018
  50. 1/6 CodingGuidelines: add shell piping guidelinesMatthew DeVore, Sep 17, 2018
  51. Eric SunshineSep 18, 2018
  52. Matthew DeVoreSep 19, 2018
  53. Eric SunshineSep 19, 2018
  54. Junio C HamanoSep 19, 2018
  55. Matthew DeVoreSep 19, 2018
  56. 2/6 tests: standardize pipe placementMatthew DeVore, Sep 17, 2018
  57. 3/6 t/*: fix ordering of expected/observed argumentsMatthew DeVore, Sep 17, 2018
  58. 4/6 tests: add linter check for pipe placement styleMatthew DeVore, Sep 17, 2018
  59. 5/6 tests: split up pipesMatthew DeVore, Sep 17, 2018
  60. Eric SunshineSep 18, 2018
  61. Matthew DeVoreSep 19, 2018
  62. 6/6 t9109-git-svn-props.sh: split up several pipesMatthew DeVore, Sep 17, 2018
  63. Eric SunshineSep 18, 2018
  64. Matthew DeVoreSep 19, 2018
  65. Eric SunshineSep 19, 2018
  66. Matthew DeVoreSep 19, 2018

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.