(owner, repo, ref, host string)
| 47 | } |
| 48 | |
| 49 | func getOrCreateListRepoClone(owner, repo, ref, host string) (string, error) { |
| 50 | ref = strings.TrimSpace(ref) |
| 51 | if ref == "" { |
| 52 | return "", errors.New("git fallback requires a non-empty ref") |
| 53 | } |
| 54 | |
| 55 | githubHost := GetGitHubHostForRepo(owner, repo) |
| 56 | if host != "" { |
| 57 | githubHost = stringutil.NormalizeGitHubHostURL(host) |
| 58 | } |
| 59 | repoURL := fmt.Sprintf("%s/%s/%s.git", githubHost, owner, repo) |
| 60 | cacheKey := fmt.Sprintf("%s|%s|%s|%s", githubHost, owner, repo, ref) |
| 61 | |
| 62 | if cloneDir, found := func() (string, bool) { |
| 63 | gitListCloneCache.mu.Lock() |
| 64 | defer gitListCloneCache.mu.Unlock() |
| 65 | if cloneDir, ok := gitListCloneCache.dirs[cacheKey]; ok { |
| 66 | if stat, err := os.Stat(filepath.Join(cloneDir, ".git")); err == nil && stat.IsDir() { |
| 67 | return cloneDir, true |
| 68 | } |
| 69 | delete(gitListCloneCache.dirs, cacheKey) |
| 70 | } |
| 71 | return "", false |
| 72 | }(); found { |
| 73 | return cloneDir, nil |
| 74 | } |
| 75 | |
| 76 | tmpDir, err := os.MkdirTemp("", "gh-aw-list-*") |
| 77 | if err != nil { |
| 78 | return "", fmt.Errorf("failed to create temp directory: %w", err) |
| 79 | } |
| 80 | |
| 81 | cloneCmd := exec.Command("git", "clone", "--depth", "1", "--branch", ref, "--single-branch", "--filter=blob:none", "--no-checkout", repoURL, tmpDir) |
| 82 | cloneOutput, err := cloneCmd.CombinedOutput() |
| 83 | if err != nil { |
| 84 | if cleanupErr := os.RemoveAll(tmpDir); cleanupErr != nil { |
| 85 | remoteLog.Printf("Failed to clean up temp directory %q: %v", tmpDir, cleanupErr) |
| 86 | } |
| 87 | remoteLog.Printf("Failed to clone repository: %s", string(cloneOutput)) |
| 88 | return "", fmt.Errorf("failed to clone repository for %s/%s@%s: %w", owner, repo, ref, err) |
| 89 | } |
| 90 | |
| 91 | existingDir, found := func() (string, bool) { |
| 92 | gitListCloneCache.mu.Lock() |
| 93 | defer gitListCloneCache.mu.Unlock() |
| 94 | if existingDir, ok := gitListCloneCache.dirs[cacheKey]; ok { |
| 95 | if stat, statErr := os.Stat(filepath.Join(existingDir, ".git")); statErr == nil && stat.IsDir() { |
| 96 | return existingDir, true |
| 97 | } |
| 98 | } |
| 99 | gitListCloneCache.dirs[cacheKey] = tmpDir |
| 100 | return "", false |
| 101 | }() |
| 102 | if found { |
| 103 | if cleanupErr := os.RemoveAll(tmpDir); cleanupErr != nil { |
| 104 | remoteLog.Printf("Failed to clean up duplicate clone %q: %v", tmpDir, cleanupErr) |
| 105 | } |
| 106 | return existingDir, nil |
no test coverage detected