Add --upstream-remote-name flag to gh replo clone (#5619)

This commit is contained in:
Ahmed Adan 2022-05-24 07:12:01 -04:00 committed by GitHub
parent eb4439d781
commit 31bee2e639
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 27 deletions

View file

@ -367,12 +367,12 @@ func RunClone(cloneURL string, args []string) (target string, err error) {
return
}
func AddUpstreamRemote(upstreamURL, cloneDir string, branches []string) error {
args := []string{"-C", cloneDir, "remote", "add"}
func AddNamedRemote(url, name, dir string, branches []string) error {
args := []string{"-C", dir, "remote", "add"}
for _, branch := range branches {
args = append(args, "-t", branch)
}
args = append(args, "-f", "upstream", upstreamURL)
args = append(args, "-f", name, url)
cloneCmd, err := GitCommand(args...)
if err != nil {
return err

View file

@ -181,37 +181,40 @@ func TestParseExtraCloneArgs(t *testing.T) {
}
}
func TestAddUpstreamRemote(t *testing.T) {
func TestAddNamedRemote(t *testing.T) {
tests := []struct {
name string
upstreamURL string
cloneDir string
branches []string
want string
title string
name string
url string
dir string
branches []string
want string
}{
{
name: "fetch all",
upstreamURL: "URL",
cloneDir: "DIRECTORY",
branches: []string{},
want: "git -C DIRECTORY remote add -f upstream URL",
title: "fetch all",
name: "test",
url: "URL",
dir: "DIRECTORY",
branches: []string{},
want: "git -C DIRECTORY remote add -f test URL",
},
{
name: "fetch specific branches only",
upstreamURL: "URL",
cloneDir: "DIRECTORY",
branches: []string{"master", "dev"},
want: "git -C DIRECTORY remote add -t master -t dev -f upstream URL",
title: "fetch specific branches only",
name: "test",
url: "URL",
dir: "DIRECTORY",
branches: []string{"trunk", "dev"},
want: "git -C DIRECTORY remote add -t trunk -t dev -f test URL",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run(tt.title, func(t *testing.T) {
cs, cmdTeardown := run.Stub()
defer cmdTeardown(t)
cs.Register(tt.want, 0, "")
err := AddUpstreamRemote(tt.upstreamURL, tt.cloneDir, tt.branches)
err := AddNamedRemote(tt.url, tt.name, tt.dir, tt.branches)
if err != nil {
t.Fatalf("error running command `git remote add -f`: %v", err)
}

View file

@ -21,8 +21,9 @@ type CloneOptions struct {
Config func() (config.Config, error)
IO *iostreams.IOStreams
GitArgs []string
Repository string
GitArgs []string
Repository string
UpstreamName string
}
func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Command {
@ -46,7 +47,9 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
defaults to the name of the authenticating user.
If the repository is a fork, its parent repository will be added as an additional
git remote called "upstream".
git remote called "upstream". The remote name can be configured using %[1]s--upstream-remote-name%[1]s.
The %[1]s--upstream-remote-name%[1]s option supports an "@owner" value which will name
the remote after the owner of the parent repository.
`, "`"),
RunE: func(cmd *cobra.Command, args []string) error {
opts.Repository = args[0]
@ -60,6 +63,7 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
},
}
cmd.Flags().StringVarP(&opts.UpstreamName, "upstream-remote-name", "u", "upstream", "Upstream remote name when cloning a fork")
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
@ -164,11 +168,15 @@ func cloneRun(opts *CloneOptions) error {
}
upstreamURL := ghrepo.FormatRemoteURL(canonicalRepo.Parent, protocol)
err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{canonicalRepo.Parent.DefaultBranchRef.Name})
upstreamName := opts.UpstreamName
if opts.UpstreamName == "@owner" {
upstreamName = canonicalRepo.Parent.RepoOwner()
}
err = git.AddNamedRemote(upstreamURL, upstreamName, cloneDir, []string{canonicalRepo.Parent.DefaultBranchRef.Name})
if err != nil {
return err
}
}
return nil
}

View file

@ -251,6 +251,42 @@ func Test_RepoClone_hasParent(t *testing.T) {
}
}
func Test_RepoClone_hasParent_upstreamRemoteName(t *testing.T) {
reg := &httpmock.Registry{}
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
httpmock.StringResponse(`
{ "data": { "repository": {
"name": "REPO",
"owner": {
"login": "OWNER"
},
"parent": {
"name": "ORIG",
"owner": {
"login": "hubot"
},
"defaultBranchRef": {
"name": "trunk"
}
}
} } }
`))
httpClient := &http.Client{Transport: reg}
cs, cmdTeardown := run.Stub()
defer cmdTeardown(t)
cs.Register(`git clone https://github.com/OWNER/REPO.git`, 0, "")
cs.Register(`git -C REPO remote add -t trunk -f test https://github.com/hubot/ORIG.git`, 0, "")
_, err := runCloneCommand(httpClient, "OWNER/REPO --upstream-remote-name test")
if err != nil {
t.Fatalf("error running command `repo clone`: %v", err)
}
}
func Test_RepoClone_withoutUsername(t *testing.T) {
reg := &httpmock.Registry{}
defer reg.Verify(t)

View file

@ -317,7 +317,7 @@ func forkRun(opts *ForkOptions) error {
}
upstreamURL := ghrepo.FormatRemoteURL(repoToFork, protocol)
err = git.AddUpstreamRemote(upstreamURL, cloneDir, []string{})
err = git.AddNamedRemote(upstreamURL, "upstream", cloneDir, []string{})
if err != nil {
return err
}