rust-lang / rust-lang/git2-rs

Issue fetching submodules with credentials callback

Open
#1,167 0 comments 1 reaction 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

Initialising and updating submodules fails when trying to authenticate via SSH key using the credentials callback. From the error message it seems as though setting the callback to pass to libgit2 doesn't actually get passed over? Either that or this is a libgit2 issue.

Details

Setting a callback with an SSH key credential for cloning the repo works correctly. But after cloning the repo and trying to initialise plus update the submodules, with a credential calllback identical to the one for cloning the repo, the following error occurs:

Error { code: -16, klass: 34, message: "remote authentication required but no callback set" }

Code snippet example to replicate issue:

let state = RefCell::new(State::default());

    let mut cb = RemoteCallbacks::new();
    cb.transfer_progress(|stats| {
        let mut state = state.borrow_mut();
        state.progress = Some(stats.to_owned());
        state.display();
        true
    });

    cb.credentials(|_url, username_from_url, _allowed| {
        Cred::ssh_key(
            username_from_url.unwrap_or("git"),
            None,
            ssh_key_path, // Relevant ssh key located in .ssh directory
            None,
        )
    });

    let mut co = CheckoutBuilder::new();
    co.progress(|path, cur, total| {
        let mut state = state.borrow_mut();
        state.path = path.map(Path::to_path_buf);
        state.current = cur;
        state.total = total;
        state.display();
    });

    let mut fo = FetchOptions::new();
    fo.remote_callbacks(cb);

    let repo = RepoBuilder::new()
        .fetch_options(fo)
        .with_checkout(co)
        .branch("develop")
        .clone(
            repo_url,
            Path::new("/tmp/test-git2/"),
        )?;

    for mut submodule in repo.submodules()? {
        let mut update_opts = SubmoduleUpdateOptions::new();
        let mut cb = RemoteCallbacks::new();
        cb.credentials(|_url, username_from_url, _allowed| {
            Cred::ssh_key(
                username_from_url.unwrap_or("git"),
                None,
                ssh_key_path,
                None,
            )
        });
        let mut fo = FetchOptions::new();
        fo.remote_callbacks(cb);

        update_opts.fetch(fo);

        submodule
            .update(true, Some(&mut update_opts))
            .map_err(|err| color_eyre::eyre::eyre!("Failed to update submodule: {:?}", err))?;
    }

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 reproduced flow using RepoBuilder::clone, RemoteCallbacks::credentials, SubmoduleUpdateOptions::new, update_opts.fetch, and submodule.update. Compare how the callback is passed during cloning with how submodule updates perform their fetch, then confirm the SSH-key callback is honored and the submodule update completes without the “no callback set” error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.