Skip to content

Commit

Permalink
Consolidate 'git clone' commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Fotios Lindiakos committed Apr 1, 2020
1 parent e066e98 commit ed62b7b
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions command/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ With no argument, the repository for the current directory is displayed.`,
RunE: repoView,
}

func runClone(cloneURL string, args []string) (target string, err error) {
cloneArgs := append(args, cloneURL)
target = path.Base(strings.TrimSuffix(cloneURL, ".git"))

cloneArgs = append([]string{"clone"}, cloneArgs...)

cloneCmd := git.GitCommand(cloneArgs...)
cloneCmd.Stdin = os.Stdin
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr

err = run.PrepareCmd(cloneCmd).Run()
return
}

func repoClone(cmd *cobra.Command, args []string) error {
cloneURL := args[0]
if !strings.Contains(cloneURL, ":") {
Expand Down Expand Up @@ -115,21 +130,13 @@ func repoClone(cmd *cobra.Command, args []string) error {
}
}

cloneArgs := []string{"clone"}
cloneArgs = append(cloneArgs, args[1:]...)
cloneArgs = append(cloneArgs, cloneURL)

cloneCmd := git.GitCommand(cloneArgs...)
cloneCmd.Stdin = os.Stdin
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
err := run.PrepareCmd(cloneCmd).Run()
cloneDir, err := runClone(cloneURL, args[1:])
if err != nil {
return err
}

if parentRepo != nil {
err := addUpstreamRemote(parentRepo, cloneURL)
err := addUpstreamRemote(parentRepo, cloneDir)
if err != nil {
return err
}
Expand All @@ -138,10 +145,9 @@ func repoClone(cmd *cobra.Command, args []string) error {
return nil
}

func addUpstreamRemote(parentRepo ghrepo.Interface, cloneURL string) error {
func addUpstreamRemote(parentRepo ghrepo.Interface, cloneDir 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
Expand Down Expand Up @@ -425,16 +431,12 @@ func repoFork(cmd *cobra.Command, args []string) error {
}
}
if cloneDesired {
cloneCmd := git.GitCommand("clone", forkedRepo.CloneURL)
cloneCmd.Stdin = os.Stdin
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
err = run.PrepareCmd(cloneCmd).Run()
cloneDir, err := runClone(forkedRepo.CloneURL, []string{})
if err != nil {
return fmt.Errorf("failed to clone fork: %w", err)
}

err = addUpstreamRemote(toFork, forkedRepo.CloneURL)
err = addUpstreamRemote(toFork, cloneDir)
if err != nil {
return err
}
Expand Down

0 comments on commit ed62b7b

Please sign in to comment.