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

Re: [PATCH] git-prompt: fix reading files with windows line endings

From
Robert Abel <rabel@robertabel.eu>
Date
Nov 30, 2017, 06:22 UTC
Message-ID
<cacbf41e-3b4a-99e2-a0e0-50bb4cd9e152@robertabel.eu>
In-Reply-To
<alpine.DEB.2.21.1.1711300100320.6482@virtualbox>
Hi Johannes,
On 30 Nov 2017 01:21, Johannes Schindelin wrote:
Show 6 quoted lines
> On Wed, 29 Nov 2017, Robert Abel wrote:
>> This means that it should be okay to just do
>>
>>> test -r "$f" && IFS=" \t\r\n" read "$@" < "$f"
> 
> I am afraid that this won't work: when I call

I managed to trick myself with that one, yes... Apparently I had already converted my HEAD back to Unix line endings.

However, I noticed that the behavior of read is apparently ambiguous for the last (or a single) variable:

From POSIX.1-2008:
Show 7 quoted lines
> If there are fewer vars than fields, the last var shall be set to a
> value comprising the following elements:
> - The field that corresponds to the last var in the normal assignment
>   sequence described above
> - The delimiter(s) that follow the field corresponding to the last var
> - The remaining fields and their delimiters, with trailing IFS white
>   space ignored

I read that last "ignored" as "trailing IFS white space shall not be appended". Apparently, people implementing read read it as "trailing IFS while space shall not be processed further"

Thus, the behavior for trailing IFS white space is different in case of one or two variables:

    printf '123 456\r\n' | while IFS=$' \t\r\n' read foo bar
    do
        printf 'foo: %s' "$foo" | hexdump -C
        printf 'bar: %s' "$bar" | hexdump -C
    done
This works as expected trimming the trailing \r:
    00000000  66 6f 6f 3a 20 31 32 33                           |foo: 123|
    00000008
    00000000  62 61 72 3a 20 34 35 36                           |bar: 456|
    00000008
While doing the same just reading a single variable
    printf '123 456\r\n' | while IFS=$' \t\r\n' read foo
    do
        printf 'foo: %s' "$foo" | hexdump -C
        printf 'bar: %s' "$bar" | hexdump -C
    done
prints
    00000000  66 6f 6f 3a 20 31 32 33  20 34 35 36 0d           |foo:
123 456.|
    0000000d
    00000000  62 61 72 3a 20                                    |bar: |
    00000005
Notice the 0d at the end of foo, which didn't get trimmed.

So reading a dummy variable along with the actual content variable works for git-prompt:

    __git_eread ()
    {
        local f="$1"
        local dummy
        shift
        test -r "$f" && IFS=$'\r\n' read "$@" dummy < "$f"
    }
I feel like this would be the most readable solution thus far.
Regards,
Robert
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 6 of 25 in “git-prompt: fix reading files with windows line endings”
  1. Robert AbelNov 28, 2017
  2. git-prompt: fix reading files with windows line endingsRobert Abel, Nov 28, 2017
  3. Johannes SchindelinNov 29, 2017
  4. Robert AbelNov 29, 2017
  5. Johannes SchindelinNov 30, 2017
  6. Robert AbelNov 30, 2017
  7. Johannes SchindelinNov 30, 2017
  8. Robert AbelNov 30, 2017
  9. Johannes SchindelinDec 1, 2017
  10. 1/2 git-prompt: make __git_eread intended use explicitRobert Abel, Dec 1, 2017
  11. 2/2 git-prompt: fix reading files with windows line endingsRobert Abel, Dec 1, 2017
  12. Johannes SchindelinDec 4, 2017
  13. Junio C HamanoDec 4, 2017
  14. Robert AbelDec 4, 2017
  15. Junio C HamanoDec 5, 2017
  16. Robert AbelDec 5, 2017
  17. Junio C HamanoDec 5, 2017
  18. Robert AbelDec 5, 2017
  19. 1/2 git-prompt: make __git_eread intended use explicitRobert Abel, Dec 5, 2017
  20. 2/2 git-prompt: fix reading files with windows line endingsRobert Abel, Dec 5, 2017
  21. 1/2 git-prompt: make __git_eread intended use explicitRobert Abel, Dec 4, 2017
  22. 2/2 git-prompt: fix reading files with windows line endingsRobert Abel, Dec 4, 2017
  23. SZEDER GáborNov 30, 2017
  24. Johannes SchindelinNov 30, 2017
  25. SZEDER GáborNov 30, 2017

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.