History

[pack] one more todo down, it should work now, right?…

…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.

Sebastian Thiel committed 5 years ago

Commit 69a9ff11 parent 4bdebdd2 files · +53 −23Browse files

Unified · Split

2 files
  1. git-pack/src/data/input/lookup_ref_delta_objects.rs +18 −9
  2. git-pack/tests/pack/data/input.rs +35 −14
git-pack/src/data/input/lookup_ref_delta_objects.rs+18−9
@@ -41,7 +41,7 @@ where · 40 unmodified lines
4141 new_ofs.try_into().expect("offset value is never becomes negative")
4242 }
4343
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
4545 /// mean the object shrunk, usually because there header changed from ref-deltas to ofs deltas.
4646 fn track_change(
4747 &mut self,
@@ -121,11 +121,12 @@ where · 73 unmodified lines
121121 }
122122 Some(Ok(base_entry))
123123 }
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))
129130 }
130131 }
131132 }
@@ -160,9 +161,17 @@ where · 28 unmodified lines
160161 .expect("a base that is behind us in the pack");
161162 self.shift_entry_and_point_to_base_by_offset(&mut entry, new_distance);
162163 }
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);
166175 }
167176 }
168177 } else {
git-pack/tests/pack/data/input.rs+35−14
@@ -46,21 +46,28 @@ mod lookup_ref_delta_objects { · 45 unmodified lines
4646 entries
4747 }
4848
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+
4957 fn into_results_iter(entries: Vec<input::Entry>) -> impl Iterator<Item = Result<input::Entry, input::Error>> {
5058 entries.into_iter().map(Ok)
5159 }
5260
5361 #[test]
54− fn only_ref_deltas_are_handled() {
62+ fn only_ref_deltas_are_handled() -> crate::Result {
5563 let input = compute_offsets(vec![entry(base(), D_A), entry(delta_ofs(100), D_B)]);
5664 let expected = input.clone();
57− assert_eq!(
65+ let actual =
5866 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(())
6471 }
6572
6673 #[test]
@@ -72,8 +79,9 @@ mod lookup_ref_delta_objects { · 5 unmodified lines
7279 let inserted_data = D_D;
7380 let mut inserted = entry(base(), inserted_data);
7481 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]);
7785
7886 let mut calls = 0;
7987 let actual = LookupRefDeltaObjectsIter::new(into_results_iter(input), |_oid, buf| {
@@ -90,7 +98,7 @@ mod lookup_ref_delta_objects { · 10 unmodified lines
9098 .unwrap();
9199
92100 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");
94102
95103 assert_eq!(&actual[0], &inserted, "first object is inserted one");
96104
@@ -122,18 +130,31 @@ mod lookup_ref_delta_objects { · 25 unmodified lines
122130 "the pack offset was adjusted to accommodate for preceding objects"
123131 );
124132
125− let actual_last = &actual[4];
126− last_entry.pack_offset =
133+ let actual_third = &actual[4];
134+ let third_entry_pack_offset =
127135 inserted.bytes_in_pack() + first_altered_len + inserted.bytes_in_pack() + altered.bytes_in_pack();
128136 assert_eq!(
129− extract_delta_offset(actual_last.header),
137+ extract_delta_offset(actual_third.header),
130138 altered.bytes_in_pack(),
131139 "delta offset was adjusted to deal with change in size of predecessor(s)"
132140 );
133141 assert_eq!(
134− actual_last.pack_offset, last_entry.pack_offset,
142+ actual_third.pack_offset, third_entry_pack_offset,
135143 "last entry is at the right position in the pack"
136144 );
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);
137158 }
138159
139160 #[test]

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