Skip to content

Git designed for AI agents.

gittest: the fan-out removal is armed for one repository

Federico Eliab35fc2759b18
diff --git a/internal/git/gittest/fanout_removal.go b/internal/git/gittest/fanout_removal.go
index cdbb3c4..f3bc559 100644
--- a/internal/git/gittest/fanout_removal.go
+++ b/internal/git/gittest/fanout_removal.go
@@ -16,13 +16,15 @@ import (
"github.com/stretchr/testify/require"
)
-// fanOutRemovalShim is an LD_PRELOAD library whose mkdir removes a loose object directory
-// (objects/xx) of a repository right after creating it, as housekeeping's git prune-packed or git
-// prune may at that moment: git's create_tmpfile then fails its single retry. Directories under any
-// other parent than one named "objects", such as those of a quarantine, are left alone.
+// fanOutRemovalShim is an LD_PRELOAD library whose mkdir removes a loose object directory (xx, two
+// hex digits) of the object directory GITTEST_FANOUT_REMOVAL_DIR right after creating it, as
+// housekeeping's git prune-packed or git prune may at that moment: git's create_tmpfile then fails
+// its single retry. Directories anywhere else, such as those of a quarantine, are left alone.
const fanOutRemovalShim = `#define _GNU_SOURCE
#include <ctype.h>
#include <dlfcn.h>
+#include <limits.h>
+#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -31,37 +33,80 @@ const fanOutRemovalShim = `#define _GNU_SOURCE
int mkdir(const char *path, mode_t mode) {
int (*real_mkdir)(const char *, mode_t) = dlsym(RTLD_NEXT, "mkdir");
int ret = real_mkdir(path, mode);
- size_t len = strlen(path);
- if (ret == 0 && len >= 10 && strncmp(path + len - 10, "objects/", 8) == 0 &&
- (len == 10 || path[len - 11] == '/') &&
- isxdigit((unsigned char)path[len - 2]) && isxdigit((unsigned char)path[len - 1]))
+ const char *target = getenv("GITTEST_FANOUT_REMOVAL_DIR");
+ const char *base = strrchr(path, '/');
+ char parent[PATH_MAX], resolved[PATH_MAX];
+
+ base = base ? base + 1 : path;
+ if (ret != 0 || !target || strlen(base) != 2 ||
+ !isxdigit((unsigned char)base[0]) || !isxdigit((unsigned char)base[1]))
+ return ret;
+ if (base == path) {
+ strcpy(parent, ".");
+ } else {
+ size_t len = base - 1 - path;
+ if (len >= sizeof(parent))
+ return ret;
+ memcpy(parent, path, len);
+ parent[len] = '\0';
+ }
+ if (realpath(parent[0] ? parent : "/", resolved) && strcmp(resolved, target) == 0)
rmdir(path);
return ret;
}
`
-// NewFanOutRemovingCommandFactory returns a command factory whose Git commands lose every loose
-// object directory they create in a repository's object directory right after creating it. A Git
-// command that writes a loose object straight into a repository fails with "unable to create
-// temporary file"; one that writes into a quarantine, which Migrate links into the repository,
-// does not. The library is compiled with cc, and checked to take effect before it is returned.
-func NewFanOutRemovingCommandFactory(tb testing.TB, ctx context.Context, cfg config.Cfg) *InterceptingCommandFactory {
+// NewFanOutRemovingCommandFactory returns a command factory whose Git commands, once armed for a
+// repository, lose every loose object directory they create in that repository's object directory
+// right after creating it. A Git command that writes a loose object straight into the repository
+// then fails with "unable to create temporary file"; one that writes into a quarantine, which
+// Migrate links into the repository, or that keeps a pack, does not.
+//
+// arm packs the repository at repoPath, removes its loose object directories as housekeeping does,
+// so that every loose object written afterwards needs its directory created, and arms the
+// commands for it. Before, the commands run as usual, so that a test sets up its repositories
+// through the server. The library is compiled with cc, and checked to take effect before it is
+// returned: without cc, or where the dynamic loader does not honour LD_PRELOAD (Linux does), the
+// test fails rather than pass without exercising anything.
+func NewFanOutRemovingCommandFactory(tb testing.TB, ctx context.Context, cfg config.Cfg) (factory *InterceptingCommandFactory, arm func(repoPath string)) {
tb.Helper()
dir := testhelper.TempDir(tb)
source := filepath.Join(dir, "fanout_removal.c")
library := filepath.Join(dir, "fanout_removal.so")
+ armed := filepath.Join(dir, "armed")
require.NoError(tb, os.WriteFile(source, []byte(fanOutRemovalShim), mode.File))
if out, err := exec.Command("cc", "-shared", "-fPIC", "-o", library, source, "-ldl").CombinedOutput(); err != nil {
require.FailNow(tb, "compiling the fan-out removal library", "%v: %s", err, out)
}
- factory := NewInterceptingCommandFactory(tb, ctx, cfg, func(execEnv gitcmd.ExecutionEnvironment) string {
- return fmt.Sprintf("#!/usr/bin/env bash\nLD_PRELOAD=%q exec %q \"$@\"\n", library, execEnv.BinaryPath)
+ arm = func(repoPath string) {
+ tb.Helper()
+
+ Exec(tb, cfg, "-C", repoPath, "repack", "-a", "-d")
+ Exec(tb, cfg, "-C", repoPath, "prune-packed")
+ fanOut, err := filepath.Glob(filepath.Join(repoPath, "objects", "[0-9a-f][0-9a-f]"))
+ require.NoError(tb, err)
+ require.Empty(tb, fanOut, "loose objects left: are they unreachable?")
+
+ objectDir, err := filepath.EvalSymlinks(filepath.Join(repoPath, "objects"))
+ require.NoError(tb, err)
+ require.NoError(tb, os.WriteFile(armed, []byte(objectDir), mode.File))
+ }
+
+ factory = NewInterceptingCommandFactory(tb, ctx, cfg, func(execEnv gitcmd.ExecutionEnvironment) string {
+ return fmt.Sprintf(`#!/usr/bin/env bash
+if test -e %[1]q
+then
+ export GITTEST_FANOUT_REMOVAL_DIR="$(cat %[1]q)" LD_PRELOAD=%[2]q${LD_PRELOAD:+:$LD_PRELOAD}
+fi
+exec %[3]q "$@"
+`, armed, library, execEnv.BinaryPath)
})
// The library must take effect, or a test using it would pass without exercising anything.
- repo, _ := CreateRepository(tb, ctx, cfg, CreateRepositoryConfig{SkipCreationViaService: true})
+ repo, repoPath := CreateRepository(tb, ctx, cfg, CreateRepositoryConfig{SkipCreationViaService: true})
+ arm(repoPath)
var stderr bytes.Buffer
cmd, err := factory.New(ctx, repo, gitcmd.Command{
Name: "hash-object",
@@ -70,6 +115,7 @@ func NewFanOutRemovingCommandFactory(tb testing.TB, ctx context.Context, cfg con
require.NoError(tb, err)
require.Error(tb, cmd.Wait(), "the fan-out removal library does not take effect")
require.Contains(tb, stderr.String(), "unable to create temporary file")
+ require.NoError(tb, os.Remove(armed))
- return factory
+ return factory, arm
}
diff --git a/internal/gitaly/service/operations/fanout_removal_test.go b/internal/gitaly/service/operations/fanout_removal_test.go
index 933db7a..7a48424 100644
--- a/internal/gitaly/service/operations/fanout_removal_test.go
+++ b/internal/gitaly/service/operations/fanout_removal_test.go
@@ -3,7 +3,6 @@ package operations
import (
"context"
"fmt"
- "path/filepath"
"testing"
"github.com/feder-cr/git-ai/internal/featureflag"
@@ -21,36 +20,27 @@ import (
// setupFanOutRemoval runs the operations service with Git commands that lose every loose object
// directory they create in a repository right after creating it, as housekeeping's git
-// prune-packed may at that moment (gittest.NewFanOutRemovingCommandFactory). A write RPC that
-// writes into a quarantine and migrates it succeeds; one that writes straight into the repository
-// fails.
-func setupFanOutRemoval(t *testing.T) (context.Context, config.Cfg, gitalypb.OperationServiceClient) {
+// prune-packed may at that moment (gittest.NewFanOutRemovingCommandFactory), once armed for a
+// repository. A write RPC that writes into a quarantine and migrates it succeeds; one that writes
+// straight into the repository fails.
+func setupFanOutRemoval(t *testing.T) (context.Context, config.Cfg, gitalypb.OperationServiceClient, func(repoPath string)) {
ctx := featureflag.OutgoingCtxWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)
cfg := testcfg.Build(t)
- factory := gittest.NewFanOutRemovingCommandFactory(t, ctx, cfg)
- return setupOperationsServiceWithCfg(t, ctx, cfg, testserver.WithGitCommandFactory(factory))
-}
-
-// packWithoutFanOut packs every object of the repository and removes the emptied loose object
-// directories, as housekeeping does, so that every object the RPC writes needs one created.
-func packWithoutFanOut(t *testing.T, cfg config.Cfg, repoPath string) {
- gittest.Exec(t, cfg, "-C", repoPath, "repack", "-a", "-d")
- gittest.Exec(t, cfg, "-C", repoPath, "prune-packed")
- fanOut, err := filepath.Glob(filepath.Join(repoPath, "objects", "[0-9a-f][0-9a-f]"))
- require.NoError(t, err)
- require.Empty(t, fanOut)
+ factory, arm := gittest.NewFanOutRemovingCommandFactory(t, ctx, cfg)
+ ctx, cfg, client := setupOperationsServiceWithCfg(t, ctx, cfg, testserver.WithGitCommandFactory(factory))
+ return ctx, cfg, client, arm
}
func TestUserApplyPatch_fanOutDirectoryRemoved(t *testing.T) {
t.Parallel()
- ctx, cfg, client := setupFanOutRemoval(t)
+ ctx, cfg, client, arm := setupFanOutRemoval(t)
repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
parentCommitID := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(
gittest.TreeEntry{Path: "file", Mode: "100644", Content: "change me\n"},
))
- packWithoutFanOut(t, cfg, repoPath)
+ arm(repoPath)
stream, err := client.UserApplyPatch(ctx)
require.NoError(t, err)
@@ -103,7 +93,7 @@ index 3742e48..e40a3b9 100644
func TestUserMergeToRef_fanOutDirectoryRemoved(t *testing.T) {
t.Parallel()
- ctx, cfg, client := setupFanOutRemoval(t)
+ ctx, cfg, client, arm := setupFanOutRemoval(t)
repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)
base := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries(
@@ -117,7 +107,7 @@ func TestUserMergeToRef_fanOutDirectoryRemoved(t *testing.T) {
gittest.TreeEntry{Path: "base", Mode: "100644", Content: "base\n"},
gittest.TreeEntry{Path: "theirs", Mode: "100644", Content: "theirs\n"},
))
- packWithoutFanOut(t, cfg, repoPath)
+ arm(repoPath)
response, err := client.UserMergeToRef(ctx, &gitalypb.UserMergeToRefRequest{
Repository: repoProto,
@@ -144,7 +134,7 @@ func TestUserLand_proposalsFanOutDirectoryRemoved(t *testing.T) {
ctx := testhelper.Context(t)
cfg := testcfg.Build(t)
- factory := gittest.NewFanOutRemovingCommandFactory(t, ctx, cfg)
+ factory, arm := gittest.NewFanOutRemovingCommandFactory(t, ctx, cfg)
dir, argv := resolverScript(t, "")
writeAnswer(t, dir, modelAnswer())
@@ -164,7 +154,7 @@ func TestUserLand_proposalsFanOutDirectoryRemoved(t *testing.T) {
testserver.WithGitCommandFactory(factory),
)
gittest.Exec(t, fx.cfg, "-C", fx.repoPath, "update-ref", "refs/heads/landed", fx.commit.String())
- packWithoutFanOut(t, fx.cfg, fx.repoPath)
+ arm(fx.repoPath)
_, err := fx.client.UserLand(fx.ctx, fx.request("Land"))
requireMergeConflict(t, err)