History

[tree-diff] A step closer to handling additions in a directory

Sebastian Thiel committed 5 years ago

Commit a11f2101 parent f1a575c6 files · +65 −11Browse files

Showing the changes to git-diff/src (3 of 6 files). Show all

Unified · Split

3 files
  1. git-diff/src/visit/changes.rs +20 −8
  2. git-diff/src/visit/record.rs +3 −2
  3. git-diff/src/visit/recorder.rs +14 −0
git-diff/src/visit/changes.rs+20−8
@@ -52,26 +52,38 @@ impl<'a> visit::Changes<'a> { · 51 unmodified lines
5252 use std::cmp::Ordering::*;
5353 match lhs.filename.cmp(rhs.filename) {
5454 Equal => {
55+ use tree::EntryMode::*;
5556 if lhs.oid != rhs.oid || lhs.mode != rhs.mode {
5657 delegate.update_path_component(
5758 PathComponent::new(lhs.filename, &mut path_id),
5859 PathComponentUpdateMode::Replace,
5960 );
60− if delegate
61− .record(Change::Modification {
61+ let record_result = if (lhs.mode.is_no_tree() && rhs.mode.is_tree())
62+ || (rhs.mode.is_tree() && rhs.mode.is_no_tree())
63+ {
64+ delegate.record(Change::Deletion {
65+ entry_mode: lhs.mode,
66+ oid: lhs.oid.to_owned(),
67+ path_id,
68+ })
69+ } else {
70+ delegate.record(Change::Modification {
6271 previous_entry_mode: lhs.mode,
6372 previous_oid: lhs.oid.to_owned(),
6473 entry_mode: rhs.mode,
6574 oid: rhs.oid.to_owned(),
6675 path_id,
6776 })
68− .cancelled()
69− {
77+ };
78+ if record_result.cancelled() {
7079 break Err(Error::Cancelled);
7180 }
7281 }
73− if lhs.mode == tree::EntryMode::Tree || rhs.mode == tree::EntryMode::Tree {
74− todo!("handle recursion")
82+ match (lhs.mode, rhs.mode) {
83+ (Tree, Tree) => todo!("recurse tree|tree"),
84+ (lhs, Tree) if !lhs.is_tree() => todo!("recurse non-tree|tree"),
85+ (Tree, rhs) if !rhs.is_tree() => todo!("recurse tree|non-tree"),
86+ _both_are_not_trees => {}
7587 }
7688 }
7789 Less => todo!("entry compares less - catch up"),
@@ -85,8 +97,8 @@ impl<'a> visit::Changes<'a> { · 7 unmodified lines
8597 );
8698 if delegate
8799 .record(Change::Deletion {
88− previous_entry_mode: lhs.mode,
89− previous_oid: lhs.oid.to_owned(),
100+ entry_mode: lhs.mode,
101+ oid: lhs.oid.to_owned(),
90102 path_id,
91103 })
92104 .cancelled()
git-diff/src/visit/record.rs+3−2
@@ -3,6 +3,7 @@ use git_object::{bstr::BStr, tree}; · 2 unmodified lines
33
44 pub type PathId = usize;
55
6+/// `path_id`s are kept for rename or copy tracking to allow referring to a path in the tree more easily.
67 pub enum Change {
78 Addition {
89 entry_mode: tree::EntryMode,
@@ -11,8 +12,8 @@ pub enum Change { · 2 unmodified lines
1112 },
1213 Copy,
1314 Deletion {
14− previous_entry_mode: tree::EntryMode,
15− previous_oid: ObjectId,
15+ entry_mode: tree::EntryMode,
16+ oid: ObjectId,
1617 path_id: PathId,
1718 },
1819 Modification {
git-diff/src/visit/recorder.rs+14−0
@@ -13,6 +13,11 @@ pub enum Change { · 12 unmodified lines
1313 oid: ObjectId,
1414 path: PathBuf,
1515 },
16+ Deletion {
17+ entry_mode: tree::EntryMode,
18+ oid: ObjectId,
19+ path: PathBuf,
20+ },
1621 Modification {
1722 previous_entry_mode: tree::EntryMode,
1823 previous_oid: ObjectId,
@@ -70,6 +75,15 @@ impl record::Record for Recorder { · 51 unmodified lines
7075 fn record(&mut self, change: record::Change) -> record::Action {
7176 use record::Change::*;
7277 self.records.push(match change {
78+ Deletion {
79+ entry_mode,
80+ oid,
81+ path_id: _,
82+ } => Change::Deletion {
83+ entry_mode,
84+ oid,
85+ path: self.path_buf(),
86+ },
7387 Addition {
7488 entry_mode,
7589 oid,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t