Skip to content

Commit f83ee41

Browse files
derrickstoleedscho
authored andcommitted
index-pack: allow revisiting REF_DELTA chains
As detailed in the previous changes to t5309-pack-delta-cycles.sh, the logic within 'git index-pack' to analyze an incoming thin packfile with REF_DELTAs is suspect. The algorithm is overly cautious around delta cycles, and that leads in fact to failing even when there is no cycle. This change adjusts the algorithm to no longer fail in these cases. In fact, these cycle cases will no longer fail but more importantly the valid cases will no longer fail, either. The resulting packfile from the --fix-thin operation will not have cycles either since REF_DELTAs are forbidden from the on-disk format and OFS_DELTAs are impossible to write as a cycle. The crux of the matter is how the algorithm works when the REF_DELTAs point to base objects that exist in the local repository. When reading the thin packfile, the object IDs for the delta objects are unknown so we do not have the delta chain structure automatically. Instead, we need to start somewhere by selecting a delta whose base is inside our current object database. Consider the case where the packfile has two REF_DELTA objects, A and B, and the delta chain looks like "A depends on B" and "B depends on C" for some third object C, where C is already in the current repository. The algorithm _should_ start with all objects that depend on C, finding B, and then moving on to all objects depending on B, finding A. However, if the repository also already has object B, then the delta chain can be analyzed in a different order. The deltas with base B can be analyzed first, finding A, and then the deltas with base C are analyzed, finding B. The algorithm currently continues to look for objects that depend on B, finding A again. This fails due to A's 'real_type' member already being overwritten from OBJ_REF_DELTA to the correct object type. This scenario is possible in a typical 'git fetch' where the client does not advertise B as a 'have' but requests A as a 'want' (and C is noticed as a common object based on other 'have's). The reason this isn't typically seen is that most Git servers use OFS_DELTAs to represent deltas within a packfile. However, if a server uses only REF_DELTAs, then this kind of issue can occur. There is nothing in the explicit packfile format that states this use of inter-pack REF_DELTA is incorrect, only that REF_DELTAs should not be used in the on-disk representation to avoid cycles. This die() was introduced in ab791dd (index-pack: fix race condition with duplicate bases, 2014-08-29). Several refactors have adjusted the error message and the surrounding logic, but this issue has existed for a longer time as that was only a conversion from an assert(). The tests in t5309 originated in 3b910d0 (add tests for indexing packs with delta cycles, 2013-08-23) and b2ef3d9 (test index-pack on packs with recoverable delta cycles, 2013-08-23). These changes make note that the current behavior of handling "resolvable" cycles is mostly a documentation-only test, not that this behavior is the best way for Git to handle the situation. The fix here is somewhat complicated due to the amount of state being adjusted by the loop within threaded_second_pass(). Instead of trying to resume the start of the loop while adjusting the necessary context, I chose to scan the REF_DELTAs depending on the current 'parent' and skip any that have already been processed. This necessarily leaves us in a state where 'child' and 'child_obj' could be left as NULL and that must be handled later. There is also some careful handling around skipping REF_DELTAs when there are also OFS_DELTAs depending on that parent. There may be value in extending 'test-tool pack-deltas' to allow writing OFS_DELTAs in order to exercise this logic across the delta types. Signed-off-by: Derrick Stolee <[email protected]>
1 parent b823b7a commit f83ee41

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

