1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package ermitegit_test
4
5import (
6 "context"
7 "os"
8 "os/exec"
9 "path/filepath"
10 "testing"
11
12 "github.com/stretchr/testify/require"
13
14 "bnema.dev/ermite/internal/adapters/gitcli"
15 "bnema.dev/ermite/internal/domain"
16)
17
18// EnvBenchRepo names a real repository (its .git directory) for the
19// comparison test and the benchmarks, which skip without it. It is never
20// modified: it is cloned.
21const EnvBenchRepo = "ERMITE_GIT_BENCH_REPO"
22
23// bigRepo is a bare clone of the repository of EnvBenchRepo, configured like
24// a repository ermite creates and maintained the way ermite does.
25type bigRepo struct {
26 name domain.RepoName
27 dir string
28}
29
30// cloneBench clones the repository of EnvBenchRepo into the data directory with
31// --no-local (a real pack transfer, so that the clone is packed as a push
32// would leave it), applies the settings of gitcli.Stage and runs
33// gitcli.Maintain. It skips the test when the variable is unset.
34func cloneBench(tb testing.TB, g *gitcli.Git, data string) bigRepo {
35 tb.Helper()
36 src := os.Getenv(EnvBenchRepo)
37 if src == "" {
38 tb.Skipf("%s is not set", EnvBenchRepo)
39 }
40 name, err := domain.ParseRepoName("bench")
41 require.NoError(tb, err)
42 dir := gitcli.RepoDir(data, name)
43 run := func(args ...string) {
44 tb.Helper()
45 cmd := exec.Command("git", args...)
46 cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "HOME=" + filepath.Join(data, "run", "git-home"), "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null"}
47 out, err := cmd.CombinedOutput()
48 require.NoError(tb, err, "git %v: %s", args, out)
49 }
50 run("clone", "--quiet", "--bare", "--no-local", src, dir)
51 for _, kv := range [][2]string{{"repack.writeBitmaps", "true"}, {"core.commitGraph", "true"}, {"commitGraph.changedPathsVersion", "2"}} {
52 run("-C", dir, "config", "--local", kv[0], kv[1])
53 }
54 run("-C", dir, "gc", "--quiet", "--prune=now")
55 require.NoError(tb, g.Maintain(context.Background(), name))
56 return bigRepo{name: name, dir: dir}
57}