rust-lang / rust-lang/git2-rs

AnnotatedCommit::refname panicks on option unwrapping

Open
#690 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
450
Avg merge
11m
Merged PRs (30d)
1

Description

Hello all,

I'm in the process of trying to use git2 to create a worktree pointing to the merge-base of two references.

I figured out how to get the merge-base, but this gives me an Oid, and AFAIU I need a Reference to be able to create a worktree pointing to it, because WorktreeAddOptions requires it. So I'm trying to get a Reference back from this Oid that merge_base returned me.

The way I'm currently trying this is:

    let base_oid = repo
        .merge_base(
            base_ref
                .target()
                .context("getting target for base reference")?,
            to_check_ref
                .target()
                .context("getting target for to-check reference")?,
        )
        .context("finding the merge-base of the base branch and the to-check reference")?;
    let base_annotated_commit = repo
        .find_annotated_commit(base_oid)
        .context("creating an annotated commit for the merge-base")?;
    let base_refname = base_annotated_commit
        .refname()
        .context("retrieving refname from annotated commit for the merge-base")?;
    let base_ref = repo
        .resolve_reference_from_short_name(base_refname)
        .context("retrieving reference for the merge-base")?
        .resolve()
        .context("resolving merge-base reference")?;

However, this panics at the base_annotated_commit.refname() step, with the following backtrace:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/ekleog/.cargo/registry/src/github.com-1ecc6299db9ec823/git2-0.13.17/src/merge.rs:40:84
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::option::Option<T>::unwrap
             at /build/rustc-1.49.0-src/library/core/src/option.rs:386:21
   4: git2::merge::AnnotatedCommit::refname_bytes
             at /home/ekleog/.cargo/registry/src/github.com-1ecc6299db9ec823/git2-0.13.17/src/merge.rs:40:18
   5: git2::merge::AnnotatedCommit::refname
             at /home/ekleog/.cargo/registry/src/github.com-1ecc6299db9ec823/git2-0.13.17/src/merge.rs:35:24
   6: nixpkgs_check::run
             at ./src/main.rs:80:24
   7: nixpkgs_check::main
             at ./src/main.rs:142:11
   8: core::ops::function::FnOnce::call_once
             at /build/rustc-1.49.0-src/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

The full reproducer is at https://github.com/Ekleog/nixpkgs-check/tree/libgit2-repro ; it can be reproduced by doing eg. cargo run -- HEAD main, that will get the merge-base between HEAD and main, and then try to check it out in a worktree created by tempdir, without success.

What do you think about this issue? Also, do you know of a way to do what I'm trying to do?

Anyway, thank you for git2, it looks quite neat to use from my documentation-side look at it, even though for now my attempt was cut short :)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/merge.rs at AnnotatedCommit::refname and refname_bytes, then run the linked reproducer with cargo run -- HEAD main. Check why the merge-base path produces no refname and ensure the reproducer no longer panics while preserving the expected reference or error behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.