Skip to content

Commit 06e5967

Browse files
Unify workspace and hook per rev filter
Previously the handling was slightly different due to the workspace root being added for the tree filtering but not for the "additional parents" subtract filter. Now we always add it and rely on the optimizer to remove them. Change: unify-per-rev
1 parent d97b534 commit 06e5967

File tree

1 file changed

+30
-55
lines changed

1 file changed

+30
-55
lines changed

josh-core/src/filter/mod.rs

Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,22 @@ fn resolve_workspace_redirect<'a>(
12721272
}
12731273

12741274
fn get_workspace<'a>(repo: &'a git2::Repository, tree: &'a git2::Tree<'a>, path: &Path) -> Filter {
1275-
let ws_path = normalize_path(&path.join("workspace.josh"));
1275+
let path = if let Some((_, path)) = resolve_workspace_redirect(repo, &tree, path) {
1276+
path
1277+
} else {
1278+
path.to_owned()
1279+
};
1280+
let wsj_file = to_filter(Op::File(Path::new("workspace.josh").to_owned()));
1281+
let base = to_filter(Op::Subdir(path.to_owned()));
1282+
let wsj_file = chain(base, wsj_file);
1283+
compose(
1284+
wsj_file,
1285+
compose(get_filter(repo, tree, &path.join("workspace.josh")), base),
1286+
)
1287+
}
1288+
1289+
fn get_filter<'a>(repo: &'a git2::Repository, tree: &'a git2::Tree<'a>, path: &Path) -> Filter {
1290+
let ws_path = normalize_path(path);
12761291
let ws_id = ok_or!(tree.get_path(&ws_path), {
12771292
return to_filter(Op::Empty);
12781293
})
@@ -1548,40 +1563,22 @@ fn apply_to_commit2(
15481563
}
15491564
}
15501565

1551-
let commit_filter = filter;
1552-
1553-
let cw = get_workspace(repo, &commit.tree()?, ws_path);
1566+
let commit_filter = get_workspace(repo, &commit.tree()?, &ws_path);
15541567

15551568
let parent_filters = commit
15561569
.parents()
15571570
.map(|parent| {
15581571
rs_tracing::trace_scoped!("parent", "id": parent.id().to_string());
1559-
1560-
let p = if let Some((_, p)) =
1561-
resolve_workspace_redirect(repo, &parent.tree()?, ws_path)
1562-
{
1563-
p
1564-
} else {
1565-
ws_path.clone()
1566-
};
1567-
15681572
let pcw = get_workspace(
15691573
repo,
15701574
&parent.tree().unwrap_or_else(|_| tree::empty(repo)),
1571-
&p,
1575+
&ws_path,
15721576
);
15731577
Ok((parent, pcw))
15741578
})
15751579
.collect::<JoshResult<Vec<_>>>()?;
15761580

1577-
return per_rev_filter(
1578-
transaction,
1579-
commit,
1580-
filter,
1581-
commit_filter,
1582-
cw,
1583-
parent_filters,
1584-
);
1581+
return per_rev_filter(transaction, commit, filter, commit_filter, parent_filters);
15851582
}
15861583
Op::Fold => {
15871584
let filtered_parent_ids = commit
@@ -1607,7 +1604,6 @@ fn apply_to_commit2(
16071604
}
16081605
Op::Hook(hook) => {
16091606
let commit_filter = transaction.lookup_filter_hook(&hook, commit.id())?;
1610-
let cw = commit_filter;
16111607

16121608
let parent_filters = commit
16131609
.parents()
@@ -1617,14 +1613,7 @@ fn apply_to_commit2(
16171613
})
16181614
.collect::<JoshResult<Vec<_>>>()?;
16191615

1620-
return per_rev_filter(
1621-
transaction,
1622-
commit,
1623-
filter,
1624-
commit_filter,
1625-
cw,
1626-
parent_filters,
1627-
);
1616+
return per_rev_filter(transaction, commit, filter, commit_filter, parent_filters);
16281617
}
16291618
_ => {
16301619
let filtered_parent_ids = commit
@@ -1767,24 +1756,7 @@ fn apply2<'a>(transaction: &'a cache::Transaction, op: &Op, x: Apply<'a>) -> Jos
17671756
.with_tree(tree::invert_paths(transaction, "", x.tree().clone())?))
17681757
}
17691758

1770-
Op::Workspace(path) => {
1771-
let wsj_file = to_filter(Op::File(Path::new("workspace.josh").to_owned()));
1772-
let base = to_filter(Op::Subdir(path.to_owned()));
1773-
let wsj_file = chain(base, wsj_file);
1774-
1775-
if let Some((redirect, _)) = resolve_workspace_redirect(repo, x.tree(), path) {
1776-
return apply(transaction, redirect, x.clone());
1777-
}
1778-
1779-
apply(
1780-
transaction,
1781-
compose(
1782-
wsj_file,
1783-
compose(get_workspace(repo, &x.tree(), path), base),
1784-
),
1785-
x,
1786-
)
1787-
}
1759+
Op::Workspace(path) => apply(transaction, get_workspace(repo, &x.tree(), &path), x),
17881760

17891761
Op::Compose(filters) => {
17901762
let filtered: Vec<_> = filters
@@ -1875,8 +1847,12 @@ fn unapply_workspace<'a>(
18751847
match op {
18761848
Op::Workspace(path) => {
18771849
let tree = pre_process_tree(transaction.repo(), tree)?;
1878-
let workspace = get_workspace(transaction.repo(), &tree, Path::new(""));
1879-
let original_workspace = get_workspace(transaction.repo(), &parent_tree, path);
1850+
let workspace = get_filter(transaction.repo(), &tree, Path::new("workspace.josh"));
1851+
let original_workspace = get_filter(
1852+
transaction.repo(),
1853+
&parent_tree,
1854+
&path.join("workspace.josh"),
1855+
);
18801856

18811857
let root = to_filter(Op::Subdir(path.to_owned()));
18821858
let wsj_file = to_filter(Op::File(Path::new("workspace.josh").to_owned()));
@@ -2071,15 +2047,14 @@ fn per_rev_filter(
20712047
commit: &git2::Commit,
20722048
filter: Filter,
20732049
commit_filter: Filter,
2074-
cw: Filter,
20752050
parent_filters: Vec<(git2::Commit, Filter)>,
20762051
) -> JoshResult<Option<git2::Oid>> {
20772052
// Compute the difference between the current commit's filter and each parent's filter.
20782053
// This determines what new content should be contributed by that parent in the filtered history.
20792054
let extra_parents = parent_filters
20802055
.into_iter()
20812056
.map(|(parent, pcw)| {
2082-
let f = opt::optimize(to_filter(Op::Subtract(cw, pcw)));
2057+
let f = opt::optimize(to_filter(Op::Subtract(commit_filter, pcw)));
20832058
apply_to_commit2(&to_op(f), &parent, transaction)
20842059
})
20852060
.collect::<JoshResult<Option<Vec<_>>>>()?;
@@ -2099,8 +2074,8 @@ fn per_rev_filter(
20992074

21002075
// Special case: `:pin` filter needs to be aware of filtered history
21012076
let pin_details = if let Some(&parent) = normal_parents.first() {
2102-
let legalized_a = legalize_pin(cw, &|f| f);
2103-
let legalized_b = legalize_pin(cw, &|f| to_filter(Op::Exclude(f)));
2077+
let legalized_a = legalize_pin(commit_filter, &|f| f);
2078+
let legalized_b = legalize_pin(commit_filter, &|f| to_filter(Op::Exclude(f)));
21042079

21052080
if legalized_a != legalized_b {
21062081
let pin_subtract = apply(

0 commit comments

Comments
 (0)