Commit 668a20f6fe41d7f9c2f4c32b1ee521bff6d459a8

Stefan Sperling 2020-03-18T16:13:41

rewritten got-index-pack; sorry about the monster commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
diff --git a/got/got.c b/got/got.c
index 9fce345..5559abf 100644
--- a/got/got.c
+++ b/got/got.c
@@ -971,7 +971,7 @@ done:
 
 static const struct got_error *
 fetch_progress(void *arg, const char *message, off_t packfile_size,
-    int nobjects_total, int nobjects_indexed)
+    int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
 {
 	int *did_something = arg;
 	char scaled[FMT_SCALED_STRSIZE];
@@ -979,13 +979,16 @@ fetch_progress(void *arg, const char *message, off_t packfile_size,
 	if (message) {
 		printf("\rserver: %s", message);
 		*did_something = 1;
-	} else if (packfile_size > 0 || nobjects_indexed > 0) {
-		printf("\rfetching...");
+	} else if (packfile_size > 0 || nobj_indexed > 0) {
+		printf("\r");
 		if (fmt_scaled(packfile_size, scaled) == 0)
-			printf(" %*s", FMT_SCALED_STRSIZE, scaled);
-		if (nobjects_indexed > 0)
-			printf(" indexed %d/%d objects", nobjects_indexed,
-			    nobjects_total);
+			printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
+		if (nobj_indexed > 0)
+			printf("; indexed %d/%d objects", nobj_indexed,
+			    nobj_total);
+		if (nobj_resolved > 0)
+			printf("; resolved %d/%d deltified objects ",
+			    nobj_resolved, nobj_total - nobj_loose);
 		*did_something = 1;
 	}
 	fflush(stdout);
@@ -1079,18 +1082,18 @@ cmd_clone(int argc, char *argv[])
 		struct got_object_id *id = pe->data;
 		struct got_reference *ref;
 
-		err = got_object_id_str(&id_str, id);
-		if (err)
-			goto done;
 
 		err = got_ref_alloc(&ref, refname, id);
-		if (err) {
-			free(id_str);
+		if (err)
 			goto done;
-		}
 
+		#if 0
+		err = got_object_id_str(&id_str, id);
+		if (err)
+			goto done;
 		printf("%s: %s\n", got_ref_get_name(ref), id_str);
 		free(id_str);
+		#endif
 		err = got_ref_write(ref, repo);
 		got_ref_close(ref);
 		if (err)
diff --git a/include/got_fetch.h b/include/got_fetch.h
index 5c84e0c..f81637b 100644
--- a/include/got_fetch.h
+++ b/include/got_fetch.h
@@ -40,7 +40,7 @@ const struct got_error *got_fetch_connect(int *, const char *, const char *,
 
 /* A callback function which gets invoked with progress information to print. */
 typedef const struct got_error *(*got_fetch_progress_cb)(void *,
-    const char *, off_t, int, int);
+    const char *, off_t, int, int, int, int);
 
 /*
  * Attempt to fetch a packfile from a server. This pack file will contain
diff --git a/lib/fetch.c b/lib/fetch.c
index 734df05..19368f1 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -451,7 +451,8 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 			while ((s = strsep(&s0, "\r")) != NULL) {
 				if (*s == '\0')
 					continue;
-				err = progress_cb(progress_arg, s, 0, 0, 0);
+				err = progress_cb(progress_arg, s,
+				    packfile_size_cur, 0, 0, 0, 0);
 				if (err)
 					break;
 			}
@@ -460,7 +461,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 				goto done;
 		} else if (packfile_size_cur != packfile_size) {
 			err = progress_cb(progress_arg, NULL,
-			    packfile_size_cur, 0, 0);
+			    packfile_size_cur, 0, 0, 0, 0);
 			if (err)
 				break;
 			packfile_size = packfile_size_cur;
@@ -496,7 +497,8 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 	}
 	imsg_init(&idxibuf, imsg_idxfds[0]);
 
-	err = got_privsep_send_index_pack_req(&idxibuf, npackfd, *pack_hash);
+	err = got_privsep_send_index_pack_req(&idxibuf, (*pack_hash)->sha1,
+	    npackfd);
 	if (err != NULL)
 		goto done;
 	npackfd = -1;
@@ -506,15 +508,17 @@ got_fetch_pack(struct got_object_id **pack_hash, struct got_pathlist_head *refs,
 	nidxfd = -1;
 	done = 0;
 	while (!done) {
-		int nobjects_total, nobjects_indexed;
-		err = got_privsep_recv_index_progress(&done, &nobjects_total,
-		    &nobjects_indexed, &idxibuf);
+		int nobj_total, nobj_indexed, nobj_loose, nobj_resolved;
+
+		err = got_privsep_recv_index_progress(&done, &nobj_total,
+		    &nobj_indexed, &nobj_loose, &nobj_resolved,
+		    &idxibuf);
 		if (err != NULL)
 			goto done;
-		if (nobjects_indexed != 0) {
+		if (nobj_indexed != 0) {
 			err = progress_cb(progress_arg, NULL,
-			    packfile_size, nobjects_total,
-			    nobjects_indexed);
+			    packfile_size, nobj_total,
+			    nobj_indexed, nobj_loose, nobj_resolved);
 			if (err)
 				break;
 		}
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index 363157f..15e9a29 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -27,6 +27,14 @@ struct got_pack {
 const struct got_error *got_pack_stop_privsep_child(struct got_pack *);
 const struct got_error *got_pack_close(struct got_pack *);
 
+const struct got_error *got_pack_parse_offset_delta(off_t *, size_t *,
+    struct got_pack *, off_t, int);
+const struct got_error *got_pack_resolve_delta_chain(struct got_delta_chain *,
+    struct got_packidx *, struct got_pack *, off_t, size_t, int, size_t,
+    unsigned int);
+const struct got_error *got_pack_parse_object_type_and_size(uint8_t *,
+    uint64_t *, size_t *, struct got_pack *, off_t);
+
 #define GOT_PACK_PREFIX		"pack-"
 #define GOT_PACKFILE_SUFFIX	".pack"
 #define GOT_PACKIDX_SUFFIX		".idx"
@@ -171,6 +179,8 @@ const struct got_error *got_packfile_open_object(struct got_object **,
     struct got_pack *, struct got_packidx *, int, struct got_object_id *);
 const struct got_error *got_pack_get_max_delta_object_size(uint64_t *,
     struct got_object *, struct got_pack *);
+const struct got_error *got_pack_dump_delta_chain_to_mem(uint8_t **, size_t *,
+    struct got_delta_chain *, struct got_pack *);
 const struct got_error *got_packfile_extract_object(struct got_pack *,
     struct got_object *, FILE *, FILE *, FILE *);
 const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 20e7744..b949a92 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -277,12 +277,24 @@ struct got_imsg_fetch_download_progress {
 	off_t packfile_bytes;
 };
 
+/* Structure for GOT_IMSG_IDXPACK_REQUEST data. */
+struct got_imsg_index_pack_request {
+	uint8_t pack_hash[SHA1_DIGEST_LENGTH];
+} __attribute__((__packed__));
+
 /* Structure for GOT_IMSG_IDXPACK_PROGRESS data. */
 struct got_imsg_index_pack_progress {
 	/* Total number of objects in pack file. */
-	int nobjects_total;
+	int nobj_total;
+
 	/* Number of objects indexed so far. */
-	int nobjects_indexed;
+	int nobj_indexed;
+
+	/* Number of non-deltified objects in pack file. */
+	int nobj_loose;
+
+	/* Number of deltified objects resolved so far. */
+	int nobj_resolved;
 };
 
 /* Structure for GOT_IMSG_PACKIDX. */
@@ -363,13 +375,13 @@ const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
 const struct got_error *got_privsep_send_tmpfd(struct imsgbuf *, int);
 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
     struct got_object *);
-const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *, int,
-    struct got_object_id *);
+const struct got_error *got_privsep_send_index_pack_req(struct imsgbuf *,
+    uint8_t *, int);
 const struct got_error *got_privsep_send_index_pack_progress(struct imsgbuf *,
-    int, int);
+    int, int, int, int);
 const struct got_error *got_privsep_send_index_pack_done(struct imsgbuf *);
 const struct got_error *got_privsep_recv_index_progress(int *, int *, int *,
-    struct imsgbuf *ibuf);
+    int *, int *, struct imsgbuf *ibuf);
 const struct got_error *got_privsep_send_fetch_req(struct imsgbuf *, int,
     struct got_pathlist_head *);
 const struct got_error *got_privsep_send_fetch_symrefs(struct imsgbuf *,
diff --git a/lib/pack.c b/lib/pack.c
index c2147b8..7f264cd 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -561,8 +561,8 @@ got_pack_close(struct got_pack *pack)
 	return err;
 }
 
