diff options
author | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2013-04-16 19:46:41 +0400 |
commit | a50086d174658914d4d6462afbc83b02825b1f5b (patch) | |
tree | e8daa1c7bf678222cf351445179837bed7db3a72 /examples/network/index-pack.c | |
parent | 5b9fac39d8a76b9139667c26a63e6b3f204b3977 (diff) | |
parent | f124ebd457bfbf43de3516629aaba5a279636e04 (diff) |
Merge branch 'development'v0.18.0
Diffstat (limited to 'examples/network/index-pack.c')
-rw-r--r-- | examples/network/index-pack.c | 162 |
1 files changed, 74 insertions, 88 deletions
diff --git a/examples/network/index-pack.c b/examples/network/index-pack.c index 03f3ae37e..889305da8 100644 --- a/examples/network/index-pack.c +++ b/examples/network/index-pack.c @@ -2,101 +2,87 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#ifdef _WIN32 +# include <io.h> +# include <Windows.h> + +# define open _open +# define read _read +# define close _close + +#define ssize_t unsigned int +#else +# include <unistd.h> +#endif #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) +static int index_cb(const git_transfer_progress *stats, void *data) { - printf("\rProcessing %d of %d", stats->processed, stats->total); -} + (void)data; + printf("\rProcessing %d of %d", stats->indexed_objects, stats->total_objects); -int index_pack(git_repository *repo, int argc, char **argv) -{ - git_indexer_stream *idx; - git_indexer_stats stats = {0, 0}; - int error, fd; - char hash[GIT_OID_HEXSZ + 1] = {0}; - ssize_t read_bytes; - char buf[512]; - - if (argc < 2) { - fprintf(stderr, "I need a packfile\n"); - return EXIT_FAILURE; - } - - if (git_indexer_stream_new(&idx, ".git") < 0) { - puts("bad idx"); - return -1; - } - - if ((fd = open(argv[1], 0)) < 0) { - perror("open"); - return -1; - } - - do { - read_bytes = read(fd, buf, sizeof(buf)); - if (read_bytes < 0) - break; - - if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0) - goto cleanup; - - printf("\rIndexing %d of %d", stats.processed, stats.total); - } while (read_bytes > 0); - - if (read_bytes < 0) { - error = -1; - perror("failed reading"); - goto cleanup; - } - - if ((error = git_indexer_stream_finalize(idx, &stats)) < 0) - goto cleanup; - - printf("\rIndexing %d of %d\n", stats.processed, stats.total); - - git_oid_fmt(hash, git_indexer_stream_hash(idx)); - puts(hash); - -cleanup: - close(fd); - git_indexer_stream_free(idx); - return error; + return 0; } -int index_pack_old(git_repository *repo, int argc, char **argv) +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 < 0) - 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 < 0) - 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 0; + git_indexer_stream *idx; + git_transfer_progress stats = {0, 0}; + int error; + char hash[GIT_OID_HEXSZ + 1] = {0}; + int fd; + ssize_t read_bytes; + char buf[512]; + + (void)repo; + + if (argc < 2) { + fprintf(stderr, "usage: %s index-pack <packfile>\n", argv[-1]); + return EXIT_FAILURE; + } + + if (git_indexer_stream_new(&idx, ".", NULL, NULL) < 0) { + puts("bad idx"); + return -1; + } + + if ((fd = open(argv[1], 0)) < 0) { + perror("open"); + return -1; + } + + do { + read_bytes = read(fd, buf, sizeof(buf)); + if (read_bytes < 0) + break; + + if ((error = git_indexer_stream_add(idx, buf, read_bytes, &stats)) < 0) + goto cleanup; + + index_cb(&stats, NULL); + } while (read_bytes > 0); + + if (read_bytes < 0) { + error = -1; + perror("failed reading"); + goto cleanup; + } + + if ((error = git_indexer_stream_finalize(idx, &stats)) < 0) + goto cleanup; + + printf("\rIndexing %d of %d\n", stats.indexed_objects, stats.total_objects); + + git_oid_fmt(hash, git_indexer_stream_hash(idx)); + puts(hash); + + cleanup: + close(fd); + git_indexer_stream_free(idx); + return error; } |