repo fork: reuse existing git remote

Avoid adding a new git remote for a fork if an existing remote is found
that points to the exact forked repo.
This commit is contained in:
Mislav Marohnić 2020-03-27 13:27:22 +01:00
parent 0f98d56649
commit 965f2704f3
2 changed files with 34 additions and 1 deletions

View file

@ -328,6 +328,15 @@ func repoFork(cmd *cobra.Command, args []string) error {
}
if inParent {
remotes, err := ctx.Remotes()
if err != nil {
return err
}
if remote, err := remotes.FindByRepo(forkedRepo.RepoOwner(), forkedRepo.RepoName()); err == nil {
fmt.Fprintf(out, "%s Using existing remote %s\n", greenCheck, utils.Bold(remote.Name))
return nil
}
remoteDesired := remotePref == "true"
if remotePref == "prompt" {
err = Confirm("Would you like to add a remote for the fork?", &remoteDesired)

View file

@ -40,6 +40,31 @@ func TestRepoFork_already_forked(t *testing.T) {
}
}
func TestRepoFork_reuseRemote(t *testing.T) {
initContext = func() context.Context {
ctx := context.NewBlank()
ctx.SetBaseRepo("OWNER/REPO")
ctx.SetBranch("master")
ctx.SetRemotes(map[string]string{
"upstream": "OWNER/REPO",
"origin": "someone/REPO",
})
return ctx
}
http := initFakeHTTP()
http.StubRepoResponse("OWNER", "REPO")
defer http.StubWithFixture(200, "forkResult.json")()
output, err := RunCommand(repoForkCmd, "repo fork")
if err != nil {
t.Errorf("got unexpected error: %v", err)
}
if !strings.Contains(output.String(), "Using existing remote origin") {
t.Errorf("output did not match: %q", output)
return
}
}
func stubSince(d time.Duration) func() {
originalSince := Since
Since = func(t time.Time) time.Duration {
@ -579,7 +604,6 @@ func TestRepoCreate_orgWithTeam(t *testing.T) {
}
}
func TestRepoView(t *testing.T) {
initBlankContext("OWNER/REPO", "master")
http := initFakeHTTP()