Skip to content

Commit 693054d

Browse files
authored
feat: automatically set upstream tracking when creating worktrees from existing branches (#3)
* feat: add upstream tracking setup for new worktrees * fix: update installation instructions to use 'bash' instead of 'sh' * Add comprehensive test coverage for upstream tracking feature - Add 9 unit tests for checkAndSetUpstream method and createWorktree integration - Add 9 integration tests covering all upstream tracking scenarios - Tests verify automatic upstream setup for local branches without tracking - Tests ensure no interference with remote/new branch worktree creation - Tests cover error handling, multiple remotes, and configuration integration - All tests passing: unit tests (212/212) and integration tests (9/9) * Fixing lint error
1 parent 96575b8 commit 693054d

4 files changed

Lines changed: 795 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ Use the official install script for automatic platform detection and installatio
2323

2424
```bash
2525
# Quick install (latest version)
26-
curl -fsSL https://raw.githubusercontent.com/tinmancoding/wt/main/install.sh | sh
26+
curl -fsSL https://raw.githubusercontent.com/tinmancoding/wt/main/install.sh | bash
2727

2828
# Install specific version
29-
WT_INSTALL_VERSION=v0.2.1 curl -fsSL https://raw.githubusercontent.com/tinmancoding/wt/main/install.sh | sh
29+
WT_INSTALL_VERSION=v0.2.1 curl -fsSL https://raw.githubusercontent.com/tinmancoding/wt/main/install.sh | bash
3030

3131
# Install to custom path (default: ~/.local/bin)
32-
WT_INSTALL_PATH=/usr/local/bin curl -fsSL https://raw.githubusercontent.com/tinmancoding/wt/main/install.sh | sh
32+
WT_INSTALL_PATH=/usr/local/bin curl -fsSL https://raw.githubusercontent.com/tinmancoding/wt/main/install.sh | bash
3333

3434
# Verify installation
3535
wt --help

src/worktree.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,44 @@ export class WorktreeOperations {
156156
}
157157
}
158158

159+
async checkAndSetUpstream(repoInfo: RepositoryInfo, branchName: string): Promise<void> {
160+
try {
161+
// Check if the local branch has an upstream tracking branch
162+
// Use the main git directory but check the branch state
163+
const trackingResult = await this.services.git.executeCommandWithResult(repoInfo.gitDir, [
164+
'for-each-ref',
165+
'--format=%(upstream)',
166+
`refs/heads/${branchName}`
167+
]);
168+
169+
if (trackingResult.exitCode === 0 && trackingResult.stdout.trim()) {
170+
// Upstream is already set
171+
this.services.logger.log(`Branch '${branchName}' already has upstream tracking configured`);
172+
return;
173+
}
174+
175+
// No upstream set, try to find a matching remote branch
176+
const remoteInfo = await this.findRemoteBranch(repoInfo, branchName);
177+
178+
if (remoteInfo.exists && remoteInfo.remoteName) {
179+
// Set up tracking to the remote branch using the main git directory
180+
const remoteBranchRef = `${remoteInfo.remoteName}/${branchName}`;
181+
await this.services.git.executeCommand(repoInfo.gitDir, [
182+
'branch',
183+
'--set-upstream-to',
184+
remoteBranchRef,
185+
branchName
186+
]);
187+
this.services.logger.log(`Set upstream tracking for branch '${branchName}' to '${remoteBranchRef}'`);
188+
} else {
189+
this.services.logger.log(`No matching remote branch found for '${branchName}', skipping upstream setup`);
190+
}
191+
} catch (error) {
192+
// Upstream setup failures are not fatal, just warn
193+
this.services.logger.warn(`Warning: Failed to set upstream for branch '${branchName}': ${error instanceof Error ? error.message : 'Unknown error'}`);
194+
}
195+
}
196+
159197
async resolveBranch(repoInfo: RepositoryInfo, branchName: string, config: WTConfig): Promise<BranchResolution> {
160198
// First, perform auto-fetch if enabled
161199
await this.performAutoFetch(repoInfo, config);
@@ -208,6 +246,9 @@ export class WorktreeOperations {
208246
}
209247
await this.services.git.executeCommand(repoInfo.gitDir, ['worktree', 'add', worktreePath, branchName]);
210248
this.services.logger.log(`Created worktree for existing local branch '${branchName}' at ${worktreePath}`);
249+
250+
// Check and set upstream tracking if needed after worktree creation
251+
await this.checkAndSetUpstream(repoInfo, branchName);
211252
break;
212253
}
213254

0 commit comments

Comments
 (0)