ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: reimplement divergent template keyword without evolution (#32)

This rewrites the `divergent` template keyword to be based on the
number of visible commits with a given change id. That's the same as
before; it's just that it's not based on the `Evolution` object's view
of which commits are visible anymore.

This is the last thing that depended on the evolution state!
This commit is contained in:
Martin von Zweigbergk 2021-10-06 22:50:11 -07:00
parent a750be2b9b
commit 09feb2e281
2 changed files with 30 additions and 7 deletions

View file

@ -242,7 +242,7 @@ fn parse_commit_keyword<'a>(repo: RepoRef<'a>, pair: Pair<Rule>) -> (Property<'a
"branches" => Property::String(Box::new(BranchProperty { repo })),
"tags" => Property::String(Box::new(TagProperty { repo })),
"git_refs" => Property::String(Box::new(GitRefsProperty { repo })),
"divergent" => Property::Boolean(Box::new(DivergentProperty { repo })),
"divergent" => Property::Boolean(Box::new(DivergentProperty::new(repo))),
"conflict" => Property::Boolean(Box::new(ConflictProperty)),
name => panic!("unexpected identifier: {}", name),
};

View file

@ -13,13 +13,15 @@
// limitations under the License.
use std::borrow::BorrowMut;
use std::collections::{HashMap, HashSet};
use std::io;
use std::ops::Add;
use std::ops::{Add, AddAssign};
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, Signature};
use jujutsu_lib::backend::{ChangeId, CommitId, Signature};
use jujutsu_lib::commit::Commit;
use jujutsu_lib::repo::RepoRef;
use jujutsu_lib::revset::RevsetExpression;
use crate::formatter::Formatter;
@ -286,13 +288,34 @@ impl TemplateProperty<Commit, String> for GitRefsProperty<'_> {
}
}
pub struct DivergentProperty<'a> {
pub repo: RepoRef<'a>,
pub struct DivergentProperty {
divergent_changes: HashSet<ChangeId>,
}
impl TemplateProperty<Commit, bool> for DivergentProperty<'_> {
impl DivergentProperty {
pub fn new(repo: RepoRef) -> Self {
// TODO: Create a persistent index from change id to commit ids.
let mut commit_count_by_change: HashMap<ChangeId, i32> = HashMap::new();
for index_entry in RevsetExpression::all().evaluate(repo).unwrap().iter() {
let change_id = index_entry.change_id();
commit_count_by_change
.entry(change_id)
.or_default()
.add_assign(1);
}
let mut divergent_changes = HashSet::new();
for (change_id, count) in commit_count_by_change {
if count > 1 {
divergent_changes.insert(change_id);
}
}
Self { divergent_changes }
}
}
impl TemplateProperty<Commit, bool> for DivergentProperty {
fn extract(&self, context: &Commit) -> bool {
self.repo.evolution().is_divergent(context.change_id())
self.divergent_changes.contains(context.change_id())
}
}