]> git.proxmox.com Git - libgit2.git/blob - examples/network/index-pack.c
Fix example compilation
[libgit2.git] / examples / network / index-pack.c
1 #include <git2.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include "common.h"
10
11 // This could be run in the main loop whilst the application waits for
12 // the indexing to finish in a worker thread
13 static int index_cb(const git_indexer_stats *stats, void *data)
14 {
15 data = data;
16 printf("\rProcessing %d of %d", stats->processed, stats->total);
17
18 return 0;
19 }
20
21 int index_pack(git_repository *repo, int argc, char **argv)
22 {
23 git_indexer_stream *idx;
24 git_indexer_stats stats = {0, 0};
25 int error, fd;
26 char hash[GIT_OID_HEXSZ + 1] = {0};
27 ssize_t read_bytes;
28 char buf[512];
29
30 repo = repo;
31 if (argc < 2) {
32 fprintf(stderr, "I need a packfile\n");
33 return EXIT_FAILURE;
34 }
35
36 if (git_indexer_stream_new(&idx, ".", NULL, NULL) < 0) {
37 puts("bad idx");
38 return -1;
39 }
40
41 if ((fd = open(argv[1], 0)) < 0) {
42 perror("open");
43 return -1;
44 }
45
46 do {
47 read_bytes = read(fd, buf, sizeof(buf));
48 if (read_bytes < 0)
49 break;
50
51 if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0)
52 goto cleanup;
53
54 index_cb(&stats, NULL);
55 } while (read_bytes > 0);
56
57 if (read_bytes < 0) {
58 error = -1;
59 perror("failed reading");
60 goto cleanup;
61 }
62
63 if ((error = git_indexer_stream_finalize(idx, &stats)) < 0)
64 goto cleanup;
65
66 printf("\rIndexing %d of %d\n", stats.processed, stats.total);
67
68 git_oid_fmt(hash, git_indexer_stream_hash(idx));
69 puts(hash);
70
71 cleanup:
72 close(fd);
73 git_indexer_stream_free(idx);
74 return error;
75 }