-
-
Notifications
You must be signed in to change notification settings - Fork 65
fix(git): validate refs before worktree creation and improve main branch detection #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -147,6 +147,18 @@ async function remoteTrackingRefExists(repoRoot: string, branch: string): Promis | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** Check whether a local branch ref exists. */ | ||||||||||||||||||||||
| async function localBranchExists(repoRoot: string, branch: string): Promise<boolean> { | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| await exec('git', ['rev-parse', '--verify', `refs/heads/${branch}`], { | ||||||||||||||||||||||
| cwd: repoRoot, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| return true; | ||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||
| return false; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| async function detectMainBranchUncached(repoRoot: string): Promise<string> { | ||||||||||||||||||||||
| // Try remote HEAD reference first | ||||||||||||||||||||||
| const branch = await resolveOriginHead(repoRoot); | ||||||||||||||||||||||
|
|
@@ -168,10 +180,13 @@ async function detectMainBranchUncached(repoRoot: string): Promise<string> { | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Check common default branch names | ||||||||||||||||||||||
| // Check common default branch names (remote-tracking first, then local) | ||||||||||||||||||||||
| for (const candidate of ['main', 'master']) { | ||||||||||||||||||||||
| if (await remoteTrackingRefExists(repoRoot, candidate)) return candidate; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| for (const candidate of ['main', 'master']) { | ||||||||||||||||||||||
| if (await localBranchExists(repoRoot, candidate)) return candidate; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Empty repo (no commits yet) — use configured default branch or fall back to "main" | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
|
|
@@ -393,6 +408,26 @@ export async function createWorktree( | |||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // Validate the start-point ref exists before attempting worktree creation | ||||||||||||||||||||||
| const startRef = baseBranch || 'HEAD'; | ||||||||||||||||||||||
| try { | ||||||||||||||||||||||
| await exec('git', ['rev-parse', '--verify', startRef], { cwd: repoRoot }); | ||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||
| const isEmptyRepo = await exec('git', ['rev-list', '-n1', '--all'], { cwd: repoRoot }) | ||||||||||||||||||||||
| .then(({ stdout }) => !stdout.trim()) | ||||||||||||||||||||||
| .catch(() => true); | ||||||||||||||||||||||
|
Comment on lines
+416
to
+418
|
||||||||||||||||||||||
| const isEmptyRepo = await exec('git', ['rev-list', '-n1', '--all'], { cwd: repoRoot }) | |
| .then(({ stdout }) => !stdout.trim()) | |
| .catch(() => true); | |
| let isEmptyRepo = false; | |
| try { | |
| const { stdout } = await exec('git', ['rev-list', '-n1', '--all'], { cwd: repoRoot }); | |
| isEmptyRepo = !stdout.trim(); | |
| } catch (err) { | |
| throw err; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New behavior is introduced in
detectMainBranchUncached()(fallback to localrefs/heads/*) andcreateWorktree()(start-point ref validation + custom error messages), but there are no tests in this PR exercising these paths. Sinceelectron/ipc/git.test.tsalready mocks git commands, add targeted tests for: (1) no-remote + localmainexists => returnsmain; (2)createWorktreewith missingbaseBranchin a non-empty repo => throws the new “Branch … does not exist” error; (3) empty repo => throws the new “no commits” error.