@@ -32,50 +32,90 @@ impl Default for Action {
32
32
33
33
/// Represents any possible change in order to turn one tree into another.
34
34
#[ derive( Debug , Clone , Copy ) ]
35
- pub struct Change < ' a , ' repo , ' other_repo > {
35
+ pub struct Change < ' a , ' old , ' new > {
36
36
/// The location of the file or directory described by `event`, if tracking was enabled.
37
37
///
38
38
/// Otherwise this value is always an empty path.
39
39
pub location : & ' a BStr ,
40
40
/// The diff event itself to provide information about what would need to change.
41
- pub event : change:: Event < ' repo , ' other_repo > ,
41
+ pub event : change:: Event < ' old , ' new > ,
42
42
}
43
43
44
44
///
45
45
pub mod change {
46
+ use crate :: bstr:: ByteSlice ;
46
47
use crate :: Id ;
48
+ use git_object:: tree:: EntryMode ;
47
49
48
50
/// An event emitted when finding differences between two trees.
49
51
#[ derive( Debug , Clone , Copy ) ]
50
- pub enum Event < ' repo , ' other_repo > {
52
+ pub enum Event < ' old , ' new > {
51
53
/// An entry was added, like the addition of a file or directory.
52
54
Addition {
53
55
/// The mode of the added entry.
54
56
entry_mode : git_object:: tree:: EntryMode ,
55
57
/// The object id of the added entry.
56
- id : Id < ' other_repo > ,
58
+ id : Id < ' new > ,
57
59
} ,
58
60
/// An entry was deleted, like the deletion of a file or directory.
59
61
Deletion {
60
62
/// The mode of the deleted entry.
61
63
entry_mode : git_object:: tree:: EntryMode ,
62
64
/// The object id of the deleted entry.
63
- id : Id < ' repo > ,
65
+ id : Id < ' old > ,
64
66
} ,
65
67
/// An entry was modified, e.g. changing the contents of a file adjusts its object id and turning
66
68
/// a file into a symbolic link adjusts its mode.
67
69
Modification {
68
70
/// The mode of the entry before the modification.
69
71
previous_entry_mode : git_object:: tree:: EntryMode ,
70
72
/// The object id of the entry before the modification.
71
- previous_id : Id < ' repo > ,
73
+ previous_id : Id < ' old > ,
72
74
73
75
/// The mode of the entry after the modification.
74
76
entry_mode : git_object:: tree:: EntryMode ,
75
77
/// The object id after the modification.
76
- id : Id < ' other_repo > ,
78
+ id : Id < ' new > ,
77
79
} ,
78
80
}
81
+
82
+ /// A platform to keep temporary information to perform line diffs.
83
+ pub struct DiffPlatform < ' old , ' new > {
84
+ old : crate :: Object < ' old > ,
85
+ new : crate :: Object < ' new > ,
86
+ }
87
+
88
+ impl < ' old , ' new > Event < ' old , ' new > {
89
+ /// Produce a platform for performing a line-diff, or `None` if this is not a [`Modification`][Event::Modification]
90
+ /// or one of the entries to compare is not a blob.
91
+ pub fn diff ( & self ) -> Option < Result < DiffPlatform < ' old , ' new > , crate :: object:: find:: existing:: Error > > {
92
+ match self {
93
+ Event :: Modification {
94
+ previous_entry_mode : EntryMode :: BlobExecutable | EntryMode :: Blob ,
95
+ previous_id,
96
+ entry_mode : EntryMode :: BlobExecutable | EntryMode :: Blob ,
97
+ id,
98
+ } => match previous_id. object ( ) . and_then ( |old| id. object ( ) . map ( |new| ( old, new) ) ) {
99
+ Ok ( ( old, new) ) => Some ( Ok ( DiffPlatform { old, new } ) ) ,
100
+ Err ( err) => Some ( Err ( err) ) ,
101
+ } ,
102
+ _ => None ,
103
+ }
104
+ }
105
+ }
106
+
107
+ impl < ' old , ' new > DiffPlatform < ' old , ' new > {
108
+ /// Create a platform for performing various tasks to diff text.
109
+ ///
110
+ /// It can be used to traverse [all line changes](git_diff::lines::similar::TextDiff::iter_all_changes()) for example.
111
+ // TODO: How should this integrate with configurable algorithms? Maybe users should get it themselves and pass it here?
112
+ pub fn text < ' bufs > (
113
+ & self ,
114
+ algorithm : git_diff:: lines:: Algorithm ,
115
+ ) -> git_diff:: lines:: similar:: TextDiff < ' _ , ' _ , ' bufs , [ u8 ] > {
116
+ git_diff:: lines:: with ( self . old . data . as_bstr ( ) , self . new . data . as_bstr ( ) , algorithm)
117
+ }
118
+ }
79
119
}
80
120
81
121
/// Diffing
@@ -126,12 +166,12 @@ impl<'a, 'repo> Platform<'a, 'repo> {
126
166
}
127
167
128
168
/// Add the item to compare to.
129
- impl < ' a , ' repo > Platform < ' a , ' repo > {
169
+ impl < ' a , ' old > Platform < ' a , ' old > {
130
170
/// Call `for_each` repeatedly with all changes that are needed to convert the source of the diff to the tree to `other`.
131
- pub fn for_each_to_obtain_tree < ' other_repo , E > (
171
+ pub fn for_each_to_obtain_tree < ' new , E > (
132
172
& mut self ,
133
- other : & Tree < ' other_repo > ,
134
- for_each : impl FnMut ( Change < ' _ , ' repo , ' other_repo > ) -> Result < Action , E > ,
173
+ other : & Tree < ' new > ,
174
+ for_each : impl FnMut ( Change < ' _ , ' old , ' new > ) -> Result < Action , E > ,
135
175
) -> Result < ( ) , Error >
136
176
where
137
177
E : std:: error:: Error + Sync + Send + ' static ,
@@ -159,9 +199,9 @@ impl<'a, 'repo> Platform<'a, 'repo> {
159
199
}
160
200
}
161
201
162
- struct Delegate < ' repo , ' other_repo , VisitFn , E > {
163
- repo : & ' repo Repository ,
164
- other_repo : & ' other_repo Repository ,
202
+ struct Delegate < ' old , ' new , VisitFn , E > {
203
+ repo : & ' old Repository ,
204
+ other_repo : & ' new Repository ,
165
205
tracking : Option < Tracking > ,
166
206
location : BString ,
167
207
path_deque : VecDeque < BString > ,
@@ -186,9 +226,9 @@ impl<A, B> Delegate<'_, '_, A, B> {
186
226
}
187
227
}
188
228
189
- impl < ' repo , ' other_repo , VisitFn , E > git_diff:: tree:: Visit for Delegate < ' repo , ' other_repo , VisitFn , E >
229
+ impl < ' old , ' new , VisitFn , E > git_diff:: tree:: Visit for Delegate < ' old , ' new , VisitFn , E >
190
230
where
191
- VisitFn : for < ' delegate > FnMut ( Change < ' delegate , ' repo , ' other_repo > ) -> Result < Action , E > ,
231
+ VisitFn : for < ' delegate > FnMut ( Change < ' delegate , ' old , ' new > ) -> Result < Action , E > ,
192
232
E : std:: error:: Error + Sync + Send + ' static ,
193
233
{
194
234
fn pop_front_tracked_path_and_set_current ( & mut self ) {
0 commit comments