-static const struct got_error *
-parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
+const struct got_error *
+got_pack_parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
     struct got_pack *pack, off_t offset)
 {
 	uint8_t t = 0;
@@ -681,8 +681,8 @@ parse_negative_offset(int64_t *offset, size_t *len, struct got_pack *pack,
 	return NULL;
 }
 
-static const struct got_error *
-parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
+const struct got_error *
+got_pack_parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
     off_t offset, int tslen)
 {
 	const struct got_error *err;
@@ -706,10 +706,6 @@ parse_offset_delta(off_t *base_offset, size_t *len, struct got_pack *pack,
 }
 
 static const struct got_error *
-resolve_delta_chain(struct got_delta_chain *, struct got_packidx *,
-    struct got_pack *, off_t, size_t, int, size_t, unsigned int);
-
-static const struct got_error *
 read_delta_data(uint8_t **delta_buf, size_t *delta_len,
     size_t delta_data_offset, struct got_pack *pack)
 {
@@ -759,7 +755,7 @@ resolve_offset_delta(struct got_delta_chain *deltas,
 	off_t delta_data_offset;
 	size_t consumed;
 
-	err = parse_offset_delta(&base_offset, &consumed, pack,
+	err = got_pack_parse_offset_delta(&base_offset, &consumed, pack,
 	    delta_offset, tslen);
 	if (err)
 		return err;
@@ -783,12 +779,12 @@ resolve_offset_delta(struct got_delta_chain *deltas,
 	if (base_offset >= pack->filesize)
 		return got_error(GOT_ERR_PACK_OFFSET);
 
-	err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
-	    pack, base_offset);
+	err = got_pack_parse_object_type_and_size(&base_type, &base_size,
+	    &base_tslen, pack, base_offset);
 	if (err)
 		return err;
 
-	return resolve_delta_chain(deltas, packidx, pack, base_offset,
+	return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
 	    base_tslen, base_type, base_size, recursion - 1);
 }
 
@@ -824,10 +820,6 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
 		delta_data_offset = lseek(pack->fd, 0, SEEK_CUR);
 		if (delta_data_offset == -1)
 			return got_error_from_errno("lseek");
-		err = got_inflate_to_mem_fd(&delta_buf, &delta_len, NULL,
-		    pack->fd);
-		if (err)
-			return err;
 	}
 
 	err = add_delta(deltas, delta_offset, tslen, delta_type, delta_size,
@@ -838,7 +830,7 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
 	/* Delta base must be in the same pack file. */
 	idx = got_packidx_get_object_idx(packidx, &id);
 	if (idx == -1)
-		return got_error(GOT_ERR_BAD_PACKFILE);
+		return got_error(GOT_ERR_NO_OBJ);
 
 	base_offset = get_object_offset(packidx, idx);
 	if (base_offset == (uint64_t)-1)
@@ -847,19 +839,19 @@ resolve_ref_delta(struct got_delta_chain *deltas, struct got_packidx *packidx,
 	if (base_offset >= pack->filesize)
 		return got_error(GOT_ERR_PACK_OFFSET);
 
-	err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
-	    pack, base_offset);
+	err = got_pack_parse_object_type_and_size(&base_type, &base_size,
+	    &base_tslen, pack, base_offset);
 	if (err)
 		return err;
 
-	return resolve_delta_chain(deltas, packidx, pack, base_offset,
+	return got_pack_resolve_delta_chain(deltas, packidx, pack, base_offset,
 	    base_tslen, base_type, base_size, recursion - 1);
 }
 
-static const struct got_error *
-resolve_delta_chain(struct got_delta_chain *deltas, struct got_packidx *packidx,
-    struct got_pack *pack, off_t delta_offset, size_t tslen, int delta_type,
-    size_t delta_size, unsigned int recursion)
+const struct got_error *
+got_pack_resolve_delta_chain(struct got_delta_chain *deltas,
+    struct got_packidx *packidx, struct got_pack *pack, off_t delta_offset,
+    size_t tslen, int delta_type, size_t delta_size, unsigned int recursion)
 {
 	const struct got_error *err = NULL;
 
@@ -913,8 +905,9 @@ open_delta_object(struct got_object **obj, struct got_packidx *packidx,
 	(*obj)->flags |= GOT_OBJ_FLAG_PACKED;
 	(*obj)->pack_idx = idx;
 
-	err = resolve_delta_chain(&(*obj)->deltas, packidx, pack, offset,
-	    tslen, delta_type, delta_size, GOT_DELTA_CHAIN_RECURSION_MAX);
+	err = got_pack_resolve_delta_chain(&(*obj)->deltas, packidx, pack,
+	    offset, tslen, delta_type, delta_size,
+	    GOT_DELTA_CHAIN_RECURSION_MAX);
 	if (err)
 		goto done;
 
@@ -946,7 +939,8 @@ got_packfile_open_object(struct got_object **obj, struct got_pack *pack,
 	if (offset == (uint64_t)-1)
 		return got_error(GOT_ERR_BAD_PACKIDX);
 
-	err = parse_object_type_and_size(&type, &size, &tslen, pack, offset);
+	err = got_pack_parse_object_type_and_size(&type, &size, &tslen,
+	    pack, offset);
 	if (err)
 		return err;
 
@@ -1034,7 +1028,7 @@ got_pack_get_max_delta_object_size(uint64_t *size, struct got_object *obj,
 	return get_delta_chain_max_size(size, &obj->deltas, pack);
 }
 
-static const struct got_error *
+const struct got_error *
 dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
     struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
 {
@@ -1198,8 +1192,8 @@ done:
 	return err;
 }
 
-static const struct got_error *
-dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
+const struct got_error *
+got_pack_dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
     struct got_delta_chain *deltas, struct got_pack *pack)
 {
 	const struct got_error *err = NULL;
@@ -1377,7 +1371,8 @@ got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
 			err = got_inflate_to_mem_fd(buf, len, NULL, pack->fd);
 		}
 	} else
-		err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack);
+		err = got_pack_dump_delta_chain_to_mem(buf, len, &obj->deltas,
+		    pack);
 
 	return err;
 }
diff --git a/lib/privsep.c b/lib/privsep.c
index 986fccc..b42b1cd 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -767,12 +767,14 @@ done:
 }
 
 const struct got_error *
-got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_id *hash)
+got_privsep_send_index_pack_req(struct imsgbuf *ibuf, uint8_t *pack_hash,
+    int fd)
 {
 	const struct got_error *err = NULL;
 
+	/* Keep in sync with struct got_imsg_index_pack_request */
 	if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_REQUEST, 0, 0, fd,
-	    hash->sha1, SHA1_DIGEST_LENGTH) == -1) {
+	    pack_hash, SHA1_DIGEST_LENGTH) == -1) {
 		err = got_error_from_errno("imsg_compose INDEX_REQUEST");
 		close(fd);
 		return err;
@@ -781,13 +783,15 @@ got_privsep_send_index_pack_req(struct imsgbuf *ibuf, int fd, struct got_object_
 }
 
 const struct got_error *
-got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobjects_total,
-    int nobjects_indexed)
+got_privsep_send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
+    int nobj_indexed, int nobj_loose, int nobj_resolved)
 {
 	struct got_imsg_index_pack_progress iprogress;
 
-	iprogress.nobjects_total = nobjects_total;
-	iprogress.nobjects_indexed = nobjects_indexed;
+	iprogress.nobj_total = nobj_total;
+	iprogress.nobj_indexed = nobj_indexed;
+	iprogress.nobj_loose = nobj_loose;
+	iprogress.nobj_resolved = nobj_resolved;
 
 	if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
 	    &iprogress, sizeof(iprogress)) == -1)
@@ -805,8 +809,9 @@ got_privsep_send_index_pack_done(struct imsgbuf *ibuf)
 }
 
 const struct got_error *
