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

Re: [PATCH/RFC 1/2] git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Apr 3, 2011, 15:25 UTC
Message-ID
<BANLkTikf0s=tyGSnmgf1zmRAe3WnzB4JUA@mail.gmail.com>
In-Reply-To
<7vpqq99i11.fsf@alter.siamese.dyndns.org>
On Thu, Mar 3, 2011 at 01:14, Junio C Hamano <gitster@pobox.com> wrote:
Show 55 quoted lines
> Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
>
>> In a previous incarnation of the gettext series I implemented the
>> eval_gettext() fallback like this:
>>
>>     eval_gettext() {
>>         gettext_out=$(gettext "$1")
>>         gettext_eval="printf '%s' \"$gettext_out\""
>>         printf "%s" "`eval \"$gettext_eval\"`"
>>     }
>>
>> This was clever, but would incorrectly handle cases where the variable
>> being interpolated contained spaces. E.g.:
>>
>>     cmd="git foo"; eval_gettext "command: \$cmd"
>>
>> Would emit "command: gitfoo", instead of the correct "command: git
>> foo".
>
> Hmm, are you sure you got your quoting right?
>
>    $ cat >1.sh <<\EOF
>    #!/bin/sh
>    gettext () {
>            case "$1" in
>            'command: $cmd') echo 'dcomman: $cmd' ;;
>            *) echo "GETTEXT POISON" ;;
>            esac
>    }
>
>    eval_gettext() {
>            gettext_out=$(gettext "$1") &&
>            gettext_eval="printf '%s' \"$gettext_out\"" &&
>            gettext_cmd=$(eval "$gettext_eval") &&
>            printf "%s" "$gettext_cmd"
>    }
>
>    cmd="git foo"
>    eval_gettext "command: \$cmd"
>    EOF
>    $ sh -x 1.sh
>    + cmd='git foo'
>    + eval_gettext 'command: $cmd'
>    ++ gettext 'command: $cmd'
>    ++ case "$1" in
>    ++ echo 'dcomman: $cmd'
>    + gettext_out='dcomman: $cmd'
>    + gettext_eval='printf '\''%s'\'' "dcomman: $cmd"'
>    ++ eval 'printf '\''%s'\'' "dcomman: $cmd"'
>    +++ printf %s 'dcomman: git foo'
>    + gettext_cmd='dcomman: git foo'
>    + printf %s 'dcomman: git foo'
>    dcomman: git foo
>
> Am I grossly missing something from what you are trying to do here?

(CC-ing the list again, so there's a record of this in the mail archive).

Your eval_gettext() is better, but it still fails in cases where the string contains "'s. E.g.:

    test_expect_success C_LOCALE_OUTPUT 'eval_gettext: our
eval_gettext() fallback can interpolate variables with spaces in them'
'
        cmdline="git am" &&
        export cmdline;
        printf "When you have resolved this problem run \"git am
--resolved\"." >expect &&
        eval_gettext "When you have resolved this problem run
\"\$cmdline --resolved\"." >actual
        test_cmp expect actual
    '
If this were just:
    test_expect_success C_LOCALE_OUTPUT 'eval_gettext: our
eval_gettext() fallback can interpolate variables with spaces in them'
'
        cmdline="git am" &&
        export cmdline;
        printf "When you have resolved this problem run git am
--resolved." >expect &&
        eval_gettext "When you have resolved this problem run
\$cmdline --resolved." >actual
        test_cmp expect actual
    '
Your version would work.

Maybe there's some portable version of doing this that works with variables with spaces mixed with quotes. But unless someone can point it out I'll submit a series with git-sh-i18n--envsubst and tests demonstrating why it's needed soon.q

Previous: Ævar Arnfjörð Bjarmason
Message 4 of 4 in “no-op shell script i18n infrastructure”
  1. 0/2 no-op shell script i18n infrastructureÆvar Arnfjörð Bjarmason, Mar 1, 2011
  2. 1/2 git-sh-i18n--envsubst: our own envsubst(1) for eval_gettext()Ævar Arnfjörð Bjarmason, Mar 1, 2011
  3. 2/2 git-sh-i18n.sh: add no-op gettext() and eval_gettext() wrappersÆvar Arnfjörð Bjarmason, Mar 1, 2011
  4. Ævar Arnfjörð BjarmasonApr 3, 2011

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.