void test_pack_indexer__corrupt_length(void) { git_indexer *idx = NULL; git_transfer_progress stats = { 0 }; git_repository *repo; git_odb *odb; git_oid id, should_id; cl_git_pass(git_repository_init(&repo, "thin.git", true)); cl_git_pass(git_repository_odb(&odb, repo)); /* Store the missing base into your ODB so the indexer can fix the pack */ cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJ_BLOB)); git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18"); cl_assert_equal_oid(&should_id, &id); cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL, NULL)); cl_git_pass(git_indexer_append( idx, corrupt_thin_pack, corrupt_thin_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); cl_assert(giterr_last() != NULL); cl_assert_equal_i(giterr_last()->klass, GITERR_ZLIB); git_indexer_free(idx); git_odb_free(odb); git_repository_free(repo); }
static int pack_backend__writepack(struct git_odb_writepack **out, git_odb_backend *_backend, git_odb *odb, git_transfer_progress_cb progress_cb, void *progress_payload) { struct pack_backend *backend; struct pack_writepack *writepack; assert(out && _backend); *out = NULL; backend = (struct pack_backend *)_backend; writepack = (pack_writepack*) git__calloc(1, sizeof(struct pack_writepack)); GITERR_CHECK_ALLOC(writepack); if (git_indexer_new(&writepack->indexer, backend->pack_folder, 0, odb, progress_cb, progress_payload) < 0) { git__free(writepack); return -1; } writepack->parent.backend = _backend; writepack->parent.append = pack_backend__writepack_append; writepack->parent.commit = pack_backend__writepack_commit; writepack->parent.free = pack_backend__writepack_free; *out = (git_odb_writepack *)writepack; return 0; }
int index_pack_old(git_repository *repo, int argc, char **argv) { git_indexer *indexer; git_indexer_stats stats; int error; char hash[GIT_OID_HEXSZ + 1] = {0}; if (argc < 2) { fprintf(stderr, "I need a packfile\n"); return EXIT_FAILURE; } // Create a new indexer error = git_indexer_new(&indexer, argv[1]); if (error < 0) return error; // Index the packfile. This function can take a very long time and // should be run in a worker thread. error = git_indexer_run(indexer, &stats); if (error < 0) return error; // Write the information out to an index file error = git_indexer_write(indexer); // Get the packfile's hash (which should become it's filename) git_oid_fmt(hash, git_indexer_hash(indexer)); puts(hash); git_indexer_free(indexer); return 0; }
int index_pack(git_repository *repo, int argc, char **argv) { git_indexer *idx; git_transfer_progress stats = {0, 0}; int error, fd; char hash[GIT_OID_HEXSZ + 1] = {0}; ssize_t read_bytes; char buf[512]; repo = repo; if (argc < 2) { fprintf(stderr, "I need a packfile\n"); return EXIT_FAILURE; } if (git_indexer_new(&idx, ".", 0, NULL, NULL, NULL) < 0) { puts("bad idx"); return -1; } if ((fd = open(argv[1], 0)) < 0) { perror("open"); return -1; } do { read_bytes = read(fd, buf, sizeof(buf)); if (read_bytes < 0) break; if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0) goto cleanup; index_cb(&stats, NULL); } while (read_bytes > 0); if (read_bytes < 0) { error = -1; perror("failed reading"); goto cleanup; } if ((error = git_indexer_commit(idx, &stats)) < 0) goto cleanup; printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects); git_oid_fmt(hash, git_indexer_hash(idx)); puts(hash); cleanup: close(fd); git_indexer_free(idx); return error; }
void test_pack_indexer__missing_trailer(void) { git_indexer *idx = 0; git_transfer_progress stats = { 0 }; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); cl_git_pass(git_indexer_append( idx, missing_trailer_pack, missing_trailer_pack_len, &stats)); cl_git_fail(git_indexer_commit(idx, &stats)); cl_assert(giterr_last() != NULL); cl_assert_equal_i(giterr_last()->klass, GITERR_INDEXER); git_indexer_free(idx); }
void test_pack_indexer__out_of_order(void) { git_indexer *idx; git_transfer_progress stats; cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); cl_git_pass(git_indexer_append(idx, out_of_order_pack, out_of_order_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 3); cl_assert_equal_i(stats.received_objects, 3); cl_assert_equal_i(stats.indexed_objects, 3); git_indexer_free(idx); }
void test_pack_indexer__no_tmp_files(void) { git_indexer *idx = NULL; git_buf path = GIT_BUF_INIT; git_buf first_tmp_file = GIT_BUF_INIT; /* Precondition: there are no temporary files. */ cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); git_buf_free(&path); cl_assert(git_buf_len(&first_tmp_file) == 0); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); git_indexer_free(idx); cl_git_pass(git_buf_sets(&path, clar_sandbox_path())); cl_git_pass(find_tmp_file_recurs(&first_tmp_file, &path)); git_buf_free(&path); cl_assert(git_buf_len(&first_tmp_file) == 0); git_buf_free(&first_tmp_file); }
void test_pack_indexer__fix_thin(void) { git_indexer *idx = NULL; git_transfer_progress stats = { 0 }; git_repository *repo; git_odb *odb; git_oid id, should_id; cl_git_pass(git_repository_init(&repo, "thin.git", true)); cl_git_pass(git_repository_odb(&odb, repo)); /* Store the missing base into your ODB so the indexer can fix the pack */ cl_git_pass(git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJ_BLOB)); git_oid_fromstr(&should_id, "e68fe8129b546b101aee9510c5328e7f21ca1d18"); cl_assert_equal_oid(&should_id, &id); cl_git_pass(git_indexer_new(&idx, ".", 0, odb, NULL, NULL)); cl_git_pass(git_indexer_append(idx, thin_pack, thin_pack_len, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 2); cl_assert_equal_i(stats.received_objects, 2); cl_assert_equal_i(stats.indexed_objects, 2); cl_assert_equal_i(stats.local_objects, 1); git_oid_fromstr(&should_id, "fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13"); cl_assert_equal_oid(&should_id, git_indexer_hash(idx)); git_indexer_free(idx); git_odb_free(odb); git_repository_free(repo); /* * The pack's name/hash only tells us what objects there are, * so we need to go through the packfile again in order to * figure out whether we calculated the trailer correctly. */ { unsigned char buffer[128]; int fd; ssize_t read; struct stat st; const char *name = "pack-fefdb2d740a3a6b6c03a0c7d6ce431c6d5810e13.pack"; fd = p_open(name, O_RDONLY); cl_assert(fd != -1); cl_git_pass(p_stat(name, &st)); cl_git_pass(git_indexer_new(&idx, ".", 0, NULL, NULL, NULL)); read = p_read(fd, buffer, sizeof(buffer)); cl_assert(read != -1); p_close(fd); cl_git_pass(git_indexer_append(idx, buffer, read, &stats)); cl_git_pass(git_indexer_commit(idx, &stats)); cl_assert_equal_i(stats.total_objects, 3); cl_assert_equal_i(stats.received_objects, 3); cl_assert_equal_i(stats.indexed_objects, 3); cl_assert_equal_i(stats.local_objects, 0); git_indexer_free(idx); } }