-got_privsep_recv_index_progress(int *done, int *nobjects_total,
-    int *nobjects_indexed, struct imsgbuf *ibuf)
+got_privsep_recv_index_progress(int *done, int *nobj_total,
+    int *nobj_indexed, int *nobj_loose, int *nobj_resolved,
+    struct imsgbuf *ibuf)
 {
 	const struct got_error *err = NULL;
 	struct imsg imsg;
@@ -814,8 +819,9 @@ got_privsep_recv_index_progress(int *done, int *nobjects_total,
 	size_t datalen;
 
 	*done = 0;
-	*nobjects_total = 0;
-	*nobjects_indexed = 0;
+	*nobj_total = 0;
+	*nobj_indexed = 0;
+	*nobj_resolved = 0;
 
 	err = got_privsep_recv_imsg(&imsg, ibuf, 0);
 	if (err)
@@ -836,8 +842,10 @@ got_privsep_recv_index_progress(int *done, int *nobjects_total,
 			break;
 		}
 		iprogress = (struct got_imsg_index_pack_progress *)imsg.data;
-		*nobjects_total = iprogress->nobjects_total;
-		*nobjects_indexed = iprogress->nobjects_indexed;
+		*nobj_total = iprogress->nobj_total;
+		*nobj_indexed = iprogress->nobj_indexed;
+		*nobj_loose = iprogress->nobj_loose;
+		*nobj_resolved = iprogress->nobj_resolved;
 		break;
 	case GOT_IMSG_IDXPACK_DONE:
 		if (datalen != 0) {
diff --git a/libexec/got-index-pack/Makefile b/libexec/got-index-pack/Makefile
index fcaafeb..483b835 100644
--- a/libexec/got-index-pack/Makefile
+++ b/libexec/got-index-pack/Makefile
@@ -4,7 +4,7 @@
 
 PROG=		got-index-pack
 SRCS=		got-index-pack.c error.c inflate.c object_parse.c object_idset.c \
-		path.c privsep.c sha1.c
+		delta_cache.c delta.c pack.c path.c privsep.c sha1.c
 
 CPPFLAGS = -I${.CURDIR}/../../include -I${.CURDIR}/../../lib
 LDADD = -lutil -lz
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index 4247c3e..a520e6a 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
+ * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
  *
  * Permission to use, copy, modify, and distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -47,145 +48,40 @@
 #include "got_lib_object_parse.h"
 #include "got_lib_object_idset.h"
 #include "got_lib_privsep.h"
+#include "got_lib_pack.h"
+#include "got_lib_delta_cache.h"
 
-typedef struct Cinfo	Cinfo;
-typedef struct Tinfo	Tinfo;
-typedef struct Object	Object;
-typedef struct Pack	Pack;
-typedef struct Buf	Buf;
-typedef struct Dirent	Dirent;
-typedef struct Idxent	Idxent;
-typedef struct Ols	Ols;
-
-enum {
-	/* 5k objects should be enough */
-	Cachemax	= 5*1024,
-	Pathmax		= 512,
-	Hashsz		= 20,
-	Pktmax		= 65536,
-
-	Nproto	= 16,
-	Nport	= 16,
-	Nhost	= 256,
-	Npath	= 128,
-	Nrepo	= 64,
-	Nbranch	= 32,
-};
+struct got_indexed_object {
+	struct got_object_id id;
 
-typedef enum Type {
-	GNone	= 0,
-	GCommit	= 1,
-	GTree	= 2,
-	GBlob	= 3,
-	GTag	= 4,
-	GOdelta	= 6,
-	GRdelta	= 7,
-} Type;
-
-enum {
-	Cloaded	= 1 << 0,
-	Cidx	= 1 << 1,
-	Ccache	= 1 << 2,
-	Cexist	= 1 << 3,
-	Cparsed	= 1 << 5,
-};
+	/*
+	 * Has this object been fully resolved?
+	 * If so, we know its ID, otherwise we don't and 'id' is invalid.
+	 */
+	int valid;
 
-struct Dirent {
-	char *name;
-	int modref;
-	int mode;
-	struct got_object_id h;
-};
+	/* Offset of type+size field for this object in pack file. */
+	off_t off;
 
-struct Object {
-	/* Git data */
-	struct got_object_id	hash;
-	Type	type;
-
-	/* Cache */
-	int	id;
-	int	flag;
-	int	refs;
-	Object	*next;
-	Object	*prev;
-
-	/* For indexing */
-	off_t	off;
-	off_t	len;
-	uint32_t	crc;
-
-	/* Everything below here gets cleared */
-	char	*all;
-	char	*data;
-	/* size excludes header */
-	off_t	size;
-
-	union {
-		Cinfo *commit;
-		Tinfo *tree;
-	};
-};
+	/* Type+size values parsed from pack file. */
+	uint8_t type;
+	uint64_t size;
 
-struct Tinfo {
-	/* Tree */
-	Dirent	*ent;
-	int	nent;
-};
+	/* Length of on-disk type+size data. */
+	size_t tslen; 
 
-struct Cinfo {
-	/* Commit */
-	struct got_object_id	*parent;
-	int	nparent;
-	struct got_object_id	tree;
-	char	*author;
-	char	*committer;
-	char	*msg;
-	int	nmsg;
-	off_t	ctime;
-	off_t	mtime;
-};
+	/* Length of object data following type+size. */
+	size_t len; 
 
-typedef struct Buf Buf;
+	uint32_t crc;
 
-struct Buf {
-	int len;
-	int sz;
-	char *data;
-};
+	/* For ref deltas. */
+	struct got_object_id ref_id;
 
-static int	readpacked(FILE *, Object *, int);
-static Object	*readidxobject(FILE *, struct got_object_id, int);
-
-struct got_object_idset *objcache;
-int	next_object_id;
-Object *lruhead;
-Object *lrutail;
-int	ncache;
-
-#define GETBE16(b)\
-		((((b)[0] & 0xFFul) <<  8) | \
-		 (((b)[1] & 0xFFul) <<  0))
-
-#define GETBE32(b)\
-		((((b)[0] & 0xFFul) << 24) | \
-		 (((b)[1] & 0xFFul) << 16) | \
-		 (((b)[2] & 0xFFul) <<  8) | \
-		 (((b)[3] & 0xFFul) <<  0))
-#define GETBE64(b)\
-		((((b)[0] & 0xFFull) << 56) | \
-		 (((b)[1] & 0xFFull) << 48) | \
-		 (((b)[2] & 0xFFull) << 40) | \
-		 (((b)[3] & 0xFFull) << 32) | \
-		 (((b)[4] & 0xFFull) << 24) | \
-		 (((b)[5] & 0xFFull) << 16) | \
-		 (((b)[6] & 0xFFull) <<  8) | \
-		 (((b)[7] & 0xFFull) <<  0))
-
-#define PUTBE16(b, n)\
-	do{ \
-		(b)[0] = (n) >> 8; \
-		(b)[1] = (n) >> 0; \
-	} while(0)
+	/* For offset deltas. */
+	off_t base_offset;
+	size_t base_offsetlen;
+};
 
 #define PUTBE32(b, n)\
 	do{ \
@@ -207,1039 +103,629 @@ int	ncache;
 		(b)[7] = (n) >> 0; \
 	} while(0)
 
-static int
-charval(int c, int *err)
+static const struct got_error *
+get_obj_type_label(const char **label, int obj_type)
 {
-	if(c >= '0' && c <= '9')
-		return c - '0';
-	if(c >= 'a' && c <= 'f')
-		return c - 'a' + 10;
-	if(c >= 'A' && c <= 'F')
-		return c - 'A' + 10;
-	*err = 1;
-	return -1;
-}
+	const struct got_error *err = NULL;
 
-static int
-hparse(struct got_object_id *h, char *b)
-{
-	int i, err;
-
-	err = 0;
-	for(i = 0; i < sizeof(h->sha1); i++){
-		err = 0;
-		h->sha1[i] = 0;
-		h->sha1[i] |= ((charval(b[2*i], &err) & 0xf) << 4);
-		h->sha1[i] |= ((charval(b[2*i+1], &err)& 0xf) << 0);
-		if(err)
-			return -1;
+	switch (obj_type) {
+	case GOT_OBJ_TYPE_BLOB:
+		*label = GOT_OBJ_LABEL_BLOB;
+		break;
+	case GOT_OBJ_TYPE_TREE:
+		*label = GOT_OBJ_LABEL_TREE;
+		break;
+	case GOT_OBJ_TYPE_COMMIT:
+		*label = GOT_OBJ_LABEL_COMMIT;
+		break;
+	case GOT_OBJ_TYPE_TAG:
+		*label = GOT_OBJ_LABEL_TAG;
+		break;
+	default:
+		*label = NULL;
+		err = got_error(GOT_ERR_OBJ_TYPE);
+		break;
 	}
-	return 0;
-}
-
-static void *
-emalloc(size_t n)
-{
-	void *v;
-
-	v = calloc(n, 1);
-	if(v == NULL)
-		err(1, "malloc:");
-	return v;
-}
-
-static void *
-erealloc(void *p, ulong n)
-{
-	void *v;
-
-	v = realloc(p, n);
-	if(v == NULL)
-		err(1, "realloc:");
-	memset(v, 0, n);
-	return v;
-}
-
-static int
-hasheq(struct got_object_id *a, struct got_object_id *b)
-{
-	return memcmp(a->sha1, b->sha1, sizeof(a->sha1)) == 0;
-}
 
-static char *
-typestr(int t)
-{
-	char *types[] = {
-		"???",
-		"commit",
-		"tree",
-		"blob",
-		"tag",
-		"odelta",
-		"rdelta",
-	};
-	if (t < 0 || t >= sizeof(types)/sizeof(types[0]))
-		abort();
-	return types[t];
+	return err;
 }
 
-static char *
-hashfmt(char *out, size_t nout, struct got_object_id *h)
-{
-	int i, n, c0, c1;
-	char *p;
-
-	if (nout < 2*sizeof(h->sha1) + 1)
-		return NULL;
-	p = out;
-	for(i = 0; i < sizeof(h->sha1); i++){
-		n = (h->sha1[i] >> 4) & 0xf;
-		c0 = (n >= 10) ? n-10 + 'a' : n + '0';
-		n = h->sha1[i] & 0xf;
-		c1 = (n >= 10) ? n-10 + 'a' : n + '0';
-		*p++ = c0;
-		*p++ = c1;
-	}
-	*p++ = 0;
-	return out;
-}
 
-static void
-clear(Object *o)
+static const struct got_error *
+read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
 {
-	if(!o)
-		return;
-
-	assert(o->refs == 0);
-	assert((o->flag & Ccache) == 0);
-	assert(o->flag & Cloaded);
-	switch(o->type){
-	case GCommit:
-		if(!o->commit)
+	const struct got_error *err = NULL;
+	SHA1_CTX ctx;
+	uint8_t *data;
+	size_t datalen;
+	ssize_t n;
+	char *header;
+	size_t headerlen;
+	const char *obj_label;
+
+	err = got_pack_parse_object_type_and_size(&obj->type, &obj->size, &obj->tslen,
+	    pack, obj->off);
+	if (err)
+		return err;
+
+	switch (obj->type) {
+	case GOT_OBJ_TYPE_BLOB:
+	case GOT_OBJ_TYPE_COMMIT:
+	case GOT_OBJ_TYPE_TREE:
+	case GOT_OBJ_TYPE_TAG:
+		/* XXX TODO reading large objects into memory is bad! */
+		err = got_inflate_to_mem_fd(&data, &datalen, &obj->len, pack->fd);
+		if (err)
+			break;
+		SHA1Init(&ctx);
+		err = get_obj_type_label(&obj_label, obj->type);
+		if (err)
+			break;
+		if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
+			err = got_error_from_errno("asprintf");
+			free(data);
+			break;
+		}
+		headerlen = strlen(header) + 1;
+		SHA1Update(&ctx, header, headerlen);
+		SHA1Update(&ctx, data, datalen);
+		SHA1Final(obj->id.sha1, &ctx);
+		free(header);
+		free(data);
+		break;
+	case GOT_OBJ_TYPE_REF_DELTA:
+		memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
+		n = read(pack->fd, &obj->ref_id.sha1, SHA1_DIGEST_LENGTH);
+		if (n == -1) {
+			err = got_error_from_errno("read");
+			break;
+		}
+		if (n < sizeof(obj->id)) {
+			err = got_error(GOT_ERR_BAD_PACKFILE);
 			break;
-		free(o->commit->parent);
-		free(o->commit->author);
-		free(o->commit->committer);
-		free(o->commit);
-		o->commit = NULL;
+		}
+		err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
+		if (err)
+			break;
+		obj->len += SHA1_DIGEST_LENGTH;
 		break;
-	case GTree:
-		if(!o->tree)
+	case GOT_OBJ_TYPE_OFFSET_DELTA:
+		memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
+		err = got_pack_parse_offset_delta(&obj->base_offset,
+		    &obj->base_offsetlen, pack, obj->off, obj->tslen);
+		if (err)
 			break;
-		free(o->tree->ent);
-		free(o->tree);
-		o->tree = NULL;
+		err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
+		if (err)
+			break;
+		obj->len += obj->base_offsetlen;
 		break;
 	default:
+		err = got_error(GOT_ERR_OBJ_TYPE);
 		break;
 	}
 
-	free(o->all);
-	o->all = NULL;
-	o->data = NULL;
-	o->flag &= ~Cloaded;
+	return err;
 }
 
-static void
-unref(Object *o)
+static const struct got_error *
+hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
 {
-	if(!o)
-		return;
-	o->refs--;
-	if(!o->refs)
-		clear(o);
-}
+	ssize_t w;
 
-static Object*
-ref(Object *o)
-{
-	o->refs++;
-	return o;
-}
+	SHA1Update(ctx, buf, len);
 
-static void
-cache(Object *o)
-{
-	char buf[41];
-	Object *p;
-
-	hashfmt(buf, sizeof(buf), &o->hash);
-	if(o == lruhead)
-		return;
-	if(o == lrutail)
-		lrutail = lrutail->prev;
-	if(!(o->flag & Cexist)){
-		got_object_idset_add(objcache, &o->hash, o);
-		o->id = next_object_id++;
-		o->flag |= Cexist;
-	}
-	if(o->prev)
-		o->prev->next = o->next;
-	if(o->next)
-		o->next->prev = o->prev;
-	if(lrutail == o){
-		lrutail = o->prev;
-		lrutail->next = NULL;
-	}else if(!lrutail)
-		lrutail = o;
-	if(lruhead)
-		lruhead->prev = o;
-	o->next = lruhead;
-	o->prev = NULL;
-	lruhead = o;
-
-	if(!(o->flag & Ccache)){
-		o->flag |= Ccache;
-		ref(o);
-		ncache++;
-	}
-	while(ncache > Cachemax){
-		p = lrutail;
-		lrutail = p->prev;
-		lrutail->next = NULL;
-		p->flag &= ~Ccache;
-		p->prev = NULL;
-		p->next = NULL;
-		unref(p);
-		ncache--;
-	}
+	w = write(fd, buf, len);
+	if (w == -1)
+		return got_error_from_errno("write");
+	if (w != len)
+		return got_error(GOT_ERR_IO);
+
+	return NULL;
 }
 
-static int
-preadbe32(FILE *b, int *v, off_t off)
+static const struct got_error *
+object_crc(int packfd, struct got_indexed_object *obj)
 {
-	char buf[4];
+	char buf[8096];
+	size_t n;
+	ssize_t r;
 
-	if(fseek(b, off, SEEK_SET) == -1)
-		return -1;
-	if(fread(buf, 1, sizeof(buf), b) == -1)
-		return -1;
-	*v = GETBE32(buf);
+	obj->crc = 0;
+	if (lseek(packfd, obj->off + obj->tslen, SEEK_SET) == -1)
+		return got_error_from_errno("lseek");
 
+	obj->crc = crc32(0L, NULL, 0);
+	for (n = obj->len; n > 0; n -= r){
+		r = read(packfd, buf, n > sizeof(buf) ? sizeof(buf) : n);
+		if (r == -1)
+			return got_error_from_errno("read");
+		if (r == 0)
+			return NULL;
+		obj->crc = crc32(obj->crc, buf, r);
+	}
 	return 0;
 }
+
+#if 0
 static int
-preadbe64(FILE *b, off_t *v, off_t off)
+indexed_obj_cmp(const void *pa, const void *pb)
 {
-	char buf[8];
+	struct got_indexed_object *a, *b;
 
-	if(fseek(b, off, SEEK_SET) == -1)
-		return -1;
-	if(fread(buf, 1, sizeof(buf), b) == -1)
-		return -1;
-	*v = GETBE64(buf);
-	return 0;
+	a = *(struct got_indexed_object **)pa;
+	b = *(struct got_indexed_object **)pb;
+	return got_object_id_cmp(&a->id, &b->id);
 }
+#endif
 
-static int
-readvint(char *p, char **pp)
+static const struct got_error *
+resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
+    struct got_indexed_object *obj)
 {
-	int i, n, c;
-
-	i = 0;
-	n = 0;
-	do {
-		c = *p++;
-		n |= (c & 0x7f) << i;
-		i += 7;
-	} while (c & 0x80);
-	*pp = p;
-
-	return n;
-}
+	const struct got_error *err = NULL;
+	struct got_delta_chain deltas;
+	struct got_delta *delta;
+	uint8_t *buf = NULL;
+	size_t len;
+	SHA1_CTX ctx;
+	char *header;
+	size_t headerlen;
+	int base_obj_type;
+	const char *obj_label;
+
+	deltas.nentries = 0;
+	SIMPLEQ_INIT(&deltas.entries);
+
+	err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
+	    obj->off, obj->tslen, obj->type, obj->size,
+	    GOT_DELTA_CHAIN_RECURSION_MAX);
+	if (err)
+		goto done;
 
-static int
-applydelta(Object *dst, Object *base, char *d, int nd)
-{
-	char *r, *b, *ed, *er;
-	int n, nr, c;
-	off_t o, l;
-
-	ed = d + nd;
-	b = base->data;
-	n = readvint(d, &d);
-	if(n != base->size){
-		fprintf(stderr, "mismatched source size\n");
-		return -1;
-	}
+	/* XXX TODO reading large objects into memory is bad! */
+	err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
+	if (err)
+		goto done;
 
-	nr = readvint(d, &d);
-	r = emalloc(nr + 64);
-	n = snprintf(r, 64, "%s %d", typestr(base->type), nr) + 1;
-	dst->all = r;
-	dst->type = base->type;
-	dst->data = r + n;
-	dst->size = nr;
-	er = dst->data + nr;
-	r = dst->data;
-
-	while(1){
-		if(d == ed)
-			break;
-		c = *d++;
-		if(!c){
-			fprintf(stderr, "bad delta encoding\n");
-			return -1;
-		}
-		/* copy from base */
-		if(c & 0x80){
-			o = 0;
-			l = 0;
-			/* Offset in base */
-			if(c & 0x01 && d != ed) o |= (*d++ <<  0) & 0x000000ff;
-			if(c & 0x02 && d != ed) o |= (*d++ <<  8) & 0x0000ff00;
-			if(c & 0x04 && d != ed) o |= (*d++ << 16) & 0x00ff0000;
-			if(c & 0x08 && d != ed) o |= (*d++ << 24) & 0xff000000;
-
-			/* Length to copy */
-			if(c & 0x10 && d != ed) l |= (*d++ <<  0) & 0x0000ff;
-			if(c & 0x20 && d != ed) l |= (*d++ <<  8) & 0x00ff00;
-			if(c & 0x40 && d != ed) l |= (*d++ << 16) & 0xff0000;
-			if(l == 0) l = 0x10000;
-
-			assert(o + l <= base->size);
-			memmove(r, b + o, l);
-			r += l;
-		/* inline data */
-		}else{
-			memmove(r, d, c);
-			d += c;
-			r += c;
-		}
+	SHA1Init(&ctx);
 
+	err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
+	if (err)
+		goto done;
+	err = get_obj_type_label(&obj_label, base_obj_type);
+	if (err)
+		goto done;
+	if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
+		err = got_error_from_errno("asprintf");
+		goto done;
 	}
-	if(r != er){
-		fprintf(stderr, "truncated delta (%zd)\n", er - r);
-		return -1;
+	headerlen = strlen(header) + 1;
+	SHA1Update(&ctx, header, headerlen);
+	SHA1Update(&ctx, buf, len);
+	SHA1Final(obj->id.sha1, &ctx);
+done:
+	free(buf);
+	while (!SIMPLEQ_EMPTY(&deltas.entries)) {
+		delta = SIMPLEQ_FIRST(&deltas.entries);
+		SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
+		free(delta);
 	}
-
-	return nr;
+	return err;
 }
 
+/* Determine the slot in the pack index a given object ID should use. */
 static int
-readrdelta(FILE *f, Object *o, int nd, int flag)
+find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
 {
-	const struct got_error *e;
-	struct got_object_id h;
-	Object *b;
-	uint8_t *d;
-	size_t n;
+	u_int8_t id0 = sha1[0];
+	uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
+	int left = 0, right = nindexed - 1;
+	int cmp = 0, i = 0;
 
-	d = NULL;
-	if(fread(h.sha1, 1, sizeof(h.sha1), f) != sizeof(h.sha1))
-		goto error;
-	if(hasheq(&o->hash, &h))
-		goto error;
-	if ((e = got_inflate_to_mem(&d, &n, NULL, f)) != NULL)
-		goto error;
-	o->len = ftello(f) - o->off;
-	if(d == NULL || n != nd)
-		goto error;
-	if((b = readidxobject(f, h, flag)) == NULL)
-		goto error;
-	if(applydelta(o, b, d, n) == -1)
-		goto error;
-	free(d);
-	return 0;
-error:
-	free(d);
-	return -1;
-}
+	if (id0 > 0)
+		left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
 
-static int
-readodelta(FILE *f, Object *o, off_t nd, off_t p, int flag)
-{
-	Object b;
-	uint8_t *d;
-	off_t r;
-	size_t n;
-	int c;
-
-	r = 0;
-	d = NULL;
-	while(1){
-		if((c = fgetc(f)) == -1)
-			goto error;
-		r |= c & 0x7f;
-		if (!(c & 0x80))
-			break;
-		r++;
-		r <<= 7;
-	}while(c & 0x80);
+	while (left <= right) {
+		struct got_packidx_object_id *oid;
 
-	if(r > p){
-		fprintf(stderr, "junk offset -%lld (from %lld)\n", r, p);
-		goto error;
+		i = ((left + right) / 2);
+		oid = &packidx->hdr.sorted_ids[i];
+
+		cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
+		if (cmp == 0)
+			return -1; /* object already indexed */
+		else if (cmp > 0)
+			left = i + 1;
+		else if (cmp < 0)
+			right = i - 1;
 	}
 
-	if (got_inflate_to_mem(&d, &n, NULL, f) != NULL)
-		goto error;
-	o->len = ftello(f) - o->off;
-	if(d == NULL || n != nd)
-		goto error;
-	if(fseek(f, p - r, SEEK_SET) == -1)
-		goto error;
-	if(readpacked(f, &b, flag) == -1)
-		goto error;
-	if(applydelta(o, &b, d, nd) == -1)
-		goto error;
-	free(d);
-	return 0;
-error:
-	free(d);
-	return -1;
+	return left;
 }
 
-static int
-readpacked(FILE *f, Object *o, int flag)
+#if 0
+static void
+print_packidx(struct got_packidx *packidx)
 {
-	const struct got_error *e;
-	int c, s, n;
-	off_t l, p;
-	size_t ndata;
-	uint8_t *data;
-	Type t;
-	Buf b;
-
-	p = ftello(f);
-	c = fgetc(f);
-	if(c == -1)
-		return -1;
-	l = c & 0xf;
-	s = 4;
-	t = (c >> 4) & 0x7;
-	if(!t){
-		fprintf(stderr, "unknown type for byte %x\n", c);
-		return -1;
-	}
-	while(c & 0x80){
-		if((c = fgetc(f)) == -1)
-			return -1;
-		l |= (c & 0x7f) << s;
-		s += 7;
-	}
+	uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
+	int i;
 
-	switch(t){
-	default:
-		fprintf(stderr, "invalid object at %lld\n", ftello(f));
-		return -1;
-	case GCommit:
-	case GTree:
-	case GTag:
-	case GBlob:
-		b.sz = 64 + l;
-
-		b.data = emalloc(b.sz);
-		n = snprintf(b.data, 64, "%s %lld", typestr(t), l) + 1;
-		b.len = n;
-		e = got_inflate_to_mem(&data, &ndata, NULL, f);
-		if (e != NULL || n + ndata >= b.sz) {
-			free(b.data);
-			return -1;
-		}
-		memcpy(b.data + n, data, ndata);
-		o->len = ftello(f) - o->off;
-		o->type = t;
-		o->all = b.data;
-		o->data = b.data + n;
-		o->size = ndata;
-		free(data);
-		break;
-	case GOdelta:
-		if(readodelta(f, o, l, p, flag) == -1)
-			return -1;
-		break;
-	case GRdelta:
-		if(readrdelta(f, o, l, flag) == -1)
-			return -1;
-		break;
+	fprintf(stderr, "object IDs:\n");
+	for (i = 0; i < nindexed; i++) {
+		char hex[SHA1_DIGEST_STRING_LENGTH];
+		got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
+		    hex, sizeof(hex));
+		fprintf(stderr, "%s\n", hex);
 	}
-	o->flag |= Cloaded|flag;
-	return 0;
-}
+	fprintf(stderr, "\n");
 
-static int
-readloose(FILE *f, Object *o, int flag)
-{
-	struct { char *tag; int type; } *p, types[] = {
-		{"blob", GBlob},
-		{"tree", GTree},
-		{"commit", GCommit},
-		{"tag", GTag},
-		{NULL},
-	};
-	char *s, *e;
-	uint8_t *d;
-	off_t sz;
-	size_t n;
-	int l;
-
-	if (got_inflate_to_mem(&d, &n, NULL, f) != NULL)
-		return -1;
-
-	s = (char *)d;
-	o->type = GNone;
-	for(p = types; p->tag; p++){
-		l = strlen(p->tag);
-		if(strncmp(s, p->tag, l) == 0){
-			s += l;
-			o->type = p->type;
-			while(!isspace(*s))
-				s++;
-			break;
-		}
-	}
-	if(o->type == GNone){
-		free(o->data);
-		return -1;
-	}
-	sz = strtol(s, &e, 0);
-	if(e == s || *e++ != 0){
-		fprintf(stderr, "malformed object header\n");
-		goto error;
+	fprintf(stderr, "object offsets:\n");
+	for (i = 0; i < nindexed; i++) {
+		uint32_t offset = be32toh(packidx->hdr.offsets[i]);
+		if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
+			int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
+			fprintf(stderr, "%u -> %llu\n", offset, 
+			    be64toh(packidx->hdr.large_offsets[j]));
+		} else
+			fprintf(stderr, "%u\n", offset);
 	}
-	if(sz != n - (e - (char *)d)){
-		fprintf(stderr, "mismatched sizes\n");
-		goto error;
-	}
-	o->size = sz;
-	o->data = e;
-	o->all = d;
-	o->flag |= Cloaded|flag;
-	return 0;
+	fprintf(stderr, "\n");
 
-error:
-	free(d);
-	return -1;
+	fprintf(stderr, "fanout table:");
+	for (i = 0; i <= 0xff; i++)
+		fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
+	fprintf(stderr, "\n");
 }
+#endif
 
-static off_t
-searchindex(FILE *f, struct got_object_id h)
+static void
+update_packidx(int *nlarge, struct got_packidx *packidx, int nobj,
+    struct got_indexed_object *obj)
 {
-	int lo, hi, idx, i, nent;
-	off_t o, oo;
-	struct got_object_id hh;
+	int i, n, idx;
+	uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
 
-	o = 8;
-	/*
-	 * Read the fanout table. The fanout table
-	 * contains 256 entries, corresponsding to
-	 * the first byte of the hash. Each entry
-	 * is a 4 byte big endian integer, containing
-	 * the total number of entries with a leading
-	 * byte <= the table index, allowing us to
-	 * rapidly do a binary search on them.
-	 */
-	if (h.sha1[0] == 0){
-		lo = 0;
-		if(preadbe32(f, &hi, o) == -1)
-			goto err;
-	} else {
-		o += h.sha1[0]*4 - 4;
-		if(preadbe32(f, &lo, o + 0) == -1)
-			goto err;
-		if(preadbe32(f, &hi, o + 4) == -1)
-			goto err;
+	idx = find_object_idx(packidx, obj->id.sha1);
+	if (idx == -1) {
+		char hex[SHA1_DIGEST_STRING_LENGTH];
+		got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
+		return; /* object already indexed */
 	}
-	if(hi == lo)
-		goto notfound;
-	if(preadbe32(f, &nent, 8 + 255*4) == -1)
-		goto err;
 
-	/*
-	 * Now that we know the range of hashes that the
-	 * entry may exist in, read them in so we can do
-	 * a bsearch.
-	 */
-	idx = -1;
-	fseek(f, Hashsz*lo + 8 + 256*4, SEEK_SET);
-	for(i = 0; i < hi - lo; i++){
-		if(fread(hh.sha1, 1, sizeof(hh.sha1), f) == -1)
-			goto err;
-		if(hasheq(&hh, &h))
-			idx = lo + i;
-	}
-	if(idx == -1)
-		goto notfound;
+	memmove(&packidx->hdr.sorted_ids[idx + 1],
+	    &packidx->hdr.sorted_ids[idx],
+	    sizeof(struct got_packidx_object_id) * (nindexed - idx));
+	memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
+	    sizeof(uint32_t) * (nindexed - idx));
 
-
-	/*
-	 * We found the entry. If it's 32 bits, then we
-	 * can just return the oset, otherwise the 32
-	 * bit entry contains the oset to the 64 bit
-	 * entry.
-	 */
-	oo = 8;			/* Header */
-	oo += 256*4;		/* Fanout table */
-	oo += Hashsz*nent;	/* Hashes */
-	oo += 4*nent;		/* Checksums */
-	oo += 4*idx;		/* Offset offset */
-	if(preadbe32(f, &i, oo) == -1)
-		goto err;
-	o = i & 0xffffffff;
-	if(o & (1ull << 31)){
-		o &= 0x7fffffff;
-		if(preadbe64(f, &o, o) == -1)
-			goto err;
-	}
-	return o;
-
-err:
-	fprintf(stderr, "unable to read packfile\n");
-	return -1;
-notfound:
-	{
-		char hstr[41];
-		hashfmt(hstr, sizeof(hstr), &h);
-		fprintf(stdout, "could not find object %s\n", hstr);
-	}
-	return -1;
-}
-
-/*
- * Scans for non-empty word, copying it into buf.
- * Strips off word, leading, and trailing space
- * from input.
- *
- * Returns -1 on empty string or error, leaving
- * input unmodified.
- */
-static int
-scanword(char **str, int *nstr, char *buf, int nbuf)
-{
-	char *p;
-	int n, r;
-
-	r = -1;
-	p = *str;
-	n = *nstr;
-	while(n && isblank(*p)){
-		n--;
-		p++;
+	memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
+	    SHA1_DIGEST_LENGTH);
+	if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
+		packidx->hdr.offsets[idx] = htobe32(obj->off);
+	else {
+		packidx->hdr.offsets[idx] = htobe32(*nlarge |
+		    GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
+		packidx->hdr.large_offsets[*nlarge] = htobe64(obj->off);
+		(*nlarge)++;
 	}
 
-	for(; n && *p && !isspace(*p); p++, n--){
-		r = 0;
-		*buf++ = *p;
-		nbuf--;
-		if(nbuf == 0)
-			return -1;
-	}
-	while(n && isblank(*p)){
-		n--;
-		p++;
-	}
-	*buf = 0;
-	*str = p;
-	*nstr = n;
-	return r;
-}
-
-static void
-nextline(char **str, int *nstr)
-{
-	char *s;
-
-	if((s = strchr(*str, '\n')) != NULL){
-		*nstr -= s - *str + 1;
-		*str = s + 1;
+	for (i = obj->id.sha1[0]; i <= 0xff; i++) {
+		n = be32toh(packidx->hdr.fanout_table[i]);
+		packidx->hdr.fanout_table[i] = htobe32(n + 1);
 	}
 }
 
-static int
-parseauthor(char **str, int *nstr, char **name, off_t *time)
+static const struct got_error *
+index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
+    struct imsgbuf *ibuf)
 {
-	return 0;
-}
+	const struct got_error *err;
+	struct got_packfile_hdr hdr;
+	struct got_packidx packidx;
+	char buf[8];
+	int nobj, nvalid, nloose, nlarge = 0, nresolved = 0, i;
+	struct got_indexed_object **objects = NULL, *obj;
+	SHA1_CTX ctx;
+	uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
+	ssize_t r, w;
+	int pass;
+
+	/* Check pack file header. */
+	r = read(pack->fd, &hdr, sizeof(hdr));
+	if (r == -1)
+		return got_error_from_errno("read");
+	if (r < sizeof(hdr))
+		return got_error_msg(GOT_ERR_BAD_PACKFILE,
+		    "short packfile header");
+
+	if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
+		return got_error_msg(GOT_ERR_BAD_PACKFILE,
+		    "bad packfile signature");
+	if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
+		return got_error_msg(GOT_ERR_BAD_PACKFILE,
+		    "bad packfile version");
+	nobj = betoh32(hdr.nobjects);
+	if (nobj == 0)
+		return got_error_msg(GOT_ERR_BAD_PACKFILE,
+		    "bad packfile with zero objects");
 
-static void
-parsecommit(Object *o)
-{
-	char *p, *t, buf[128];
-	int np;
-
-	p = o->data;
-	np = o->size;
-	o->commit = emalloc(sizeof(Cinfo));
-	while(1){
-		if(scanword(&p, &np, buf, sizeof(buf)) == -1)
-			break;
-		if(strcmp(buf, "tree") == 0){
-			if(scanword(&p, &np, buf, sizeof(buf)) == -1)
-				errx(1, "invalid commit: tree missing");
-			if(hparse(&o->commit->tree, buf) == -1)
-				errx(1, "invalid commit: garbled tree");
-		}else if(strcmp(buf, "parent") == 0){
-			if(scanword(&p, &np, buf, sizeof(buf)) == -1)
-				errx(1, "invalid commit: missing parent");
-			o->commit->parent = realloc(o->commit->parent, ++o->commit->nparent * sizeof(struct got_object_id));
-			if(!o->commit->parent)
-				err(1, "unable to malloc: ");
-			if(hparse(&o->commit->parent[o->commit->nparent - 1], buf) == -1)
-				errx(1, "invalid commit: garbled parent");
-		}else if(strcmp(buf, "author") == 0){
-			parseauthor(&p, &np, &o->commit->author, &o->commit->mtime);
-		}else if(strcmp(buf, "committer") == 0){
-			parseauthor(&p, &np, &o->commit->committer, &o->commit->ctime);
-		}else if(strcmp(buf, "gpgsig") == 0){
-			/* just drop it */
-			if((t = strstr(p, "-----END PGP SIGNATURE-----")) == NULL)
-				errx(1, "malformed gpg signature");
-			np -= t - p;
-			p = t;
-		}
-		nextline(&p, &np);
+	/*
+	 * Create an in-memory pack index which will grow as objects
+	 * IDs in the pack file are discovered. Only fields used to
+	 * read deltified objects will be needed by the pack.c library
+	 * code, so setting up just a pack index header is sufficient.
+	 */
+	memset(&packidx, 0, sizeof(packidx));
+	packidx.hdr.magic = malloc(sizeof(uint32_t));
+	if (packidx.hdr.magic == NULL)
+		return got_error_from_errno("calloc");
+	*packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
+	packidx.hdr.version = malloc(sizeof(uint32_t));
+	if (packidx.hdr.version == NULL) {
+		err = got_error_from_errno("malloc");
+		goto done;
 	}
-	while (np && isspace(*p)) {
-		p++;
-		np--;
+	*packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
+	packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
+	    sizeof(uint32_t));
+	if (packidx.hdr.fanout_table == NULL) {
+		err = got_error_from_errno("calloc");
+		goto done;
 	}
-	o->commit->msg = p;
-	o->commit->nmsg = np;
-}
-
-static void
-parsetree(Object *o)
-{
-	char *p, buf[256];
-	int np, nn, m;
-	Dirent *t;
-
-	p = o->data;
-	np = o->size;
-	o->tree = emalloc(sizeof(Tinfo));
-	while(np > 0){
-		if(scanword(&p, &np, buf, sizeof(buf)) == -1)
-			break;
-		o->tree->ent = erealloc(o->tree->ent, ++o->tree->nent * sizeof(Dirent));
-		t = &o->tree->ent[o->tree->nent - 1];
-		memset(t, 0, sizeof(Dirent));
-		m = strtol(buf, NULL, 8);
-		/* FIXME: symlinks and other BS */
-		if(m == 0160000){
-			t->mode |= S_IFDIR;
-			t->modref = 1;
-		}
-		t->mode = m & 0777;
-		if(m & 0040000)
-			t->mode |= S_IFDIR;
-		t->name = p;
-		nn = strlen(p) + 1;
-		p += nn;
-		np -= nn;
-		if(np < sizeof(t->h.sha1))
-			errx(1, "malformed tree, remaining %d (%s)", np, p);
-		memcpy(t->h.sha1, p, sizeof(t->h.sha1));
-		p += sizeof(t->h.sha1);
-		np -= sizeof(t->h.sha1);
+	packidx.hdr.sorted_ids = calloc(nobj,
+	    sizeof(struct got_packidx_object_id));
+	if (packidx.hdr.sorted_ids == NULL) {
+		err = got_error_from_errno("calloc");
+		goto done;
 	}
-}
-
-void
-parseobject(Object *o)
-{
-	if(o->flag & Cparsed)
-		return;
-	switch(o->type){
-	case GTree:	parsetree(o);	break;
-	case GCommit:	parsecommit(o);	break;
-	//case GTag:	parsetag(o);	break;
-	default:	break;
+	packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
+	if (packidx.hdr.offsets == NULL) {
+		err = got_error_from_errno("calloc");
+		goto done;
 	}
-	o->flag |= Cparsed;
-}
-
-static Object*
-readidxobject(FILE *idx, struct got_object_id h, int flag)
-{
-	char path[Pathmax];
-	char hbuf[41];
-	FILE *f;
-	Object *obj;
-	int l, n;
-	off_t o;
-	struct dirent *ent;
-	DIR *d;
-
-
-	if ((obj = got_object_idset_lookup_data(objcache, &h))) {
-		if(obj->flag & Cloaded)
-			return obj;
-		if(obj->flag & Cidx){
-			assert(idx != NULL);
-			o = ftello(idx);
-			if(fseek(idx, obj->off, SEEK_SET) == -1)
-				errx(1, "could not seek to object offset");
-			if(readpacked(idx, obj, flag) == -1)
-				errx(1, "could not reload object");
-			if(fseek(idx, o, SEEK_SET) == -1)
-				errx(1, "could not restore offset");
-			cache(obj);
-			return obj;
+	/* Large offsets table is empty for pack files < 2 GB. */
+	if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
+		packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
+		if (packidx.hdr.large_offsets == NULL) {
+			err = got_error_from_errno("calloc");
+			goto done;
 		}
 	}
 
-	d = NULL;
-	/* We're not putting it in the cache yet... */
-	obj = emalloc(sizeof(Object));
-	obj->id = next_object_id + 1;
-	obj->hash = h;
-
-	hashfmt(hbuf, sizeof(hbuf), &h);
-	snprintf(path, sizeof(path), ".git/objects/%c%c/%s", hbuf[0], hbuf[1], hbuf + 2);
-	if((f = fopen(path, "r")) != NULL){
-		if(readloose(f, obj, flag) == -1)
-			goto error;
-		fclose(f);
-		parseobject(obj);
-		hashfmt(hbuf, sizeof(hbuf), &obj->hash);
-		fprintf(stderr, "object %s cached\n", hbuf);
-		cache(obj);
-		return obj;
-	}
-
-	o = -1;
-	if ((d = opendir(".git/objects/pack")) == NULL)
-		err(1, "open pack dir");
-	while ((ent = readdir(d)) != NULL) {
-		l = strlen(ent->d_name);
-		if(l > 4 && strcmp(ent->d_name + l - 4, ".idx") != 0)
-			continue;
-		snprintf(path, sizeof(path), ".git/objects/pack/%s", ent->d_name);
-		if((f = fopen(path, "r")) == NULL)
-			continue;
-		o = searchindex(f, h);
-		fclose(f);
-		if(o == -1)
-			continue;
-		break;
-	}
-	closedir(d);
-
-	if (o == -1)
-		goto error;
-
-	if((n = snprintf(path, sizeof(path), "%s", path)) >= sizeof(path) - 4)
-		goto error;
-	memcpy(path + n - 4, ".pack", 6);
-	if((f = fopen(path, "r")) == NULL)
-		goto error;
-	if(fseek(f, o, SEEK_SET) == -1)
-		goto error;
-	if(readpacked(f, obj, flag) == -1)
-		goto error;
-	fclose(f);
-	parseobject(obj);
-	cache(obj);
-	return obj;
-error:
-	free(obj);
-	return NULL;
-}
+	nvalid = 0;
+	nloose = 0;
+	objects = calloc(nobj, sizeof(struct got_indexed_object *));
+	if (objects == NULL)
+		return got_error_from_errno("calloc");
 
-Object*
-readobject(struct got_object_id h)
-{
-	Object *o;
+	/*
+	 * First pass: locate all objects and identify un-deltified objects.
+	 *
+	 * When this pass has completed we will know offset, type, size, and
+	 * CRC information for all objects in this pack file. We won't know
+	 * any of the actual object IDs of deltified objects yet since we
+	 * will not yet attempt to combine deltas.
+	 */
+	pass = 1;
+	for (i = 0; i < nobj; i++) {
+		err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
+		    nloose, 0);
+		if (err)
+			goto done;
+
+		obj = calloc(1, sizeof(*obj));
+		if (obj == NULL) {
+			err = got_error_from_errno("calloc");
+			goto done;
+		}
 
-	o = readidxobject(NULL, h, 0);
-	if(o)
-		ref(o);
-	return o;
-}
+		/* Store offset to type+size information for this object. */
+		obj->off = lseek(pack->fd, 0, SEEK_CUR);
+		if (obj->off == -1) {
+			err = got_error_from_errno("lseek");
+			goto done;
+		}
 
-int
-objcmp(const void *pa, const void *pb)
-{
-	Object *a, *b;
+		err = read_packed_object(pack, obj);
+		if (err)
+			goto done;
 
-	a = *(Object**)pa;
-	b = *(Object**)pb;
-	return memcmp(a->hash.sha1, b->hash.sha1, sizeof(a->hash.sha1));
-}
+		objects[i] = obj;
 
-static int
-hwrite(FILE *b, void *buf, int len, SHA1_CTX *ctx)
-{
-	SHA1Update(ctx, buf, len);
-	return fwrite(buf, 1, len, b);
-}
+		if (0) {
+		err = object_crc(pack->fd, obj);
+		if (err)
+			goto done;
+		}
 
-static uint32_t
-objectcrc(FILE *f, Object *o)
-{
-	char buf[8096];
-	int n, r;
-
-	o->crc = 0;
-	fseek(f, o->off, SEEK_SET);
-	for(n = o->len; n > 0; n -= r){
-		r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
-		if(r == -1)
-			return -1;
-		if(r == 0)
-			return 0;
-		o->crc = crc32(o->crc, buf, r);
-	}
-	return 0;
-}
+		if (obj->type == GOT_OBJ_TYPE_BLOB ||
+		    obj->type == GOT_OBJ_TYPE_TREE ||
+		    obj->type == GOT_OBJ_TYPE_COMMIT ||
+		    obj->type == GOT_OBJ_TYPE_TAG) {
+			objects[i]->valid = 1;
+			nloose++;
+			update_packidx(&nlarge, &packidx, nobj, obj);
+		}
 
-int
-indexpack(int packfd, int idxfd, struct got_object_id *packhash,
-    struct imsgbuf *ibuf)
-{
-	char hdr[4*3], buf[8];
-	int nobj, nvalid, nbig, n, i, step;
-	Object *o, **objects;
-	char *valid;
-	SHA1_CTX ctx, objctx;
-	FILE *f;
-	struct got_object_id h;
-	int c;
-
-	if ((f = fdopen(packfd, "r")) == NULL)
-		return -1;
-	if (fseek(f, 0, SEEK_SET) == -1)
-		return -1;
-	if (fread(hdr, 1, sizeof(hdr), f) != sizeof(hdr)) {
-		fprintf(stderr, "short read on header\n");
-		return -1;
-	}
-	if (memcmp(hdr, "PACK\0\0\0\2", 8) != 0) {
-		fprintf(stderr, "invalid header\n");
-		return -1;
+		if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
+		    SEEK_SET) == -1) {
+			err = got_error_from_errno("lseek");
+			goto done;
+		}
 	}
+	nvalid = nloose;
 
-	nvalid = 0;
-	nobj = GETBE32(hdr + 8);
-	objects = calloc(nobj, sizeof(Object*));
-	valid = calloc(nobj, sizeof(char));
-	step = nobj/100;
-	if(!step)
-		step++;
+	/*
+	 * Second pass: We can now resolve deltas to compute the IDs of
+	 * objects which appear in deltified form. Because deltas can be
+	 * chained this pass may require a couple of iterations until all
+	 * IDs of deltified objects have been discovered.
+	 */
+	pass++;
 	while (nvalid != nobj) {
-		got_privsep_send_index_pack_progress(ibuf, nobj, nvalid);
-		n = 0;
+		int n = 0;
 		for (i = 0; i < nobj; i++) {
-			if (valid[i]) {
-				n++;
+			if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
+			    objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
 				continue;
+
+			if (objects[i]->valid)
+				continue;
+
+			obj = objects[i];
+			if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
+				err = got_error_from_errno("lseek");
+				goto done;
 			}
-			if (!objects[i]) {
-				o = emalloc(sizeof(Object));
-				o->off = ftello(f);
-				objects[i] = o;
-			}
-			o = objects[i];
-			fseek(f, o->off, SEEK_SET);
-			if (readpacked(f, o, Cidx) == 0){
-				SHA1Init(&objctx);
-				SHA1Update(&objctx, (uint8_t*)o->all, o->size + strlen(o->all) + 1);
-				SHA1Final(o->hash.sha1, &objctx);
-				cache(o);
-				valid[i] = 1;
-				n++;
+
+			err = resolve_deltified_object(pack, &packidx, obj);
+			if (err) {
+				if (err->code != GOT_ERR_NO_OBJ)
+					goto done;
+				/*
+				 * We cannot resolve this object yet because
+				 * a delta base is unknown. Try again later.
+				 */
+				continue;
 			}
-			if(objectcrc(f, o) == -1)
-				return -1;
+
+			objects[i]->valid = 1;
+			n++;
+			update_packidx(&nlarge, &packidx, nobj, obj);
+			err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
+			    nloose, nresolved + n);
+			if (err)
+				goto done;
+
+		}
+		if (pass++ > 3 && n == 0) {
+			static char msg[64];
+			snprintf(msg, sizeof(msg), "could not resolve "
+			    "any of deltas; packfile could be corrupt");
+			err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
+			goto done;
+			
 		}
-		if (n == nvalid) {
-			errx(1, "fix point reached too early: %d/%d", nvalid, nobj);
-			goto error;
+		if (nloose + nresolved == nobj) {
+			static char msg[64];
+			snprintf(msg, sizeof(msg),
+			    "fix point reached too early: %d/%d/%d", nvalid, nresolved, nobj);
+			err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
+			goto done;
 		}
-		nvalid = n;
+		nresolved += n;
+		nvalid += nresolved;
 	}
-	fclose(f);
 
-	SHA1Init(&ctx);
-	qsort(objects, nobj, sizeof(Object*), objcmp);
-	if((f = fdopen(idxfd, "w")) == NULL)
-		return -1;
-	if(hwrite(f, "\xfftOc\x00\x00\x00\x02", 8, &ctx) != 8)
-		goto error;
-	/* fanout table */
-	c = 0;
-	for(i = 0; i < 256; i++){
-		while(c < nobj && (objects[c]->hash.sha1[0] & 0xff) <= i)
-			c++;
-		PUTBE32(buf, c);
-		hwrite(f, buf, 4, &ctx);
-	}
-	for(i = 0; i < nobj; i++){
-		o = objects[i];
-		hwrite(f, o->hash.sha1, sizeof(o->hash.sha1), &ctx);
+	if (nloose + nresolved != nobj) {
+		static char msg[64];
+		snprintf(msg, sizeof(msg),
+		    "discovered only %d of %d objects", nloose + nresolved, nobj);
+		err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
+		goto done;
 	}
 
-	/* pointless, nothing uses this */
+	/* We may have seen duplicates. Update our total object count. */
+	nobj = betoh32(packidx.hdr.fanout_table[0xff]);
+
+	SHA1Init(&ctx);
+	err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
+	if (err)
+		goto done;
+	err = hwrite(idxfd, packidx.hdr.fanout_table,
+	    GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
+	if (err)
+		goto done;
+	err = hwrite(idxfd, packidx.hdr.sorted_ids,
+	    nobj * SHA1_DIGEST_LENGTH, &ctx);
+	if (err)
+		goto done;
 	for(i = 0; i < nobj; i++){
 		PUTBE32(buf, objects[i]->crc);
-		hwrite(f, buf, 4, &ctx);
+		err = hwrite(idxfd, buf, 4, &ctx);
+		if (err)
+			goto done;
 	}
+	err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t), &ctx);
+	if (err)
+		goto done;
+	if (nlarge > 0) {
+		err = hwrite(idxfd, packidx.hdr.large_offsets,
+		    nlarge * sizeof(uint64_t), &ctx);
+		if (err)
+			goto done;
+	}
+	err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
+	if (err)
+		goto done;
 
-	nbig = 0;
-	for(i = 0; i < nobj; i++){
-		if(objects[i]->off <= (1ull<<31))
-			PUTBE32(buf, objects[i]->off);
-		else
-			PUTBE32(buf, (1ull << 31) | nbig++);
-		hwrite(f, buf, 4, &ctx);
+	SHA1Final(packidx_hash, &ctx);
+	w = write(idxfd, packidx_hash, sizeof(packidx_hash));
+	if (w == -1) {
+		err = got_error_from_errno("write");
+		goto done;
 	}
-	for(i = 0; i < nobj; i++){
-		if(objects[i]->off > (1ull<<31)){
-			PUTBE64(buf, objects[i]->off);
-			hwrite(f, buf, 8, &ctx);
-		}
+	if (w != sizeof(packidx_hash)) {
+		err = got_error(GOT_ERR_IO);
+		goto done;
 	}
-	hwrite(f, packhash->sha1, sizeof(packhash->sha1), &ctx);
-	SHA1Final(h.sha1, &ctx);
-	fwrite(h.sha1, 1, sizeof(h.sha1), f);
-
-	free(objects);
-	free(valid);
-	fclose(f);
-	return 0;
-
-error:
-	free(objects);
-	free(valid);
-	fclose(f);
-	return -1;
+done:
+	free(packidx.hdr.magic);
+	free(packidx.hdr.version);
+	free(packidx.hdr.fanout_table);
+	free(packidx.hdr.sorted_ids);
+	free(packidx.hdr.offsets);
+	free(packidx.hdr.large_offsets);
+	return err;
 }
 
 int
 main(int argc, char **argv)
 {
-	const struct got_error *err = NULL;
-	struct got_object_id packhash;
+	const struct got_error *err = NULL, *close_err;
 	struct imsgbuf ibuf;
 	struct imsg imsg;
-	int packfd, idxfd;
+	int idxfd = -1;
+	struct got_pack pack;
+	uint8_t pack_hash[SHA1_DIGEST_LENGTH];
+	off_t packfile_size;
+#if 0
+	static int attached;
+	while (!attached)
+		sleep(1);
+#endif
+
+	memset(&pack, 0, sizeof(pack));
+	pack.fd = -1;
+	pack.delta_cache = got_delta_cache_alloc(100,
+	    GOT_DELTA_RESULT_SIZE_CACHED_MAX);
+	if (pack.delta_cache == NULL) {
+		err = got_error_from_errno("got_delta_cache_alloc");
+		goto done;
+	}
 
-	objcache = got_object_idset_alloc();
 	imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
-	if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
-		if (err->code == GOT_ERR_PRIVSEP_PIPE)
-			err = NULL;
+
+	err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
+	if (err)
 		goto done;
-	}
 	if (imsg.hdr.type == GOT_IMSG_STOP)
 		goto done;
 	if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
 		err = got_error(GOT_ERR_PRIVSEP_MSG);
 		goto done;
 	}
-	if (imsg.hdr.len - IMSG_HEADER_SIZE != SHA1_DIGEST_LENGTH) {
+	if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
 		err = got_error(GOT_ERR_PRIVSEP_LEN);
 		goto done;
 	}
-	packfd = imsg.fd;
-	memcpy(packhash.sha1, imsg.data, SHA1_DIGEST_LENGTH);
+	memcpy(pack_hash, imsg.data, sizeof(pack_hash));
+	pack.fd = imsg.fd;
 
-	if((err = got_privsep_recv_imsg(&imsg, &ibuf, 0)) != 0) {
-		if (err->code == GOT_ERR_PRIVSEP_PIPE)
-			err = NULL;
+	err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
+	if (err)
 		goto done;
-	}
 	if (imsg.hdr.type == GOT_IMSG_STOP)
 		goto done;
 	if (imsg.hdr.type != GOT_IMSG_TMPFD) {
@@ -1252,15 +738,37 @@ main(int argc, char **argv)
 	}
 	idxfd = imsg.fd;
 
-	indexpack(packfd, idxfd, &packhash, &ibuf);
+	if (lseek(pack.fd, 0, SEEK_END) == -1) {
+		err = got_error_from_errno("lseek");
+		goto done;
+	}
+	packfile_size = lseek(pack.fd, 0, SEEK_CUR);
+	if (packfile_size == -1) {
+		err = got_error_from_errno("lseek");
+		goto done;
+	}
+	pack.filesize = packfile_size; /* XXX off_t vs size_t */
+
+	if (lseek(pack.fd, 0, SEEK_SET) == -1) {
+		err = got_error_from_errno("lseek");
+		goto done;
+	}
+
+	err = index_pack(&pack, idxfd, pack_hash, &ibuf);
 done:
-	if(err != NULL)
-		got_privsep_send_error(&ibuf, err);
-	else
+	close_err = got_pack_close(&pack);
+	if (close_err && err == NULL)
+		err = close_err;
+	if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+
+	if (err == NULL)
 		err = got_privsep_send_index_pack_done(&ibuf);
-	if(err != NULL) {
+	if (err) {
+		got_privsep_send_error(&ibuf, err);
 		fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
 		got_privsep_send_error(&ibuf, err);
+		exit(1);
 	}
 
 	exit(0);