]> git.proxmox.com Git - libgit2.git/blob - examples/network/index-pack.c
indexer: don't use '/objects/pack/' unconditionally
[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 "common.h"
6
7 // This could be run in the main loop whilst the application waits for
8 // the indexing to finish in a worker thread
9 int index_cb(const git_indexer_stats *stats, void *data)
10 {
11 printf("\rProcessing %d of %d", stats->processed, stats->total);
12 }
13
14 int index_pack(git_repository *repo, int argc, char **argv)
15 {
16 git_indexer_stream *idx;
17 git_indexer_stats stats = {0, 0};
18 int error, fd;
19 char hash[GIT_OID_HEXSZ + 1] = {0};
20 ssize_t read_bytes;
21 char buf[512];
22
23 if (argc < 2) {
24 fprintf(stderr, "I need a packfile\n");
25 return EXIT_FAILURE;
26 }
27
28 if (git_indexer_stream_new(&idx, ".") < 0) {
29 puts("bad idx");
30 return -1;
31 }
32
33 if ((fd = open(argv[1], 0)) < 0) {
34 perror("open");
35 return -1;
36 }
37
38 do {
39 read_bytes = read(fd, buf, sizeof(buf));
40 if (read_bytes < 0)
41 break;
42
43 if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0)
44 goto cleanup;
45
46 printf("\rIndexing %d of %d", stats.processed, stats.total);
47 } while (read_bytes > 0);
48
49 if (read_bytes < 0) {
50 error = -1;
51 perror("failed reading");
52 goto cleanup;
53 }
54
55 if ((error = git_indexer_stream_finalize(idx, &stats)) < 0)
56 goto cleanup;
57
58 printf("\rIndexing %d of %d\n", stats.processed, stats.total);
59
60 git_oid_fmt(hash, git_indexer_stream_hash(idx));
61 puts(hash);
62
63 cleanup:
64 close(fd);
65 git_indexer_stream_free(idx);
66 return error;
67 }
68
69 int index_pack_old(git_repository *repo, int argc, char **argv)
70 {
71 git_indexer *indexer;
72 git_indexer_stats stats;
73 int error;
74 char hash[GIT_OID_HEXSZ + 1] = {0};
75
76 if (argc < 2) {
77 fprintf(stderr, "I need a packfile\n");
78 return EXIT_FAILURE;
79 }
80
81 // Create a new indexer
82 error = git_indexer_new(&indexer, argv[1]);
83 if (error < 0)
84 return error;
85
86 // Index the packfile. This function can take a very long time and
87 // should be run in a worker thread.
88 error = git_indexer_run(indexer, &stats);
89 if (error < 0)
90 return error;
91
92 // Write the information out to an index file
93 error = git_indexer_write(indexer);
94
95 // Get the packfile's hash (which should become it's filename)
96 git_oid_fmt(hash, git_indexer_hash(indexer));
97 puts(hash);
98
99 git_indexer_free(indexer);
100
101 return 0;
102 }