t5318: extract helpers to lib-commit-graph.sh

The graph_git_behavior helper is useful for testing that certain Git commands behave the same when using the commit-graph and when not using the commit-graph. Extract it to a new lib-commit-graph.sh file for use in new test scripts that will split out from t5318. While doing this extraction, also extract graph_read_expect and the logic for priming the test_oid_cache. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Mar 1, 2022 at 19:48 UTC 17925e0602983b1a1dbdd418c2fc2c70ca5faf5b
2 files changed +50 -48
t/lib-commit-graph.sh new
+48
@@ -0,0 +1,48 @@
1 +#!/bin/sh
2 +
3 +# Helper functions for testing commit-graphs.
4 +
5 +# Initialize OID cache with oid_version
6 +test_oid_cache <<-EOF
7 +oid_version sha1:1
8 +oid_version sha256:2
9 +EOF
10 +
11 +graph_git_two_modes() {
12 + git -c core.commitGraph=true $1 >output &&
13 + git -c core.commitGraph=false $1 >expect &&
14 + test_cmp expect output
15 +}
16 +
17 +graph_git_behavior() {
18 + MSG=$1
19 + DIR=$2
20 + BRANCH=$3
21 + COMPARE=$4
22 + test_expect_success "check normal git operations: $MSG" '
23 + cd "$TRASH_DIRECTORY/$DIR" &&
24 + graph_git_two_modes "log --oneline $BRANCH" &&
25 + graph_git_two_modes "log --topo-order $BRANCH" &&
26 + graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
27 + graph_git_two_modes "branch -vv" &&
28 + graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
29 + '
30 +}
31 +
32 +graph_read_expect() {
33 + OPTIONAL=""
34 + NUM_CHUNKS=3
35 + if test -n "$2"
36 + then
37 + OPTIONAL=" $2"
38 + NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
39 + fi
40 + cat >expect <<- EOF
41 + header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
42 + num_commits: $1
43 + chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
44 + options:
45 + EOF
46 + test-tool read-graph >output &&
47 + test_cmp expect output
48 +}
t/t5318-commit-graph.sh
+2 -48
@@ -29,12 +29,7 @@ test_expect_success 'setup full repo' '
29 cd "$TRASH_DIRECTORY/full" &&
30 git init &&
31 git config core.commitGraph true &&
32 - objdir=".git/objects" &&
33 -
34 - test_oid_cache <<-EOF
35 - oid_version sha1:1
36 - oid_version sha256:2
37 - EOF
32 + objdir=".git/objects"
33 '
34
35 test_expect_success POSIXPERM 'tweak umask for modebit tests' '
@@ -69,47 +64,10 @@ test_expect_success 'create commits and repack' '
64 git repack
65 '
66
72 -graph_git_two_modes() {
73 - git -c core.commitGraph=true $1 >output &&
74 - git -c core.commitGraph=false $1 >expect &&
75 - test_cmp expect output
76 -}
77 -
78 -graph_git_behavior() {
79 - MSG=$1
80 - DIR=$2
81 - BRANCH=$3
82 - COMPARE=$4
83 - test_expect_success "check normal git operations: $MSG" '
84 - cd "$TRASH_DIRECTORY/$DIR" &&
85 - graph_git_two_modes "log --oneline $BRANCH" &&
86 - graph_git_two_modes "log --topo-order $BRANCH" &&
87 - graph_git_two_modes "log --graph $COMPARE..$BRANCH" &&
88 - graph_git_two_modes "branch -vv" &&
89 - graph_git_two_modes "merge-base -a $BRANCH $COMPARE"
90 - '
91 -}
67 +. "$TEST_DIRECTORY"/lib-commit-graph.sh
68
69 graph_git_behavior 'no graph' full commits/3 commits/1
70
95 -graph_read_expect() {
96 - OPTIONAL=""
97 - NUM_CHUNKS=3
98 - if test ! -z "$2"
99 - then
100 - OPTIONAL=" $2"
101 - NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
102 - fi
103 - cat >expect <<- EOF
104 - header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
105 - num_commits: $1
106 - chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
107 - options:
108 - EOF
109 - test-tool read-graph >output &&
110 - test_cmp expect output
111 -}
112 -
71 test_expect_success 'exit with correct error on bad input to --stdin-commits' '
72 cd "$TRASH_DIRECTORY/full" &&
73 # invalid, non-hex OID
@@ -826,10 +784,6 @@ test_expect_success 'set up and verify repo with generation data overflow chunk'
784 objdir=".git/objects" &&
785 UNIX_EPOCH_ZERO="@0 +0000" &&
786 FUTURE_DATE="@2147483646 +0000" &&
829 - test_oid_cache <<-EOF &&
830 - oid_version sha1:1
831 - oid_version sha256:2
832 - EOF
787 cd "$TRASH_DIRECTORY" &&
788 mkdir repo &&
789 cd repo &&