summaryrefslogtreecommitdiff
path: root/examples/network/index-pack.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <carlos@cmartin.tk>2011-10-03 00:33:13 +0200
committerCarlos Martín Nieto <carlos@cmartin.tk>2011-10-03 02:32:32 +0200
commit2401262778fa50cea30d1988cec45dcb82b50712 (patch)
tree80adb600f2bb02c941314dc725706827890e07c9 /examples/network/index-pack.c
parentcd19ca9584bd01925e05e94e7f3bddae6880acda (diff)
downloadlibgit2-2401262778fa50cea30d1988cec45dcb82b50712.tar.gz
examples: add ls-remote, fetch and index-pack examples
Signed-off-by: Carlos Martín Nieto <carlos@cmartin.tk>
Diffstat (limited to 'examples/network/index-pack.c')
-rw-r--r--examples/network/index-pack.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c
new file mode 100644
index 000000000..671035fb7
--- /dev/null
+++ b/examples/network/index-pack.c
@@ -0,0 +1,47 @@
+#include <git2.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "common.h"
+
+// This could be run in the main loop whilst the application waits for
+// the indexing to finish in a worker thread
+int index_cb(const git_indexer_stats *stats, void *data)
+{
+ printf("\rProcessing %d of %d", stats->processed, stats->total);
+}
+
+int index_pack(git_repository *repo, int argc, char **argv)
+{
+ git_indexer *indexer;
+ git_indexer_stats stats;
+ int error;
+ char hash[GIT_OID_HEXSZ + 1] = {0};
+
+ if (argc < 2) {
+ fprintf(stderr, "I need a packfile\n");
+ return EXIT_FAILURE;
+ }
+
+ // Create a new indexer
+ error = git_indexer_new(&indexer, argv[1]);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ // Index the packfile. This function can take a very long time and
+ // should be run in a worker thread.
+ error = git_indexer_run(indexer, &stats);
+ if (error < GIT_SUCCESS)
+ return error;
+
+ // Write the information out to an index file
+ error = git_indexer_write(indexer);
+
+ // Get the packfile's hash (which should become it's filename)
+ git_oid_fmt(hash, git_indexer_hash(indexer));
+ puts(hash);
+
+ git_indexer_free(indexer);
+
+ return GIT_SUCCESS;
+}