Skip to content

make rev() detect all parents of the given commits #1326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions josh-core/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ lazy_static! {
std::sync::Mutex::new(std::collections::HashMap::new());
static ref WORKSPACES: std::sync::Mutex<std::collections::HashMap<git2::Oid, Filter>> =
std::sync::Mutex::new(std::collections::HashMap::new());
static ref ANCESTORS: std::sync::Mutex<std::collections::HashMap<git2::Oid, std::collections::HashSet<git2::Oid>>> =
std::sync::Mutex::new(std::collections::HashMap::new());
}

/// Filters are represented as `git2::Oid`, however they are not ever stored
Expand Down Expand Up @@ -482,20 +484,23 @@ fn apply_to_commit2(

let id = commit.id();

if let Some(startfilter) = filters.get(&id) {
let mut f2 = filters.clone();
f2.remove(&id);
f2.insert(git2::Oid::zero(), *startfilter);
let op = if f2.len() == 1 {
to_op(*startfilter)
} else {
Op::Rev(f2)
};
if let Some(start) = apply_to_commit2(&op, commit, transaction)? {
transaction.insert(filter, id, start, true);
return Ok(Some(start));
} else {
return Ok(None);
for (&filter_tip, startfilter) in filters.iter() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This iterates the filters in unspecified order, as HashMap does not have stable iteration order. This means if the parent-sets of the commits given in a rev overlap, behavior would be non-deterministic...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filters is already a BTreeMap for exactly that reason.
It's probably slower, but determinism is more important.
Looking at the usage we could change it to an ordered Vec though 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, maybe it should error if two of the parent-sets overlap?

if filter_tip != git2::Oid::zero() && is_ancestor_of(repo, id, filter_tip)? {
// Remove this filter but preserve the others.
let mut f2 = filters.clone();
f2.remove(&filter_tip);
f2.insert(git2::Oid::zero(), *startfilter);
let op = if f2.len() == 1 {
to_op(*startfilter)
} else {
Op::Rev(f2)
};
if let Some(start) = apply_to_commit2(&op, commit, transaction)? {
transaction.insert(filter, id, start, true);
return Ok(Some(start));
} else {
return Ok(None);
}
}
}

Expand Down Expand Up @@ -980,6 +985,33 @@ pub fn make_permissions_filter(filter: Filter, whitelist: Filter, blacklist: Fil
opt::optimize(filter)
}

/// Check if `commit` is an ancestor of `tip`.
///
/// Creates a cache for a given `tip` so repeated queries with the same `tip` are more efficient.
fn is_ancestor_of(repo: &git2::Repository, commit: git2::Oid, tip: git2::Oid) -> JoshResult<bool> {
let mut ancestor_cache = ANCESTORS.lock().unwrap();
let ancestors = match ancestor_cache.entry(tip) {
std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
std::collections::hash_map::Entry::Vacant(entry) => {
tracing::trace!("is_ancestor_of tip={tip}");
// Recursively compute all ancestors of `tip`.
// Invariant: Everything in `todo` is also in `ancestors`.
let mut todo = vec![tip];
let mut ancestors = std::collections::HashSet::from_iter(todo.iter().copied());
while let Some(commit) = todo.pop() {
for parent in repo.find_commit(commit)?.parent_ids() {
if ancestors.insert(parent) {
// Newly inserted! Also handle its parents.
todo.push(parent);
}
}
}
entry.insert(ancestors)
}
};
Ok(ancestors.contains(&commit))
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading