Skip to content

Commit 8f80180

Browse files
derrickstoleegitster
authored andcommitted
maintenance: test commit-graph auto condition
The auto condition for the commit-graph maintenance task walks refs looking for commits that are not in the commit-graph file. This was added in 4ddc79b (maintenance: add auto condition for commit-graph task, 2020-09-17) but was left untested. The initial goal of this change was to demonstrate the feature works properly by adding tests. However, there was an off-by-one error that caused the basic tests around maintenance.commit-graph.auto=1 to fail when it should work. The subtlety is that if a ref tip is not in the commit-graph, then we were not adding that to the total count. In the test, we see that we have only added one commit since our last commit-graph write, so the auto condition would say there is nothing to do. The fix is simple: add the check for the commit-graph position to see that the tip is not in the commit-graph file before starting our walk. Since this happens before adding to the DFS stack, we do not need to clear our (currently empty) commit list. This does add some extra complexity for the test, because we also want to verify that the walk along the parents actually does some work. This means we need to add at least two commits in a row without writing the commit-graph. However, we also need to make sure no additional refs are pointing to the middle of this list or else the for_each_ref() in should_write_commit_graph() might visit these commits as tips instead of doing a DFS walk. Hence, the last two commits are added with "git commit" instead of "test_commit". Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 25914c4 commit 8f80180

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

builtin/gc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,9 +737,15 @@ static int dfs_on_ref(const char *refname,
737737
commit = lookup_commit(the_repository, oid);
738738
if (!commit)
739739
return 0;
740-
if (parse_commit(commit))
740+
if (parse_commit(commit) ||
741+
commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
741742
return 0;
742743

744+
data->num_not_in_graph++;
745+
746+
if (data->num_not_in_graph >= data->limit)
747+
return 1;
748+
743749
commit_list_append(commit, &stack);
744750

745751
while (!result && stack) {

t/t7900-maintenance.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,35 @@ test_expect_success 'run --task=<task>' '
5252
test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
5353
'
5454

55+
test_expect_success 'commit-graph auto condition' '
56+
COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
57+
58+
GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
59+
git -c maintenance.commit-graph.auto=1 $COMMAND &&
60+
GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
61+
git -c maintenance.commit-graph.auto="-1" $COMMAND &&
62+
63+
test_commit first &&
64+
65+
GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
66+
git -c maintenance.commit-graph.auto=0 $COMMAND &&
67+
GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
68+
git -c maintenance.commit-graph.auto=1 $COMMAND &&
69+
70+
git commit --allow-empty -m "second" &&
71+
git commit --allow-empty -m "third" &&
72+
73+
GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
74+
git -c maintenance.commit-graph.auto=2 $COMMAND &&
75+
76+
COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
77+
test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
78+
test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
79+
test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
80+
test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
81+
test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
82+
'
83+
5584
test_expect_success 'run --task=bogus' '
5685
test_must_fail git maintenance run --task=bogus 2>err &&
5786
test_i18ngrep "is not a valid task" err

0 commit comments

Comments
 (0)