t6601: add helper for testing path-walk API

Add some tests based on the current behavior, doing interesting checks for different sets of branches, ranges, and the --boundary option. This sets a baseline for the behavior and we can extend it as new options are introduced. Store and output a 'batch_nr' value so we can demonstrate that the paths are grouped together in a batch and not following some other ordering. This allows us to test the depth-first behavior of the path-walk API. However, we purposefully do not test the order of the objects in the batch, so the output is compared to the expected output through a sort. It is important to mention that the behavior of the API will change soon as we start to handle UNINTERESTING objects differently, but these tests will demonstrate the change in behavior. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Dec 20, 2024 at 16:21 UTC d190124f277952bd828f932d87e76deabedf0c83
6 files changed +209 -1
Documentation/technical/api-path-walk.txt
+2 -1
@@ -42,4 +42,5 @@ commits.
42 Examples
43 --------
44
45 -See example usages in future changes.
45 +See example usages in:
46 + `t/helper/test-path-walk.c`
Makefile
+1
@@ -822,6 +822,7 @@ TEST_BUILTINS_OBJS += test-parse-options.o
822 TEST_BUILTINS_OBJS += test-parse-pathspec-file.o
823 TEST_BUILTINS_OBJS += test-partial-clone.o
824 TEST_BUILTINS_OBJS += test-path-utils.o
825 +TEST_BUILTINS_OBJS += test-path-walk.o
826 TEST_BUILTINS_OBJS += test-pcre2-config.o
827 TEST_BUILTINS_OBJS += test-pkt-line.o
828 TEST_BUILTINS_OBJS += test-proc-receive.o
t/helper/test-path-walk.c new
+84
@@ -0,0 +1,84 @@
1 +#define USE_THE_REPOSITORY_VARIABLE
2 +
3 +#include "test-tool.h"
4 +#include "environment.h"
5 +#include "hex.h"
6 +#include "object-name.h"
7 +#include "object.h"
8 +#include "pretty.h"
9 +#include "revision.h"
10 +#include "setup.h"
11 +#include "parse-options.h"
12 +#include "path-walk.h"
13 +#include "oid-array.h"
14 +
15 +static const char * const path_walk_usage[] = {
16 + N_("test-tool path-walk <options> -- <revision-options>"),
17 + NULL
18 +};
19 +
20 +struct path_walk_test_data {
21 + uintmax_t batch_nr;
22 + uintmax_t tree_nr;
23 + uintmax_t blob_nr;
24 +};
25 +
26 +static int emit_block(const char *path, struct oid_array *oids,
27 + enum object_type type, void *data)
28 +{
29 + struct path_walk_test_data *tdata = data;
30 + const char *typestr;
31 +
32 + if (type == OBJ_TREE)
33 + tdata->tree_nr += oids->nr;
34 + else if (type == OBJ_BLOB)
35 + tdata->blob_nr += oids->nr;
36 + else
37 + BUG("we do not understand this type");
38 +
39 + typestr = type_name(type);
40 +
41 + for (size_t i = 0; i < oids->nr; i++)
42 + printf("%"PRIuMAX":%s:%s:%s\n",
43 + tdata->batch_nr, typestr, path,
44 + oid_to_hex(&oids->oid[i]));
45 +
46 + tdata->batch_nr++;
47 + return 0;
48 +}
49 +
50 +int cmd__path_walk(int argc, const char **argv)
51 +{
52 + int res;
53 + struct rev_info revs = REV_INFO_INIT;
54 + struct path_walk_info info = PATH_WALK_INFO_INIT;
55 + struct path_walk_test_data data = { 0 };
56 + struct option options[] = {
57 + OPT_END(),
58 + };
59 +
60 + setup_git_directory();
61 + revs.repo = the_repository;
62 +
63 + argc = parse_options(argc, argv, NULL,
64 + options, path_walk_usage,
65 + PARSE_OPT_KEEP_UNKNOWN_OPT | PARSE_OPT_KEEP_ARGV0);
66 +
67 + if (argc > 1)
68 + setup_revisions(argc, argv, &revs, NULL);
69 + else
70 + usage(path_walk_usage[0]);
71 +
72 + info.revs = &revs;
73 + info.path_fn = emit_block;
74 + info.path_fn_data = &data;
75 +
76 + res = walk_objects_by_path(&info);
77 +
78 + printf("trees:%" PRIuMAX "\n"
79 + "blobs:%" PRIuMAX "\n",
80 + data.tree_nr, data.blob_nr);
81 +
82 + release_revisions(&revs);
83 + return res;
84 +}
t/helper/test-tool.c
+1
@@ -52,6 +52,7 @@ static struct test_cmd cmds[] = {
52 { "parse-subcommand", cmd__parse_subcommand },
53 { "partial-clone", cmd__partial_clone },
54 { "path-utils", cmd__path_utils },
55 + { "path-walk", cmd__path_walk },
56 { "pcre2-config", cmd__pcre2_config },
57 { "pkt-line", cmd__pkt_line },
58 { "proc-receive", cmd__proc_receive },
t/helper/test-tool.h
+1
@@ -45,6 +45,7 @@ int cmd__parse_pathspec_file(int argc, const char** argv);
45 int cmd__parse_subcommand(int argc, const char **argv);
46 int cmd__partial_clone(int argc, const char **argv);
47 int cmd__path_utils(int argc, const char **argv);
48 +int cmd__path_walk(int argc, const char **argv);
49 int cmd__pcre2_config(int argc, const char **argv);
50 int cmd__pkt_line(int argc, const char **argv);
51 int cmd__proc_receive(int argc, const char **argv);
t/t6601-path-walk.sh new
+120
@@ -0,0 +1,120 @@
1 +#!/bin/sh
2 +
3 +TEST_PASSES_SANITIZE_LEAK=true
4 +
5 +test_description='direct path-walk API tests'
6 +
7 +. ./test-lib.sh
8 +
9 +test_expect_success 'setup test repository' '
10 + git checkout -b base &&
11 +
12 + mkdir left &&
13 + mkdir right &&
14 + echo a >a &&
15 + echo b >left/b &&
16 + echo c >right/c &&
17 + git add . &&
18 + git commit -m "first" &&
19 +
20 + echo d >right/d &&
21 + git add right &&
22 + git commit -m "second" &&
23 +
24 + echo bb >left/b &&
25 + git commit -a -m "third" &&
26 +
27 + git checkout -b topic HEAD~1 &&
28 + echo cc >right/c &&
29 + git commit -a -m "topic"
30 +'
31 +
32 +test_expect_success 'all' '
33 + test-tool path-walk -- --all >out &&
34 +
35 + cat >expect <<-EOF &&
36 + 0:tree::$(git rev-parse topic^{tree})
37 + 0:tree::$(git rev-parse base^{tree})
38 + 0:tree::$(git rev-parse base~1^{tree})
39 + 0:tree::$(git rev-parse base~2^{tree})
40 + 1:tree:right/:$(git rev-parse topic:right)
41 + 1:tree:right/:$(git rev-parse base~1:right)
42 + 1:tree:right/:$(git rev-parse base~2:right)
43 + 2:blob:right/d:$(git rev-parse base~1:right/d)
44 + 3:blob:right/c:$(git rev-parse base~2:right/c)
45 + 3:blob:right/c:$(git rev-parse topic:right/c)
46 + 4:tree:left/:$(git rev-parse base:left)
47 + 4:tree:left/:$(git rev-parse base~2:left)
48 + 5:blob:left/b:$(git rev-parse base~2:left/b)
49 + 5:blob:left/b:$(git rev-parse base:left/b)
50 + 6:blob:a:$(git rev-parse base~2:a)
51 + blobs:6
52 + trees:9
53 + EOF
54 +
55 + test_cmp_sorted expect out
56 +'
57 +
58 +test_expect_success 'topic only' '
59 + test-tool path-walk -- topic >out &&
60 +
61 + cat >expect <<-EOF &&
62 + 0:tree::$(git rev-parse topic^{tree})
63 + 0:tree::$(git rev-parse base~1^{tree})
64 + 0:tree::$(git rev-parse base~2^{tree})
65 + 1:tree:right/:$(git rev-parse topic:right)
66 + 1:tree:right/:$(git rev-parse base~1:right)
67 + 1:tree:right/:$(git rev-parse base~2:right)
68 + 2:blob:right/d:$(git rev-parse base~1:right/d)
69 + 3:blob:right/c:$(git rev-parse base~2:right/c)
70 + 3:blob:right/c:$(git rev-parse topic:right/c)
71 + 4:tree:left/:$(git rev-parse base~2:left)
72 + 5:blob:left/b:$(git rev-parse base~2:left/b)
73 + 6:blob:a:$(git rev-parse base~2:a)
74 + blobs:5
75 + trees:7
76 + EOF
77 +
78 + test_cmp_sorted expect out
79 +'
80 +
81 +test_expect_success 'topic, not base' '
82 + test-tool path-walk -- topic --not base >out &&
83 +
84 + cat >expect <<-EOF &&
85 + 0:tree::$(git rev-parse topic^{tree})
86 + 1:tree:right/:$(git rev-parse topic:right)
87 + 2:blob:right/d:$(git rev-parse topic:right/d)
88 + 3:blob:right/c:$(git rev-parse topic:right/c)
89 + 4:tree:left/:$(git rev-parse topic:left)
90 + 5:blob:left/b:$(git rev-parse topic:left/b)
91 + 6:blob:a:$(git rev-parse topic:a)
92 + blobs:4
93 + trees:3
94 + EOF
95 +
96 + test_cmp_sorted expect out
97 +'
98 +
99 +test_expect_success 'topic, not base, boundary' '
100 + test-tool path-walk -- --boundary topic --not base >out &&
101 +
102 + cat >expect <<-EOF &&
103 + 0:tree::$(git rev-parse topic^{tree})
104 + 0:tree::$(git rev-parse base~1^{tree})
105 + 1:tree:right/:$(git rev-parse topic:right)
106 + 1:tree:right/:$(git rev-parse base~1:right)
107 + 2:blob:right/d:$(git rev-parse base~1:right/d)
108 + 3:blob:right/c:$(git rev-parse base~1:right/c)
109 + 3:blob:right/c:$(git rev-parse topic:right/c)
110 + 4:tree:left/:$(git rev-parse base~1:left)
111 + 5:blob:left/b:$(git rev-parse base~1:left/b)
112 + 6:blob:a:$(git rev-parse base~1:a)
113 + blobs:5
114 + trees:5
115 + EOF
116 +
117 + test_cmp_sorted expect out
118 +'
119 +
120 +test_done