Skip to content

Commit fde12a1

Browse files
authoredDec 16, 2023
Merge pull request libgit2#6681 from libgit2/ethomson/index_pack
cli: add `index-pack` command
2 parents 460b336 + cba3469 commit fde12a1

File tree

5 files changed

+179
-2
lines changed

5 files changed

+179
-2
lines changed
 

‎src/cli/cmd.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ extern int cmd_clone(int argc, char **argv);
3030
extern int cmd_config(int argc, char **argv);
3131
extern int cmd_hash_object(int argc, char **argv);
3232
extern int cmd_help(int argc, char **argv);
33+
extern int cmd_index_pack(int argc, char **argv);
3334

3435
#endif /* CLI_cmd_h__ */

‎src/cli/cmd_index_pack.c

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

‎src/cli/main.c

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const cli_cmd_spec cli_cmds[] = {
3737
{ "config", cmd_config, "View or set configuration values " },
3838
{ "hash-object", cmd_hash_object, "Hash a raw object and product its object ID" },
3939
{ "help", cmd_help, "Display help information" },
40+
{ "index-pack", cmd_index_pack, "Create an index for a packfile" },
4041
{ NULL }
4142
};
4243

‎src/cli/progress.c

+51-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,21 @@ static int fetch_receiving(
242242
done ? ", done." : "");
243243
}
244244

245-
static int fetch_resolving(
245+
static int indexer_indexing(
246+
cli_progress *progress,
247+
const git_indexer_progress *stats)
248+
{
249+
bool done = (stats->received_objects == stats->total_objects);
250+
251+
return progress_printf(progress, false,
252+
"Indexing objects: %3d%% (%d/%d)%s\r",
253+
percent(stats->received_objects, stats->total_objects),
254+
stats->received_objects,
255+
stats->total_objects,
256+
done ? ", done." : "");
257+
}
258+
259+
static int indexer_resolving(
246260
cli_progress *progress,
247261
const git_indexer_progress *stats)
248262
{
@@ -283,7 +297,42 @@ int cli_progress_fetch_transfer(const git_indexer_progress *stats, void *payload
283297
/* fall through */
284298

285299
case CLI_PROGRESS_RESOLVING:
286-
error = fetch_resolving(progress, stats);
300+
error = indexer_resolving(progress, stats);
301+
break;
302+
303+
default:
304+
/* should not be reached */
305+
GIT_ASSERT(!"unexpected progress state");
306+
}
307+
308+
return error;
309+
}
310+
311+
int cli_progress_indexer(
312+
const git_indexer_progress *stats,
313+
void *payload)
314+
{
315+
cli_progress *progress = (cli_progress *)payload;
316+
int error = 0;
317+
318+
switch (progress->action) {
319+
case CLI_PROGRESS_NONE:
320+
progress->action = CLI_PROGRESS_INDEXING;
321+
/* fall through */
322+
323+
case CLI_PROGRESS_INDEXING:
324+
if ((error = indexer_indexing(progress, stats)) < 0)
325+
break;
326+
327+
if (stats->indexed_deltas == stats->total_deltas)
328+
break;
329+
330+
progress_complete(progress);
331+
progress->action = CLI_PROGRESS_RESOLVING;
332+
/* fall through */
333+
334+
case CLI_PROGRESS_RESOLVING:
335+
error = indexer_resolving(progress, stats);
287336
break;
288337

289338
default:

‎src/cli/progress.h

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
typedef enum {
2323
CLI_PROGRESS_NONE,
2424
CLI_PROGRESS_RECEIVING,
25+
CLI_PROGRESS_INDEXING,
2526
CLI_PROGRESS_RESOLVING,
2627
CLI_PROGRESS_CHECKING_OUT
2728
} cli_progress_t;
@@ -74,6 +75,17 @@ extern int cli_progress_fetch_transfer(
7475
const git_indexer_progress *stats,
7576
void *payload);
7677

78+
/**
79+
* Prints indexer progress to the console. Suitable for a
80+
* `progress_cb` callback for `git_indexer_options`.
81+
*
82+
* @param stats The indexer stats
83+
* @param payload A pointer to the cli_progress
84+
*/
85+
extern int cli_progress_indexer(
86+
const git_indexer_progress *stats,
87+
void *payload);
88+
7789
/**
7890
* Prints checkout progress to the console. Suitable for a
7991
* `progress_cb` callback for `git_checkout_options`.

0 commit comments

Comments
 (0)
Please sign in to comment.