Handle --bare clone targets (#9271)

Co-authored-by: Andy Feller <andyfeller@github.com>
This commit is contained in:
Hiran Wijesinghe 2024-07-24 06:35:24 -04:00 committed by GitHub
parent adc8b82d11
commit fd51a69308
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -11,6 +11,7 @@ import (
"path"
"regexp"
"runtime"
"slices"
"sort"
"strings"
"sync"
@ -594,6 +595,10 @@ func (c *Client) Clone(ctx context.Context, cloneURL string, args []string, mods
cloneArgs = append(cloneArgs, target)
} else {
target = path.Base(strings.TrimSuffix(cloneURL, ".git"))
if slices.Contains(cloneArgs, "--bare") {
target += ".git"
}
}
cloneArgs = append([]string{"clone"}, cloneArgs...)
cmd, err := c.AuthenticatedCommand(ctx, cloneArgs...)

View file

@ -1267,6 +1267,7 @@ func TestClientPush(t *testing.T) {
func TestClientClone(t *testing.T) {
tests := []struct {
name string
args []string
mods []CommandModifier
cmdExitStatus int
cmdStdout string
@ -1277,22 +1278,37 @@ func TestClientClone(t *testing.T) {
}{
{
name: "clone",
args: []string{},
wantCmdArgs: `path/to/git -c credential.helper= -c credential.helper=!"gh" auth git-credential clone github.com/cli/cli`,
wantTarget: "cli",
},
{
name: "accepts command modifiers",
args: []string{},
mods: []CommandModifier{WithRepoDir("/path/to/repo")},
wantCmdArgs: `path/to/git -C /path/to/repo -c credential.helper= -c credential.helper=!"gh" auth git-credential clone github.com/cli/cli`,
wantTarget: "cli",
},
{
name: "git error",
args: []string{},
cmdExitStatus: 1,
cmdStderr: "git error message",
wantCmdArgs: `path/to/git -c credential.helper= -c credential.helper=!"gh" auth git-credential clone github.com/cli/cli`,
wantErrorMsg: "failed to run git: git error message",
},
{
name: "bare clone",
args: []string{"--bare"},
wantCmdArgs: `path/to/git -c credential.helper= -c credential.helper=!"gh" auth git-credential clone --bare github.com/cli/cli`,
wantTarget: "cli.git",
},
{
name: "bare clone with explicit target",
args: []string{"cli-bare", "--bare"},
wantCmdArgs: `path/to/git -c credential.helper= -c credential.helper=!"gh" auth git-credential clone --bare github.com/cli/cli cli-bare`,
wantTarget: "cli-bare",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -1301,7 +1317,7 @@ func TestClientClone(t *testing.T) {
GitPath: "path/to/git",
commandContext: cmdCtx,
}
target, err := client.Clone(context.Background(), "github.com/cli/cli", []string{}, tt.mods...)
target, err := client.Clone(context.Background(), "github.com/cli/cli", tt.args, tt.mods...)
assert.Equal(t, tt.wantCmdArgs, strings.Join(cmd.Args[3:], " "))
if tt.wantErrorMsg == "" {
assert.NoError(t, err)