]> git.proxmox.com Git - libgit2.git/blob - examples/network/index-pack.c
New upstream version 0.28.4+dfsg.1
[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 #ifdef _WIN32
9 # include <io.h>
10 # include <Windows.h>
11
12 # define open _open
13 # define read _read
14 # define close _close
15
16 #define ssize_t unsigned int
17 #else
18 # include <unistd.h>
19 #endif
20 #include "common.h"
21
22 /*
23 * This could be run in the main loop whilst the application waits for
24 * the indexing to finish in a worker thread
25 */
26 static int index_cb(const git_transfer_progress *stats, void *data)
27 {
28 (void)data;
29 printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects);
30
31 return 0;
32 }
33
34 int index_pack(git_repository *repo, int argc, char **argv)
35 {
36 git_indexer *idx;
37 git_transfer_progress stats = {0, 0};
38 int error;
39 char hash[GIT_OID_HEXSZ + 1] = {0};
40 int fd;
41 ssize_t read_bytes;
42 char buf[512];
43
44 (void)repo;
45
46 if (argc < 2) {
47 fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]);
48 return EXIT_FAILURE;
49 }
50
51 if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
52 puts("bad idx");
53 return -1;
54 }
55
56 if ((fd = open(argv[1], 0)) < 0) {
57 perror("open");
58 return -1;
59 }
60
61 do {
62 read_bytes = read(fd, buf, sizeof(buf));
63 if (read_bytes < 0)
64 break;
65
66 if ((error = git_indexer_append(idx, buf, read_bytes, &stats)) < 0)
67 goto cleanup;
68
69 index_cb(&stats, NULL);
70 } while (read_bytes > 0);
71
72 if (read_bytes < 0) {
73 error = -1;
74 perror("failed reading");
75 goto cleanup;
76 }
77
78 if ((error = git_indexer_commit(idx, &stats)) < 0)
79 goto cleanup;
80
81 printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects);
82
83 git_oid_fmt(hash, git_indexer_hash(idx));
84 puts(hash);
85
86 cleanup:
87 close(fd);
88 git_indexer_free(idx);
89 return error;
90 }