]> git.proxmox.com Git - libgit2.git/blob - examples/index-pack.c
Reupload to unstable
[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 char hash[GIT_OID_HEXSZ + 1] = {0};
21 int fd;
22 ssize_t read_bytes;
23 char buf[512];
24
25 (void)repo;
26
27 if (argc < 2) {
28 fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
29 return EXIT_FAILURE;
30 }
31
32 if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
33 puts("bad idx");
34 return -1;
35 }
36
37 if ((fd = open(argv[1], 0)) < 0) {
38 perror("open");
39 return -1;
40 }
41
42 do {
43 read_bytes = read(fd, buf, sizeof(buf));
44 if (read_bytes < 0)
45 break;
46
47 if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
48 goto cleanup;
49
50 index_cb(&stats, NULL);
51 } while (read_bytes > 0);
52
53 if (read_bytes < 0) {
54 error = -1;
55 perror("failed reading");
56 goto cleanup;
57 }
58
59 if ((error = git_indexer_commit(idx, &stats)) < 0)
60 goto cleanup;
61
62 printf("\rIndexing %u of %u\n", stats.indexed_objects, stats.total_objects);
63
64 git_oid_fmt(hash, git_indexer_hash(idx));
65 puts(hash);
66
67 cleanup:
68 close(fd);
69 git_indexer_free(idx);
70 return error;
71 }