Add "upstream" remote after repo fork

This commit is contained in:
Mislav Marohnić 2020-03-31 18:36:41 +02:00
parent 0e9b48d876
commit fa3d65b2b6
2 changed files with 56 additions and 28 deletions

View file

@ -129,14 +129,7 @@ func repoClone(cmd *cobra.Command, args []string) error {
}
if parentRepo != nil {
// TODO: support SSH remote URLs
upstreamURL := fmt.Sprintf("https://github.com/%s.git", ghrepo.FullName(parentRepo))
cloneDir := path.Base(strings.TrimSuffix(cloneURL, ".git"))
cloneCmd := git.GitCommand("-C", cloneDir, "remote", "add", "upstream", upstreamURL)
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
err := run.PrepareCmd(cloneCmd).Run()
err := addUpstreamRemote(parentRepo, cloneURL)
if err != nil {
return err
}
@ -145,6 +138,17 @@ func repoClone(cmd *cobra.Command, args []string) error {
return nil
}
func addUpstreamRemote(parentRepo ghrepo.Interface, cloneURL string) error {
// TODO: support SSH remote URLs
upstreamURL := fmt.Sprintf("https://github.com/%s.git", ghrepo.FullName(parentRepo))
cloneDir := path.Base(strings.TrimSuffix(cloneURL, ".git"))
cloneCmd := git.GitCommand("-C", cloneDir, "remote", "add", "upstream", upstreamURL)
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
return run.PrepareCmd(cloneCmd).Run()
}
func repoCreate(cmd *cobra.Command, args []string) error {
projectDir, projectDirErr := git.ToplevelDir()
@ -389,12 +393,28 @@ func repoFork(cmd *cobra.Command, args []string) error {
}
}
if remoteDesired {
_, err := git.AddRemote("fork", forkedRepo.CloneURL)
remoteName := "origin"
remotes, err := ctx.Remotes()
if err != nil {
return err
}
if _, err := remotes.FindByName(remoteName); err == nil {
renameTarget := "upstream"
renameCmd := git.GitCommand("remote", "rename", remoteName, renameTarget)
err = run.PrepareCmd(renameCmd).Run()
if err != nil {
return err
}
fmt.Fprintf(out, "%s Renamed %s remote to %s\n", greenCheck, utils.Bold(remoteName), utils.Bold(renameTarget))
}
_, err = git.AddRemote(remoteName, forkedRepo.CloneURL)
if err != nil {
return fmt.Errorf("failed to add remote: %w", err)
}
fmt.Fprintf(out, "%s Remote added at %s\n", greenCheck, utils.Bold("fork"))
fmt.Fprintf(out, "%s Added remote %s\n", greenCheck, utils.Bold(remoteName))
}
} else {
cloneDesired := clonePref == "true"
@ -414,6 +434,11 @@ func repoFork(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to clone fork: %w", err)
}
err = addUpstreamRemote(toFork, forkedRepo.CloneURL)
if err != nil {
return err
}
fmt.Fprintf(out, "%s Cloned fork\n", greenCheck)
}
}

View file

@ -151,8 +151,8 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
}
expectedCmds := []string{
"git remote add -f fork https://github.com/someone/repo.git",
"git fetch fork",
"git remote rename origin upstream",
"git remote add -f origin https://github.com/someone/repo.git",
}
for x, cmd := range seenCmds {
@ -163,7 +163,7 @@ func TestRepoFork_in_parent_yes(t *testing.T) {
test.ExpectLines(t, output.String(),
"Created fork someone/REPO",
"Remote added at fork")
"Added remote origin")
}
func TestRepoFork_outside_yes(t *testing.T) {
@ -171,11 +171,11 @@ func TestRepoFork_outside_yes(t *testing.T) {
http := initFakeHTTP()
defer http.StubWithFixture(200, "forkResult.json")()
var seenCmd *exec.Cmd
defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
seenCmd = cmd
return &test.OutputStub{}
})()
cs, restore := test.InitCmdStubber()
defer restore()
cs.Stub("") // git clone
cs.Stub("") // git remote add
output, err := RunCommand(repoForkCmd, "repo fork --clone OWNER/REPO")
if err != nil {
@ -184,7 +184,8 @@ func TestRepoFork_outside_yes(t *testing.T) {
eq(t, output.Stderr(), "")
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
eq(t, strings.Join(cs.Calls[0].Args, " "), "git clone https://github.com/someone/repo.git")
eq(t, strings.Join(cs.Calls[1].Args, " "), "git -C repo remote add upstream https://github.com/OWNER/REPO.git")
test.ExpectLines(t, output.String(),
"Created fork someone/REPO",
@ -196,11 +197,11 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
http := initFakeHTTP()
defer http.StubWithFixture(200, "forkResult.json")()
var seenCmd *exec.Cmd
defer run.SetPrepareCmd(func(cmd *exec.Cmd) run.Runnable {
seenCmd = cmd
return &test.OutputStub{}
})()
cs, restore := test.InitCmdStubber()
defer restore()
cs.Stub("") // git clone
cs.Stub("") // git remote add
oldConfirm := Confirm
Confirm = func(_ string, result *bool) error {
@ -216,7 +217,8 @@ func TestRepoFork_outside_survey_yes(t *testing.T) {
eq(t, output.Stderr(), "")
eq(t, strings.Join(seenCmd.Args, " "), "git clone https://github.com/someone/repo.git")
eq(t, strings.Join(cs.Calls[0].Args, " "), "git clone https://github.com/someone/repo.git")
eq(t, strings.Join(cs.Calls[1].Args, " "), "git -C repo remote add upstream https://github.com/OWNER/REPO.git")
test.ExpectLines(t, output.String(),
"Created fork someone/REPO",
@ -283,8 +285,8 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
}
expectedCmds := []string{
"git remote add -f fork https://github.com/someone/repo.git",
"git fetch fork",
"git remote rename origin upstream",
"git remote add -f origin https://github.com/someone/repo.git",
}
for x, cmd := range seenCmds {
@ -295,7 +297,8 @@ func TestRepoFork_in_parent_survey_yes(t *testing.T) {
test.ExpectLines(t, output.String(),
"Created fork someone/REPO",
"Remote added at fork")
"Renamed origin remote to upstream",
"Added remote origin")
}
func TestRepoFork_in_parent_survey_no(t *testing.T) {