Merge pull request #708 from cli/detect-existing-fork

repo fork: reuse existing git remote if available
This commit is contained in:
Mislav Marohnić 2020-03-30 12:06:27 +02:00 committed by GitHub
commit c7b0abd849
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 13 deletions

View file

@ -303,14 +303,6 @@ func repoFork(cmd *cobra.Command, args []string) error {
s.FinalMSG = utils.Gray(fmt.Sprintf("- %s\n", loading))
s.Start()
authLogin, err := ctx.AuthLogin()
if err != nil {
s.Stop()
return fmt.Errorf("could not determine current username: %w", err)
}
possibleFork := ghrepo.New(authLogin, toFork.RepoName())
forkedRepo, err := api.ForkRepo(apiClient, toFork)
if err != nil {
s.Stop()
@ -323,11 +315,11 @@ func repoFork(cmd *cobra.Command, args []string) error {
// returns the fork repo data even if it already exists -- with no change in status code or
// anything. We thus check the created time to see if the repo is brand new or not; if it's not,
// we assume the fork already existed and report an error.
created_ago := Since(forkedRepo.CreatedAt)
if created_ago > time.Minute {
createdAgo := Since(forkedRepo.CreatedAt)
if createdAgo > time.Minute {
fmt.Fprintf(out, "%s %s %s\n",
utils.Yellow("!"),
utils.Bold(ghrepo.FullName(possibleFork)),
utils.Bold(ghrepo.FullName(forkedRepo)),
"already exists")
} else {
fmt.Fprintf(out, "%s Created fork %s\n", greenCheck, utils.Bold(ghrepo.FullName(forkedRepo)))
@ -338,6 +330,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

@ -18,9 +18,8 @@ import (
func TestRepoFork_already_forked(t *testing.T) {
initContext = func() context.Context {
ctx := context.NewBlank()
ctx.SetBaseRepo("REPO")
ctx.SetBaseRepo("OWNER/REPO")
ctx.SetBranch("master")
ctx.SetAuthLogin("someone")
ctx.SetRemotes(map[string]string{
"origin": "OWNER/REPO",
})
@ -41,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 {