Merge pull request #2280 from gitbutlerapp/refactor-rebase-function

Refactor rebase function
This commit is contained in:
Nikita Galaiko 2024-01-17 13:50:56 +01:00 committed by GitHub
commit 4dec1fef4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 47 deletions

View File

@ -73,19 +73,3 @@ impl<'repo> Commit<'repo> {
self.commit.raw_header()
}
}
pub struct AnnotatedCommit<'repo> {
annotated_commit: git2::AnnotatedCommit<'repo>,
}
impl<'repo> From<git2::AnnotatedCommit<'repo>> for AnnotatedCommit<'repo> {
fn from(annotated_commit: git2::AnnotatedCommit<'repo>) -> Self {
Self { annotated_commit }
}
}
impl<'repo> From<&'repo AnnotatedCommit<'repo>> for &'repo git2::AnnotatedCommit<'repo> {
fn from(val: &'repo AnnotatedCommit<'repo>) -> Self {
&val.annotated_commit
}
}

View File

@ -6,8 +6,8 @@ use git2_hooks::HookResult;
use crate::keys;
use super::{
AnnotatedCommit, Blob, Branch, Commit, Config, Index, Oid, Reference, Refname, Remote, Result,
Signature, Tree, TreeBuilder, Url,
Blob, Branch, Commit, Config, Index, Oid, Reference, Refname, Remote, Result, Signature, Tree,
TreeBuilder, Url,
};
// wrapper around git2::Repository to get control over how it's used.
@ -66,25 +66,36 @@ impl Repository {
.map_err(Into::into)
}
pub fn find_annotated_commit(&self, id: Oid) -> Result<AnnotatedCommit<'_>> {
self.0
.find_annotated_commit(id.into())
.map(AnnotatedCommit::from)
.map_err(Into::into)
}
pub fn rebase(
&self,
branch: Option<&AnnotatedCommit<'_>>,
upstream: Option<&AnnotatedCommit<'_>>,
onto: Option<&AnnotatedCommit<'_>>,
branch_oid: Option<Oid>,
upstream_oid: Option<Oid>,
onto_oid: Option<Oid>,
opts: Option<&mut git2::RebaseOptions<'_>>,
) -> Result<git2::Rebase<'_>> {
let annotated_branch = if let Some(branch) = branch_oid {
Some(self.0.find_annotated_commit(branch.into())?)
} else {
None
};
let annotated_upstream = if let Some(upstream) = upstream_oid {
Some(self.0.find_annotated_commit(upstream.into())?)
} else {
None
};
let annotated_onto = if let Some(onto) = onto_oid {
Some(self.0.find_annotated_commit(onto.into())?)
} else {
None
};
self.0
.rebase(
branch.map(Into::into),
upstream.map(Into::into),
onto.map(Into::into),
annotated_branch.as_ref(),
annotated_upstream.as_ref(),
annotated_onto.as_ref(),
opts,
)
.map_err(Into::into)

View File

@ -435,19 +435,13 @@ pub fn update_base_branch(
// branch was not pushed to upstream yet. attempt a rebase,
let (_, committer) = project_repository.git_signatures(user)?;
let annotated_branch_head = repo
.find_annotated_commit(branch.head)
.context("failed to find annotated commit")?;
let annotated_upstream_base = repo
.find_annotated_commit(new_target_commit.id())
.context("failed to find annotated commit")?;
let mut rebase_options = git2::RebaseOptions::new();
rebase_options.quiet(true);
rebase_options.inmemory(true);
let mut rebase = repo
.rebase(
Some(&annotated_branch_head),
Some(&annotated_upstream_base),
Some(branch.head),
Some(new_target_commit.id()),
None,
Some(&mut rebase_options),
)

View File

@ -330,19 +330,13 @@ pub fn apply_branch(
} else {
// branch was not pushed to upstream yet. attempt a rebase,
let (_, committer) = project_repository.git_signatures(user)?;
let annotated_branch_head = repo
.find_annotated_commit(branch.head)
.context("failed to find annotated branch head commit")?;
let annotated_upstream_base = repo
.find_annotated_commit(target_commit.id())
.context("failed to find annotated target commit")?;
let mut rebase_options = git2::RebaseOptions::new();
rebase_options.quiet(true);
rebase_options.inmemory(true);
let mut rebase = repo
.rebase(
Some(&annotated_branch_head),
Some(&annotated_upstream_base),
Some(branch.head),
Some(target_commit.id()),
None,
Some(&mut rebase_options),
)