index-pack.c 1.37 KB
Newer Older
1
#include "common.h"
2

3 4 5 6
/*
 * This could be run in the main loop whilst the application waits for
 * the indexing to finish in a worker thread
 */
7
static int index_cb(const git_indexer_progress *stats, void *data)
8
{
9
	(void)data;
10
	printf("\rProcessing %u of %u", stats->indexed_objects, stats->total_objects);
11 12

	return 0;
13 14
}

15
int lg2_index_pack(git_repository *repo, int argc, char **argv)
16
{
17
	git_indexer *idx;
18
	git_indexer_progress stats = {0, 0};
19
	int error;
20
	char hash[GIT_OID_HEXSZ + 1] = {0};
21
	int fd;
22 23 24
	ssize_t read_bytes;
	char buf[512];

25 26
	(void)repo;

27
	if (argc < 2) {
28
		fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
29 30 31
		return EXIT_FAILURE;
	}

32
	if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
33 34 35 36 37 38 39 40 41 42 43 44 45 46
		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;

47
		if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
48 49
			goto cleanup;

50
		index_cb(&stats, NULL);
51 52 53 54 55 56 57 58
	} while (read_bytes > 0);

	if (read_bytes < 0) {
		error = -1;
		perror("failed reading");
		goto cleanup;
	}

59
	if ((error = git_indexer_commit(idx, &stats)) < 0)
60 61
		goto cleanup;

62
	printf("\rIndexing %u of %u\n", stats.indexed_objects, stats.total_objects);
63

64
	git_oid_fmt(hash, git_indexer_hash(idx));
65 66 67 68
	puts(hash);

 cleanup:
	close(fd);
69
	git_indexer_free(idx);
70
	return error;
71
}