test-reach: create new test tool for ref_newer

As we prepare to change the behavior of the algorithms in commit-reach.c, create a new test-tool subcommand 'reach' to test these methods on interesting commit-graph shapes. To use the new test-tool, use 'test-tool reach <method>' and provide input to stdin that describes the inputs to the method. Currently, we only implement the ref_newer method, which requires two commits. Use lines "A:<committish>" and "B:<committish>" for the two inputs. We will expand this input later to accommodate methods that take lists of commits. The test t6600-test-reach.sh creates a repo whose commits form a two-dimensional grid. This grid makes it easy for us to determine reachability because commit-A-B can reach commit-X-Y if and only if A is at least X and B is at least Y. This helps create interesting test cases for each result of the methods in commit-reach.c. We test all methods in three different states of the commit-graph file: Non-existent (no generation numbers), fully computed, and mixed (some commits have generation numbers and others do not). Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 20, 2018 at 16:33 UTC ab176ac4ae9456a97ff4904c0222c6a0fc63a130
5 files changed +152
Makefile
+1
@@ -716,6 +716,7 @@ TEST_BUILTINS_OBJS += test-mktemp.o
716 TEST_BUILTINS_OBJS += test-online-cpus.o
717 TEST_BUILTINS_OBJS += test-path-utils.o
718 TEST_BUILTINS_OBJS += test-prio-queue.o
719 +TEST_BUILTINS_OBJS += test-reach.o
720 TEST_BUILTINS_OBJS += test-read-cache.o
721 TEST_BUILTINS_OBJS += test-ref-store.o
722 TEST_BUILTINS_OBJS += test-regex.o
t/helper/test-reach.c new
+63
@@ -0,0 +1,63 @@
1 +#include "test-tool.h"
2 +#include "cache.h"
3 +#include "commit.h"
4 +#include "commit-reach.h"
5 +#include "config.h"
6 +#include "parse-options.h"
7 +#include "tag.h"
8 +
9 +int cmd__reach(int ac, const char **av)
10 +{
11 + struct object_id oid_A, oid_B;
12 + struct strbuf buf = STRBUF_INIT;
13 + struct repository *r = the_repository;
14 +
15 + setup_git_directory();
16 +
17 + if (ac < 2)
18 + exit(1);
19 +
20 +
21 + while (strbuf_getline(&buf, stdin) != EOF) {
22 + struct object_id oid;
23 + struct object *o;
24 + struct commit *c;
25 + if (buf.len < 3)
26 + continue;
27 +
28 + if (get_oid_committish(buf.buf + 2, &oid))
29 + die("failed to resolve %s", buf.buf + 2);
30 +
31 + o = parse_object(r, &oid);
32 + o = deref_tag_noverify(o);
33 +
34 + if (!o)
35 + die("failed to load commit for input %s resulting in oid %s\n",
36 + buf.buf, oid_to_hex(&oid));
37 +
38 + c = object_as_type(r, o, OBJ_COMMIT, 0);
39 +
40 + if (!c)
41 + die("failed to load commit for input %s resulting in oid %s\n",
42 + buf.buf, oid_to_hex(&oid));
43 +
44 + switch (buf.buf[0]) {
45 + case 'A':
46 + oidcpy(&oid_A, &oid);
47 + break;
48 +
49 + case 'B':
50 + oidcpy(&oid_B, &oid);
51 + break;
52 +
53 + default:
54 + die("unexpected start of line: %c", buf.buf[0]);
55 + }
56 + }
57 + strbuf_release(&buf);
58 +
59 + if (!strcmp(av[1], "ref_newer"))
60 + printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
61 +
62 + exit(0);
63 +}
t/helper/test-tool.c
+1
@@ -26,6 +26,7 @@ static struct test_cmd cmds[] = {
26 { "online-cpus", cmd__online_cpus },
27 { "path-utils", cmd__path_utils },
28 { "prio-queue", cmd__prio_queue },
29 + { "reach", cmd__reach },
30 { "read-cache", cmd__read_cache },
31 { "ref-store", cmd__ref_store },
32 { "regex", cmd__regex },
t/helper/test-tool.h
+1
@@ -20,6 +20,7 @@ int cmd__mktemp(int argc, const char **argv);
20 int cmd__online_cpus(int argc, const char **argv);
21 int cmd__path_utils(int argc, const char **argv);
22 int cmd__prio_queue(int argc, const char **argv);
23 +int cmd__reach(int argc, const char **argv);
24 int cmd__read_cache(int argc, const char **argv);
25 int cmd__ref_store(int argc, const char **argv);
26 int cmd__regex(int argc, const char **argv);
t/t6600-test-reach.sh new
+86
@@ -0,0 +1,86 @@
1 +#!/bin/sh
2 +
3 +test_description='basic commit reachability tests'
4 +
5 +. ./test-lib.sh
6 +
7 +# Construct a grid-like commit graph with points (x,y)
8 +# with 1 <= x <= 10, 1 <= y <= 10, where (x,y) has
9 +# parents (x-1, y) and (x, y-1), keeping in mind that
10 +# we drop a parent if a coordinate is nonpositive.
11 +#
12 +# (10,10)
13 +# / \
14 +# (10,9) (9,10)
15 +# / \ / \
16 +# (10,8) (9,9) (8,10)
17 +# / \ / \ / \
18 +# ( continued...)
19 +# \ / \ / \ /
20 +# (3,1) (2,2) (1,3)
21 +# \ / \ /
22 +# (2,1) (2,1)
23 +# \ /
24 +# (1,1)
25 +#
26 +# We use branch 'commit-x-y' to refer to (x,y).
27 +# This grid allows interesting reachability and
28 +# non-reachability queries: (x,y) can reach (x',y')
29 +# if and only if x' <= x and y' <= y.
30 +test_expect_success 'setup' '
31 + for i in $(test_seq 1 10)
32 + do
33 + test_commit "1-$i" &&
34 + git branch -f commit-1-$i
35 + done &&
36 + for j in $(test_seq 1 9)
37 + do
38 + git reset --hard commit-$j-1 &&
39 + x=$(($j + 1)) &&
40 + test_commit "$x-1" &&
41 + git branch -f commit-$x-1 &&
42 +
43 + for i in $(test_seq 2 10)
44 + do
45 + git merge commit-$j-$i -m "$x-$i" &&
46 + git branch -f commit-$x-$i
47 + done
48 + done &&
49 + git commit-graph write --reachable &&
50 + mv .git/objects/info/commit-graph commit-graph-full &&
51 + git show-ref -s commit-5-5 | git commit-graph write --stdin-commits &&
52 + mv .git/objects/info/commit-graph commit-graph-half &&
53 + git config core.commitGraph true
54 +'
55 +
56 +test_three_modes () {
57 + test_when_finished rm -rf .git/objects/info/commit-graph &&
58 + test-tool reach $1 <input >actual &&
59 + test_cmp expect actual &&
60 + cp commit-graph-full .git/objects/info/commit-graph &&
61 + test-tool reach $1 <input >actual &&
62 + test_cmp expect actual &&
63 + cp commit-graph-half .git/objects/info/commit-graph &&
64 + test-tool reach $1 <input >actual &&
65 + test_cmp expect actual
66 +}
67 +
68 +test_expect_success 'ref_newer:miss' '
69 + cat >input <<-\EOF &&
70 + A:commit-5-7
71 + B:commit-4-9
72 + EOF
73 + echo "ref_newer(A,B):0" >expect &&
74 + test_three_modes ref_newer
75 +'
76 +
77 +test_expect_success 'ref_newer:hit' '
78 + cat >input <<-\EOF &&
79 + A:commit-5-7
80 + B:commit-2-3
81 + EOF
82 + echo "ref_newer(A,B):1" >expect &&
83 + test_three_modes ref_newer
84 +'
85 +
86 +test_done