wrap remote

This commit is contained in:
Nikita Galaiko 2023-08-30 15:09:59 +02:00 committed by GitButler
parent 5fc365658e
commit 1adb0e4edb
7 changed files with 68 additions and 33 deletions

View File

@ -68,8 +68,6 @@ impl Repository {
.with_context(|| format!("{}: failed to open git repository", path.display()))?;
git_repository
.odb()
.map_err(Error::Git)?
.add_disk_alternate(project_objects_path.to_str().unwrap())
.map_err(Error::Git)?;
@ -90,7 +88,6 @@ impl Repository {
.with_context(|| format!("{}: failed to initialize git repository", path.display()))?;
git_repository
.odb()?
.add_disk_alternate(project_objects_path.to_str().unwrap())
.context("failed to add disk alternate")?;
@ -126,7 +123,7 @@ impl Repository {
&self.project_id
}
fn remote(&self) -> Result<Option<(git2::Remote, String)>> {
fn remote(&self) -> Result<Option<(git::Remote, String)>> {
let user = self.users_store.get().context("failed to get user")?;
let project = self
.project_store
@ -193,11 +190,7 @@ impl Repository {
fetch_opts.custom_headers(headers);
remote
.fetch(
&["refs/heads/*:refs/remotes/*"],
Some(&mut fetch_opts),
None,
)
.fetch(&["refs/heads/*:refs/remotes/*"], Some(&mut fetch_opts))
.with_context(|| format!("failed to pull from remote {}", remote.url().unwrap()))?;
tracing::info!(
@ -246,7 +239,7 @@ impl Repository {
// Push to the remote
remote
.push(&[remote_refspec], Some(&mut push_options))
.push(&[&remote_refspec], Some(&mut push_options))
.with_context(|| {
format!(
"failed to push refs/heads/current to {}",

View File

@ -17,3 +17,6 @@ pub use branch::*;
mod tree;
pub use tree::*;
mod remote;
pub use remote::*;

View File

@ -1,4 +1,4 @@
use super::{Commit, Result};
use super::{Commit, Result, Tree};
pub struct Reference<'repo> {
reference: git2::Reference<'repo>,
@ -23,8 +23,8 @@ impl<'repo> Reference<'repo> {
self.reference.peel_to_commit().map(Commit::from)
}
pub fn peel_to_tree(&self) -> Result<git2::Tree<'repo>> {
self.reference.peel_to_tree()
pub fn peel_to_tree(&self) -> Result<Tree<'repo>> {
self.reference.peel_to_tree().map(Tree::from)
}
pub fn rename(

View File

@ -0,0 +1,37 @@
use super::Result;
pub struct Remote<'repo> {
inner: git2::Remote<'repo>,
}
impl<'repo> From<git2::Remote<'repo>> for Remote<'repo> {
fn from(inner: git2::Remote<'repo>) -> Self {
Self { inner }
}
}
impl<'repo> Remote<'repo> {
pub fn name(&self) -> Option<&str> {
self.inner.name()
}
pub fn url(&self) -> Option<&str> {
self.inner.url()
}
pub fn push(
&mut self,
refspec: &[&str],
opts: Option<&mut git2::PushOptions<'_>>,
) -> Result<()> {
self.inner.push(refspec, opts)
}
pub fn fetch(
&mut self,
refspec: &[&str],
opts: Option<&mut git2::FetchOptions<'_>>,
) -> Result<()> {
self.inner.fetch(refspec, opts, None)
}
}

View File

@ -1,6 +1,6 @@
use std::path;
use super::{Branch, Commit, Reference, Result, Tree};
use super::{Branch, Commit, Reference, Remote, Result, Tree};
// wrapper around git2::Repository to get control over how it's used.
pub struct Repository(git2::Repository);
@ -29,12 +29,8 @@ impl Repository {
Ok(Repository(inner))
}
pub fn odb(&self) -> Result<git2::Odb> {
self.0.odb()
}
pub fn revparse_single(&self, spec: &str) -> Result<git2::Object> {
self.0.revparse_single(spec)
pub fn add_disk_alternate(&self, path: &str) -> Result<()> {
self.0.odb().and_then(|odb| odb.add_disk_alternate(path))
}
pub fn find_annotated_commit(&self, id: git2::Oid) -> Result<git2::AnnotatedCommit<'_>> {
@ -212,12 +208,12 @@ impl Repository {
self.0.statuses(options)
}
pub fn remote_anonymous(&self, url: &str) -> Result<git2::Remote> {
self.0.remote_anonymous(url)
pub fn remote_anonymous(&self, url: &str) -> Result<Remote> {
self.0.remote_anonymous(url).map(Remote::from)
}
pub fn find_remote(&self, name: &str) -> Result<git2::Remote> {
self.0.find_remote(name)
pub fn find_remote(&self, name: &str) -> Result<Remote> {
self.0.find_remote(name).map(Remote::from)
}
pub fn find_branch(&self, name: &str, branch_type: git2::BranchType) -> Result<Branch> {
@ -228,7 +224,7 @@ impl Repository {
self.0.refname_to_id(name)
}
pub fn checkout_head(&self, opts: Option<&mut git2::build::CheckoutBuilder<'_>>) -> Result<()> {
pub fn checkout_head(&self, opts: Option<&mut git2::build::CheckoutBuilder>) -> Result<()> {
self.0.checkout_head(opts)
}
@ -266,12 +262,12 @@ impl Repository {
}
#[cfg(test)]
pub fn remote(&self, name: &str, url: &str) -> Result<git2::Remote> {
self.0.remote(name, url)
pub fn remote(&self, name: &str, url: &str) -> Result<Remote> {
self.0.remote(name, url).map(Remote::from)
}
#[cfg(test)]
pub fn references(&self) -> Result<impl Iterator<Item = Result<super::Reference>>> {
pub fn references(&self) -> Result<impl Iterator<Item = Result<Reference>>> {
self.0
.references()
.map(|iter| iter.map(|reference| reference.map(Reference::from)))

View File

@ -1,5 +1,7 @@
use std::path;
use git2::TreeEntry;
use super::Result;
pub struct Tree<'repo> {
@ -34,4 +36,8 @@ impl<'repo> Tree<'repo> {
{
self.tree.walk(mode, callback)
}
pub fn get_name(&self, filename: &str) -> Option<TreeEntry> {
self.tree.get_name(filename)
}
}

View File

@ -353,7 +353,7 @@ impl<'repository> Repository<'repository> {
pub fn git_unstage_files<P: AsRef<std::path::Path>>(&self, paths: Vec<P>) -> Result<()> {
let head_tree = self.git_repository.head()?.peel_to_tree()?;
let mut head_index = git2::Index::new()?;
head_index.read_tree(&head_tree)?;
head_index.read_tree((&head_tree).into())?;
let mut index = self.git_repository.index()?;
for path in paths {
let path = path.as_ref();
@ -380,7 +380,7 @@ impl<'repository> Repository<'repository> {
// returns a remote and makes sure that the push url is an ssh url
// if url is already ssh, or not set at all, then it returns the remote as is.
fn get_remote(&'repository self, name: &str) -> Result<git2::Remote<'repository>> {
fn get_remote(&'repository self, name: &str) -> Result<git::Remote<'repository>> {
let remote = self
.git_repository
.find_remote(name)
@ -429,7 +429,7 @@ impl<'repository> Repository<'repository> {
);
match remote.push(
&[format!("{}:refs/heads/{}", head, branch.branch())],
&[&format!("{}:refs/heads/{}", head, branch.branch())],
Some(&mut git2::PushOptions::new().remote_callbacks(remote_callbacks)),
) {
Ok(()) => {
@ -500,10 +500,10 @@ impl<'repository> Repository<'repository> {
fetch_opts.remote_callbacks(remote_callbacks);
fetch_opts.prune(git2::FetchPrune::On);
let refspec = format!("+refs/heads/*:refs/remotes/{}/*", remote_name);
let refspec = &format!("+refs/heads/*:refs/remotes/{}/*", remote_name);
tracing::info!("{}: git fetch {}", self.project.id, &refspec);
match remote.fetch(&[refspec], Some(&mut fetch_opts), None) {
match remote.fetch(&[refspec], Some(&mut fetch_opts)) {
Ok(()) => {
tracing::info!("{}: fetched {}", self.project.id, remote_name);
return Ok(());