|
| 1 | +/* |
| 2 | + * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 | + * |
| 4 | + * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 | + * a Linking Exception. For full terms see the included COPYING file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <git2.h> |
| 9 | +#include "common.h" |
| 10 | +#include "cmd.h" |
| 11 | +#include "progress.h" |
| 12 | + |
| 13 | +#define COMMAND_NAME "index-pack" |
| 14 | + |
| 15 | +#define BUFFER_SIZE (1024 * 1024) |
| 16 | + |
| 17 | +static int show_help, verbose, read_stdin; |
| 18 | +static char *filename; |
| 19 | +static cli_progress progress = CLI_PROGRESS_INIT; |
| 20 | + |
| 21 | +static const cli_opt_spec opts[] = { |
| 22 | + { CLI_OPT_TYPE_SWITCH, "help", 0, &show_help, 1, |
| 23 | + CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, NULL, |
| 24 | + "display help about the " COMMAND_NAME " command" }, |
| 25 | + |
| 26 | + { CLI_OPT_TYPE_SWITCH, "verbose", 'v', &verbose, 1, |
| 27 | + CLI_OPT_USAGE_DEFAULT, NULL, "display progress output" }, |
| 28 | + |
| 29 | + { CLI_OPT_TYPE_LITERAL }, |
| 30 | + |
| 31 | + { CLI_OPT_TYPE_SWITCH, "stdin", 0, &read_stdin, 1, |
| 32 | + CLI_OPT_USAGE_REQUIRED, NULL, "read from stdin" }, |
| 33 | + { CLI_OPT_TYPE_ARG, "pack-file", 0, &filename, 0, |
| 34 | + CLI_OPT_USAGE_CHOICE, "pack-file", "packfile path" }, |
| 35 | + |
| 36 | + { 0 }, |
| 37 | +}; |
| 38 | + |
| 39 | +static void print_help(void) |
| 40 | +{ |
| 41 | + cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts); |
| 42 | + printf("\n"); |
| 43 | + |
| 44 | + printf("Indexes a packfile and writes the index to disk.\n"); |
| 45 | + printf("\n"); |
| 46 | + |
| 47 | + printf("Options:\n"); |
| 48 | + |
| 49 | + cli_opt_help_fprint(stdout, opts); |
| 50 | +} |
| 51 | + |
| 52 | +int cmd_index_pack(int argc, char **argv) |
| 53 | +{ |
| 54 | + cli_opt invalid_opt; |
| 55 | + git_indexer *idx = NULL; |
| 56 | + git_indexer_options idx_opts = GIT_INDEXER_OPTIONS_INIT; |
| 57 | + git_indexer_progress stats = {0}; |
| 58 | + char buf[BUFFER_SIZE]; |
| 59 | + ssize_t read_len; |
| 60 | + int fd, ret; |
| 61 | + |
| 62 | + if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU)) |
| 63 | + return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt); |
| 64 | + |
| 65 | + if (show_help) { |
| 66 | + print_help(); |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + if (verbose) { |
| 71 | + idx_opts.progress_cb = cli_progress_indexer; |
| 72 | + idx_opts.progress_cb_payload = &progress; |
| 73 | + } |
| 74 | + |
| 75 | + if (read_stdin) { |
| 76 | + fd = fileno(stdin); |
| 77 | + } else if ((fd = p_open(filename, O_RDONLY)) < 0) { |
| 78 | + ret = cli_error_git(); |
| 79 | + goto done; |
| 80 | + } |
| 81 | + |
| 82 | +#ifdef GIT_EXPERIMENTAL_SHA256 |
| 83 | + ret = git_indexer_new(&idx, ".", GIT_OID_SHA1, &idx_opts); |
| 84 | +#else |
| 85 | + ret = git_indexer_new(&idx, ".", 0, NULL, &idx_opts); |
| 86 | +#endif |
| 87 | + |
| 88 | + if (ret < 0) { |
| 89 | + ret = cli_error_git(); |
| 90 | + goto done; |
| 91 | + } |
| 92 | + |
| 93 | + while ((read_len = p_read(fd, buf, sizeof(buf))) > 0) { |
| 94 | + if (git_indexer_append(idx, buf, (size_t)read_len, &stats) < 0) { |
| 95 | + ret = cli_error_git(); |
| 96 | + goto done; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (!read_stdin) |
| 101 | + p_close(fd); |
| 102 | + |
| 103 | + if (git_indexer_commit(idx, &stats) < 0) { |
| 104 | + ret = cli_error_git(); |
| 105 | + goto done; |
| 106 | + } |
| 107 | + |
| 108 | + cli_progress_finish(&progress); |
| 109 | + |
| 110 | +done: |
| 111 | + cli_progress_dispose(&progress); |
| 112 | + git_indexer_free(idx); |
| 113 | + return ret; |
| 114 | +} |
0 commit comments