Skip to content

checkoutOrCreateBranch crashes on repos with no commits (empty/unborn branch) #487

@lazmo88

Description

@lazmo88

Bug

When creating a session targeting a git repository that has been initialized (git init) but has no commits yet, the session creation fails with:

Checking out master...
Branch "master" does not exist. Pass createBranch to create it.

Root Cause

In server/git-utils.ts, checkoutOrCreateBranch() runs git checkout <branch> which fails on empty repos because the branch doesn't technically exist yet (it's an "unborn" branch — HEAD points to refs/heads/master symbolically, but no commit backs it).

Additionally, getRepoInfo() uses git rev-parse --abbrev-ref HEAD which fails on unborn branches and falls back to "HEAD", causing the currentBranch !== body.branch check to always be true — so it always attempts the checkout even though we're already on the intended branch.

Steps to Reproduce

  1. Create an empty git repo: mkdir test && cd test && git init
  2. Open Companion and create a session pointing to this directory
  3. Session creation fails at the "Checking out master..." step

Proposed Fix

  1. getRepoInfo(): Fall back to git symbolic-ref --short HEAD when rev-parse --abbrev-ref HEAD fails — this correctly returns the unborn branch name (e.g. master or main)
  2. checkoutOrCreateBranch(): Detect empty repos (no commits) early. If the requested branch matches the current unborn branch, return successfully. If a different branch is requested, use git checkout --orphan <branch> instead of regular checkout.

Environment

  • Companion v0.72.0
  • Repo: git init with files but zero commits
  • Branch: master (default from git init)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions