linera-io/linera-protocol · error

Failed to get current branch name

Error message

Failed to get current branch name

What it means

In local mode, GithubContext::get_local_git_info opens the repo with git2, resolves HEAD, and reads the branch shorthand. This error means HEAD is a branch but git2 could not produce a valid shorthand for its name — in practice a non-UTF-8 or otherwise malformed reference name. (A detached HEAD takes the other branch and fails with a different message.)

Source

Thrown at linera-summary/src/github.rs:94

    /// Returns the commit hash at the PR's head.
    pub fn pr_commit_hash(&self) -> &str {
        &self.pr_commit_hash
    }

    /// Returns the repository the PR belongs to.
    pub fn repository(&self) -> &GithubRepository {
        &self.repository
    }

    fn get_local_git_info() -> Result<(String, String, String)> {
        let repo = git2::Repository::open_from_env().context("Failed to open git repository")?;

        let head = repo.head()?;
        let commit_hash = head.peel_to_commit()?.id().to_string();

        let branch_name = if head.is_branch() {
            head.shorthand()
                .ok_or_else(|| anyhow!("Failed to get current branch name"))?
                .to_string()
        } else {
            anyhow::bail!("HEAD is not on a branch - it may be detached");
        };

        // This local mode is only used for testing, so we're just hardcoding `main` as the base branch for now.
        Ok((commit_hash, branch_name, "main".to_string()))
    }

    fn from_env(is_local: bool, pr_number: Option<u64>) -> Result<Self> {
        let env_pr_commit_hash = env::var("GITHUB_PR_COMMIT_HASH");
        let env_pr_branch = env::var("GITHUB_PR_BRANCH");
        let env_base_branch = env::var("GITHUB_BASE_BRANCH");
        let env_pr_number = env::var("GITHUB_PR_NUMBER");

        let (pr_commit_hash, pr_branch, base_branch, pr_number) = if is_local {
            let (commit_hash, branch_name, base) = Self::get_local_git_info()?;
            (

View on GitHub (pinned to 6c226ddcb3)

Solutions

  1. Check out a branch with a plain UTF-8 name: git checkout -b summary-run && rerun
  2. Inspect the ref: git symbolic-ref HEAD and the file under .git/refs/ or packed-refs for encoding damage
  3. If you did not intend local mode, run in CI mode instead where the branch comes from GITHUB_PR_BRANCH

Example fix

# before
(non-UTF-8 branch checked out)
# after
git checkout -b summary-run && linera-summary --local ...
Defensive patterns

Strategy: validation

Validate before calling

// Rust: pre-check that HEAD resolves to a named UTF-8 branch
fn head_branch_name(repo: &git2::Repository) -> Option<String> {
    let head = repo.head().ok()?;
    if !head.is_branch() { return None; }
    head.shorthand().map(|s| s.to_string())
}

Type guard

fn has_readable_branch(repo: &git2::Repository) -> bool {
    head_branch_name(repo).is_some()
}

Try / catch

match GithubContext::get_local_git_info() {
    Ok(info) => info,
    Err(e) => {
        eprintln!("local git context unavailable ({e:#}); checkout a UTF-8-named branch or use CI mode");
        return Err(e);
    }
}

Prevention

When it happens

Trigger: Running linera-summary in local mode from a working copy whose current branch ref name is not representable as a shorthand string. Normal UTF-8 branch names never hit this.

Common situations: Branch names with exotic or non-UTF-8 bytes created by scripting or on other filesystems; corrupted refs under .git/refs or packed-refs. Rare in everyday use.

Related errors


AI-assisted analysis of linera-io/linera-protocol@6c226ddcb3 (2026-08-22). Data as JSON: /api/errors/5bc7cf9f156d2c98. Report an issue: GitHub.