rust-lang / rust-lang/git2-rs

Request failed with status code: 401

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

I have fill all of the values, I want to know what's wrong with these code.
OS: Windows 11
rustc: 1.70.0 (90c541806 2023-05-31)
git2: 0.17.2

My code:

fn push_repo() -> Result<(), git2::Error> {
    let repo = Repository::open("cache/repo")?; 

    let mut remote = repo.find_remote("origin")?;

    remote.connect_auth(Direction::Push, Some(create_callbacks()), None).unwrap();
    repo.remote_add_push("origin", "refs/heads/temp:refs/heads/temp").unwrap();
    let mut push_options = PushOptions::default();
    let callbacks = create_callbacks();
    push_options.remote_callbacks(callbacks);
    remote.push(&["refs/heads/temp:refs/heads/temp"], Some(&mut push_options)).unwrap();

    std::mem::drop(remote);

    Ok(())
}
fn create_callbacks<'a>() -> RemoteCallbacks<'a>{
    let mut callbacks = RemoteCallbacks::new();
    callbacks.credentials(|_url, username_from_url, _allowed_types| {
        // Use the username from the url or a default one
        let username = username_from_url.unwrap_or("git");
        Cred::ssh_key(
            username,
            Some(std::path::Path::new("ssh_key/pushzig_rsa.pub")),
            std::path::Path::new("ssh_key/pushzig_rsa"),
            None,
        )
    });
    callbacks
}

Error:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { code: -1, klass: 34, message: "request failed with status code: 401" }', src\main.rs:261:74
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\zos-index-generator.exe new` (exit code: 101)

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 at src/main.rs:261 and inspect push_repo and create_callbacks, especially the SSH credential setup and the remote.push call. Reproduce the 401 with the stated Rust and git2 versions, then determine whether the failure is caused by the example's credentials or by git2-rs; document a confirmed cause and reproduction details.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.