@@ -96,6 +96,7 @@ impl Default for History {
96
96
97
97
impl Revision {
98
98
fn serialize < W : Write > ( & self , writer : & mut W ) -> std:: io:: Result < ( ) > {
99
+ // `timestamp` is ignored since `Instant`s can't be serialized.
99
100
write_usize ( writer, self . parent ) ?;
100
101
self . transaction . serialize ( writer) ?;
101
102
self . inversion . serialize ( writer) ?;
@@ -117,8 +118,7 @@ impl Revision {
117
118
}
118
119
}
119
120
120
- // Temporarily 3 for review.
121
- const HEADER_TAG : & str = "Helix Undofile 4\n " ;
121
+ const HEADER_TAG : & str = "Helix Undofile 1\n " ;
122
122
123
123
fn get_hash < R : Read > ( reader : & mut R ) -> std:: io:: Result < [ u8 ; 20 ] > {
124
124
const BUF_SIZE : usize = 8192 ;
@@ -144,6 +144,7 @@ impl History {
144
144
revision : usize ,
145
145
last_saved_revision : usize ,
146
146
) -> std:: io:: Result < ( ) > {
147
+ // Header
147
148
let mtime = std:: fs:: metadata ( path) ?
148
149
. modified ( ) ?
149
150
. duration_since ( std:: time:: UNIX_EPOCH )
@@ -164,9 +165,14 @@ impl History {
164
165
Ok ( ( ) )
165
166
}
166
167
168
+ /// Returns the deserialized [`History`] and the last_saved_revision.
167
169
pub fn deserialize < R : Read > ( reader : & mut R , path : & Path ) -> std:: io:: Result < ( usize , Self ) > {
168
170
let ( current, last_saved_revision) = Self :: read_header ( reader, path) ?;
171
+
172
+ // Since `timestamp` can't be serialized, a new timestamp is created.
169
173
let timestamp = Instant :: now ( ) ;
174
+
175
+ // Read the revisions and construct the tree.
170
176
let len = read_usize ( reader) ?;
171
177
let mut revisions: Vec < Revision > = Vec :: with_capacity ( len) ;
172
178
for _ in 0 ..len {
@@ -184,6 +190,16 @@ impl History {
184
190
Ok ( ( last_saved_revision, history) )
185
191
}
186
192
193
+ /// If two histories originate from: `A -> B (B is head)` but have deviated since then such that
194
+ /// the first history is: `A -> B -> C -> D (D is head)` and the second one is:
195
+ /// `A -> B -> E -> F (F is head)`.
196
+ /// Then they are merged into
197
+ /// ```
198
+ /// A -> B -> C -> D
199
+ /// \
200
+ /// \ -> E -> F
201
+ /// ```
202
+ /// and retain their revision heads.
187
203
pub fn merge ( & mut self , mut other : History , offset : usize ) -> std:: io:: Result < ( ) > {
188
204
let revisions = self . revisions . split_off ( offset) ;
189
205
let len = other. revisions . len ( ) ;
@@ -823,6 +839,7 @@ mod test {
823
839
824
840
quickcheck ! (
825
841
fn serde_history( original: String , changes_a: Vec <String >, changes_b: Vec <String >) -> bool {
842
+ // Constructs a set of transactions and applies them to the history.
826
843
fn create_changes( history: & mut History , doc: & mut Rope , changes: Vec <String >) {
827
844
for c in changes. into_iter( ) . map( Rope :: from) {
828
845
let transaction = crate :: diff:: compare_ropes( doc, & c) ;
@@ -843,6 +860,8 @@ mod test {
843
860
let file = tempfile:: NamedTempFile :: new( ) . unwrap( ) ;
844
861
history. serialize( & mut cursor, file. path( ) , 0 , 0 ) . unwrap( ) ;
845
862
cursor. set_position( 0 ) ;
863
+
864
+ // Check if the original and deserialized history match.
846
865
let ( _, res) = History :: deserialize( & mut cursor, file. path( ) ) . unwrap( ) ;
847
866
assert_eq!( history, res) ;
848
867
@@ -854,6 +873,8 @@ mod test {
854
873
. serialize( & mut cursor, file. path( ) , 0 , last_saved_revision)
855
874
. unwrap( ) ;
856
875
cursor. set_position( 0 ) ;
876
+
877
+ // Check if they are the same after appending new changes.
857
878
let ( _, res) = History :: deserialize( & mut cursor, file. path( ) ) . unwrap( ) ;
858
879
history == res
859
880
}
0 commit comments