summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortummychow <tummychow@users.noreply.github.com>2018-03-10 18:03:52 -0800
committertummychow <tummychow@users.noreply.github.com>2018-03-10 18:03:52 -0800
commit406c0bb29ed21750a279cf3ec69d9e20fa41d998 (patch)
tree54332f68ca7837ce39e195f3b25a8c10ef11bc63
parent5b1824f5e4e647794993a3f88837c8c54930307f (diff)
remove option from patch paths
it seems these can never actually be null (you can't be sure with cstrings but that's how it appears) and you're supposed to identify an add/delete solely from the status field. works for me
-rw-r--r--src/lib.rs46
-rw-r--r--src/owned.rs36
2 files changed, 40 insertions, 42 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 2514ada..2366054 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -68,16 +68,14 @@ pub fn run(config: &Config) -> Result<(), failure::Error> {
'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 => {
- debug!(config.logger, "skipped added hunk");
- continue 'patch;
- }
- };
+ if index_patch.status != git2::Delta::Modified {
+ debug!(config.logger, "skipped non-modified hunk";
+ "path" => String::from_utf8_lossy(index_patch.new_path.as_slice()).into_owned(),
+ "status" => format!("{:?}", index_patch.status),
+ );
+ continue 'patch;
+ }
+ let mut commuted_old_path = index_patch.old_path.as_slice();
debug!(config.logger, "commuting hunk";
"path" => String::from_utf8_lossy(commuted_old_path).into_owned(),
"header" => format!("-{},{} +{},{}",
@@ -105,23 +103,17 @@ pub fn run(config: &Config) -> Result<(), failure::Error> {
continue 'commit;
}
};
- commuted_old_path = match next_patch.old_path.as_ref() {
- Some(path) => {
- if commuted_old_path != path {
- debug!(c_logger, "changed commute path";
- "path" => String::from_utf8_lossy(path).into_owned(),
- );
- }
- path
- }
- // this commit introduced the file that the hunk
- // is part of, so the hunk cannot commute with it
- None => {
- debug!(c_logger, "found noncommutative commit by add");
- dest_commit = Some(commit);
- break 'commit;
- }
- };
+ if next_patch.status == git2::Delta::Added {
+ debug!(c_logger, "found noncommutative commit by add");
+ dest_commit = Some(commit);
+ break 'commit;
+ }
+ if commuted_old_path != next_patch.old_path.as_slice() {
+ debug!(c_logger, "changed commute path";
+ "path" => String::from_utf8_lossy(&next_patch.old_path).into_owned(),
+ );
+ commuted_old_path = next_patch.old_path.as_slice();
+ }
commuted_index_hunk = match commute::commute_diff_before(
&commuted_index_hunk,
&next_patch.hunks,
diff --git a/src/owned.rs b/src/owned.rs
index dd06182..71e7c25 100644
--- a/src/owned.rs
+++ b/src/owned.rs
@@ -27,19 +27,15 @@ impl Diff {
for (delta_idx, _delta) in diff.deltas().enumerate() {
let patch = Patch::new(&mut git2::Patch::from_diff(diff, delta_idx)?
.ok_or_else(|| failure::err_msg("got empty delta"))?)?;
- if let Some(path) = patch.old_path.as_ref() {
- if ret.by_old.contains_key(path) {
- // TODO: would this case be hit if the diff was put through copy detection?
- return Err(failure::err_msg("old path already occupied"));
- }
- ret.by_old.insert(path.to_vec(), ret.patches.len());
+ if ret.by_old.contains_key(&patch.old_path) {
+ // TODO: would this case be hit if the diff was put through copy detection?
+ return Err(failure::err_msg("old path already occupied"));
}
- if let Some(path) = patch.new_path.as_ref() {
- if ret.by_new.contains_key(path) {
- return Err(failure::err_msg("new path already occupied"));
- }
- ret.by_new.insert(path.to_vec(), ret.patches.len());
+ ret.by_old.insert(patch.old_path.clone(), ret.patches.len());
+ if ret.by_new.contains_key(&patch.new_path) {
+ return Err(failure::err_msg("new path already occupied"));
}
+ ret.by_new.insert(patch.new_path.clone(), ret.patches.len());
ret.patches.push(patch);
}
@@ -155,9 +151,9 @@ impl Hunk {
#[derive(Debug)]
pub struct Patch {
- pub old_path: Option<Vec<u8>>,
+ pub old_path: Vec<u8>,
pub old_id: git2::Oid,
- pub new_path: Option<Vec<u8>>,
+ pub new_path: Vec<u8>,
pub new_id: git2::Oid,
pub status: git2::Delta,
pub hunks: Vec<Hunk>,
@@ -165,9 +161,19 @@ pub struct Patch {
impl Patch {
pub fn new(patch: &mut git2::Patch) -> Result<Patch, failure::Error> {
let mut ret = Patch {
- old_path: patch.delta().old_file().path_bytes().map(Vec::from),
+ old_path: patch
+ .delta()
+ .old_file()
+ .path_bytes()
+ .map(Vec::from)
+ .ok_or_else(|| failure::err_msg("delta with empty old path"))?,
old_id: patch.delta().old_file().id(),
- new_path: patch.delta().new_file().path_bytes().map(Vec::from),
+ new_path: patch
+ .delta()
+ .new_file()
+ .path_bytes()
+ .map(Vec::from)
+ .ok_or_else(|| failure::err_msg("delta with empty new path"))?,
new_id: patch.delta().new_file().id(),
status: patch.delta().status(),
hunks: Vec::with_capacity(patch.num_hunks()),