Skip to content

Commit 98f8854

Browse files
derrickstoleegitster
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd7fd7a commit 98f8854

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
@@ -1109,8 +1109,8 @@ static void *threaded_second_pass(void *data)
11091109
set_thread_data(data);
11101110
for (;;) {
11111111
struct base_data *parent = NULL;
1112-
struct object_entry *child_obj;
1113-
struct base_data *child;
1112+
struct object_entry *child_obj = NULL;
1113+
struct base_data *child = NULL;
11141114

11151115
counter_lock();
11161116
display_progress(progress, nr_resolved_deltas);
@@ -1137,15 +1137,18 @@ static void *threaded_second_pass(void *data)
11371137
parent = list_first_entry(&work_head, struct base_data,
11381138
list);
11391139

1140-
if (parent->ref_first <= parent->ref_last) {
1140+
while (parent->ref_first <= parent->ref_last) {
11411141
int offset = ref_deltas[parent->ref_first++].obj_no;
11421142
child_obj = objects + offset;
1143-
if (child_obj->real_type != OBJ_REF_DELTA)
1144-
die("REF_DELTA at offset %"PRIuMAX" already resolved (duplicate base %s?)",
1145-
(uintmax_t) child_obj->idx.offset,
1146-
oid_to_hex(&parent->obj->idx.oid));
1143+
if (child_obj->real_type != OBJ_REF_DELTA) {
1144+
child_obj = NULL;
1145+
continue;
1146+
}
11471147
child_obj->real_type = parent->obj->real_type;
1148-
} else {
1148+
break;
1149+
}
1150+
1151+
if (!child_obj && parent->ofs_first <= parent->ofs_last) {
11491152
child_obj = objects +
11501153
ofs_deltas[parent->ofs_first++].obj_no;
11511154
assert(child_obj->real_type == OBJ_OFS_DELTA);
@@ -1178,29 +1181,32 @@ static void *threaded_second_pass(void *data)
11781181
}
11791182
work_unlock();
11801183

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

12001205
work_lock();
12011206
if (parent)
12021207
parent->retain_data--;
1203-
if (child->data) {
1208+
1209+
if (child && child->data) {
12041210
/*
12051211
* This child has its own children, so add it to
12061212
* work_head.
@@ -1209,7 +1215,7 @@ static void *threaded_second_pass(void *data)
12091215
base_cache_used += child->size;
12101216
prune_base_data(NULL);
12111217
free_base_data(child);
1212-
} else {
1218+
} else if (child) {
12131219
/*
12141220
* This child does not have its own children. It may be
12151221
* 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)