Skip to content

Commit 489ce0c

Browse files
committed
test-tool: add the path-walk subcommand
This is currently primarily here for me to test out a couple of ideas or to investigate how this path walk API thing works. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent fee8f88 commit 489ce0c

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,7 @@ TEST_BUILTINS_OBJS += test-parse-options.o
822822
TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
823823
TEST_BUILTINS_OBJS += test-partial-clone.o
824824
TEST_BUILTINS_OBJS += test-path-utils.o
825+
TEST_BUILTINS_OBJS += test-path-walk.o
825826
TEST_BUILTINS_OBJS += test-pcre2-config.o
826827
TEST_BUILTINS_OBJS += test-pkt-line.o
827828
TEST_BUILTINS_OBJS += test-proc-receive.o

t/helper/test-path-walk.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#define USE_THE_REPOSITORY_VARIABLE
2+
3+
#include "test-tool.h"
4+
#include "git-compat-util.h"
5+
#include "parse-options.h"
6+
#include "setup.h"
7+
#include "revision.h"
8+
#include "path-walk.h"
9+
#include "oid-array.h"
10+
#include "hex.h"
11+
12+
static const char * const path_walk_usage[] = {
13+
N_("test-tool path-walk <options>"),
14+
NULL
15+
};
16+
17+
static int verbose;
18+
19+
static int callback_fn(const char *path,
20+
struct oid_array *list,
21+
enum object_type type,
22+
void *data)
23+
{
24+
printf("visiting path '%s' (count: %"PRIuMAX") of type '%s'\n",
25+
path, list->nr, type_name(type));
26+
if (verbose)
27+
for (size_t i = 0; i < list->nr; i++)
28+
printf("\t%s\n", oid_to_hex(list->oid + i));
29+
30+
return 0;
31+
}
32+
33+
int cmd__path_walk(int argc, const char **argv)
34+
{
35+
struct rev_info revs;
36+
struct path_walk_info info = PATH_WALK_INFO_INIT;
37+
struct option options[] = {
38+
OPT_BOOL(0, "tags", &info.tags, N_("walk tags")),
39+
OPT_BOOL(0, "commits", &info.commits, N_("walk commits")),
40+
OPT_BOOL(0, "trees", &info.trees, N_("walk trees")),
41+
OPT_BOOL(0, "blobs", &info.blobs, N_("walk blobs")),
42+
OPT__VERBOSE(&verbose, N_("verbose")),
43+
OPT_END(),
44+
};
45+
int ret = 0;
46+
47+
if (argc == 2 && !strcmp(argv[1], "-h"))
48+
usage_with_options(path_walk_usage, options);
49+
50+
argc = parse_options(argc, argv, NULL, options, path_walk_usage,
51+
PARSE_OPT_KEEP_ARGV0);
52+
53+
setup_git_directory();
54+
55+
repo_init_revisions(the_repository, &revs, "");
56+
setup_revisions(argc, argv, &revs, NULL);
57+
58+
info.revs = &revs;
59+
info.path_fn = callback_fn;
60+
61+
ret = walk_objects_by_path(&info);
62+
63+
reset_revision_walk();
64+
release_revisions(&revs);
65+
66+
return ret;
67+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static struct test_cmd cmds[] = {
5454
{ "parse-subcommand", cmd__parse_subcommand },
5555
{ "partial-clone", cmd__partial_clone },
5656
{ "path-utils", cmd__path_utils },
57+
{ "path-walk", cmd__path_walk },
5758
{ "pcre2-config", cmd__pcre2_config },
5859
{ "pkt-line", cmd__pkt_line },
5960
{ "proc-receive", cmd__proc_receive },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);
4747
int cmd__parse_subcommand(int argc, const char **argv);
4848
int cmd__partial_clone(int argc, const char **argv);
4949
int cmd__path_utils(int argc, const char **argv);
50+
int cmd__path_walk(int argc, const char **argv);
5051
int cmd__pcre2_config(int argc, const char **argv);
5152
int cmd__pkt_line(int argc, const char **argv);
5253
int cmd__proc_receive(int argc, const char **argv);

0 commit comments

Comments
 (0)