…but in practice it doesn't as the offset it calculates ends up being outside of the written pack. This shouldn't be possible, and our tests thus far don't uncover this issue either.
Unified · Split
| @@ -41,7 +41,7 @@ where · 40 unmodified lines | ||
| 41 | 41 | new_ofs.try_into().expect("offset value is never becomes negative") |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | − /// positive `change` values mean an object grew or was more commonly, was inserted. Negative values | |
| 44 | + /// positive `size_change` values mean an object grew or was more commonly, was inserted. Negative values | |
| 45 | 45 | /// mean the object shrunk, usually because there header changed from ref-deltas to ofs deltas. |
| 46 | 46 | fn track_change( |
| 47 | 47 | &mut self, |
| @@ -121,11 +121,12 @@ where · 73 unmodified lines | ||
| 121 | 121 | } |
| 122 | 122 | Some(Ok(base_entry)) |
| 123 | 123 | } |
| 124 | − Some(base_entry) => { | |
| 125 | − let base_distance = | |
| 126 | − self.shifted_pack_offset(entry.pack_offset) - base_entry.shifted_pack_offset; | |
| 127 | − self.shift_entry_and_point_to_base_by_offset(&mut entry, base_distance); | |
| 128 | − Some(Ok(entry)) | |
| 124 | + Some(_base_entry) => { | |
| 125 | + todo!("need a test for reusing existing inserted bases") | |
| 126 | + // let base_distance = | |
| 127 | + // self.shifted_pack_offset(entry.pack_offset) - base_entry.shifted_pack_offset; | |
| 128 | + // self.shift_entry_and_point_to_base_by_offset(&mut entry, base_distance); | |
| 129 | + // Some(Ok(entry)) | |
| 129 | 130 | } |
| 130 | 131 | } |
| 131 | 132 | } |
| @@ -160,9 +161,17 @@ where · 28 unmodified lines | ||
| 160 | 161 | .expect("a base that is behind us in the pack"); |
| 161 | 162 | self.shift_entry_and_point_to_base_by_offset(&mut entry, new_distance); |
| 162 | 163 | } |
| 163 | − Err(_index) => { | |
| 164 | − entry.pack_offset = self.shifted_pack_offset(entry.pack_offset); | |
| 165 | − todo!("close, aggregate the size changes since then to know where we are pointing") | |
| 164 | + Err(index) => { | |
| 165 | + let change_since_offset = self.inserted_entry_length_at_offset[index..] | |
| 166 | + .iter() | |
| 167 | + .map(|c| c.size_change_in_bytes) | |
| 168 | + .sum::<i64>(); | |
| 169 | + let new_distance: u64 = { | |
| 170 | + (base_distance as i64 + change_since_offset) | |
| 171 | + .try_into() | |
| 172 | + .expect("it still points behind us") | |
| 173 | + }; | |
| 174 | + self.shift_entry_and_point_to_base_by_offset(&mut entry, new_distance); | |
| 166 | 175 | } |
| 167 | 176 | } |
| 168 | 177 | } else { |
| @@ -46,21 +46,28 @@ mod lookup_ref_delta_objects { · 45 unmodified lines | ||
| 46 | 46 | entries |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | + fn validate_pack_offsets(entries: &[input::Entry]) { | |
| 50 | + let mut offset = 0; | |
| 51 | + for (eid, entry) in entries.iter().enumerate() { | |
| 52 | + assert_eq!(entry.pack_offset, offset, "invalid pack offset for entry {}", eid); | |
| 53 | + offset += entry.bytes_in_pack(); | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 49 | 57 | fn into_results_iter(entries: Vec<input::Entry>) -> impl Iterator<Item = Result<input::Entry, input::Error>> { |
| 50 | 58 | entries.into_iter().map(Ok) |
| 51 | 59 | } |
| 52 | 60 | |
| 53 | 61 | #[test] |
| 54 | − fn only_ref_deltas_are_handled() { | |
| 62 | + fn only_ref_deltas_are_handled() -> crate::Result { | |
| 55 | 63 | let input = compute_offsets(vec![entry(base(), D_A), entry(delta_ofs(100), D_B)]); |
| 56 | 64 | let expected = input.clone(); |
| 57 | − assert_eq!( | |
| 65 | + let actual = | |
| 58 | 66 | LookupRefDeltaObjectsIter::new(into_results_iter(input), |_, _| unreachable!("not going to be called")) |
| 59 | − .collect::<Result<Vec<_>, _>>() | |
| 60 | − .unwrap(), | |
| 61 | − expected, | |
| 62 | − "it won't change the input at all" | |
| 63 | − ) | |
| 67 | + .collect::<Result<Vec<_>, _>>()?; | |
| 68 | + assert_eq!(actual, expected, "it won't change the input at all"); | |
| 69 | + validate_pack_offsets(&actual); | |
| 70 | + Ok(()) | |
| 64 | 71 | } |
| 65 | 72 | |
| 66 | 73 | #[test] |
| @@ -72,8 +79,9 @@ mod lookup_ref_delta_objects { · 5 unmodified lines | ||
| 72 | 79 | let inserted_data = D_D; |
| 73 | 80 | let mut inserted = entry(base(), inserted_data); |
| 74 | 81 | let second = entry(delta_ref(second_id), D_B); |
| 75 | − let mut last_entry = entry(delta_ofs(second.bytes_in_pack()), D_C); | |
| 76 | − let input = compute_offsets(vec![first.clone(), second, last_entry.clone()]); | |
| 82 | + let third_entry = entry(delta_ofs(second.bytes_in_pack()), D_C); | |
| 83 | + let fourth_entry = entry(delta_ofs(third_entry.bytes_in_pack()), D_D); | |
| 84 | + let input = compute_offsets(vec![first.clone(), second, third_entry, fourth_entry]); | |
| 77 | 85 | |
| 78 | 86 | let mut calls = 0; |
| 79 | 87 | let actual = LookupRefDeltaObjectsIter::new(into_results_iter(input), |_oid, buf| { |
| @@ -90,7 +98,7 @@ mod lookup_ref_delta_objects { · 10 unmodified lines | ||
| 90 | 98 | .unwrap(); |
| 91 | 99 | |
| 92 | 100 | assert_eq!(calls, 2, "there is only two objects to replace"); |
| 93 | − assert_eq!(actual.len(), 5, "two object was inserted"); | |
| 101 | + assert_eq!(actual.len(), 6, "two object was inserted"); | |
| 94 | 102 | |
| 95 | 103 | assert_eq!(&actual[0], &inserted, "first object is inserted one"); |
| 96 | 104 | |
| @@ -122,18 +130,31 @@ mod lookup_ref_delta_objects { · 25 unmodified lines | ||
| 122 | 130 | "the pack offset was adjusted to accommodate for preceding objects" |
| 123 | 131 | ); |
| 124 | 132 | |
| 125 | − let actual_last = &actual[4]; | |
| 126 | − last_entry.pack_offset = | |
| 133 | + let actual_third = &actual[4]; | |
| 134 | + let third_entry_pack_offset = | |
| 127 | 135 | inserted.bytes_in_pack() + first_altered_len + inserted.bytes_in_pack() + altered.bytes_in_pack(); |
| 128 | 136 | assert_eq!( |
| 129 | − extract_delta_offset(actual_last.header), | |
| 137 | + extract_delta_offset(actual_third.header), | |
| 130 | 138 | altered.bytes_in_pack(), |
| 131 | 139 | "delta offset was adjusted to deal with change in size of predecessor(s)" |
| 132 | 140 | ); |
| 133 | 141 | assert_eq!( |
| 134 | − actual_last.pack_offset, last_entry.pack_offset, | |
| 142 | + actual_third.pack_offset, third_entry_pack_offset, | |
| 135 | 143 | "last entry is at the right position in the pack" |
| 136 | 144 | ); |
| 145 | + let actual_fourth = &actual[5]; | |
| 146 | + let fourth_entry_pack_offset = third_entry_pack_offset + actual_third.bytes_in_pack(); | |
| 147 | + assert_eq!( | |
| 148 | + actual_fourth.pack_offset, fourth_entry_pack_offset, | |
| 149 | + "the fourth entry was moved as well" | |
| 150 | + ); | |
| 151 | + assert_eq!( | |
| 152 | + extract_delta_offset(actual_fourth.header), | |
| 153 | + actual_third.bytes_in_pack(), | |
| 154 | + "the fourth header base distance was adjusted accordingly" | |
| 155 | + ); | |
| 156 | + | |
| 157 | + validate_pack_offsets(&actual); | |
| 137 | 158 | } |
| 138 | 159 | |
| 139 | 160 | #[test] |