Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114//! Extended [`gix::Repository`].
use std::collections::{HashMap, HashSet};
#[derive(Debug, thiserror::Error)]pub enum MergeError { /// Not a failure so much as an answer: these paths couldn't be reconciled, and nothing /// reachable was written. #[error("the merge conflicted in {}", .0.join(", "))] Conflicts(Vec<String>), #[error("could not walk the commits to replay")] Walk(#[from] gix::revision::walk::Error), #[error("could not read a commit to replay")] ReadCommit(#[from] gix::revision::walk::iter::Error), #[error("could not set up the blob merge")] BlobMerge(#[from] Box<gix::repository::merge_resource_cache::Error>), #[error("could not set up the tree diff")] TreeDiff(#[from] Box<gix::repository::diff_resource_cache::Error>), #[error("could not replay the commits")] Replay(#[from] gix_rebase::replay::Error),}
pub trait RepositoryExt { /// Replay `source` and the ancestors of it that `onto` doesn't already have on top of `onto`. fn rebase( &self, source: gix::ObjectId, onto: gix::ObjectId, committer: gix::actor::Signature, ) -> Result<gix::ObjectId, MergeError>;}
impl RepositoryExt for gix::Repository { fn rebase( &self, source: gix::ObjectId, onto: gix::ObjectId, committer: gix::actor::Signature, ) -> Result<gix::ObjectId, MergeError> { let picks = replay_list(self, source, onto)?;
let options: gix_rebase::replay::Options = Default::default(); let treat_as_unresolved = options.treat_as_unresolved;
let mut blob_merge = self .merge_resource_cache(Default::default()) .map_err(Box::new)?; let mut diff_cache = self.diff_resource_cache_for_tree_diff().map_err(Box::new)?; let mut diff_state = gix::diff::tree::State::default();
let outcome = gix_rebase::replay( &onto, &picks, &committer, Default::default(), self, &mut diff_state, &mut diff_cache, &mut blob_merge, options, )?;
match outcome { gix_rebase::replay::Outcome::Complete(done) => Ok(done.tip), gix_rebase::replay::Outcome::Conflict(conflict) => Err(MergeError::Conflicts( conflict .unresolved_paths(treat_as_unresolved) .map(|path| path.to_string()) .collect(), )), } }}
/// The commits [`RepositoryExt::rebase`] replays, oldest first: everything reachable from `source`/// that `onto` doesn't have, without the merge commits — the set `git rebase` puts on its todo list.fn replay_list( repo: &gix::Repository, source: gix::ObjectId, onto: gix::ObjectId,) -> Result<Vec<gix::ObjectId>, MergeError> { let mut parents_of = HashMap::new(); for info in repo.rev_walk([source]).with_hidden([onto]).all()? { let info = info?; parents_of.insert(info.id, info.parent_ids); }
// `rev_walk` offers no topological sorting through the gix façade, and replaying a commit // before its own parent would rebase it onto the wrong tree, so lay the picks out with an // explicit post-order walk. Iterative, because a branch can be far deeper than the stack. let mut order = Vec::with_capacity(parents_of.len()); let mut seen = HashSet::new(); let mut stack = vec![(source, false)]; while let Some((id, parents_done)) = stack.pop() { let Some(parents) = parents_of.get(&id) else { continue; }; if parents_done { // Merge commits are left out, exactly as `git rebase` does without // `--rebase-merges`; the commits they brought in are replayed on their own. if parents.len() < 2 { order.push(id); } continue; } if !seen.insert(id) { continue; } stack.push((id, true)); stack.extend(parents.iter().map(|parent| (*parent, false))); } Ok(order)}