Skip to content

fix(git): validate refs before worktree creation and improve main branch detection#61

Merged
johannesjo merged 1 commit intojohannesjo:mainfrom
FourWindff:fix/worktree-creation-validation
Apr 12, 2026
Merged

fix(git): validate refs before worktree creation and improve main branch detection#61
johannesjo merged 1 commit intojohannesjo:mainfrom
FourWindff:fix/worktree-creation-validation

Conversation

@FourWindff
Copy link
Copy Markdown
Contributor

Problem

  1. detectMainBranch() only checks remote-tracking refs (refs/remotes/origin/*) when looking for main/master. In repositories without a remote (or before the first fetch), this causes the detection to fail even though a local main or master branch exists.

  2. createWorktree() does not validate that the start-point ref exists before running git worktree add. When the ref is missing (e.g. in an empty repo or with a typo), Git produces a cryptic error message that is hard for users to diagnose.

Solution

  • Add a localBranchExists() helper that checks refs/heads/<branch>.
  • Update detectMainBranch() to fall back to local branch refs when no remote-tracking ref is found.
  • Add start-point ref validation in createWorktree() before attempting git worktree add, with clear error messages for:
    • Empty repositories (no commits yet)
    • Non-existent branch references

…tion validation

Introduced a new function to check for the existence of local branches. Enhanced the worktree creation process by validating the start-point reference and handling cases for empty repositories and non-existent branches more gracefully.
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves git-related robustness in the Electron IPC layer by making default-branch detection work in repos without remotes and by validating the start-point ref before creating a new worktree, so users get clearer errors.

Changes:

  • Add localBranchExists() to detect main/master via refs/heads/* when no remote-tracking refs exist.
  • Update detectMainBranchUncached() to try remote-tracking refs first, then local branch refs.
  • Add preflight start-point validation in createWorktree() with friendlier errors for empty repos and missing base branches.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread electron/ipc/git.ts
Comment on lines +416 to +418
const isEmptyRepo = await exec('git', ['rev-list', '-n1', '--all'], { cwd: repoRoot })
.then(({ stdout }) => !stdout.trim())
.catch(() => true);
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The isEmptyRepo detection treats any failure of git rev-list -n1 --all as an empty repository (catch(() => true)), which can mask other problems (e.g., repoRoot not being a git repo, missing permissions, or git not available) and surface a misleading “no commits” error. Consider only classifying as empty when the command succeeds and returns no output; otherwise rethrow (or throw a more specific “not a git repository”/original git error).

Suggested change
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;
}

Copilot uses AI. Check for mistakes.
Comment thread electron/ipc/git.ts
Comment on lines +183 to +189
// 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;
}
Copy link

Copilot AI Apr 9, 2026

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 local refs/heads/*) and createWorktree() (start-point ref validation + custom error messages), but there are no tests in this PR exercising these paths. Since electron/ipc/git.test.ts already mocks git commands, add targeted tests for: (1) no-remote + local main exists => returns main; (2) createWorktree with missing baseBranch in a non-empty repo => throws the new “Branch … does not exist” error; (3) empty repo => throws the new “no commits” error.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Owner

Thank you very much


Generated by Claude Code

@johannesjo johannesjo merged commit 5f66a24 into johannesjo:main Apr 12, 2026
5 checks passed
@FourWindff FourWindff deleted the fix/worktree-creation-validation branch April 23, 2026 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants