summaryrefslogtreecommitdiffstats
path: root/src/cli/cmd_index_pack.c
blob: 09685c5d4db7eb7425a3eab6d1134698daa3468f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include <git2.h>
#include "common.h"
#include "cmd.h"
#include "progress.h"

#define COMMAND_NAME "index-pack"

#define BUFFER_SIZE (1024 * 1024)

static int show_help, verbose, read_stdin;
static char *filename;
static cli_progress progress = CLI_PROGRESS_INIT;

static const cli_opt_spec opts[] = {
	{ CLI_OPT_TYPE_SWITCH,    "help",     0, &show_help,   1,
	  CLI_OPT_USAGE_HIDDEN | CLI_OPT_USAGE_STOP_PARSING, NULL,
	  "display help about the " COMMAND_NAME " command" },

	{ CLI_OPT_TYPE_SWITCH,    "verbose", 'v', &verbose,    1,
	  CLI_OPT_USAGE_DEFAULT,   NULL,    "display progress output" },

	{ CLI_OPT_TYPE_LITERAL },

	{ CLI_OPT_TYPE_SWITCH,    "stdin",    0,   &read_stdin, 1,
	  CLI_OPT_USAGE_REQUIRED,  NULL,     "read from stdin" },
	{ CLI_OPT_TYPE_ARG,       "pack-file", 0,  &filename, 0,
	  CLI_OPT_USAGE_CHOICE,   "pack-file", "packfile path" },

	{ 0 },
};

static void print_help(void)
{
	cli_opt_usage_fprint(stdout, PROGRAM_NAME, COMMAND_NAME, opts);
	printf("\n");

	printf("Indexes a packfile and writes the index to disk.\n");
	printf("\n");

	printf("Options:\n");

	cli_opt_help_fprint(stdout, opts);
}

int cmd_index_pack(int argc, char **argv)
{
	cli_opt invalid_opt;
	git_indexer *idx = NULL;
	git_indexer_options idx_opts = GIT_INDEXER_OPTIONS_INIT;
	git_indexer_progress stats = {0};
	char buf[BUFFER_SIZE];
	ssize_t read_len;
	int fd, ret;

	if (cli_opt_parse(&invalid_opt, opts, argv + 1, argc - 1, CLI_OPT_PARSE_GNU))
		return cli_opt_usage_error(COMMAND_NAME, opts, &invalid_opt);

	if (show_help) {
		print_help();
		return 0;
	}

	if (verbose) {
		idx_opts.progress_cb = cli_progress_indexer;
		idx_opts.progress_cb_payload = &progress;
	}

	if (read_stdin) {
		fd = fileno(stdin);
	} else if ((fd = p_open(filename, O_RDONLY)) < 0) {
		ret = cli_error_git();
		goto done;
	}

#ifdef GIT_EXPERIMENTAL_SHA256
	ret = git_indexer_new(&idx, ".", GIT_OID_SHA1, &idx_opts);
#else
	ret = git_indexer_new(&idx, ".", 0, NULL, &idx_opts);
#endif

	if (ret < 0) {
		ret = cli_error_git();
		goto done;
	}

	while ((read_len = p_read(fd, buf, sizeof(buf))) > 0) {
		if (git_indexer_append(idx, buf, (size_t)read_len, &stats) < 0) {
			ret = cli_error_git();
			goto done;
		}
	}

	if (git_indexer_commit(idx, &stats) < 0) {
		ret = cli_error_git();
		goto done;
	}

	cli_progress_finish(&progress);

done:
	if (!read_stdin && fd >= 0)
		p_close(fd);

	cli_progress_dispose(&progress);
	git_indexer_free(idx);
	return ret;
}