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

3 4 5
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
6 7 8 9 10 11 12 13 14 15 16 17
#ifdef _WIN32
# include <io.h>
# include <Windows.h>

# define open _open
# define read _read
# define close _close

#define ssize_t unsigned int
#else
# include <unistd.h>
#endif
18

19 20 21 22
/*
 * This could be run in the main loop whilst the application waits for
 * the indexing to finish in a worker thread
 */
23
static int index_cb(const git_indexer_progress *stats, void *data)
24
{
25
	(void)data;
Ben Straub committed
26
	printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects);
27 28

	return 0;
29 30
}

31
int lg2_index_pack(git_repository *repo, int argc, char **argv)
32
{
33
	git_indexer *idx;
34
	git_indexer_progress stats = {0, 0};
35
	int error;
36
	char hash[GIT_OID_HEXSZ + 1] = {0};
37
	int fd;
38 39 40
	ssize_t read_bytes;
	char buf[512];

41 42
	(void)repo;

43
	if (argc < 2) {
44
		fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
45 46 47
		return EXIT_FAILURE;
	}

48
	if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62
		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;

63
		if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
64 65
			goto cleanup;

66
		index_cb(&stats, NULL);
67 68 69 70 71 72 73 74
	} while (read_bytes > 0);

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

75
	if ((error = git_indexer_commit(idx, &stats)) < 0)
76 77
		goto cleanup;

Ben Straub committed
78
	printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects);
79

80
	git_oid_fmt(hash, git_indexer_hash(idx));
81 82 83 84
	puts(hash);

 cleanup:
	close(fd);
85
	git_indexer_free(idx);
86
	return error;
87
}