]> git.proxmox.com Git - libgit2.git/blob - examples/index-pack.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / examples / index-pack.c
1 #include "common.h"
2
3 /*
4 * This could be run in the main loop whilst the application waits for
5 * the indexing to finish in a worker thread
6 */
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 int fd;
21 ssize_t read_bytes;
22 char buf[512];
23
24 (void)repo;
25
26 if (argc < 2) {
27 fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
28 return EXIT_FAILURE;
29 }
30
31 if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
32 puts("bad idx");
33 return -1;
34 }
35
36 if ((fd = open(argv[1], 0)) < 0) {
37 perror("open");
38 return -1;
39 }
40
41 do {
42 read_bytes = read(fd, buf, sizeof(buf));
43 if (read_bytes < 0)
44 break;
45
46 if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
47 goto cleanup;
48
49 index_cb(&stats, NULL);
50 } while (read_bytes > 0);
51
52 if (read_bytes < 0) {
53 error = -1;
54 perror("failed reading");
55 goto cleanup;
56 }
57
58 if ((error = git_indexer_commit(idx, &stats)) < 0)
59 goto cleanup;
60
61 printf("\rIndexing %u of %u\n", stats.indexed_objects, stats.total_objects);
62
63 puts(git_indexer_name(idx));
64
65 cleanup:
66 close(fd);
67 git_indexer_free(idx);
68 return error;
69 }