more wrapping

This commit is contained in:
Nikita Galaiko 2023-08-30 17:03:00 +02:00 committed by GitButler
parent 55fee3056c
commit 9c4bbb5595
33 changed files with 365 additions and 264 deletions

View File

@ -366,7 +366,7 @@ impl App {
let commit = project_repository
.git_repository
.find_commit(git2::Oid::from_str(commit_id).unwrap())?;
.find_commit(commit_id.parse()?)?;
let parent = commit.parent(0).context("failed to get parent commit")?;
let commit_tree = commit.tree().context("failed to get commit tree")?;

View File

@ -440,7 +440,9 @@ impl Repository {
tree_builder
.insert(
"session",
build_session_tree(self).context("failed to build session tree")?,
build_session_tree(self)
.context("failed to build session tree")?
.into(),
0o040000,
)
.context("failed to insert session tree")?;
@ -448,30 +450,36 @@ impl Repository {
.insert(
"wd",
build_wd_tree(self, project_repository)
.context("failed to build working directory tree")?,
.context("failed to build working directory tree")?
.into(),
0o040000,
)
.context("failed to insert wd tree")?;
tree_builder
.insert(
"logs",
build_log_tree(self, project_repository).context("failed to build logs tree")?,
build_log_tree(self, project_repository)
.context("failed to build logs tree")?
.into(),
0o040000,
)
.context("failed to insert logs tree")?;
tree_builder
.insert(
"branches",
build_branches_tree(self).context("failed to build branches tree")?,
build_branches_tree(self)
.context("failed to build branches tree")?
.into(),
0o040000,
)
.context("failed to insert branches tree")?;
let tree = tree_builder.write().context("failed to write tree")?;
let tree_id = tree_builder.write().context("failed to write tree")?.into();
let user = self.users_store.get().context("failed to get user")?;
let commit_oid = write_gb_commit(tree, self, &user).context("failed to write gb commit")?;
let commit_oid =
write_gb_commit(tree_id, self, &user).context("failed to write gb commit")?;
tracing::info!(
"{}: flushed session {} into commit {}",
@ -581,7 +589,7 @@ impl Repository {
fn migrate_history(&self, project: &projects::Project) -> Result<bool> {
let refname = format!("refs/gitbutler-{}/current", project.id);
let repo = git2::Repository::open(&project.path).context("failed to open repository")?;
let repo = git::Repository::open(&project.path).context("failed to open repository")?;
let reference = repo.find_reference(&refname);
match reference {
Err(e) => {
@ -597,29 +605,21 @@ impl Repository {
}
Result::Ok(reference) => {
let mut walker = repo.revwalk()?;
walker.push(reference.target().unwrap())?;
walker.push(reference.target().unwrap().into())?;
walker.set_sorting(git2::Sort::TOPOLOGICAL | git2::Sort::REVERSE)?;
let mut migrated = false;
for id in walker {
let id = id?;
let commit = repo.find_commit(id)?;
let commit = repo.find_commit(id.into())?;
let copy_tree = |tree: git2::Tree| -> Result<git2::Oid> {
let mut tree_builder = self.git_repository.treebuilder(None)?;
for tree_entry in tree.iter() {
let path = tree_entry.name().unwrap();
let oid = tree_entry.id();
let mode = tree_entry.filemode();
tree_builder
.insert(path, oid, mode)
.context("failed to insert tree entry")?;
}
let copy_tree = |tree: &git::Tree| -> Result<git::Oid> {
let tree_builder = self.git_repository.treebuilder(Some(tree))?;
let tree_oid = tree_builder.write()?;
Ok(tree_oid)
Ok(tree_oid.into())
};
let tree = self.git_repository.find_tree(copy_tree(commit.tree()?)?)?;
let tree = self.git_repository.find_tree(copy_tree(&commit.tree()?)?)?;
match self.git_repository.head() {
Result::Ok(head) => {
@ -666,7 +666,7 @@ impl Repository {
fn build_wd_tree(
gb_repository: &Repository,
project_repository: &project_repository::Repository,
) -> Result<git2::Oid> {
) -> Result<git::Oid> {
match gb_repository
.git_repository
.find_reference("refs/heads/current")
@ -677,7 +677,9 @@ fn build_wd_tree(
// and the session files
let tree = reference.peel_to_tree()?;
let wd_tree_entry = tree.get_name("wd").unwrap();
let wd_tree = gb_repository.git_repository.find_tree(wd_tree_entry.id())?;
let wd_tree = gb_repository
.git_repository
.find_tree(wd_tree_entry.id().into())?;
index.read_tree((&wd_tree).into())?;
let session_wd_reader = reader::DirReader::open(gb_repository.session_wd_path());
@ -727,15 +729,18 @@ fn build_wd_tree(
flags: 10, // normal flags for normal file (for the curious: https://git-scm.com/docs/index-format)
flags_extended: 0, // no extended flags
path: file_path.clone().into(),
id: gb_repository.git_repository.blob(file_content.as_bytes())?,
id: gb_repository
.git_repository
.blob(file_content.as_bytes())?
.into(),
})
.with_context(|| format!("failed to add index entry for {}", file_path))?;
}
let wd_tree_oid = index
.write_tree_to((&gb_repository.git_repository).into())
.with_context(|| "failed to write wd tree".to_string())?;
Ok(wd_tree_oid)
.context("failed to write wd tree")?;
Ok(wd_tree_oid.into())
}
Err(e) => {
if e.code() != git2::ErrorCode::NotFound {
@ -752,7 +757,7 @@ fn build_wd_tree(
fn build_wd_tree_from_repo(
gb_repository: &Repository,
project_repository: &project_repository::Repository,
) -> Result<git2::Oid> {
) -> Result<git::Oid> {
let mut index = git::Index::new()?;
// create a new in-memory git2 index and open the working one so we can cheat if none of the metadata of an entry has changed
@ -833,7 +838,7 @@ fn build_wd_tree_from_repo(
}
let tree_oid = index
.write_tree_to((&gb_repository.git_repository).into())
.write_tree_to(&gb_repository.git_repository)
.context("failed to write tree to repo")?;
Ok(tree_oid)
}
@ -924,7 +929,7 @@ fn add_wd_path(
flags: 10, // normal flags for normal file (for the curious: https://git-scm.com/docs/index-format)
flags_extended: 0, // no extended flags
path: rel_file_path.to_str().unwrap().to_string().into(),
id: blob,
id: blob.into(),
})
.with_context(|| format!("failed to add index entry for {}", rel_file_path.display()))?;
@ -952,8 +957,8 @@ fn sha256_digest(path: &std::path::Path) -> Result<String> {
Ok(format!("{:X}", digest))
}
fn build_branches_tree(gb_repository: &Repository) -> Result<git2::Oid> {
let mut index = git2::Index::new()?;
fn build_branches_tree(gb_repository: &Repository) -> Result<git::Oid> {
let mut index = git::Index::new()?;
let branches_dir = gb_repository.root().join("branches");
for file_path in fs::list_files(&branches_dir).context("failed to find branches directory")? {
@ -968,7 +973,7 @@ fn build_branches_tree(gb_repository: &Repository) -> Result<git2::Oid> {
}
let tree_oid = index
.write_tree_to((&gb_repository.git_repository).into())
.write_tree_to(&gb_repository.git_repository)
.context("failed to write index to tree")?;
Ok(tree_oid)
@ -977,8 +982,8 @@ fn build_branches_tree(gb_repository: &Repository) -> Result<git2::Oid> {
fn build_log_tree(
gb_repository: &Repository,
project_repository: &project_repository::Repository,
) -> Result<git2::Oid> {
let mut index = git2::Index::new()?;
) -> Result<git::Oid> {
let mut index = git::Index::new()?;
let logs_dir = project_repository.git_repository.path().join("logs");
for file_path in fs::list_files(logs_dir).context("failed to list log files")? {
@ -992,7 +997,7 @@ fn build_log_tree(
}
let tree_oid = index
.write_tree_to((&gb_repository.git_repository).into())
.write_tree_to(&gb_repository.git_repository)
.context("failed to write index to tree")?;
Ok(tree_oid)
@ -1000,7 +1005,7 @@ fn build_log_tree(
fn add_log_path(
rel_file_path: &std::path::Path,
index: &mut git2::Index,
index: &mut git::Index,
gb_repository: &Repository,
project_repository: &project_repository::Repository,
) -> Result<()> {
@ -1025,14 +1030,14 @@ fn add_log_path(
flags: 10, // normal flags for normal file (for the curious: https://git-scm.com/docs/index-format)
flags_extended: 0, // no extended flags
path: rel_file_path.to_str().unwrap().to_string().into(),
id: gb_repository.git_repository.blob_path(&file_path)?,
id: gb_repository.git_repository.blob_path(&file_path)?.into(),
})?;
Ok(())
}
fn build_session_tree(gb_repository: &Repository) -> Result<git2::Oid> {
let mut index = git2::Index::new()?;
fn build_session_tree(gb_repository: &Repository) -> Result<git::Oid> {
let mut index = git::Index::new()?;
// add all files in the working directory to the in-memory index, skipping for matching entries in the repo index
for file_path in
@ -1052,7 +1057,7 @@ fn build_session_tree(gb_repository: &Repository) -> Result<git2::Oid> {
}
let tree_oid = index
.write_tree_to((&gb_repository.git_repository).into())
.write_tree_to(&gb_repository.git_repository)
.context("failed to write index to tree")?;
Ok(tree_oid)
@ -1061,7 +1066,7 @@ fn build_session_tree(gb_repository: &Repository) -> Result<git2::Oid> {
// this is a helper function for build_gb_tree that takes paths under .git/gb/session and adds them to the in-memory index
fn add_file_to_index(
gb_repository: &Repository,
index: &mut git2::Index,
index: &mut git::Index,
rel_file_path: &std::path::Path,
abs_file_path: &std::path::Path,
) -> Result<()> {
@ -1084,7 +1089,7 @@ fn add_file_to_index(
flags: 10, // normal flags for normal file (for the curious: https://git-scm.com/docs/index-format)
flags_extended: 0, // no extended flags
path: rel_file_path.to_str().unwrap().into(),
id: blob,
id: blob.into(),
})
.with_context(|| format!("Failed to add file to index: {}", abs_file_path.display()))?;
@ -1095,10 +1100,10 @@ fn add_file_to_index(
// this is called once we have a tree of deltas, metadata and current wd snapshot
// and either creates or updates the refs/heads/current ref
fn write_gb_commit(
tree_id: git2::Oid,
tree_id: git::Oid,
gb_repository: &Repository,
user: &Option<users::User>,
) -> Result<git2::Oid> {
) -> Result<git::Oid> {
let comitter = git2::Signature::now("gitbutler", "gitbutler@localhost")?;
let author = match user {
None => comitter.clone(),

View File

@ -1,27 +1,41 @@
use super::Reference;
use super::{Commit, Oid, Result};
pub struct Branch<'repo> {
reference: Reference<'repo>,
branch: git2::Branch<'repo>,
}
impl<'repo> From<git2::Branch<'repo>> for Branch<'repo> {
fn from(branch: git2::Branch<'repo>) -> Self {
Self {
reference: branch.into_reference().into(),
}
Self { branch }
}
}
impl<'repo> Branch<'repo> {
pub fn name(&self) -> Option<String> {
self.reference.name().map(String::from)
pub fn name(&self) -> Option<&str> {
self.branch.get().name()
}
pub fn get(&self) -> &Reference<'repo> {
&self.reference
pub fn refname(&self) -> Option<&str> {
self.branch.get().name()
}
pub fn into_reference(self) -> Reference<'repo> {
self.reference
pub fn target(&self) -> Option<Oid> {
self.branch.get().target().map(Into::into)
}
pub fn upstream(&self) -> Result<Branch<'repo>> {
self.branch.upstream().map(Into::into)
}
pub fn refname_bytes(&self) -> &[u8] {
self.branch.get().name_bytes()
}
pub fn peel_to_commit(&self) -> Result<Commit<'repo>> {
self.branch.get().peel_to_commit().map(Into::into)
}
pub fn is_remote(&self) -> bool {
self.branch.get().is_remote()
}
}

View File

@ -1,4 +1,4 @@
use super::{Result, Tree};
use super::{Oid, Result, Tree};
pub struct Commit<'repo> {
commit: git2::Commit<'repo>,
@ -25,8 +25,8 @@ impl<'repo> From<&'repo Commit<'repo>> for &'repo git2::Commit<'repo> {
}
impl<'repo> Commit<'repo> {
pub fn id(&self) -> git2::Oid {
self.commit.id()
pub fn id(&self) -> Oid {
self.commit.id().into()
}
pub fn parent_count(&self) -> usize {
@ -34,15 +34,15 @@ impl<'repo> Commit<'repo> {
}
pub fn tree(&self) -> Result<Tree<'repo>> {
self.commit.tree().map(Tree::from)
self.commit.tree().map(Into::into)
}
pub fn tree_id(&self) -> git2::Oid {
self.commit.tree_id()
pub fn tree_id(&self) -> Oid {
self.commit.tree_id().into()
}
pub fn parent(&self, n: usize) -> Result<Commit<'repo>> {
self.commit.parent(n).map(Commit::from)
self.commit.parent(n).map(Into::into)
}
pub fn time(&self) -> git2::Time {

View File

@ -1,3 +1 @@
pub type Result<T> = std::result::Result<T, git2::Error>;

View File

@ -1,6 +1,6 @@
use std::path;
use super::{Repository, Result, Tree};
use super::{Oid, Repository, Result, Tree};
pub struct Index {
index: git2::Index,
@ -46,16 +46,16 @@ impl Index {
self.index.read_tree(tree.into())
}
pub fn write_tree_to(&mut self, repo: &Repository) -> Result<git2::Oid> {
self.index.write_tree_to(repo.into())
pub fn write_tree_to(&mut self, repo: &Repository) -> Result<Oid> {
self.index.write_tree_to(repo.into()).map(Into::into)
}
pub fn has_conflicts(&self) -> bool {
self.index.has_conflicts()
}
pub fn write_tree(&mut self) -> Result<git2::Oid> {
self.index.write_tree()
pub fn write_tree(&mut self) -> Result<Oid> {
self.index.write_tree().map(Into::into)
}
pub fn add(&mut self, entry: &git2::IndexEntry) -> Result<()> {

View File

@ -23,3 +23,6 @@ pub use remote::*;
mod index;
pub use index::*;
mod oid;
pub use oid::*;

32
src-tauri/src/git/oid.rs Normal file
View File

@ -0,0 +1,32 @@
use std::{fmt, str::FromStr};
#[derive(Debug, PartialEq, Copy, Clone, Hash, Eq)]
pub struct Oid {
oid: git2::Oid,
}
impl fmt::Display for Oid {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.oid.fmt(f)
}
}
impl FromStr for Oid {
type Err = git2::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
git2::Oid::from_str(s).map(Into::into)
}
}
impl From<git2::Oid> for Oid {
fn from(oid: git2::Oid) -> Self {
Self { oid }
}
}
impl From<Oid> for git2::Oid {
fn from(oid: Oid) -> Self {
oid.oid
}
}

View File

@ -1,4 +1,4 @@
use super::{Commit, Result, Tree};
use super::{Commit, Oid, Result, Tree};
pub struct Reference<'repo> {
reference: git2::Reference<'repo>,
@ -15,16 +15,20 @@ impl<'repo> Reference<'repo> {
self.reference.name()
}
pub fn target(&self) -> Option<git2::Oid> {
self.reference.target()
pub fn name_bytes(&self) -> &[u8] {
self.reference.name_bytes()
}
pub fn target(&self) -> Option<Oid> {
self.reference.target().map(Into::into)
}
pub fn peel_to_commit(&self) -> Result<Commit<'repo>> {
self.reference.peel_to_commit().map(Commit::from)
self.reference.peel_to_commit().map(Into::into)
}
pub fn peel_to_tree(&self) -> Result<Tree<'repo>> {
self.reference.peel_to_tree().map(Tree::from)
self.reference.peel_to_tree().map(Into::into)
}
pub fn rename(
@ -35,10 +39,14 @@ impl<'repo> Reference<'repo> {
) -> Result<Reference<'repo>> {
self.reference
.rename(new_name, force, log_message)
.map(Reference::from)
.map(Into::into)
}
pub fn delete(&mut self) -> Result<()> {
self.reference.delete()
}
pub fn is_remote(&self) -> bool {
self.reference.is_remote()
}
}

View File

@ -1,6 +1,6 @@
use std::path;
use super::{AnnotatedCommit, Branch, Commit, Index, Reference, Remote, Result, Tree};
use super::{AnnotatedCommit, Branch, Commit, Index, Oid, Reference, Remote, Result, Tree};
// wrapper around git2::Repository to get control over how it's used.
pub struct Repository(git2::Repository);
@ -33,8 +33,10 @@ impl Repository {
self.0.odb().and_then(|odb| odb.add_disk_alternate(path))
}
pub fn find_annotated_commit(&self, id: git2::Oid) -> Result<AnnotatedCommit<'_>> {
self.0.find_annotated_commit(id).map(AnnotatedCommit::from)
pub fn find_annotated_commit(&self, id: Oid) -> Result<AnnotatedCommit<'_>> {
self.0
.find_annotated_commit(id.into())
.map(AnnotatedCommit::from)
}
pub fn rebase(
@ -52,8 +54,8 @@ impl Repository {
)
}
pub fn merge_base(&self, one: git2::Oid, two: git2::Oid) -> Result<git2::Oid> {
self.0.merge_base(one, two)
pub fn merge_base(&self, one: Oid, two: Oid) -> Result<Oid> {
self.0.merge_base(one.into(), two.into()).map(Oid::from)
}
pub fn merge_trees(
@ -112,16 +114,16 @@ impl Repository {
self.0.head().map(Reference::from)
}
pub fn find_tree(&self, id: git2::Oid) -> Result<Tree> {
self.0.find_tree(id).map(Tree::from)
pub fn find_tree(&self, id: Oid) -> Result<Tree> {
self.0.find_tree(id.into()).map(Tree::from)
}
pub fn find_commit(&self, id: git2::Oid) -> Result<Commit> {
self.0.find_commit(id).map(Commit::from)
pub fn find_commit(&self, id: Oid) -> Result<Commit> {
self.0.find_commit(id.into()).map(Commit::from)
}
pub fn find_blob(&self, id: git2::Oid) -> Result<git2::Blob> {
self.0.find_blob(id)
pub fn find_blob(&self, id: Oid) -> Result<git2::Blob> {
self.0.find_blob(id.into())
}
pub fn revwalk(&self) -> Result<git2::Revwalk> {
@ -135,20 +137,24 @@ impl Repository {
pub fn branches(
&self,
filter: Option<git2::BranchType>,
) -> Result<impl Iterator<Item = Result<(git2::Branch, git2::BranchType)>>> {
self.0.branches(filter)
) -> Result<impl Iterator<Item = Result<(Branch, git2::BranchType)>>> {
self.0.branches(filter).map(|branches| {
branches.map(|branch| {
branch.map(|(branch, branch_type)| (Branch::from(branch), branch_type))
})
})
}
pub fn index(&self) -> Result<Index> {
self.0.index().map(Index::from)
self.0.index().map(Into::into)
}
pub fn blob_path(&self, path: &path::Path) -> Result<git2::Oid> {
self.0.blob_path(path)
pub fn blob_path(&self, path: &path::Path) -> Result<Oid> {
self.0.blob_path(path).map(Into::into)
}
pub fn blob(&self, data: &[u8]) -> Result<git2::Oid> {
self.0.blob(data)
pub fn blob(&self, data: &[u8]) -> Result<Oid> {
self.0.blob(data).map(Into::into)
}
pub fn commit(
@ -159,19 +165,21 @@ impl Repository {
message: &str,
tree: &Tree<'_>,
parents: &[&Commit<'_>],
) -> Result<git2::Oid> {
) -> Result<Oid> {
let parents: Vec<&git2::Commit> = parents
.iter()
.map(|c| c.to_owned().into())
.collect::<Vec<_>>();
self.0.commit(
update_ref,
author,
committer,
message,
tree.into(),
&parents,
)
self.0
.commit(
update_ref,
author,
committer,
message,
tree.into(),
&parents,
)
.map(Into::into)
}
pub fn config(&self) -> Result<git2::Config> {
@ -179,7 +187,7 @@ impl Repository {
}
pub fn treebuilder(&self, tree: Option<&Tree>) -> Result<git2::TreeBuilder> {
self.0.treebuilder(tree.map(|t| t.into()))
self.0.treebuilder(tree.map(Into::into))
}
pub fn path(&self) -> &path::Path {
@ -216,19 +224,19 @@ impl Repository {
}
pub fn remote_anonymous(&self, url: &str) -> Result<Remote> {
self.0.remote_anonymous(url).map(Remote::from)
self.0.remote_anonymous(url).map(Into::into)
}
pub fn find_remote(&self, name: &str) -> Result<Remote> {
self.0.find_remote(name).map(Remote::from)
self.0.find_remote(name).map(Into::into)
}
pub fn find_branch(&self, name: &str, branch_type: git2::BranchType) -> Result<Branch> {
self.0.find_branch(name, branch_type).map(Branch::from)
self.0.find_branch(name, branch_type).map(Into::into)
}
pub fn refname_to_id(&self, name: &str) -> Result<git2::Oid> {
self.0.refname_to_id(name)
pub fn refname_to_id(&self, name: &str) -> Result<Oid> {
self.0.refname_to_id(name).map(Into::into)
}
pub fn checkout_head(&self, opts: Option<&mut git2::build::CheckoutBuilder>) -> Result<()> {
@ -240,7 +248,7 @@ impl Repository {
index: Option<&mut Index>,
opts: Option<&mut git2::build::CheckoutBuilder<'_>>,
) -> Result<()> {
self.0.checkout_index(index.map(|i| i.into()), opts)
self.0.checkout_index(index.map(Into::into), opts)
}
pub fn checkout_tree(
@ -259,24 +267,24 @@ impl Repository {
pub fn reference(
&self,
name: &str,
id: git2::Oid,
id: Oid,
force: bool,
log_message: &str,
) -> Result<Reference> {
self.0
.reference(name, id, force, log_message)
.map(Reference::from)
.reference(name, id.into(), force, log_message)
.map(Into::into)
}
#[cfg(test)]
pub fn remote(&self, name: &str, url: &str) -> Result<Remote> {
self.0.remote(name, url).map(Remote::from)
self.0.remote(name, url).map(Into::into)
}
#[cfg(test)]
pub fn references(&self) -> Result<impl Iterator<Item = Result<Reference>>> {
self.0
.references()
.map(|iter| iter.map(|reference| reference.map(Reference::from)))
.map(|iter| iter.map(|reference| reference.map(Into::into)))
}
}

View File

@ -2,7 +2,7 @@ use std::path;
use git2::TreeEntry;
use super::Result;
use super::{Oid, Result};
pub struct Tree<'repo> {
tree: git2::Tree<'repo>,
@ -21,8 +21,8 @@ impl<'repo> From<&'repo Tree<'repo>> for &'repo git2::Tree<'repo> {
}
impl<'repo> Tree<'repo> {
pub fn id(&self) -> git2::Oid {
self.tree.id()
pub fn id(&self) -> Oid {
self.tree.id().into()
}
pub fn get_path(&self, path: &path::Path) -> Result<git2::TreeEntry<'repo>> {

View File

@ -2,6 +2,8 @@ use std::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use crate::git;
use super::{error::Error, RemoteName};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -60,12 +62,12 @@ impl FromStr for Name {
}
}
impl TryFrom<&git2::Branch<'_>> for Name {
impl TryFrom<&git::Branch<'_>> for Name {
type Error = Error;
fn try_from(value: &git2::Branch<'_>) -> std::result::Result<Self, Self::Error> {
let branch = String::from_utf8(value.name_bytes()?.to_vec()).map_err(Error::Utf8Error)?;
if value.get().is_remote() {
fn try_from(value: &git::Branch<'_>) -> std::result::Result<Self, Self::Error> {
let branch = String::from_utf8(value.refname_bytes().to_vec()).map_err(Error::Utf8Error)?;
if value.is_remote() {
Err(Error::NotLocal(branch))
} else {
match value.upstream() {

View File

@ -10,6 +10,8 @@ pub use error::Error;
pub use local::Name as LocalName;
pub use remote::Name as RemoteName;
use crate::git;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Name {
Remote(RemoteName),
@ -43,11 +45,12 @@ impl FromStr for Name {
}
}
impl TryFrom<&git2::Branch<'_>> for Name {
impl TryFrom<&git::Branch<'_>> for Name {
type Error = Error;
fn try_from(value: &git2::Branch<'_>) -> std::result::Result<Self, Self::Error> {
if value.get().is_remote() {
fn try_from(value: &git::Branch<'_>) -> std::result::Result<Self, Self::Error> {
dbg!(value.is_remote());
if value.is_remote() {
Ok(Self::Remote(RemoteName::try_from(value)?))
} else {
Ok(Self::Local(LocalName::try_from(value)?))

View File

@ -2,6 +2,8 @@ use std::{fmt, str::FromStr};
use serde::{Deserialize, Serialize};
use crate::git;
use super::error::Error;
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
@ -69,17 +71,17 @@ impl FromStr for Name {
}
}
impl TryFrom<&git2::Branch<'_>> for Name {
impl TryFrom<&git::Branch<'_>> for Name {
type Error = Error;
fn try_from(value: &git2::Branch<'_>) -> std::result::Result<Self, Self::Error> {
fn try_from(value: &git::Branch<'_>) -> std::result::Result<Self, Self::Error> {
let refname =
String::from_utf8(value.get().name_bytes().to_vec()).map_err(Error::Utf8Error)?;
String::from_utf8(value.refname_bytes().to_vec()).map_err(Error::Utf8Error)?;
if !value.get().is_remote() {
if !value.is_remote() {
return Err(Error::NotRemote(refname));
}
refname.as_str().parse()
refname.parse()
}
}

View File

@ -9,9 +9,11 @@ use std::io::{BufRead, Write};
use anyhow::Result;
use crate::git;
use super::Repository;
pub fn mark(repository: &Repository, paths: &[String], parent: Option<git2::Oid>) -> Result<()> {
pub fn mark(repository: &Repository, paths: &[String], parent: Option<git::Oid>) -> Result<()> {
let conflicts_path = repository.git_repository.path().join("conflicts");
// write all the file paths to a file on disk
let mut file = std::fs::File::create(conflicts_path)?;
@ -30,7 +32,7 @@ pub fn mark(repository: &Repository, paths: &[String], parent: Option<git2::Oid>
Ok(())
}
pub fn merge_parent(repository: &Repository) -> Result<Option<git2::Oid>> {
pub fn merge_parent(repository: &Repository) -> Result<Option<git::Oid>> {
let merge_path = repository.git_repository.path().join("base_merge_parent");
if !merge_path.exists() {
return Ok(None);
@ -41,7 +43,7 @@ pub fn merge_parent(repository: &Repository) -> Result<Option<git2::Oid>> {
let mut lines = reader.lines();
if let Some(parent) = lines.next() {
let parent = parent?;
let parent = git2::Oid::from_str(&parent)?;
let parent: git::Oid = parent.parse()?;
Ok(Some(parent))
} else {
Ok(None)

View File

@ -28,7 +28,7 @@ impl Default for Options {
pub fn workdir(
repository: &Repository,
commit_oid: &git2::Oid,
commit_oid: &git::Oid,
opts: &Options,
) -> Result<HashMap<path::PathBuf, Vec<Hunk>>> {
let commit = repository

View File

@ -237,7 +237,7 @@ impl<'repository> Repository<'repository> {
}
// returns a list of commit oids from the first oid to the second oid
pub fn l(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git2::Oid>> {
pub fn l(&self, from: git::Oid, to: LogUntil) -> Result<Vec<git::Oid>> {
match to {
LogUntil::Commit(oid) => {
let mut revwalk = self
@ -245,12 +245,14 @@ impl<'repository> Repository<'repository> {
.revwalk()
.context("failed to create revwalk")?;
revwalk
.push(from)
.push(from.into())
.context(format!("failed to push {}", from))?;
revwalk
.hide(oid)
.hide(oid.into())
.context(format!("failed to push {}", oid))?;
revwalk.collect::<Result<Vec<_>, _>>()
revwalk
.map(|oid| oid.map(|oid| oid.into()))
.collect::<Result<Vec<_>, _>>()
}
LogUntil::Take(n) => {
let mut revwalk = self
@ -258,9 +260,12 @@ impl<'repository> Repository<'repository> {
.revwalk()
.context("failed to create revwalk")?;
revwalk
.push(from)
.push(from.into())
.context(format!("failed to push {}", from))?;
revwalk.take(n).collect::<Result<Vec<_>, _>>()
revwalk
.take(n)
.map(|oid| oid.map(|oid| oid.into()))
.collect::<Result<Vec<_>, _>>()
}
LogUntil::When(cond) => {
let mut revwalk = self
@ -268,16 +273,16 @@ impl<'repository> Repository<'repository> {
.revwalk()
.context("failed to create revwalk")?;
revwalk
.push(from)
.push(from.into())
.context(format!("failed to push {}", from))?;
let mut oids = vec![];
let mut oids: Vec<git::Oid> = vec![];
for oid in revwalk {
let oid = oid.context("failed to get oid")?;
oids.push(oid);
oids.push(oid.into());
let commit = self
.git_repository
.find_commit(oid)
.find_commit(oid.into())
.context("failed to find commit")?;
if cond(&commit).context("failed to check condition")? {
@ -292,16 +297,18 @@ impl<'repository> Repository<'repository> {
.revwalk()
.context("failed to create revwalk")?;
revwalk
.push(from)
.push(from.into())
.context(format!("failed to push {}", from))?;
revwalk.collect::<Result<Vec<_>, _>>()
revwalk
.map(|oid| oid.map(|oid| oid.into()))
.collect::<Result<Vec<_>, _>>()
}
}
.context("failed to collect oids")
}
// returns a list of commits from the first oid to the second oid
pub fn log(&self, from: git2::Oid, to: LogUntil) -> Result<Vec<git::Commit>> {
pub fn log(&self, from: git::Oid, to: LogUntil) -> Result<Vec<git::Commit>> {
self.l(from, to)?
.into_iter()
.map(|oid| self.git_repository.find_commit(oid))
@ -310,7 +317,7 @@ impl<'repository> Repository<'repository> {
}
// returns the number of commits between the first oid to the second oid
pub fn distance(&self, from: git2::Oid, to: git2::Oid) -> Result<u32> {
pub fn distance(&self, from: git::Oid, to: git::Oid) -> Result<u32> {
let oids = self.l(from, LogUntil::Commit(to))?;
Ok(oids.len().try_into()?)
}
@ -319,7 +326,6 @@ impl<'repository> Repository<'repository> {
let branch = self
.git_repository
.find_branch(branch, git2::BranchType::Local)?;
let branch = branch.into_reference();
self.git_repository
.set_head(branch.name().unwrap())
.context("failed to set head")?;
@ -407,7 +413,7 @@ impl<'repository> Repository<'repository> {
pub fn push(
&self,
head: &git2::Oid,
head: &git::Oid,
branch: &branch::RemoteName,
key: &keys::PrivateKey,
) -> Result<(), Error> {
@ -695,7 +701,7 @@ pub enum Error {
type OidFilter = dyn Fn(&git::Commit) -> Result<bool>;
pub enum LogUntil {
Commit(git2::Oid),
Commit(git::Oid),
Take(usize),
When(Box<OidFilter>),
End,

View File

@ -119,7 +119,7 @@ impl Reader for DirReader {
pub struct CommitReader<'reader> {
repository: &'reader git::Repository,
commit_oid: git2::Oid,
commit_oid: git::Oid,
tree: git::Tree<'reader>,
}
@ -138,7 +138,7 @@ impl<'reader> CommitReader<'reader> {
})
}
pub fn get_commit_oid(&self) -> git2::Oid {
pub fn get_commit_oid(&self) -> git::Oid {
self.commit_oid
}
}
@ -165,7 +165,7 @@ impl Reader for CommitReader<'_> {
Ok(entry) => entry,
Err(_) => return Ok(0),
};
let blob = match self.repository.find_blob(entry.id()) {
let blob = match self.repository.find_blob(entry.id().into()) {
Ok(blob) => blob,
Err(_) => return Ok(0),
};
@ -181,7 +181,7 @@ impl Reader for CommitReader<'_> {
Ok(entry) => entry,
Err(_) => return Err(Error::NotFound),
};
let blob = match self.repository.find_blob(entry.id()) {
let blob = match self.repository.find_blob(entry.id().into()) {
Ok(blob) => blob,
Err(_) => return Err(Error::NotFound),
};

View File

@ -21,7 +21,7 @@ impl<'iterator> SessionsIterator<'iterator> {
let branches = git_repository.branches(None)?;
for branch in branches {
let (branch, _) = branch.context("failed to get branch")?;
iter.push(branch.get().peel_to_commit()?.id())
iter.push(branch.peel_to_commit()?.id().into())
.with_context(|| format!("failed to push branch {:?}", branch.name()))?;
}
@ -38,7 +38,7 @@ impl<'iterator> Iterator for SessionsIterator<'iterator> {
fn next(&mut self) -> Option<Self::Item> {
match self.iter.next() {
Some(Result::Ok(oid)) => {
let commit = match self.git_repository.find_commit(oid) {
let commit = match self.git_repository.find_commit(oid.into()) {
Result::Ok(commit) => commit,
Err(err) => return Some(Err(err.into())),
};

View File

@ -3,7 +3,7 @@ use std::{collections::HashMap, path};
use anyhow::{anyhow, Context, Result};
use crate::{
gb_repository,
gb_repository, git,
reader::{self, CommitReader, Reader},
};
@ -70,8 +70,9 @@ impl<'reader> SessionReader<'reader> {
));
};
let oid = git2::Oid::from_str(session_hash)
.with_context(|| format!("failed to parse commit hash {}", session_hash))?;
let oid: git::Oid = session_hash
.parse()
.context(format!("failed to parse commit hash {}", session_hash))?;
let commit = repository
.git_repository

View File

@ -29,7 +29,7 @@ pub fn test_repository() -> git::Repository {
repository
}
pub fn commit_all(repository: &git::Repository) -> git2::Oid {
pub fn commit_all(repository: &git::Repository) -> git::Oid {
let mut index = repository.index().expect("failed to get index");
index
.add_all(["."], git2::IndexAddOption::DEFAULT, None)

View File

@ -34,14 +34,14 @@ pub fn set_base_branch(
// lookup a branch by name
let branch = repo.find_branch(target_branch, git2::BranchType::Remote)?;
let remote_name = repo.branch_remote_name(branch.get().name().unwrap())?;
let remote_name = repo.branch_remote_name(branch.refname().unwrap())?;
let remote = repo.find_remote(&remote_name)?;
let remote_url = remote.url().unwrap();
// get a list of currently active virtual branches
// if there are no applied virtual branches, calculate the sha as the merge-base between HEAD in project_repository and this target commit
let commit = branch.get().peel_to_commit()?;
let commit = branch.peel_to_commit()?;
let mut commit_oid = commit.id();
let head_ref = repo.head().context("Failed to get HEAD reference")?;
@ -147,7 +147,7 @@ pub fn update_base_branch(
.find_branch(&target.branch_name, git2::BranchType::Remote)
.context(format!("failed to find branch {}", target.branch_name))?;
let new_target_commit = target_branch.get().peel_to_commit().context(format!(
let new_target_commit = target_branch.peel_to_commit().context(format!(
"failed to peel branch {} to commit",
target.branch_name
))?;
@ -248,7 +248,7 @@ pub fn update_base_branch(
if !merge_index.has_conflicts() {
// does not conflict with head, so lets merge it and update the head
let merge_tree_oid = merge_index
.write_tree_to(repo.into())
.write_tree_to(repo)
.context("failed to write tree")?;
// get tree from merge_tree_oid
let merge_tree = repo
@ -272,7 +272,7 @@ pub fn update_base_branch(
} else {
// get the merge tree oid from writing the index out
let merge_tree_oid = merge_index
.write_tree_to(repo.into())
.write_tree_to(repo)
.context("failed to write tree")?;
// branch head does not have conflicts, so don't unapply it, but still try to merge it's head if there are commits
@ -303,7 +303,7 @@ pub fn update_base_branch(
super::list_virtual_branches(gb_repository, project_repository)?;
} else {
let merge_tree_oid = merge_index
.write_tree_to(repo.into())
.write_tree_to(repo)
.context("failed to write tree")?;
// if the merge_tree is the same as the new_target_tree and there are no files (uncommitted changes)
// then the vbranch is fully merged, so delete it
@ -346,7 +346,7 @@ pub fn update_base_branch(
let commit_result = rebase.commit(None, &committer, None);
match commit_result {
Ok(commit_id) => {
last_rebase_head = commit_id;
last_rebase_head = commit_id.into();
}
Err(_e) => {
rebase_success = false;
@ -444,7 +444,7 @@ pub fn target_to_base_branch(
) -> Result<super::BaseBranch> {
let repo = &project_repository.git_repository;
let branch = repo.find_branch(&target.branch_name, git2::BranchType::Remote)?;
let commit = branch.get().peel_to_commit()?;
let commit = branch.peel_to_commit()?;
let oid = commit.id();
// gather a list of commits between oid and target.sha
@ -555,7 +555,7 @@ pub fn create_virtual_branch_from_branch(
} else {
let (author, committer) = gb_repository.git_signatures()?;
let new_head_tree_oid = merge_index
.write_tree_to(repo.into())
.write_tree_to(repo)
.context("failed to write merge tree")?;
let new_head_tree = repo
.find_tree(new_head_tree_oid)

View File

@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
use anyhow::Result;
use crate::project_repository::branch;
use crate::{git, project_repository::branch};
// this is the struct for the virtual branch data that is stored in our data
// store. it is more or less equivalent to a git branch reference, but it is not
@ -30,8 +30,8 @@ pub struct Branch {
pub created_timestamp_ms: u128,
pub updated_timestamp_ms: u128,
// tree is the last git tree written to a session, or merge base tree if this is new. use this for delta calculation from the session data
pub tree: git2::Oid,
pub head: git2::Oid,
pub tree: git::Oid,
pub head: git::Oid,
pub ownership: Ownership,
// order is the number by which UI should sort branches
pub order: usize,
@ -162,8 +162,18 @@ impl TryFrom<&dyn crate::reader::Reader> for Branch {
notes,
applied,
upstream,
tree: git2::Oid::from_str(&tree).unwrap(),
head: git2::Oid::from_str(&head).unwrap(),
tree: tree.parse().map_err(|e| {
crate::reader::Error::IOError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("meta/tree: {}", e),
))
})?,
head: head.parse().map_err(|e| {
crate::reader::Error::IOError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("meta/head: {}", e),
))
})?,
created_timestamp_ms,
updated_timestamp_ms,
ownership,

View File

@ -55,15 +55,15 @@ mod tests {
),
created_timestamp_ms: unsafe { TEST_INDEX } as u128,
updated_timestamp_ms: unsafe { TEST_INDEX + 100 } as u128,
head: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_INDEX }
))
head: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_INDEX
})
.parse()
.unwrap(),
tree: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef012345{}",
unsafe { TEST_INDEX + 10 }
))
tree: format!("0123456789abcdef0123456789abcdef012345{}", unsafe {
TEST_INDEX + 10
})
.parse()
.unwrap(),
ownership: Ownership {
files: vec![format!("file/{}:1-2", unsafe { TEST_INDEX })

View File

@ -128,15 +128,15 @@ mod tests {
),
created_timestamp_ms: unsafe { TEST_INDEX } as u128,
updated_timestamp_ms: unsafe { TEST_INDEX + 100 } as u128,
head: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_INDEX }
))
head: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_INDEX
})
.parse()
.unwrap(),
tree: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef012345{}",
unsafe { TEST_INDEX + 10 }
))
tree: format!("0123456789abcdef0123456789abcdef012345{}", unsafe {
TEST_INDEX + 10
})
.parse()
.unwrap(),
ownership: branch::Ownership {
files: vec![branch::FileOwnership {

View File

@ -90,7 +90,7 @@ pub fn update_gitbutler_integration(
let branch_tree = branch_head.tree()?;
if let Ok(mut result) = repo.merge_trees(&base_tree, &final_tree, &branch_tree) {
if !result.has_conflicts() {
let final_tree_oid = result.write_tree_to(repo.into())?;
let final_tree_oid = result.write_tree_to(repo)?;
final_tree = repo.find_tree(final_tree_oid)?;
}
}
@ -153,7 +153,7 @@ pub fn update_gitbutler_integration(
// write final_tree as the current index
let mut index = repo.index()?;
index.read_tree((&final_tree).into())?;
index.read_tree(&final_tree)?;
index.write()?;
// finally, update the refs/gitbutler/ heads to the states of the current virtual branches

View File

@ -69,15 +69,15 @@ mod tests {
),
created_timestamp_ms: unsafe { TEST_INDEX } as u128,
updated_timestamp_ms: unsafe { TEST_INDEX + 100 } as u128,
head: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_INDEX }
))
head: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_INDEX
})
.parse()
.unwrap(),
tree: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef012345{}",
unsafe { TEST_INDEX + 10 }
))
tree: format!("0123456789abcdef0123456789abcdef012345{}", unsafe {
TEST_INDEX + 10
})
.parse()
.unwrap(),
ownership: branch::Ownership::default(),
order: unsafe { TEST_INDEX },
@ -91,10 +91,10 @@ mod tests {
branch_name: format!("branch name{}", unsafe { TEST_TARGET_INDEX }),
remote_name: format!("remote name {}", unsafe { TEST_TARGET_INDEX }),
remote_url: format!("remote url {}", unsafe { TEST_TARGET_INDEX }),
sha: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_TARGET_INDEX }
))
sha: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_TARGET_INDEX
})
.parse()
.unwrap(),
}
}

View File

@ -6,13 +6,15 @@ use serde::{ser::SerializeStruct, Serialize, Serializer};
pub use reader::TargetReader as Reader;
pub use writer::TargetWriter as Writer;
use crate::git;
#[derive(Debug, PartialEq, Clone)]
pub struct Target {
// TODO: use project_repository::branch::RemoteName here.
pub branch_name: String,
pub remote_name: String,
pub remote_url: String,
pub sha: git2::Oid,
pub sha: git::Oid,
}
impl Serialize for Target {

View File

@ -61,15 +61,15 @@ mod tests {
),
created_timestamp_ms: unsafe { TEST_INDEX } as u128,
updated_timestamp_ms: unsafe { TEST_INDEX + 100 } as u128,
head: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_INDEX }
))
head: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_INDEX
})
.parse()
.unwrap(),
tree: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef012345{}",
unsafe { TEST_INDEX + 10 }
))
tree: format!("0123456789abcdef0123456789abcdef012345{}", unsafe {
TEST_INDEX + 10
})
.parse()
.unwrap(),
ownership: branch::Ownership {
files: vec![branch::FileOwnership {
@ -145,8 +145,8 @@ mod tests {
"git@github.com:gitbutlerapp/gitbutler-client.git"
);
assert_eq!(
read.sha,
git2::Oid::from_str("dd945831869e9593448aa622fa4342bbfb84813d").unwrap()
read.sha.to_string(),
"dd945831869e9593448aa622fa4342bbfb84813d"
);
Ok(())
@ -170,14 +170,14 @@ mod tests {
remote_name: "remote".to_string(),
branch_name: "branch".to_string(),
remote_url: "remote url".to_string(),
sha: git2::Oid::from_str("fedcba9876543210fedcba9876543210fedcba98").unwrap(),
sha: "fedcba9876543210fedcba9876543210fedcba98".parse().unwrap(),
};
let default_target = Target {
remote_name: "default remote".to_string(),
branch_name: "default branch".to_string(),
remote_url: "default remote url".to_string(),
sha: git2::Oid::from_str("0123456789abcdef0123456789abcdef01234567").unwrap(),
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
};
let branch_writer = branch::Writer::new(&gb_repo);

View File

@ -104,15 +104,15 @@ mod tests {
.unwrap(),
),
updated_timestamp_ms: unsafe { TEST_INDEX + 100 } as u128,
head: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_INDEX }
))
head: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_INDEX
})
.parse()
.unwrap(),
tree: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef012345{}",
unsafe { TEST_INDEX + 10 }
))
tree: format!("0123456789abcdef0123456789abcdef012345{}", unsafe {
TEST_INDEX + 10
})
.parse()
.unwrap(),
ownership: branch::Ownership {
files: vec![branch::FileOwnership {
@ -141,7 +141,7 @@ mod tests {
branch_name: "branch name".to_string(),
remote_name: "remote name".to_string(),
remote_url: "remote url".to_string(),
sha: git2::Oid::from_str("0123456789abcdef0123456789abcdef01234567").unwrap(),
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
};
let branch_writer = branch::Writer::new(&gb_repo);
@ -233,7 +233,7 @@ mod tests {
remote_name: "remote name".to_string(),
branch_name: "branch name".to_string(),
remote_url: "remote url".to_string(),
sha: git2::Oid::from_str("0123456789abcdef0123456789abcdef01234567").unwrap(),
sha: "0123456789abcdef0123456789abcdef01234567".parse().unwrap(),
};
let branch_writer = branch::Writer::new(&gb_repo);
@ -245,7 +245,7 @@ mod tests {
remote_name: "updated remote name".to_string(),
branch_name: "updated branch name".to_string(),
remote_url: "updated remote url".to_string(),
sha: git2::Oid::from_str("fedcba9876543210fedcba9876543210fedcba98").unwrap(),
sha: "fedcba9876543210fedcba9876543210fedcba98".parse().unwrap(),
};
target_writer.write(&branch.id, &updated_target)?;

View File

@ -211,7 +211,7 @@ fn test_track_binary_files() -> Result<()> {
// status (no files)
let branches = list_virtual_branches(&gb_repo, &project_repository).unwrap();
let commit_id = &branches[0].commits[0].id;
let commit_obj = repository.find_commit(git2::Oid::from_str(commit_id).unwrap())?;
let commit_obj = repository.find_commit(commit_id.parse().unwrap())?;
let tree = commit_obj.tree()?;
let files = tree_to_entry_list(&repository, &tree);
assert_eq!(files[0].0, "image.bin");
@ -232,7 +232,7 @@ fn test_track_binary_files() -> Result<()> {
let branches = list_virtual_branches(&gb_repo, &project_repository).unwrap();
let commit_id = &branches[0].commits[0].id;
// get tree from commit_id
let commit_obj = repository.find_commit(git2::Oid::from_str(commit_id).unwrap())?;
let commit_obj = repository.find_commit(commit_id.parse().unwrap())?;
let tree = commit_obj.tree()?;
let files = tree_to_entry_list(&repository, &tree);
@ -1083,7 +1083,7 @@ fn test_update_base_branch_base() -> Result<()> {
let branch = &branches[0];
assert_eq!(branch.files.len(), 1);
assert_eq!(branch.commits.len(), 1); // branch commit, rebased
let head_sha = git2::Oid::from_str(&branch.commits[0].id)?;
let head_sha = branch.commits[0].id.parse::<git::Oid>()?;
let head_commit = repository.find_commit(head_sha)?;
let parent = head_commit.parent(0)?;
@ -2638,7 +2638,9 @@ fn test_commit_add_and_delete_files() -> Result<()> {
// branch one test.txt has just the 1st and 3rd hunks applied
let commit2 = &branch1.commits[0].id;
let commit2 = git2::Oid::from_str(commit2).expect("failed to parse oid");
let commit2 = commit2
.parse::<git::Oid>()
.expect("failed to parse commit id");
let commit2 = repository
.find_commit(commit2)
.expect("failed to get commit object");
@ -2704,7 +2706,9 @@ fn test_commit_executable_and_symlinks() -> Result<()> {
let branch1 = &branches.iter().find(|b| b.id == branch1_id).unwrap();
let commit = &branch1.commits[0].id;
let commit = git2::Oid::from_str(commit).expect("failed to parse oid");
let commit = commit
.parse::<git::Oid>()
.expect("failed to parse commit id");
let commit = repository
.find_commit(commit)
.expect("failed to get commit object");
@ -3080,7 +3084,7 @@ fn test_apply_out_of_date_conflicting_vbranch() -> Result<()> {
let branches = list_virtual_branches(&gb_repo, &project_repository)?;
let branch1 = &branches.iter().find(|b| &b.id == branch_id).unwrap();
let last_commit = branch1.commits.first().unwrap();
let last_commit_oid = git2::Oid::from_str(&last_commit.id)?;
let last_commit_oid = last_commit.id.parse::<git::Oid>()?;
let commit = gb_repo.git_repository.find_commit(last_commit_oid)?;
assert!(!branch1.conflicted);
assert_eq!(commit.parent_count(), 2);

View File

@ -274,7 +274,7 @@ pub fn apply_branch(
let (author, committer) = gb_repository.git_signatures()?;
let message = "merge upstream";
// write the merge commit
let branch_tree_oid = merge_index.write_tree_to(repo.into())?;
let branch_tree_oid = merge_index.write_tree_to(repo)?;
branch_tree = repo.find_tree(branch_tree_oid)?;
let new_branch_head = repo.commit(
@ -398,7 +398,7 @@ pub fn unapply_branch(
let tree_oid = write_tree(project_repository, &default_target, &files)?;
let branch_tree = repo.find_tree(tree_oid)?;
if let Ok(mut result) = repo.merge_trees(&base_tree, &final_tree, &branch_tree) {
let final_tree_oid = result.write_tree_to(repo.into())?;
let final_tree_oid = result.write_tree_to(repo)?;
final_tree = repo.find_tree(final_tree_oid)?;
}
}
@ -483,10 +483,10 @@ pub fn list_remote_branches(
.filter_map(|branch| branch.upstream)
.map(|upstream| upstream.branch().to_string())
.collect::<HashSet<_>>();
let mut most_recent_branches_by_hash: HashMap<git2::Oid, (git2::Branch, u64)> = HashMap::new();
let mut most_recent_branches_by_hash: HashMap<git::Oid, (git::Branch, u64)> = HashMap::new();
for (branch, _) in repo.branches(None)?.flatten() {
if let Some(branch_oid) = branch.get().target() {
if let Some(branch_oid) = branch.target() {
// get the branch ref
let branch_commit = repo
.find_commit(branch_oid)
@ -504,6 +504,7 @@ pub fn list_remote_branches(
continue;
}
dbg!(&branch.is_remote());
let branch_name = project_repository::branch::Name::try_from(&branch)
.context("could not get branch name")?;
@ -550,7 +551,7 @@ pub fn list_remote_branches(
match most_recent_branches_by_hash.get(&branch_oid) {
Some((_, existing_seconds)) => {
let branch_name = branch.get().name().context("could not get branch name")?;
let branch_name = branch.refname().context("could not get branch name")?;
if seconds < *existing_seconds {
// this branch is older than the one we already have
continue;
@ -575,12 +576,12 @@ pub fn list_remote_branches(
}
}
let mut most_recent_branches: Vec<(git2::Branch, u64)> =
let mut most_recent_branches: Vec<(git::Branch, u64)> =
most_recent_branches_by_hash.into_values().collect();
// take the most recent 20 branches
most_recent_branches.sort_by(|a, b| b.1.cmp(&a.1)); // Sort by timestamp in descending order.
let sorted_branches: Vec<git2::Branch> = most_recent_branches
let sorted_branches: Vec<git::Branch> = most_recent_branches
.into_iter()
.map(|(branch, _)| branch)
.collect();
@ -588,8 +589,8 @@ pub fn list_remote_branches(
let mut branches: Vec<RemoteBranch> = Vec::new();
for branch in &top_branches {
let branch_name = branch.get().name().context("could not get branch name")?;
match branch.get().target() {
let branch_name = branch.refname().context("could not get branch name")?;
match branch.target() {
Some(branch_oid) => {
// get the branch ref
let branch_commit = repo
@ -998,7 +999,7 @@ pub fn commit_to_vbranch_commit(
repository: &project_repository::Repository,
target: &target::Target,
commit: &git::Commit,
upstream_commits: Option<&HashMap<git2::Oid, bool>>,
upstream_commits: Option<&HashMap<git::Oid, bool>>,
) -> Result<VirtualBranchCommit> {
let timestamp = commit.time().seconds() as u128;
let signature = commit.author();
@ -1682,7 +1683,7 @@ pub fn write_tree(
project_repository: &project_repository::Repository,
target: &target::Target,
files: &Vec<VirtualBranchFile>,
) -> Result<git2::Oid> {
) -> Result<git::Oid> {
// read the base sha into an index
let git_repository = &project_repository.git_repository;
@ -1722,7 +1723,7 @@ pub fn write_tree(
let bytes: &[u8] = path_str.as_bytes();
let blob_oid = git_repository.blob(bytes)?;
builder.insert(rel_path, blob_oid, filemode.into())?;
builder.insert(rel_path, blob_oid.into(), filemode.into())?;
} else if let Ok(tree_entry) = base_tree.get_path(rel_path) {
if file.binary {
let new_blob_oid = &file.hunks[0].diff;
@ -1756,12 +1757,12 @@ pub fn write_tree(
// create a blob
let new_blob_oid = git_repository.blob(&new_content)?;
// upsert into the builder
builder.insert(rel_path, new_blob_oid, filemode.into())?;
builder.insert(rel_path, new_blob_oid.into(), filemode.into())?;
}
} else {
// create a git blob from a file on disk
let blob_oid = git_repository.blob_path(&full_path)?;
builder.insert(rel_path, blob_oid, filemode.into())?;
builder.insert(rel_path, blob_oid.into(), filemode.into())?;
}
} else if base_tree.get_path(rel_path).is_ok() {
// remove file from index if it exists in the base tree
@ -1775,7 +1776,7 @@ pub fn write_tree(
// now write out the tree
let tree_oid = builder.write().context("failed to create updated tree")?;
Ok(tree_oid)
Ok(tree_oid.into())
}
fn _print_tree(repo: &git2::Repository, tree: &git2::Tree) -> Result<()> {
@ -1982,7 +1983,7 @@ fn is_commit_integrated(
let remote_branch = project_repository
.git_repository
.find_branch(&target.branch_name, git2::BranchType::Remote)?;
let remote_head = remote_branch.get().peel_to_commit()?;
let remote_head = remote_branch.peel_to_commit()?;
let upstream_commits = project_repository.l(
remote_head.id(),
project_repository::LogUntil::Commit(target.sha),
@ -2026,7 +2027,7 @@ fn is_commit_integrated(
}
let merge_tree_oid = merge_index
.write_tree_to((&project_repository.git_repository).into())
.write_tree_to(&project_repository.git_repository)
.context("failed to write tree")?;
// if the merge_tree is the same as the new_target_tree and there are no files (uncommitted changes)

View File

@ -223,10 +223,10 @@ mod test {
branch_name: format!("branch name {}", unsafe { TEST_TARGET_INDEX }),
remote_name: format!("remote name {}", unsafe { TEST_TARGET_INDEX }),
remote_url: format!("remote url {}", unsafe { TEST_TARGET_INDEX }),
sha: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_TARGET_INDEX }
))
sha: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_TARGET_INDEX
})
.parse()
.unwrap(),
}
}
@ -249,15 +249,15 @@ mod test {
),
created_timestamp_ms: unsafe { TEST_INDEX } as u128,
updated_timestamp_ms: unsafe { TEST_INDEX + 100 } as u128,
head: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef0123456{}",
unsafe { TEST_INDEX }
))
head: format!("0123456789abcdef0123456789abcdef0123456{}", unsafe {
TEST_INDEX
})
.parse()
.unwrap(),
tree: git2::Oid::from_str(&format!(
"0123456789abcdef0123456789abcdef012345{}",
unsafe { TEST_INDEX + 10 }
))
tree: format!("0123456789abcdef0123456789abcdef012345{}", unsafe {
TEST_INDEX + 10
})
.parse()
.unwrap(),
ownership: branch::Ownership::default(),
order: unsafe { TEST_INDEX },