Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to push local non-bare repository #1108

Open
sudeeptarlekar opened this issue Dec 27, 2024 · 2 comments
Open

Not able to push local non-bare repository #1108

sudeeptarlekar opened this issue Dec 27, 2024 · 2 comments
Labels
upstream An upstream issue with libgit2

Comments

@sudeeptarlekar
Copy link

Description

Hello all,
I am working on some Git related service and I want to push to remote repo, which happens to be clone of local repo on filesystem. But when try to push from clone to remote(basically on local file system) I end up with following error.

Error: local push doesn't (yet) support pushing to non-bare repos.; class=Invalid (3); code=BareRepo (-8)

Explanation

May be following sequence diagram will help to clarify what I am trying to achieve. The reason we are creating clone of repo, is because we are developing tool to work with multiple repos, so we don't want to spoil anything in main repo and we perform all required operation in clones repo and once everything is complete without error just push the changes to remote branch.
Image

Sample code

use std::path::PathBuf;

use anyhow::{Context, Result};
use git2::{Cred, PushOptions, RemoteCallbacks, Repository};

fn push_branch(repo: &Repository, branch: &str) -> Result<()> {
    let private_key = PathBuf::from("/Users/sudeep.tarlekar/.ssh/id_rsa");

    let mut remote_callbacks = RemoteCallbacks::new();
    remote_callbacks.credentials(|_url, username_from_url, _allowed_types| {
        Cred::ssh_key(
            username_from_url.unwrap_or_default(),
            None,
            private_key.as_ref(),
            None,
        )
    });

    let mut push_options = PushOptions::new();
    push_options.remote_callbacks(remote_callbacks);

    let mut remote = repo
        .find_remote("origin")
        .context("remote `origin` is not configured for Git repository")?;

    let refspec = format!("refs/heads/{branch}:refs/remotes/{branch}");

    remote.push(&[&refspec], Some(&mut push_options))?;
}

Expectation

Should be able to push to non-bare local repo. If it is not allowed then what is possible workaround?

Thanks in advance 😃

@weihanglo
Copy link
Member

https://github.com/libgit2/libgit2/blob/5350142b9b88f961bd019615953dadcc2fdea342/src/libgit2/transports/local.c#L403-L413

The error message is from libgit2. It doesn't support that kind of operation. As a binding I don't think we could do anything but wait for upstream.

@weihanglo weihanglo added the upstream An upstream issue with libgit2 label Dec 27, 2024
@sudeeptarlekar
Copy link
Author

That's odd, if I execute git push branch_X:branch_X then Git does work, and I see changes in local repo as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream An upstream issue with libgit2
Projects
None yet
Development

No branches or pull requests

2 participants