Skip to content

Commit 3094214

Browse files
authored
Merge pull request #2017 from GitoxideLabs/improvements
various improvements
2 parents fd49eee + 0eaced9 commit 3094214

File tree

1 file changed

+78
-51
lines changed

1 file changed

+78
-51
lines changed

gix-diff/src/rewrites/tracker.rs

Lines changed: 78 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -239,69 +239,96 @@ impl<T: Change> Tracker<T> {
239239
.then_with(|| a.path.start.cmp(&b.path.start).then(a.path.end.cmp(&b.path.end)))
240240
}
241241

242+
// Early abort: if there is no pair, don't do anything.
243+
let has_work = {
244+
let (mut num_deletions, mut num_additions, mut num_modifications) = (0, 0, 0);
245+
let mut has_work = false;
246+
for change in &self.items {
247+
match change.change.kind() {
248+
ChangeKind::Deletion => {
249+
num_deletions += 1;
250+
}
251+
ChangeKind::Modification => {
252+
// This means we have copy-tracking enabled
253+
num_modifications += 1;
254+
}
255+
ChangeKind::Addition => num_additions += 1,
256+
}
257+
if (num_deletions != 0 && num_additions != 0)
258+
|| (self.rewrites.copies.is_some() && num_modifications + num_additions > 1)
259+
{
260+
has_work = true;
261+
break;
262+
}
263+
}
264+
has_work
265+
};
266+
242267
let mut out = Outcome {
243268
options: self.rewrites,
244269
..Default::default()
245270
};
246-
self.items.sort_by(by_id_and_location);
247-
248-
// Rewrites by directory (without local changes) can be pruned out quickly,
249-
// by finding only parents, their counterpart, and then all children can be matched by
250-
// relationship ID.
251-
self.match_pairs_of_kind(
252-
visit::SourceKind::Rename,
253-
&mut cb,
254-
None, /* by identity for parents */
255-
&mut out,
256-
diff_cache,
257-
objects,
258-
Some(is_parent),
259-
)?;
260-
261-
self.match_pairs_of_kind(
262-
visit::SourceKind::Rename,
263-
&mut cb,
264-
self.rewrites.percentage,
265-
&mut out,
266-
diff_cache,
267-
objects,
268-
None,
269-
)?;
270-
271-
self.match_renamed_directories(&mut cb)?;
272-
273-
if let Some(copies) = self.rewrites.copies {
271+
if has_work {
272+
self.items.sort_by(by_id_and_location);
273+
274+
// Rewrites by directory (without local changes) can be pruned out quickly,
275+
// by finding only parents, their counterpart, and then all children can be matched by
276+
// relationship ID.
277+
self.match_pairs_of_kind(
278+
visit::SourceKind::Rename,
279+
&mut cb,
280+
None, /* by identity for parents */
281+
&mut out,
282+
diff_cache,
283+
objects,
284+
Some(is_parent),
285+
)?;
286+
274287
self.match_pairs_of_kind(
275-
visit::SourceKind::Copy,
288+
visit::SourceKind::Rename,
276289
&mut cb,
277-
copies.percentage,
290+
self.rewrites.percentage,
278291
&mut out,
279292
diff_cache,
280293
objects,
281294
None,
282295
)?;
283296

284-
match copies.source {
285-
CopySource::FromSetOfModifiedFiles => {}
286-
CopySource::FromSetOfModifiedFilesAndAllSources => {
287-
push_source_tree(&mut |change, location| {
288-
if self.try_push_change(change, location).is_none() {
289-
// make sure these aren't viable to be emitted anymore.
290-
self.items.last_mut().expect("just pushed").emitted = true;
291-
}
292-
})
293-
.map_err(|err| emit::Error::GetItemsForExhaustiveCopyDetection(Box::new(err)))?;
294-
self.items.sort_by(by_id_and_location);
295-
296-
self.match_pairs_of_kind(
297-
visit::SourceKind::Copy,
298-
&mut cb,
299-
copies.percentage,
300-
&mut out,
301-
diff_cache,
302-
objects,
303-
None,
304-
)?;
297+
self.match_renamed_directories(&mut cb)?;
298+
299+
if let Some(copies) = self.rewrites.copies {
300+
self.match_pairs_of_kind(
301+
visit::SourceKind::Copy,
302+
&mut cb,
303+
copies.percentage,
304+
&mut out,
305+
diff_cache,
306+
objects,
307+
None,
308+
)?;
309+
310+
match copies.source {
311+
CopySource::FromSetOfModifiedFiles => {}
312+
CopySource::FromSetOfModifiedFilesAndAllSources => {
313+
push_source_tree(&mut |change, location| {
314+
if self.try_push_change(change, location).is_none() {
315+
// make sure these aren't viable to be emitted anymore.
316+
self.items.last_mut().expect("just pushed").emitted = true;
317+
}
318+
})
319+
.map_err(|err| emit::Error::GetItemsForExhaustiveCopyDetection(Box::new(err)))?;
320+
self.items.sort_by(by_id_and_location);
321+
322+
self.match_pairs_of_kind(
323+
visit::SourceKind::Copy,
324+
&mut cb,
325+
copies.percentage,
326+
&mut out,
327+
diff_cache,
328+
objects,
329+
None,
330+
)?;
331+
}
305332
}
306333
}
307334
}

0 commit comments

Comments
 (0)