indexer: Add git_indexer_stream_finalize() Resolve any lingering deltas, write out the index file and rename the packfile.
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
diff --git a/include/git2/indexer.h b/include/git2/indexer.h
index 76b162e..8490ef0 100644
--- a/include/git2/indexer.h
+++ b/include/git2/indexer.h
@@ -44,6 +44,15 @@ GIT_EXTERN(int) git_indexer_stream_new(git_indexer_stream **out, const char *git
GIT_EXTERN(int) git_indexer_stream_add(git_indexer_stream *idx, void *data, size_t size, git_indexer_stats *stats);
/**
+ * Finalize the pack and index
+ *
+ * Resolve any pending deltas and write out the index file
+ *
+ * @param idx the indexer
+ */
+GIT_EXTERN(int) git_indexer_stream_finalize(git_indexer_stream *idx, git_indexer_stats *stats);
+
+/**
* Create a new indexer instance
*
* @param out where to store the indexer instance
diff --git a/src/indexer.c b/src/indexer.c
index 0a96407..7446342 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -52,12 +52,7 @@ struct git_indexer_stream {
};
struct delta_info {
- bool ofs;
- union {
- git_off_t base_off;
- git_oid base_oid;
- } u;
- git_rawobj delta_obj;
+ git_off_t delta_off;
};
const git_oid *git_indexer_hash(git_indexer *idx)
@@ -177,6 +172,7 @@ static int store_delta(git_indexer_stream *idx)
git_off_t entry_start = idx->off;
struct delta_info *delta;
size_t entry_size;
+ git_rawobj obj;
int error;
/*
@@ -192,44 +188,98 @@ static int store_delta(git_indexer_stream *idx)
if (type != GIT_OBJ_REF_DELTA && type != GIT_OBJ_OFS_DELTA)
return -1;
- delta = git__calloc(1, sizeof(struct delta_info));
- GITERR_CHECK_ALLOC(delta);
-
if (type == GIT_OBJ_REF_DELTA) {
- unsigned int left;
- unsigned char *base_info = git_mwindow_open(mwf, &w, idx->off, GIT_OID_RAWSZ, &left);
- if (base_info == NULL)
- return -1;
-
- git_oid_fromraw(&delta->u.base_oid, base_info);
- git_mwindow_close(&w);
idx->off += GIT_OID_RAWSZ;
} else {
- assert(type == GIT_OBJ_OFS_DELTA);
- delta->ofs = true;
- delta->u.base_off = get_delta_base(idx->pack, &w, &idx->off, type, entry_start);
+ git_off_t base_off;
+
+ base_off = get_delta_base(idx->pack, &w, &idx->off, type, entry_start);
git_mwindow_close(&w);
- if (delta->u.base_off < 0) {
- return (int)delta->u.base_off;
- }
+ if (base_off < 0)
+ return (int)base_off;
}
- error = packfile_unpack_compressed(&delta->delta_obj, idx->pack, &w, &idx->off, entry_size, type);
+ error = packfile_unpack_compressed(&obj, idx->pack, &w, &idx->off, entry_size, type);
if (error == GIT_ESHORTBUFFER) {
idx->off = entry_start;
return GIT_ESHORTBUFFER;
} else if (error < 0){
- puts("bad uncompressing");
- goto on_error;
+ return -1;
}
+ delta = git__calloc(1, sizeof(struct delta_info));
+ GITERR_CHECK_ALLOC(delta);
+ delta->delta_off = entry_start;
+
+ git__free(obj.data);
+
if (git_vector_insert(&idx->deltas, delta) < 0)
+ return -1;
+
+ return 0;
+}
+
+static int hash_and_save(git_indexer_stream *idx, git_rawobj *obj, git_off_t entry_start)
+{
+ int i;
+ git_oid oid;
+ void *packed;
+ size_t entry_size;
+ unsigned int left;
+ struct entry *entry;
+ git_mwindow *w = NULL;
+ git_mwindow_file *mwf = &idx->pack->mwf;
+ struct git_pack_entry *pentry;
+
+ entry = git__calloc(1, sizeof(*entry));
+ GITERR_CHECK_ALLOC(entry);
+
+ if (entry_start > UINT31_MAX) {
+ entry->offset = UINT32_MAX;
+ entry->offset_long = entry_start;
+ } else {
+ entry->offset = (uint32_t)entry_start;
+ }
+
+ /* FIXME: Parse the object instead of hashing it */
+ if (git_odb__hashobj(&oid, obj) < 0) {
+ giterr_set(GITERR_INVALID, "Failed to hash object");
+ return -1;
+ }
+
+ pentry = git__malloc(sizeof(struct git_pack_entry));
+ GITERR_CHECK_ALLOC(pentry);
+
+ git_oid_cpy(&pentry->sha1, &oid);
+ pentry->offset = entry_start;
+ if (git_vector_insert(&idx->pack->cache, pentry) < 0)
+ goto on_error;
+
+ git_oid_cpy(&entry->oid, &oid);
+ entry->crc = crc32(0L, Z_NULL, 0);
+
+ entry_size = (size_t)(idx->off - entry_start);
+ packed = git_mwindow_open(mwf, &w, entry_start, entry_size, &left);
+ if (packed == NULL)
+ goto on_error;
+
+ entry->crc = htonl(crc32(entry->crc, packed, (uInt)entry_size));
+ git_mwindow_close(&w);
+
+ /* Add the object to the list */
+ if (git_vector_insert(&idx->objects, entry) < 0)
goto on_error;
+ for (i = oid.id[0]; i < 256; ++i) {
+ idx->fanout[i]++;
+ }
+
return 0;
on_error:
- git__free(delta->delta_obj.data);
+ git__free(entry);
+ git__free(pentry);
+ git__free(obj->data);
return -1;
}
@@ -294,25 +344,7 @@ int git_indexer_stream_add(git_indexer_stream *idx, void *data, size_t size, git
git_mwindow_free_all(mwf);
while (processed < idx->nr_objects) {
git_rawobj obj;
- git_oid oid;
- struct git_pack_entry *pentry;
- git_mwindow *w = NULL;
- int i;
- unsigned int left;
git_off_t entry_start = idx->off;
- void *packed;
- size_t entry_size;
- struct entry *entry;
-
- entry = git__calloc(1, sizeof(*entry));
- GITERR_CHECK_ALLOC(entry);
-
- if (idx->off > UINT31_MAX) {
- entry->offset = UINT32_MAX;
- entry->offset_long = idx->off;
- } else {
- entry->offset = (uint32_t)idx->off;
- }
if (idx->pack->mwf.size <= idx->off + 20)
return 0;
@@ -334,49 +366,181 @@ int git_indexer_stream_add(git_indexer_stream *idx, void *data, size_t size, git
continue;
}
- /* FIXME: Parse the object instead of hashing it */
- if (git_odb__hashobj(&oid, &obj) < 0) {
- giterr_set(GITERR_INVALID, "Failed to hash object");
+ if (hash_and_save(idx, &obj, entry_start) < 0)
goto on_error;
- }
- pentry = git__malloc(sizeof(struct git_pack_entry));
- if (pentry == NULL)
- goto on_error;
+ git__free(obj.data);
- git_oid_cpy(&pentry->sha1, &oid);
- pentry->offset = entry_start;
- if (git_vector_insert(&idx->pack->cache, pentry) < 0)
- goto on_error;
+ stats->processed = ++processed;
+ }
- git_oid_cpy(&entry->oid, &oid);
- entry->crc = crc32(0L, Z_NULL, 0);
+ return 0;
- entry_size = (size_t)(idx->off - entry_start);
- packed = git_mwindow_open(mwf, &w, entry_start, entry_size, &left);
- if (packed == NULL)
- goto on_error;
+on_error:
+ git_mwindow_free_all(mwf);
+ return -1;
+}
- entry->crc = htonl(crc32(entry->crc, packed, (uInt)entry_size));
- git_mwindow_close(&w);
+static int index_path_stream(git_buf *path, git_indexer_stream *idx, const char *suffix)
+{
+ const char prefix[] = "pack-";
+ size_t slash = (size_t)path->size;
- /* Add the object to the list */
- if (git_vector_insert(&idx->objects, entry) < 0)
- goto on_error;
+ /* search backwards for '/' */
+ while (slash > 0 && path->ptr[slash - 1] != '/')
+ slash--;
- for (i = oid.id[0]; i < 256; ++i) {
- idx->fanout[i]++;
- }
+ if (git_buf_grow(path, slash + 1 + strlen(prefix) +
+ GIT_OID_HEXSZ + strlen(suffix) + 1) < 0)
+ return -1;
+
+ git_buf_truncate(path, slash);
+ git_buf_puts(path, prefix);
+ git_oid_fmt(path->ptr + path->size, &idx->hash);
+ path->size += GIT_OID_HEXSZ;
+ git_buf_puts(path, suffix);
+
+ return git_buf_oom(path) ? -1 : 0;
+}
+
+static int resolve_deltas(git_indexer_stream *idx, git_indexer_stats *stats)
+{
+ unsigned int i;
+ struct delta_info *delta;
+
+ git_vector_foreach(&idx->deltas, i, delta) {
+ git_rawobj obj;
+
+ idx->off = delta->delta_off;
+ if (git_packfile_unpack(&obj, idx->pack, &idx->off) < 0)
+ return -1;
+
+ if (hash_and_save(idx, &obj, delta->delta_off) < 0)
+ return -1;
git__free(obj.data);
+ stats->processed++;
+ }
- stats->processed = ++processed;
+ return 0;
+}
+
+int git_indexer_stream_finalize(git_indexer_stream *idx, git_indexer_stats *stats)
+{
+ git_mwindow *w = NULL;
+ unsigned int i, long_offsets = 0, left;
+ struct git_pack_idx_header hdr;
+ git_buf filename = GIT_BUF_INIT;
+ struct entry *entry;
+ void *packfile_hash;
+ git_oid file_hash;
+ SHA_CTX ctx;
+
+ if (idx->deltas.length > 0)
+ if (resolve_deltas(idx, stats) < 0)
+ return -1;
+
+ git_vector_sort(&idx->objects);
+
+ git_buf_sets(&filename, idx->pack->pack_name);
+ git_buf_truncate(&filename, filename.size - strlen("pack"));
+ git_buf_puts(&filename, "idx");
+ if (git_buf_oom(&filename))
+ return -1;
+
+ if (git_filebuf_open(&idx->index_file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS) < 0)
+ goto on_error;
+
+ /* Write out the header */
+ hdr.idx_signature = htonl(PACK_IDX_SIGNATURE);
+ hdr.idx_version = htonl(2);
+ git_filebuf_write(&idx->index_file, &hdr, sizeof(hdr));
+
+ /* Write out the fanout table */
+ for (i = 0; i < 256; ++i) {
+ uint32_t n = htonl(idx->fanout[i]);
+ git_filebuf_write(&idx->index_file, &n, sizeof(n));
+ }
+
+ /* Write out the object names (SHA-1 hashes) */
+ SHA1_Init(&ctx);
+ git_vector_foreach(&idx->objects, i, entry) {
+ git_filebuf_write(&idx->index_file, &entry->oid, sizeof(git_oid));
+ SHA1_Update(&ctx, &entry->oid, GIT_OID_RAWSZ);
+ }
+ SHA1_Final(idx->hash.id, &ctx);
+
+ /* Write out the CRC32 values */
+ git_vector_foreach(&idx->objects, i, entry) {
+ git_filebuf_write(&idx->index_file, &entry->crc, sizeof(uint32_t));
}
+ /* Write out the offsets */
+ git_vector_foreach(&idx->objects, i, entry) {
+ uint32_t n;
+
+ if (entry->offset == UINT32_MAX)
+ n = htonl(0x80000000 | long_offsets++);
+ else
+ n = htonl(entry->offset);
+
+ git_filebuf_write(&idx->index_file, &n, sizeof(uint32_t));
+ }
+
+ /* Write out the long offsets */
+ git_vector_foreach(&idx->objects, i, entry) {
+ uint32_t split[2];
+
+ if (entry->offset != UINT32_MAX)
+ continue;
+
+ split[0] = htonl(entry->offset_long >> 32);
+ split[1] = htonl(entry->offset_long & 0xffffffff);
+
+ git_filebuf_write(&idx->index_file, &split, sizeof(uint32_t) * 2);
+ }
+
+ /* Write out the packfile trailer */
+ packfile_hash = git_mwindow_open(&idx->pack->mwf, &w, idx->pack->mwf.size - GIT_OID_RAWSZ, GIT_OID_RAWSZ, &left);
+ if (packfile_hash == NULL) {
+ git_mwindow_close(&w);
+ goto on_error;
+ }
+
+ memcpy(&file_hash, packfile_hash, GIT_OID_RAWSZ);
+ git_mwindow_close(&w);
+
+ git_filebuf_write(&idx->index_file, &file_hash, sizeof(git_oid));
+
+ /* Write out the packfile trailer to the idx file as well */
+ if (git_filebuf_hash(&file_hash, &idx->index_file) < 0)
+ goto on_error;
+
+ git_filebuf_write(&idx->index_file, &file_hash, sizeof(git_oid));
+
+ /* Figure out what the final name should be */
+ if (index_path_stream(&filename, idx, ".idx") < 0)
+ goto on_error;
+
+ /* Commit file */
+ if (git_filebuf_commit_at(&idx->index_file, filename.ptr, GIT_PACK_FILE_MODE) < 0)
+ goto on_error;
+
+ git_mwindow_free_all(&idx->pack->mwf);
+
+ if (index_path_stream(&filename, idx, ".pack") < 0)
+ goto on_error;
+ /* And don't forget to rename the packfile to its new place. */
+ if (git_filebuf_commit_at(&idx->pack_file, filename.ptr, GIT_PACK_FILE_MODE) < 0)
+ return -1;
+
+ git_buf_free(&filename);
return 0;
on_error:
- git_mwindow_free_all(mwf);
+ git_mwindow_free_all(&idx->pack->mwf);
+ git_filebuf_cleanup(&idx->index_file);
+ git_buf_free(&filename);
return -1;
}