builtin/index-pack.c

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,8 @@ static void *threaded_second_pass(void *data)
11071107
set_thread_data(data);
11081108
for (;;) {
11091109
struct base_data *parent = NULL;
1110-
struct object_entry *child_obj;
1111-
struct base_data *child;
1110+
struct object_entry *child_obj = NULL;
1111+
struct base_data *child = NULL;
11121112

11131113
counter_lock();
11141114
display_progress(progress, nr_resolved_deltas);
@@ -1135,15 +1135,18 @@ static void *threaded_second_pass(void *data)
11351135
parent = list_first_entry(&work_head, struct base_data,
11361136
list);
11371137

1138-
if (parent->ref_first <= parent->ref_last) {
1138+
while (parent->ref_first <= parent->ref_last) {
11391139
int offset = ref_deltas[parent->ref_first++].obj_no;
11401140
child_obj = objects + offset;
1141-
if (child_obj->real_type != OBJ_REF_DELTA)
1142-
die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
1143-
(uintmax_t) child_obj->idx.offset,
1144-
oid_to_hex(&parent->obj->idx.oid));
1141+
if (child_obj->real_type != OBJ_REF_DELTA) {
1142+
child_obj = NULL;
1143+
continue;
1144+
}
11451145
child_obj->real_type = parent->obj->real_type;
1146-
} else {
1146+
break;
1147+
}
1148+
1149+
if (!child_obj && parent->ofs_first <= parent->ofs_last) {
11471150
child_obj = objects +
11481151
ofs_deltas[parent->ofs_first++].obj_no;
11491152
assert(child_obj->real_type == OBJ_OFS_DELTA);
@@ -1176,29 +1179,32 @@ static void *threaded_second_pass(void *data)
11761179
}
11771180
work_unlock();
11781181

1179-
if (parent) {
1180-
child = resolve_delta(child_obj, parent);
1181-
if (!child->children_remaining)
1182-
FREE_AND_NULL(child->data);
1183-
} else {
1184-
child = make_base(child_obj, NULL);
1185-
if (child->children_remaining) {
1186-
/*
1187-
* Since this child has its own delta children,
1188-
* we will need this data in the future.
1189-
* Inflate now so that future iterations will
1190-
* have access to this object's data while
1191-
* outside the work mutex.
1192-
*/
1193-
child->data = get_data_from_pack(child_obj);
1194-
child->size = child_obj->size;
1182+
if (child_obj) {
1183+
if (parent) {
1184+
child = resolve_delta(child_obj, parent);
1185+
if (!child->children_remaining)
1186+
FREE_AND_NULL(child->data);
1187+
} else{
1188+
child = make_base(child_obj, NULL);
1189+
if (child->children_remaining) {
1190+
/*
1191+
* Since this child has its own delta children,
1192+
* we will need this data in the future.
1193+
* Inflate now so that future iterations will
1194+
* have access to this object's data while
1195+
* outside the work mutex.
1196+
*/
1197+
child->data = get_data_from_pack(child_obj);
1198+
child->size = child_obj->size;
1199+
}
11951200
}
11961201
}
11971202

11981203
work_lock();
11991204
if (parent)
12001205
parent->retain_data--;
1201-
if (child->data) {
1206+
1207+
if (child && child->data) {
12021208
/*
12031209
* This child has its own children, so add it to
12041210
* work_head.
@@ -1207,7 +1213,7 @@ static void *threaded_second_pass(void *data)
12071213
base_cache_used += child->size;
12081214
prune_base_data(NULL);
12091215
free_base_data(child);
1210-
} else {
1216+
} else if (child) {
12111217
/*
12121218
* This child does not have its own children. It may be
12131219
* the last descendant of its ancestors; free those

t/t5309-pack-delta-cycles.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ test_expect_success 'index-pack detects REF_DELTA cycles' '
6060
test_expect_success 'failover to an object in another pack' '
6161
clear_packs &&
6262
git index-pack --stdin <ab.pack &&
63-
test_must_fail git index-pack --stdin --fix-thin <cycle.pack
63+
64+
# This cycle does not fail since the existence of A & B in
65+
# the repo allows us to resolve the cycle.
66+
git index-pack --stdin --fix-thin <cycle.pack
6467
'
6568

6669
test_expect_success 'failover to a duplicate object in the same pack' '
@@ -72,10 +75,13 @@ test_expect_success 'failover to a duplicate object in the same pack' '
7275
pack_obj $A
7376
} >recoverable.pack &&
7477
pack_trailer recoverable.pack &&
75-
test_must_fail git index-pack --fix-thin --stdin <recoverable.pack
78+
79+
# This cycle does not fail since the existence of a full copy
80+
# of A in the pack allows us to resolve the cycle.
81+
git index-pack --fix-thin --stdin <recoverable.pack
7682
'
7783

78-
test_expect_failure 'index-pack works with thin pack A->B->C with B on disk' '
84+
test_expect_success 'index-pack works with thin pack A->B->C with B on disk' '
7985
git init server &&
8086
(
8187
cd server &&

0 commit comments

Comments
 (0)