MCPcopy Create free account
hub / github.com/github/gh-aw / cloneRepoContentsIntoHost

Function cloneRepoContentsIntoHost

pkg/cli/trial_repository.go:547–602  ·  view source on GitHub ↗

cloneRepoContentsIntoHost clones the contents of the source repo into the host repo Uses a simplified approach with force push since host repo is freshly created

(cloneRepoSlug string, cloneRepoVersion string, hostRepoSlug string, verbose bool)

Source from the content-addressed store, hash-verified

545// cloneRepoContentsIntoHost clones the contents of the source repo into the host repo
546// Uses a simplified approach with force push since host repo is freshly created
547func cloneRepoContentsIntoHost(cloneRepoSlug string, cloneRepoVersion string, hostRepoSlug string, verbose bool) error {
548 if verbose {
549 fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Cloning contents from %s into host repository %s", cloneRepoSlug, hostRepoSlug)))
550 }
551
552 // Save the original working directory to restore it later
553 originalDir, err := os.Getwd()
554 if err != nil {
555 return fmt.Errorf("failed to get current directory: %w", err)
556 }
557 defer os.Chdir(originalDir)
558
559 // Create temporary directory to clone the source repo
560 tempCloneDir := filepath.Join(os.TempDir(), fmt.Sprintf("gh-aw-clone-%x", time.Now().UnixNano()))
561 defer os.RemoveAll(tempCloneDir)
562
563 // Clone the source repository
564 cloneURL := trialRepositoryGitURL(cloneRepoSlug)
565
566 output, err := workflow.RunGitCombined(fmt.Sprintf("Cloning %s...", cloneRepoSlug), "clone", cloneURL, tempCloneDir)
567 if err != nil {
568 return fmt.Errorf("failed to clone source repository %s: %w (output: %s)", cloneURL, err, string(output))
569 }
570
571 // Change to the cloned repository directory
572 if err := os.Chdir(tempCloneDir); err != nil {
573 return fmt.Errorf("failed to change to clone directory: %w", err)
574 }
575
576 // If a version/tag/SHA is specified, checkout that ref
577 if cloneRepoVersion != "" {
578 checkoutCmd := exec.Command("git", "checkout", cloneRepoVersion)
579 if output, err := checkoutCmd.CombinedOutput(); err != nil {
580 return fmt.Errorf("failed to checkout ref '%s': %w (output: %s)", cloneRepoVersion, err, string(output))
581 }
582 }
583
584 // Add the host repository as a new remote
585 hostURL := trialRepositoryGitURL(hostRepoSlug)
586 remoteCmd := exec.Command("git", "remote", "add", "host", hostURL)
587 if output, err := remoteCmd.CombinedOutput(); err != nil {
588 return fmt.Errorf("failed to add host remote: %w (output: %s)", err, string(output))
589 }
590
591 // Force push the current branch to the host repository's main branch
592 pushCmd := exec.Command("git", "push", "--force", "host", "HEAD:main")
593 if output, err := pushCmd.CombinedOutput(); err != nil {
594 return fmt.Errorf("failed to force push to host repository: %w (output: %s)", err, string(output))
595 }
596
597 if verbose {
598 fmt.Fprintln(os.Stderr, console.FormatSuccessMessage(fmt.Sprintf("Successfully pushed contents from %s to %s", cloneRepoSlug, hostRepoSlug)))
599 }
600
601 return nil
602}

Callers 1

RunWorkflowTrialsFunction · 0.85

Calls 5

FormatInfoMessageFunction · 0.92
RunGitCombinedFunction · 0.92
FormatSuccessMessageFunction · 0.92
trialRepositoryGitURLFunction · 0.85
ErrorfMethod · 0.45

Tested by

no test coverage detected