index-pack.c 1.31 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 20
	int error;
	int fd;
21 22 23
	ssize_t read_bytes;
	char buf[512];

24 25
	(void)repo;

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

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

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

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

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

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

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

63
	puts(git_indexer_name(idx));
64 65 66

 cleanup:
	close(fd);
67
	git_indexer_free(idx);
68
	return error;
69
}