summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortummychow <tummychow@users.noreply.github.com>2018-02-26 22:33:11 -0800
committertummychow <tummychow@users.noreply.github.com>2018-02-26 22:33:11 -0800
commitcc34a415c76e576f8b8229a9ef26b2bd51ef5cef (patch)
tree167b5106cdc077438819ad90050e2ba3e1dfe521
parent5438990e63a65d48a1b6c5da4ff75ee49474502e (diff)
implement basic absorb commute loop
-rw-r--r--src/lib.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 304a28e..2fed7f3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -65,5 +65,56 @@ pub fn run(config: &Config) -> Result<(), failure::Error> {
"index" => format!("{:?}", index),
);
+ 'patch: for index_patch in index.iter() {
+ 'hunk: for index_hunk in &index_patch.hunks {
+ let mut commuted_index_hunk = index_hunk.clone();
+ let mut commuted_old_path = match index_patch.old_path.as_ref() {
+ Some(path) => path,
+ // this index patch is for a newly added file, so it
+ // can't be absorbed, and the whole patch should be
+ // skipped
+ None => continue 'patch,
+ };
+
+ // find the newest commit that the hunk cannot commute
+ // with
+ let mut dest_commit = None;
+ 'commit: for &(ref commit, ref diff) in &stack {
+ let next_patch = match diff.by_new(commuted_old_path) {
+ Some(patch) => patch,
+ // this commit doesn't touch the hunk's file, so
+ // they trivially commute, and the next commit
+ // should be considered
+ None => continue 'commit,
+ };
+ commuted_old_path = match next_patch.old_path.as_ref() {
+ Some(path) => path,
+ // this commit introduced the file that the hunk
+ // is part of, so the hunk cannot commute with it
+ None => {
+ dest_commit = Some(commit);
+ break 'commit;
+ }
+ };
+ commuted_index_hunk =
+ match commute::commute_diff_before(&commuted_index_hunk, &next_patch.hunks) {
+ Some(hunk) => hunk,
+ // this commit contains a hunk that cannot
+ // commute with the hunk being absorbed
+ None => {
+ dest_commit = Some(commit);
+ break 'commit;
+ }
+ };
+ }
+ let dest_commit = match dest_commit {
+ Some(commit) => commit,
+ // the hunk commutes with every commit in the stack,
+ // so there is no commit to absorb it into
+ None => continue 'hunk,
+ };
+ }
+ }
+
Ok(())
}