stgit icon indicating copy to clipboard operation
stgit copied to clipboard

Alias expansion discards quoting, resulting in unexpected errors

Open larsks opened this issue 2 years ago • 2 comments

This comes from the mailing list. Given an alias like this:

[stgit "alias"]
        ra = edit --author "Test User <[email protected]>"

stgit will interpret this as:

stg edit --author Test User <[email protected]>

Resulting in the error:

error: Found argument '<[email protected]>' which wasn't expected, or isn't valid in this context

The workaround is to double-quote the quoted strings:

[stgit "alias"]
        ra = edit --author '"Test User <[email protected]>"'

This works as intended, but it shouldn't be necessary.

larsks avatar Jul 23 '22 23:07 larsks

The problem appears to be in src/alias.rs, where an Alias is defined as:

pub(crate) struct Alias {
    pub kind: AliasKind,
    pub name: String,
    pub command: String,
}

If we're going to preserve quoted strings, command should be a list of strings. It looks as if the split_command_line method does the right thing ("Single- and double-quoted substrings are preserved."), but I don't see where this is used.

larsks avatar Jul 23 '22 23:07 larsks

I believe this is working correctly. The quoting rules within a git config file are a bit tricky. Unless the double-quotes are escaped as shown below (or captured within single quotes as shown above), StGit does not see the quote characters in the raw string from the config file (as returned by libgit2).

To setup this alias correctly from the command line, we would run:

$ git config stgit.alias.ra 'edit --author "Test User <[email protected]>"'

which would give us a config file with:

[stgit "alias"]
        ra = edit --author \"Test User <[email protected]>\"

This is all consistent with how git handles its aliases. Consider the following config:

[alias]
        commit-default1 = commit --message "Default Message"
        commit-default2 = commit --message \"Default Message\"

The commit-default1 alias uses the problematic quoting strategy and leads to the same problem as the original StGit ra alias from the issue description:

$ git commit-default1
error: pathspec 'Message' did not match any file(s) known to git

But with the double-quotes escaped properly, it works as intended:

$ git commit-default2
[master cd99cfc] Default Message
 Author: A Ú Thor <[email protected]>
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

For completeness, here is how the commit-default2 alias would be setup from the command line:

$ git config alias.commit-default2 'commit --message "Default Message"'

jpgrayson avatar Jul 24 '22 22:07 jpgrayson