Esempio n. 1
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;
}
Esempio n. 2
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;
}
Esempio n. 3
0
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);
	}
}