Skip to content

Commit

Permalink
Add ext_common_ancestor feature to Cargo.toml
Browse files Browse the repository at this point in the history
Add ext_common_ancestor feature to Cargo.toml so that we could
experiment with using the external common ancestor functionality rather
than the built in one to see if it is more performant when dealing with
larger numbers of branches.

<!-- ps-id: c1e664d4-101e-4817-9ea9-e5dc7a1df114 -->
  • Loading branch information
drewdeponte committed Nov 15, 2023
1 parent 109f265 commit a3fd36d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ clap_mangen = "0.2.14"

[features]
backup_cmd = []
ext_common_ancestor = []
2 changes: 2 additions & 0 deletions src/ps/private/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod create_commit;
mod create_cwd_repo;
mod create_signed_commit;
mod create_unsigned_commit;
mod ext_common_ancestor;
mod ext_delete_remote_branch;
mod ext_fetch;
mod ext_push;
Expand Down Expand Up @@ -68,6 +69,7 @@ pub use create_commit::*;
pub use create_cwd_repo::*;
pub use create_signed_commit::*;
pub use create_unsigned_commit::*;
pub use ext_common_ancestor::*;
pub use ext_delete_remote_branch::*;
pub use ext_fetch::*;
pub use ext_push::*;
Expand Down
53 changes: 53 additions & 0 deletions src/ps/private/git/ext_common_ancestor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use super::super::utils;
use git2;
use std::result::Result;

#[derive(Debug)]
pub enum ExtCommonAncestorError {
Unhandled(Box<dyn std::error::Error>),
}

impl std::fmt::Display for ExtCommonAncestorError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unhandled(e) => write!(f, "{}", e),
}
}
}

impl std::error::Error for ExtCommonAncestorError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Unhandled(e) => Some(e.as_ref()),
}
}
}

impl From<utils::ExecuteWithOutputError> for ExtCommonAncestorError {
fn from(value: utils::ExecuteWithOutputError) -> Self {
Self::Unhandled(value.into())
}
}

impl From<std::string::FromUtf8Error> for ExtCommonAncestorError {
fn from(value: std::string::FromUtf8Error) -> Self {
Self::Unhandled(value.into())
}
}

impl From<git2::Error> for ExtCommonAncestorError {
fn from(value: git2::Error) -> Self {
Self::Unhandled(value.into())
}
}

pub fn ext_common_ancestor(
one: git2::Oid,
two: git2::Oid,
) -> Result<git2::Oid, ExtCommonAncestorError> {
let output =
utils::execute_with_output("git", &["merge-base", &one.to_string(), &two.to_string()])?;
let sha = String::from_utf8(output.stdout)?;
let common_ancestor_oid = git2::Oid::from_str(sha.trim())?;
Ok(common_ancestor_oid)
}
11 changes: 8 additions & 3 deletions src/ps/private/state_computation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub fn get_list_branch_info(
#[derive(Debug)]
pub enum GetPatchInfoCollectionError {
GetBranchHeadOid,
GetCommonAncestor(git::CommonAncestorError),
GetCommonAncestor(Box<dyn std::error::Error>),
GetCommits(git::GitError),
GetRevisionOid(git2::Error),
FindCommit(git2::Error),
Expand All @@ -269,7 +269,7 @@ impl std::error::Error for GetPatchInfoCollectionError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::GetBranchHeadOid => None,
Self::GetCommonAncestor(e) => Some(e),
Self::GetCommonAncestor(e) => Some(e.as_ref()),
Self::GetCommits(e) => Some(e),
Self::GetRevisionOid(e) => Some(e),
Self::FindCommit(e) => Some(e),
Expand All @@ -296,8 +296,13 @@ pub fn get_patch_info_collection(
.get()
.target()
.ok_or(GetPatchInfoCollectionError::GetBranchHeadOid)?;

#[cfg(not(feature = "ext_common_ancestor"))]
let common_ancestor_oid = git::common_ancestor(repo, branch_head_oid, base_oid)
.map_err(GetPatchInfoCollectionError::GetCommonAncestor)?;
.map_err(|e| GetPatchInfoCollectionError::GetCommonAncestor(e.into()))?;
#[cfg(feature = "ext_common_ancestor")]
let common_ancestor_oid = git::ext_common_ancestor(branch_head_oid, base_oid)
.map_err(|e| GetPatchInfoCollectionError::GetCommonAncestor(e.into()))?;

let revwalk = git::get_revs(
repo,
Expand Down

0 comments on commit a3fd36d

Please sign in to comment.