rust-lang / rust-lang/git2-rs

Submodule update fails when submodule OID is detached

Open
#1,164 1 comment 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

Summary

Tried to clone and submodule-update repo where some submodules point to detached commits; got "object not found" error; could not find any configuration option in git2-rs that would help.

Expected to work with default settings, since it works fine in git command line.

Details

I have a program built using git2-rs, and am trying to run it on the repo https://github.com/Cork-Technology/Depeg-swap .

This repo is interesting in that two of its submodules point to commits which are not in any branch in the target repo, namely https://github.com/Uniswap/v2-core/commit/89b08b5b8deac65f7bea81c5eea2183e31dc5dad and https://github.com/Uniswap/v2-periphery/commit/6820d8be7e111cbda3358226cc3a13489b6a625e .

Submodule::update fails when running on these submodules.

Simplified code:

    info!("Cloning repo");
    let path = Path::new("/some/tmp/path");
    let mut repo = git2::build::RepoBuilder::new();
    repo.clone("https://github.com/Cork-Technology/Depeg-swap", path)?;

    let local_repo = git2::Repository::open(path)?;
    for mut submodule in local_repo.submodules()? {
        info!("Updating submodule: {:?}", submodule.name());
        let mut update_opts = git2::SubmoduleUpdateOptions::new();
        update_opts.allow_fetch(true);
        match submodule.update(true, Some(&mut update_opts)) {
            Ok(_) => (),
            Err(e) => warn!("Failed to update submodule: {:?}", e),
        }
    }

Output (error lines bolded):

T08:15:59.175948Z  INFO myprogram Cloning repo
2025-05-27T08:16:01.035119Z  INFO myprogram: Updating submodule: Some("lib/BokkyPooBahsDateTimeLibrary")
2025-05-27T08:16:01.931884Z  INFO myprogram: Updating submodule: Some("lib/Cork-Hook")
2025-05-27T08:16:02.528602Z  INFO myprogram: Updating submodule: Some("lib/forge-std")
2025-05-27T08:16:03.243375Z  INFO myprogram: Updating submodule: Some("lib/openzeppelin-contracts")
2025-05-27T08:16:19.119010Z  INFO myprogram: Updating submodule: Some("lib/openzeppelin-contracts-upgradeable")
2025-05-27T08:16:31.146724Z  INFO  myprogram: Updating submodule: Some("lib/openzeppelin-foundry-upgrades")
2025-05-27T08:16:31.903167Z  INFO myprogram: Updating submodule: Some("lib/permit2")
2025-05-27T08:16:33.215988Z  INFO myprogram: Updating submodule: Some("lib/prb-math")
2025-05-27T08:16:35.373183Z  INFO myprogram: Updating submodule: Some("lib/v2-core")
**2025-05-27T08:16:36.121215Z  WARN myprogram: Failed to update submodule: Error { code: -3, klass: 9, message: "object not found - no match for id (89b08b5b8deac65f7bea81c5eea2183e31dc5dad)" }**
2025-05-27T08:16:36.121263Z  INFO myprogram: Updating submodule: Some("lib/v2-periphery")
**2025-05-27T08:16:36.954935Z  WARN myprogram: Failed to update submodule: Error { code: -3, klass: 9, message: "object not found - no match for id (6820d8be7e111cbda3358226cc3a13489b6a625e)" }**

I have scoured the docs and source of git2-rs looking for options that could potentially allow this operation to succeed. The only one I found is allow_fetch, which is already on. I tried looking through libgit2 as well, though I did not get very far.

Running git submodule update from the command line works without a hitch, so it seems sensible to hope that this would work with default settings. Interestingly, when cloning the v2-core and v2-periphery repositories directly, I am not able to checkout the respective revisions (89b08b and 6820d8be7e) without first doing a git fetch origin.

Anyway,

Ask

Either

  1. There is a way to make this work with git2-rs that I did not find, or
  2. This is a missing configuration option in git2-rs, or
  3. This is a missing configuration option in libgit2, or
  4. It should just work; this is a bug in either git2-rs or libgit2

Which one is it?

And if it's (1), I would really appreciate some help making this work.

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 with the Submodule::update path and the RepoBuilder::clone and SubmoduleUpdateOptions::allow_fetch entry points shown in the report. Reproduce the failure against the Depeg-swap repository and compare the behavior with command-line git submodule update, focusing on detached submodule commits. Done means establishing whether git2-rs or libgit2 needs a fix or configuration change and adding a regression test if the relevant test location is found.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.