Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"io"
"os"
"os/exec"
"slices"
"strings"
"time"

goversion "github.com/mcuadros/go-version"
)

// Command contains the name, arguments and environment variables of a command.
Expand Down Expand Up @@ -206,7 +209,12 @@ func (c *Command) RunInDirWithOptions(dir string, opts ...RunInDirOptions) (err
}()
}

cmd := exec.CommandContext(ctx, c.name, c.args...)
args, err := ArgsForBinVersion(c.args)
if err != nil {
return err
}

cmd := exec.CommandContext(ctx, c.name, args...)
if len(c.envs) > 0 {
cmd.Env = append(os.Environ(), c.envs...)
}
Expand Down Expand Up @@ -308,3 +316,44 @@ func (c *Command) Run() ([]byte, error) {
}
return stdout, nil
}

// endOfOptions makes Git treat every following argument as a positional
// argument, even when it starts with a dash.
const endOfOptions = "--end-of-options"

// ArgsForBinVersion returns the Git arguments to run with the Git binary in
// use. See argsForVersion for how "--end-of-options" is handled. Callers that
// run Git without this package should pass their arguments through it.
func ArgsForBinVersion(args []string) ([]string, error) {
i := slices.Index(args, endOfOptions)
if i < 0 {
return args, nil
}
version, err := BinVersion()
if err != nil {
return nil, fmt.Errorf("get Git version: %v", err)
}
return argsForVersion(args, i, version)
}

// argsForVersion returns the arguments to run with the given Git version, where
// args[i] is endOfOptions. Git 2.24 introduced endOfOptions, but some commands,
// e.g., checkout, reset and grep, only accept it since Git 2.43.1. For older
// versions it drops endOfOptions and refuses positional arguments that Git would
// take as options, i.e., those starting with a dash before any "--".
func argsForVersion(args []string, i int, version string) ([]string, error) {
if goversion.Compare(version, "2.43.1", ">=") {
return args, nil
}

positional := args[i+1:]
for _, arg := range positional {
if arg == "--" {
break
}
if strings.HasPrefix(arg, "-") {
return nil, fmt.Errorf("argument %q starts with a dash, which Git %s would take as an option", arg, version)
}
}
return append(append(make([]string, 0, len(args)-1), args[:i]...), positional...), nil
}
59 changes: 59 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package git

import (
"slices"
"testing"
"time"

Expand Down Expand Up @@ -63,3 +64,61 @@ func TestCommand_RunWithTimeout(t *testing.T) {
_, err := NewCommand("version").WithTimeout(time.Nanosecond).Run()
assert.Equal(t, ErrExecTimeout, err)
}

func TestArgsForVersion(t *testing.T) {
tests := []struct {
name string
args []string
version string
want []string
wantErr bool
}{
{
name: "kept when Git accepts it for every command",
args: []string{"checkout", "--end-of-options", "main"},
version: "2.43.1",
want: []string{"checkout", "--end-of-options", "main"},
},
{
name: "kept on a newer Windows build",
args: []string{"reset", "--hard", "--end-of-options", "HEAD"},
version: "2.55.0.windows.1",
want: []string{"reset", "--hard", "--end-of-options", "HEAD"},
},
{
name: "dropped before 2.43.1",
args: []string{"reset", "--hard", "--end-of-options", "HEAD"},
version: "2.43.0",
want: []string{"reset", "--hard", "HEAD"},
},
{
name: "dropped on Git without it at all",
args: []string{"rev-list", "--count", "--end-of-options", "main", "--", "-file"},
version: "2.1.4",
want: []string{"rev-list", "--count", "main", "--", "-file"},
},
{
name: "option-like positional argument refused before 2.43.1",
args: []string{"checkout", "--end-of-options", "--orphan=x"},
version: "2.43.0",
wantErr: true,
},
{
name: "option-like positional argument kept since 2.43.1",
args: []string{"checkout", "--end-of-options", "--orphan=x"},
version: "2.43.1",
want: []string{"checkout", "--end-of-options", "--orphan=x"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := argsForVersion(test.args, slices.Index(test.args, endOfOptions), test.version)
if test.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, test.want, got)
})
}
}
6 changes: 4 additions & 2 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func Move(repoPath, src, dst string, opts ...MoveOptions) error {
opt = opts[0]
}

_, err := NewCommand("mv").AddOptions(opt.CommandOptions).AddArgs("--end-of-options", src, dst).RunInDirWithTimeout(opt.Timeout, repoPath)
_, err := NewCommand("mv").AddOptions(opt.CommandOptions).AddArgs("--", src, dst).RunInDirWithTimeout(opt.Timeout, repoPath)
return err
}

Expand Down Expand Up @@ -475,7 +475,9 @@ func CreateCommit(repoPath string, committer *Signature, message string, opts ..
opt.Author = committer
}
cmd = cmd.AddArgs(fmt.Sprintf("--author='%s <%s>'", opt.Author.Name, opt.Author.Email)).
AddArgs("-m", message).
// Attach the message to its option, so that a message equal to
// "--end-of-options" is not taken for the separator itself.
AddArgs("--message=" + message).
AddOptions(opt.CommandOptions).
AddArgs("--end-of-options")

Expand Down
24 changes: 24 additions & 0 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ func TestRepository_Move(t *testing.T) {
dst: "runme.sh",
opt: MoveOptions{},
},
{
src: "runme.sh",
dst: "-runme.sh",
opt: MoveOptions{},
},
{
src: "-runme.sh",
dst: "runme.sh",
opt: MoveOptions{},
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
Expand Down Expand Up @@ -455,6 +465,20 @@ func TestRepository_Commit(t *testing.T) {
assert.Equal(t, author.Email, c.Author.Email)
assert.Equal(t, message+"\n", c.Message)
})

t.Run("message looks like an option", func(t *testing.T) {
fpath := filepath.Join(r.Path(), "MESSAGE_LOOKS_LIKE_OPTION")
err = ioutil.WriteFile(fpath, []byte("something"), 0600)
require.NoError(t, err)
require.NoError(t, r.Add(AddOptions{All: true}))

message := "--end-of-options"
require.NoError(t, r.Commit(committer, message))

c, err := r.CatFileCommit("master")
require.NoError(t, err)
assert.Equal(t, message+"\n", c.Message)
})
}

func TestRepository_RevParse(t *testing.T) {
Expand Down
Loading