1
- #define DISABLE_SIGN_COMPARE_WARNINGS
2
-
3
1
#include "git-compat-util.h"
4
2
#include "abspath.h"
5
3
#include "config.h"
24
22
#define BITMAP_POS_UNKNOWN (~((uint32_t)0))
25
23
#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
26
24
#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
25
+ #define NO_PREFERRED_PACK (~((uint32_t)0))
27
26
28
27
extern int midx_checksum_valid (struct multi_pack_index * m );
29
28
extern void clear_midx_files_ext (const char * object_dir , const char * ext ,
@@ -104,7 +103,7 @@ struct write_midx_context {
104
103
unsigned large_offsets_needed :1 ;
105
104
uint32_t num_large_offsets ;
106
105
107
- int preferred_pack_idx ;
106
+ uint32_t preferred_pack_idx ;
108
107
109
108
int incremental ;
110
109
uint32_t num_multi_pack_indexes_before ;
@@ -260,7 +259,7 @@ static void midx_fanout_sort(struct midx_fanout *fanout)
260
259
static void midx_fanout_add_midx_fanout (struct midx_fanout * fanout ,
261
260
struct multi_pack_index * m ,
262
261
uint32_t cur_fanout ,
263
- int preferred_pack )
262
+ uint32_t preferred_pack )
264
263
{
265
264
uint32_t start = m -> num_objects_in_base , end ;
266
265
uint32_t cur_object ;
@@ -274,7 +273,7 @@ static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
274
273
end = m -> num_objects_in_base + ntohl (m -> chunk_oid_fanout [cur_fanout ]);
275
274
276
275
for (cur_object = start ; cur_object < end ; cur_object ++ ) {
277
- if ((preferred_pack > -1 ) &&
276
+ if ((preferred_pack != NO_PREFERRED_PACK ) &&
278
277
(preferred_pack == nth_midxed_pack_int_id (m , cur_object ))) {
279
278
/*
280
279
* Objects from preferred packs are added
@@ -364,7 +363,8 @@ static void compute_sorted_entries(struct write_midx_context *ctx,
364
363
preferred , cur_fanout );
365
364
}
366
365
367
- if (-1 < ctx -> preferred_pack_idx && ctx -> preferred_pack_idx < start_pack )
366
+ if (ctx -> preferred_pack_idx != NO_PREFERRED_PACK &&
367
+ ctx -> preferred_pack_idx < start_pack )
368
368
midx_fanout_add_pack_fanout (& fanout , ctx -> info ,
369
369
ctx -> preferred_pack_idx , 1 ,
370
370
cur_fanout );
@@ -843,7 +843,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
843
843
uint32_t commits_nr ,
844
844
unsigned flags )
845
845
{
846
- int ret , i ;
846
+ int ret ;
847
847
uint16_t options = 0 ;
848
848
struct bitmap_writer writer ;
849
849
struct pack_idx_entry * * index ;
@@ -871,7 +871,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
871
871
* this order).
872
872
*/
873
873
ALLOC_ARRAY (index , pdata -> nr_objects );
874
- for (i = 0 ; i < pdata -> nr_objects ; i ++ )
874
+ for (uint32_t i = 0 ; i < pdata -> nr_objects ; i ++ )
875
875
index [i ] = & pdata -> objects [i ].idx ;
876
876
877
877
bitmap_writer_init (& writer , ctx -> repo , pdata ,
@@ -892,7 +892,7 @@ static int write_midx_bitmap(struct write_midx_context *ctx,
892
892
* happens between bitmap_writer_build_type_index() and
893
893
* bitmap_writer_finish().
894
894
*/
895
- for (i = 0 ; i < pdata -> nr_objects ; i ++ )
895
+ for (uint32_t i = 0 ; i < pdata -> nr_objects ; i ++ )
896
896
index [ctx -> pack_order [i ]] = & pdata -> objects [i ].idx ;
897
897
898
898
bitmap_writer_select_commits (& writer , commits , commits_nr );
@@ -938,39 +938,21 @@ static struct multi_pack_index *lookup_multi_pack_index(struct repository *r,
938
938
return result ;
939
939
}
940
940
941
- static int fill_packs_from_midx (struct write_midx_context * ctx ,
942
- const char * preferred_pack_name , uint32_t flags )
941
+ static int fill_packs_from_midx (struct write_midx_context * ctx )
943
942
{
944
943
struct multi_pack_index * m ;
945
944
946
945
for (m = ctx -> m ; m ; m = m -> base_midx ) {
947
946
uint32_t i ;
948
947
949
948
for (i = 0 ; i < m -> num_packs ; i ++ ) {
950
- ALLOC_GROW (ctx -> info , ctx -> nr + 1 , ctx -> alloc );
951
-
952
- /*
953
- * If generating a reverse index, need to have
954
- * packed_git's loaded to compare their
955
- * mtimes and object count.
956
- *
957
- * If a preferred pack is specified, need to
958
- * have packed_git's loaded to ensure the chosen
959
- * preferred pack has a non-zero object count.
960
- */
961
- if (flags & MIDX_WRITE_REV_INDEX ||
962
- preferred_pack_name ) {
963
- if (prepare_midx_pack (ctx -> repo , m ,
964
- m -> num_packs_in_base + i )) {
965
- error (_ ("could not load pack" ));
966
- return 1 ;
967
- }
968
-
969
- if (open_pack_index (m -> packs [i ]))
970
- die (_ ("could not open index for %s" ),
971
- m -> packs [i ]-> pack_name );
949
+ if (prepare_midx_pack (ctx -> repo , m ,
950
+ m -> num_packs_in_base + i )) {
951
+ error (_ ("could not load pack" ));
952
+ return 1 ;
972
953
}
973
954
955
+ ALLOC_GROW (ctx -> info , ctx -> nr + 1 , ctx -> alloc );
974
956
fill_pack_info (& ctx -> info [ctx -> nr ++ ], m -> packs [i ],
975
957
m -> pack_names [i ],
976
958
m -> num_packs_in_base + i );
@@ -1074,11 +1056,13 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1074
1056
{
1075
1057
struct strbuf midx_name = STRBUF_INIT ;
1076
1058
unsigned char midx_hash [GIT_MAX_RAWSZ ];
1077
- uint32_t i , start_pack ;
1059
+ uint32_t start_pack ;
1078
1060
struct hashfile * f = NULL ;
1079
1061
struct lock_file lk ;
1080
1062
struct tempfile * incr ;
1081
- struct write_midx_context ctx = { 0 };
1063
+ struct write_midx_context ctx = {
1064
+ .preferred_pack_idx = NO_PREFERRED_PACK ,
1065
+ };
1082
1066
int bitmapped_packs_concat_len = 0 ;
1083
1067
int pack_name_concat_len = 0 ;
1084
1068
int dropped_packs = 0 ;
@@ -1141,8 +1125,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1141
1125
ctx .num_multi_pack_indexes_before ++ ;
1142
1126
m = m -> base_midx ;
1143
1127
}
1144
- } else if (ctx .m && fill_packs_from_midx (& ctx , preferred_pack_name ,
1145
- flags ) < 0 ) {
1128
+ } else if (ctx .m && fill_packs_from_midx (& ctx )) {
1129
+ result = 1 ;
1146
1130
goto cleanup ;
1147
1131
}
1148
1132
@@ -1186,22 +1170,22 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1186
1170
goto cleanup ; /* nothing to do */
1187
1171
1188
1172
if (preferred_pack_name ) {
1189
- ctx .preferred_pack_idx = -1 ;
1173
+ ctx .preferred_pack_idx = NO_PREFERRED_PACK ;
1190
1174
1191
- for (i = 0 ; i < ctx .nr ; i ++ ) {
1175
+ for (size_t i = 0 ; i < ctx .nr ; i ++ ) {
1192
1176
if (!cmp_idx_or_pack_name (preferred_pack_name ,
1193
1177
ctx .info [i ].pack_name )) {
1194
1178
ctx .preferred_pack_idx = i ;
1195
1179
break ;
1196
1180
}
1197
1181
}
1198
1182
1199
- if (ctx .preferred_pack_idx == -1 )
1183
+ if (ctx .preferred_pack_idx == NO_PREFERRED_PACK )
1200
1184
warning (_ ("unknown preferred pack: '%s'" ),
1201
1185
preferred_pack_name );
1202
1186
} else if (ctx .nr &&
1203
1187
(flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP ))) {
1204
- struct packed_git * oldest = ctx .info [ctx . preferred_pack_idx ].p ;
1188
+ struct packed_git * oldest = ctx .info [0 ].p ;
1205
1189
ctx .preferred_pack_idx = 0 ;
1206
1190
1207
1191
if (packs_to_drop && packs_to_drop -> nr )
@@ -1213,7 +1197,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1213
1197
* pack-order has all of its objects selected from that pack
1214
1198
* (and not another pack containing a duplicate)
1215
1199
*/
1216
- for (i = 1 ; i < ctx .nr ; i ++ ) {
1200
+ for (size_t i = 1 ; i < ctx .nr ; i ++ ) {
1217
1201
struct packed_git * p = ctx .info [i ].p ;
1218
1202
1219
1203
if (!oldest -> num_objects || p -> mtime < oldest -> mtime ) {
@@ -1229,18 +1213,23 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1229
1213
* objects to resolve, so the preferred value doesn't
1230
1214
* matter.
1231
1215
*/
1232
- ctx .preferred_pack_idx = -1 ;
1216
+ ctx .preferred_pack_idx = NO_PREFERRED_PACK ;
1233
1217
}
1234
1218
} else {
1235
1219
/*
1236
1220
* otherwise don't mark any pack as preferred to avoid
1237
1221
* interfering with expiration logic below
1238
1222
*/
1239
- ctx .preferred_pack_idx = -1 ;
1223
+ ctx .preferred_pack_idx = NO_PREFERRED_PACK ;
1240
1224
}
1241
1225
1242
- if (ctx .preferred_pack_idx > -1 ) {
1226
+ if (ctx .preferred_pack_idx != NO_PREFERRED_PACK ) {
1243
1227
struct packed_git * preferred = ctx .info [ctx .preferred_pack_idx ].p ;
1228
+
1229
+ if (open_pack_index (preferred ))
1230
+ die (_ ("failed to open preferred pack %s" ),
1231
+ ctx .info [ctx .preferred_pack_idx ].pack_name );
1232
+
1244
1233
if (!preferred -> num_objects ) {
1245
1234
error (_ ("cannot select preferred pack %s with no objects" ),
1246
1235
preferred -> pack_name );
@@ -1252,7 +1241,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1252
1241
compute_sorted_entries (& ctx , start_pack );
1253
1242
1254
1243
ctx .large_offsets_needed = 0 ;
1255
- for (i = 0 ; i < ctx .entries_nr ; i ++ ) {
1244
+ for (size_t i = 0 ; i < ctx .entries_nr ; i ++ ) {
1256
1245
if (ctx .entries [i ].offset > 0x7fffffff )
1257
1246
ctx .num_large_offsets ++ ;
1258
1247
if (ctx .entries [i ].offset > 0xffffffff )
@@ -1262,10 +1251,10 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1262
1251
QSORT (ctx .info , ctx .nr , pack_info_compare );
1263
1252
1264
1253
if (packs_to_drop && packs_to_drop -> nr ) {
1265
- int drop_index = 0 ;
1254
+ size_t drop_index = 0 ;
1266
1255
int missing_drops = 0 ;
1267
1256
1268
- for (i = 0 ; i < ctx .nr && drop_index < packs_to_drop -> nr ; i ++ ) {
1257
+ for (size_t i = 0 ; i < ctx .nr && drop_index < packs_to_drop -> nr ; i ++ ) {
1269
1258
int cmp = strcmp (ctx .info [i ].pack_name ,
1270
1259
packs_to_drop -> items [drop_index ].string );
1271
1260
@@ -1296,7 +1285,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1296
1285
* pack_perm[old_id] = new_id
1297
1286
*/
1298
1287
ALLOC_ARRAY (ctx .pack_perm , ctx .nr );
1299
- for (i = 0 ; i < ctx .nr ; i ++ ) {
1288
+ for (size_t i = 0 ; i < ctx .nr ; i ++ ) {
1300
1289
if (ctx .info [i ].expired ) {
1301
1290
dropped_packs ++ ;
1302
1291
ctx .pack_perm [ctx .info [i ].orig_pack_int_id ] = PACK_EXPIRED ;
@@ -1305,7 +1294,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1305
1294
}
1306
1295
}
1307
1296
1308
- for (i = 0 ; i < ctx .nr ; i ++ ) {
1297
+ for (size_t i = 0 ; i < ctx .nr ; i ++ ) {
1309
1298
if (ctx .info [i ].expired )
1310
1299
continue ;
1311
1300
pack_name_concat_len += strlen (ctx .info [i ].pack_name ) + 1 ;
@@ -1352,13 +1341,15 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1352
1341
incr = mks_tempfile_m (midx_name .buf , 0444 );
1353
1342
if (!incr ) {
1354
1343
error (_ ("unable to create temporary MIDX layer" ));
1355
- return -1 ;
1344
+ result = -1 ;
1345
+ goto cleanup ;
1356
1346
}
1357
1347
1358
1348
if (adjust_shared_perm (r , get_tempfile_path (incr ))) {
1359
1349
error (_ ("unable to adjust shared permissions for '%s'" ),
1360
1350
get_tempfile_path (incr ));
1361
- return -1 ;
1351
+ result = -1 ;
1352
+ goto cleanup ;
1362
1353
}
1363
1354
1364
1355
f = hashfd (r -> hash_algo , get_tempfile_fd (incr ),
@@ -1449,6 +1440,9 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1449
1440
* have been freed in the previous if block.
1450
1441
*/
1451
1442
1443
+ if (ctx .num_multi_pack_indexes_before == UINT32_MAX )
1444
+ die ("too many multi-pack-indexes" );
1445
+
1452
1446
CALLOC_ARRAY (keep_hashes , ctx .num_multi_pack_indexes_before + 1 );
1453
1447
1454
1448
if (ctx .incremental ) {
@@ -1458,34 +1452,38 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1458
1452
1459
1453
if (!chainf ) {
1460
1454
error_errno (_ ("unable to open multi-pack-index chain file" ));
1461
- return -1 ;
1455
+ result = -1 ;
1456
+ goto cleanup ;
1462
1457
}
1463
1458
1464
- if (link_midx_to_chain (ctx .base_midx ) < 0 )
1465
- return -1 ;
1459
+ if (link_midx_to_chain (ctx .base_midx ) < 0 ) {
1460
+ result = -1 ;
1461
+ goto cleanup ;
1462
+ }
1466
1463
1467
1464
get_split_midx_filename_ext (r -> hash_algo , & final_midx_name ,
1468
1465
object_dir , midx_hash , MIDX_EXT_MIDX );
1469
1466
1470
1467
if (rename_tempfile (& incr , final_midx_name .buf ) < 0 ) {
1471
1468
error_errno (_ ("unable to rename new multi-pack-index layer" ));
1472
- return -1 ;
1469
+ result = -1 ;
1470
+ goto cleanup ;
1473
1471
}
1474
1472
1475
1473
strbuf_release (& final_midx_name );
1476
1474
1477
1475
keep_hashes [ctx .num_multi_pack_indexes_before ] =
1478
1476
xstrdup (hash_to_hex_algop (midx_hash , r -> hash_algo ));
1479
1477
1480
- for (i = 0 ; i < ctx .num_multi_pack_indexes_before ; i ++ ) {
1478
+ for (uint32_t i = 0 ; i < ctx .num_multi_pack_indexes_before ; i ++ ) {
1481
1479
uint32_t j = ctx .num_multi_pack_indexes_before - i - 1 ;
1482
1480
1483
1481
keep_hashes [j ] = xstrdup (hash_to_hex_algop (get_midx_checksum (m ),
1484
1482
r -> hash_algo ));
1485
1483
m = m -> base_midx ;
1486
1484
}
1487
1485
1488
- for (i = 0 ; i < ctx .num_multi_pack_indexes_before + 1 ; i ++ )
1486
+ for (uint32_t i = 0 ; i <= ctx .num_multi_pack_indexes_before ; i ++ )
1489
1487
fprintf (get_lock_file_fp (& lk ), "%s\n" , keep_hashes [i ]);
1490
1488
} else {
1491
1489
keep_hashes [ctx .num_multi_pack_indexes_before ] =
@@ -1503,7 +1501,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1503
1501
ctx .incremental );
1504
1502
1505
1503
cleanup :
1506
- for (i = 0 ; i < ctx .nr ; i ++ ) {
1504
+ for (size_t i = 0 ; i < ctx .nr ; i ++ ) {
1507
1505
if (ctx .info [i ].p ) {
1508
1506
close_pack (ctx .info [i ].p );
1509
1507
free (ctx .info [i ].p );
@@ -1516,7 +1514,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1516
1514
free (ctx .pack_perm );
1517
1515
free (ctx .pack_order );
1518
1516
if (keep_hashes ) {
1519
- for (i = 0 ; i < ctx .num_multi_pack_indexes_before + 1 ; i ++ )
1517
+ for (uint32_t i = 0 ; i <= ctx .num_multi_pack_indexes_before ; i ++ )
1520
1518
free ((char * )keep_hashes [i ]);
1521
1519
free (keep_hashes );
1522
1520
}
0 commit comments