Skip to content

Not able to push local non-bare repository #1108

Closed as not planned
Closed as not planned
@sudeeptarlekar

Description

@sudeeptarlekar

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 😃

Metadata

Metadata

Assignees

No one assigned

    Labels

    upstreamAn upstream issue with libgit2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions