Skip to content
Draft
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions docs/src/reference/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ commits that don't match any of the other shas.
Produce the history that would be the result of pushing the passed branches with the
passed filters into the upstream.

### Prune trivial merge commits **:prune=trivial-merge**

Produce a history that skips all merge commits whose tree is identical to the first parents
tree.
Normally Josh will keep all commits in the filtered history whose tree differs from any of it's
parents.

Filter order matters
--------------------

Expand Down
30 changes: 30 additions & 0 deletions josh-core/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ enum Op {
Rev(std::collections::BTreeMap<LazyRef, Filter>),
Join(std::collections::BTreeMap<LazyRef, Filter>),
Linear,
Prune,
Unsign,

RegexReplace(Vec<(regex::Regex, String)>),
Expand Down Expand Up @@ -485,6 +486,7 @@ fn spec2(op: &Op) -> String {
Op::Unsign => ":unsign".to_string(),
Op::Subdir(path) => format!(":/{}", parse::quote_if(&path.to_string_lossy())),
Op::File(path) => format!("::{}", parse::quote_if(&path.to_string_lossy())),
Op::Prune => ":prune=trivial-merge".to_string(),
Op::Prefix(path) => format!(":prefix={}", parse::quote_if(&path.to_string_lossy())),
Op::Pattern(pattern) => format!("::{}", parse::quote_if(pattern)),
Op::Author(author, email) => {
Expand Down Expand Up @@ -811,6 +813,33 @@ fn apply_to_commit2(
))
.transpose();
}
Op::Prune => {
let p: Vec<_> = commit.parent_ids().collect();

if p.len() > 0 {
let parent = some_or!(transaction.get(filter, p[0]), {
return Ok(None);
});

let parent_tree = transaction.repo().find_commit(parent)?.tree_id();

if parent_tree == commit.tree_id() {
return Ok(Some(history::drop_commit(
commit,
vec![parent],
transaction,
filter,
)?));
}
}

RewriteData {
tree: commit.tree()?,
message: None,
author: None,
committer: None,
}
}
Op::Unsign => {
let parents: Vec<_> = commit.parent_ids().collect();

Expand Down Expand Up @@ -998,6 +1027,7 @@ fn apply2<'a>(
Op::Committer(_, _) => Ok(tree),
Op::Squash(Some(_)) => Err(josh_error("not applicable to tree")),
Op::Linear => Ok(tree),
Op::Prune => Ok(tree),
Op::Unsign => Ok(tree),
Op::Rev(_) => Err(josh_error("not applicable to tree")),
Op::Join(_) => Err(josh_error("not applicable to tree")),
Expand Down
1 change: 1 addition & 0 deletions josh-core/src/filter/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ pub fn invert(filter: Filter) -> JoshResult<Filter> {
let result = match to_op(filter) {
Op::Nop => Some(Op::Nop),
Op::Linear => Some(Op::Nop),
Op::Prune => Some(Op::Prune),
Op::Unsign => Some(Op::Unsign),
Op::Empty => Some(Op::Empty),
Op::Subdir(path) => Some(Op::Prefix(path)),
Expand Down
16 changes: 16 additions & 0 deletions josh-core/src/filter/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ fn make_op(args: &[&str]) -> JoshResult<Op> {
["SQUASH"] => Ok(Op::Squash(None)),
["SQUASH", _ids @ ..] => Err(josh_error("SQUASH with ids can't be parsed")),
["linear"] => Ok(Op::Linear),
["prune", "trivial-merge"] => Ok(Op::Prune),
["prune"] => Err(josh_error(indoc!(
r#"
Filter ":prune" requires an argument.

Note: use "=" to provide the argument value:

:prune=trivial-merge
"#
))),
["prune", _] => Err(josh_error(indoc!(
r#"
Filter ":prune" only supports "trivial-merge"
as arguement value.
Copy link
Collaborator

Choose a reason for hiding this comment

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

argument

"#
))),
["unsign"] => Ok(Op::Unsign),
["PATHS"] => Ok(Op::Paths),
["INDEX"] => Ok(Op::Index),
Expand Down
47 changes: 47 additions & 0 deletions tests/filter/prune_trivial_merge.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
$ export RUST_BACKTRACE=1
$ git init -q 1> /dev/null

$ echo contents1 > file1
$ git add .
$ git commit -m "add file1" 1> /dev/null

$ git log --graph --pretty=%s
* add file1

$ git checkout -b branch1
Switched to a new branch 'branch1'
$ echo contents2 > file2
$ git add .
$ git commit -m "add file2" 1> /dev/null

$ git checkout master
Switched to branch 'master'

$ echo contents3 > file1
$ git add .
$ git commit -m "mod file1" 1> /dev/null

$ git merge -q branch1 --no-ff
$ git log --graph --pretty=%s
* Merge branch 'branch1'
|\
| * add file2
* | mod file1
|/
* add file1

$ josh-filter -s ::file1
[3] ::file1
$ git log --graph --pretty=%s FILTERED_HEAD
* Merge branch 'branch1'
|\
* | mod file1
|/
* add file1
$ josh-filter -s ::file1:prune=trivial-merge
[2] :prune=trivial-merge
[3] ::file1

$ git log --graph --pretty=%s FILTERED_HEAD
* mod file1
* add file1