File: index-pack.c

package info (click to toggle)
libgit2 1.9.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 60,764 kB
  • sloc: ansic: 203,205; javascript: 2,458; sh: 1,763; python: 384; perl: 99; php: 65; makefile: 33
file content (76 lines) | stat: -rw-r--r-- 1,444 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "common.h"

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

	return 0;
}

int lg2_index_pack(git_repository *repo, int argc, char **argv)
{
	git_indexer *idx;
	git_indexer_progress stats = {0, 0};
	int error;
	int fd;
	ssize_t read_bytes;
	char buf[512];

	(void)repo;

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

#ifdef GIT_EXPERIMENTAL_SHA256
	error = git_indexer_new(&idx, ".", NULL);
#else
	error = git_indexer_new(&idx, ".", 0, NULL, NULL);
#endif

	if (error < 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 %u of %u\n", stats.indexed_objects, stats.total_objects);

	puts(git_indexer_name(idx));

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