Skip to content

Commit b77d96d

Browse files
committed
♻️ refactor(worktree): pass repoDir to git commands and streamline PR remote detection
Signed-off-by: samzong <samzong.lu@gmail.com>
1 parent 23adaa7 commit b77d96d

1 file changed

Lines changed: 26 additions & 33 deletions

File tree

internal/worktree/pr.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
// DetectPRRemote auto-detects the best remote for fetching PRs
14-
func (c *Client) DetectPRRemote() (string, error) {
15-
result, err := c.runner.Run("remote")
14+
func (c *Client) DetectPRRemote(repoDir string) (string, error) {
15+
result, err := c.runner.Run("-C", repoDir, "remote")
1616
if err != nil {
1717
return "", fmt.Errorf("failed to list remotes: %w", err)
1818
}
@@ -22,34 +22,27 @@ func (c *Client) DetectPRRemote() (string, error) {
2222
return "", errors.New("no git remotes found")
2323
}
2424

25-
// 1. Prefer upstream
26-
for _, remote := range remotes {
27-
if remote == "upstream" {
28-
return remote, nil
25+
// Prefer upstream, then origin, then single remote
26+
preferences := []string{"upstream", "origin"}
27+
for _, preferred := range preferences {
28+
for _, remote := range remotes {
29+
if remote == preferred {
30+
return remote, nil
31+
}
2932
}
3033
}
3134

32-
// 2. Fallback to origin
33-
for _, remote := range remotes {
34-
if remote == "origin" {
35-
return remote, nil
36-
}
37-
}
38-
39-
// 3. If only one remote, use it
4035
if len(remotes) == 1 {
4136
return remotes[0], nil
4237
}
43-
44-
// 4. Multiple remotes but no common names
4538
return "", fmt.Errorf("multiple remotes found (%v) but no 'upstream' or 'origin'. Use --remote to specify", remotes)
4639
}
4740

4841
// PRExists checks if a PR exists on the remote
49-
func (c *Client) PRExists(prNumber int, remote string) (bool, string, error) {
42+
func (c *Client) PRExists(prNumber int, remote, repoDir string) (bool, string, error) {
5043
refPath := fmt.Sprintf("refs/pull/%d/head", prNumber)
5144

52-
result, err := c.runner.Run("ls-remote", remote, refPath)
45+
result, err := c.runner.Run("-C", repoDir, "ls-remote", remote, refPath)
5346
if err != nil {
5447
return false, "", fmt.Errorf("failed to query remote: %w", err)
5548
}
@@ -69,41 +62,41 @@ func (c *Client) PRExists(prNumber int, remote string) (bool, string, error) {
6962

7063
// AddPR creates a worktree from a Pull Request
7164
func (c *Client) AddPR(prNumber int, remote string) error {
72-
// 1. Auto-detect remote if not specified
65+
// Setup paths to get repoDir
66+
root, err := c.GetWorktreeRoot()
67+
if err != nil {
68+
return fmt.Errorf("failed to find worktree root: %w", err)
69+
}
70+
repoDir := repoDirForGit(root)
71+
72+
// Auto-detect remote if not specified
7373
if remote == "" {
74-
detectedRemote, err := c.DetectPRRemote()
74+
detectedRemote, err := c.DetectPRRemote(repoDir)
7575
if err != nil {
7676
return err
7777
}
7878
remote = detectedRemote
7979
fmt.Printf("Auto-detected remote: %s\n", remote)
8080
}
8181

82-
// 2. Check PR exists
83-
exists, commitHash, err := c.PRExists(prNumber, remote)
82+
// Verify PR exists
83+
exists, commitHash, err := c.PRExists(prNumber, remote, repoDir)
8484
if err != nil {
8585
return err
8686
}
8787
if !exists {
8888
return fmt.Errorf("PR #%d not found on remote '%s'", prNumber, remote)
8989
}
9090

91-
// 3. Setup paths
91+
// Prepare worktree paths
9292
branchName := fmt.Sprintf("pr-%d", prNumber)
93-
94-
root, err := c.GetWorktreeRoot()
95-
if err != nil {
96-
return fmt.Errorf("failed to find worktree root: %w", err)
97-
}
98-
repoDir := repoDirForGit(root)
9993
targetPath := filepath.Join(root, branchName)
10094

101-
// 4. Check directory doesn't exist
10295
if _, err := os.Stat(targetPath); err == nil {
10396
return fmt.Errorf("directory already exists: %s", targetPath)
10497
}
10598

106-
// 5. Fetch PR
99+
// Fetch PR from remote
107100
refSpec := fmt.Sprintf("pull/%d/head:%s", prNumber, branchName)
108101
fmt.Printf("Fetching PR #%d from %s...\n", prNumber, remote)
109102

@@ -113,14 +106,14 @@ func (c *Client) AddPR(prNumber int, remote string) error {
113106
return gitutil.WrapGitError("failed to fetch PR", result, err)
114107
}
115108

116-
// 6. Create worktree
109+
// Create worktree
117110
addArgs := []string{"-C", repoDir, "worktree", "add", targetPath, branchName}
118111
result, err = c.runner.RunLogged(addArgs...)
119112
if err != nil {
120113
return gitutil.WrapGitError("failed to create worktree", result, err)
121114
}
122115

123-
// 7. Sync shared resources
116+
// Sync shared resources
124117
if err := c.SyncSharedResources(branchName); err != nil {
125118
fmt.Printf("Warning: failed to sync shared resources: %v\n", err)
126119
}

0 commit comments

Comments
 (0)