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

Re: [PATCH 1/3] vimdiff: new implementation with layout support

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Nov 8, 2021, 00:47 UTC
Message-ID
<CAPig+cRiMVzMd5yEZkRvwdAjf-tawGGrSsCBUAVs+pTTw5KunA@mail.gmail.com>
In-Reply-To
<CAJDDKr5KZK3wpox6FJPOL9koPr=k5+qTzZtPPFEmg4iZsvwoog@mail.gmail.com>
On Sun, Nov 7, 2021 at 5:42 PM David Aguilar <davvid@gmail.com> wrote:
Show 7 quoted lines
> On Thu, Nov 4, 2021 at 9:10 AM Fernando Ramos <greenfoo@u92.eu> wrote:
> > +       AUX=$(echo "$LAYOUT" | grep -oe "[A-Z]\+\*")
>
> From Documentatin/CodingGuidelines:
>
> As to use of grep, stick to a subset of BRE (namely, no {m,n},
>    [::], [==], or [..]) for portability.
Also, `grep -o` isn't POSIX.
Show 15 quoted lines
> > +       if test $(echo $LAYOUT | wc -w) == "1"
> > +       then
> > +               CMD="$CMD | bufdo diffthis"
> > +        else
> > +               CMD="$CMD | tabdo windo diffthis"
> > +       fi
>
> The output of "wc -c" is non-portable. It contains leading whitespace
> on some platforms.
>
> The test expression should be:
>
>    test "$value" = 1
>
> with a single "=" rather than "==".

For clarification, the leading whitespace emitted by some `wc` implementations is only a problem when encapsulated in a string. For instance, like this:

    if test "$(... | wc -w)" = "1"

in which case " 1" won't equal "1". The usage here, however, should be okay since the output is not quoted.

Quite correct about using "=" (or even "-eq") here rather than "==", though.
Show 16 quoted lines
> > +       if $base_present
> > +       then
> > +               eval "$merge_tool_path" \
> > +                       -f $FINAL_CMD "$LOCAL" "$BASE" "$REMOTE" "$MERGED"
> > +       else
> > +               [...]
> > +               eval "$merge_tool_path" \
> > +                       -f $FINAL_CMD "$LOCAL" "$REMOTE" "$MERGED"
> > +       fi
> > +
> > +       ret="$?"
> > +       if test "$ret" -eq 0
>
> This should be:
>
>     if test "$ret" = 0
Or simpler, no need for `ret` at all:
    if test $? -eq 0
(or `if test $? = 0` -- either works)

Another (perhaps better) alternative is to assign the result of `eval` to `ret` at the point of invocation, which lessens the cognitive load a bit since you don't have to scan backward through the code trying to figure out what $? refers to.

Also, why is `eval` needed here? Is there something non-obvious going on? (Genuine question; I didn't trace the code thoroughly to understand.)

Show 13 quoted lines
> > +                       eval cp -- \$"$FINAL_TARGET" "$MERGED"
>
> This eval may not be safe when the value contains whitespace or shell
> metacharacters.
>
> I think it might be better to just spell it out and be explicit.
>
> It's more code but it'll be easier to follow:
> [...]
> if test -n "$source_path"
> then
>     cp -- "$source_path" "$MERGED"
> fi
I suspect `--` also needs to be avoided since it is not POSIX.
Previous: David AguilarNext: Fernando Ramos
Message 4 of 9 in “vimdiff: new layout option + docs”
  1. 0/3 vimdiff: new layout option + docsFernando Ramos, Nov 4, 2021
  2. 1/3 vimdiff: new implementation with layout supportFernando Ramos, Nov 4, 2021
  3. David AguilarNov 7, 2021
  4. Eric SunshineNov 8, 2021
  5. 2/3 vimdiff: add tool documentationFernando Ramos, Nov 4, 2021
  6. David AguilarNov 7, 2021
  7. Eric SunshineNov 8, 2021
  8. Junio C HamanoNov 8, 2021
  9. 3/3 vimdiff: remove deprecated {,g,n}vimdiff{1,2,3} variantsFernando Ramos, Nov 4, 2021

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.