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

@ -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
}