Skip to content

Commit 30b7b35

Browse files
committed
Save work before checking out remote branch
1 parent cce9a18 commit 30b7b35

File tree

1 file changed

+65
-22
lines changed

1 file changed

+65
-22
lines changed

src/index.ts

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,44 @@ export class ClaudeSandbox {
4242
let branchName = "";
4343

4444
if (this.config.prNumber) {
45-
// Checkout PR
45+
// Checkout PR - get the actual branch name from GitHub
4646
console.log(chalk.blue(`Fetching PR #${this.config.prNumber}...`));
4747
try {
48-
branchName = `pr-${this.config.prNumber}`;
48+
const { execSync } = require("child_process");
4949

50-
// Check if PR branch already exists locally
50+
// Check for uncommitted changes first
51+
const status = await this.git.status();
52+
if (!status.isClean()) {
53+
console.log(chalk.yellow("Warning: You have uncommitted changes. Committing them first..."));
54+
await this.git.add("./*");
55+
await this.git.commit("Save work before checking out PR");
56+
console.log(chalk.green("✓ Committed uncommitted changes"));
57+
}
58+
59+
// Get PR info to find the actual branch name
60+
const prInfo = execSync(`gh pr view ${this.config.prNumber} --json headRefName`, {
61+
encoding: 'utf-8',
62+
cwd: process.cwd()
63+
});
64+
const prData = JSON.parse(prInfo);
65+
const actualBranchName = prData.headRefName;
66+
67+
console.log(chalk.blue(`PR #${this.config.prNumber} uses branch: ${actualBranchName}`));
68+
69+
// Fetch the PR
70+
await this.git.fetch('origin', `pull/${this.config.prNumber}/head:${actualBranchName}`);
71+
72+
// Check if branch exists locally
5173
const branches = await this.git.branchLocal();
52-
if (branches.all.includes(branchName)) {
53-
// PR branch exists, just checkout
54-
await this.git.checkout(branchName);
55-
console.log(chalk.green(`✓ Switched to existing PR branch: ${branchName}`));
74+
if (branches.all.includes(actualBranchName)) {
75+
await this.git.checkout(actualBranchName);
76+
console.log(chalk.green(`✓ Switched to existing branch: ${actualBranchName}`));
5677
} else {
57-
// Fetch and create new PR branch
58-
await this.git.fetch("origin", `pull/${this.config.prNumber}/head:${branchName}`);
59-
await this.git.checkout(branchName);
60-
console.log(chalk.green(`✓ Checked out PR #${this.config.prNumber}`));
78+
await this.git.checkout(actualBranchName);
79+
console.log(chalk.green(`✓ Checked out PR #${this.config.prNumber} as branch: ${actualBranchName}`));
6180
}
81+
82+
branchName = actualBranchName;
6283
} catch (error) {
6384
console.error(chalk.red(`✗ Failed to checkout PR #${this.config.prNumber}:`), error);
6485
throw error;
@@ -67,21 +88,43 @@ export class ClaudeSandbox {
6788
// Checkout remote branch
6889
console.log(chalk.blue(`Checking out remote branch: ${this.config.remoteBranch}...`));
6990
try {
70-
await this.git.fetch("origin");
71-
const localBranchName = this.config.remoteBranch.replace("origin/", "");
91+
// Check for uncommitted changes first
92+
const status = await this.git.status();
93+
if (!status.isClean()) {
94+
console.log(chalk.yellow("Warning: You have uncommitted changes. Committing them first..."));
95+
await this.git.add("./*");
96+
await this.git.commit("Save work before checking out remote branch");
97+
console.log(chalk.green("✓ Committed uncommitted changes"));
98+
}
7299

73-
// Check if local branch already exists
100+
// Parse remote/branch format
101+
const parts = this.config.remoteBranch.split('/');
102+
if (parts.length < 2) {
103+
throw new Error('Remote branch must be in format "remote/branch" (e.g., "origin/feature-branch")');
104+
}
105+
106+
const remote = parts[0];
107+
const branch = parts.slice(1).join('/');
108+
109+
console.log(chalk.blue(`Remote: ${remote}, Branch: ${branch}`));
110+
111+
// Fetch from remote
112+
await this.git.fetch(remote);
113+
114+
// Check if local branch exists
74115
const branches = await this.git.branchLocal();
75-
if (branches.all.includes(localBranchName)) {
76-
// Local branch exists, just checkout
77-
await this.git.checkout(localBranchName);
78-
console.log(chalk.green(`✓ Switched to existing branch: ${localBranchName}`));
116+
if (branches.all.includes(branch)) {
117+
// Local branch exists, switch to it and pull
118+
await this.git.checkout(branch);
119+
await this.git.pull(remote, branch);
120+
console.log(chalk.green(`✓ Switched to existing local branch: ${branch}`));
79121
} else {
80-
// Create new local branch from remote
81-
await this.git.checkoutBranch(localBranchName, this.config.remoteBranch);
82-
console.log(chalk.green(`✓ Checked out remote branch: ${this.config.remoteBranch}`));
122+
// Create new local branch tracking the remote
123+
await this.git.checkoutBranch(branch, `${remote}/${branch}`);
124+
console.log(chalk.green(`✓ Created and checked out new branch: ${branch} tracking ${remote}/${branch}`));
83125
}
84-
branchName = localBranchName;
126+
127+
branchName = branch;
85128
} catch (error) {
86129
console.error(chalk.red(`✗ Failed to checkout remote branch ${this.config.remoteBranch}:`), error);
87130
throw error;

0 commit comments

Comments
 (0)