Git designed for AI agents.
operations: UserApplyPatch writes its objects into a quarantine
Federico Eliac3715fb2b48c
diff --git a/internal/git/gittest/fanout_removal.go b/internal/git/gittest/fanout_removal.gonew file mode 100644index 0000000..cdbb3c4--- /dev/null+++ b/internal/git/gittest/fanout_removal.go@@ -0,0 +1,75 @@+package gittest++import (+ "bytes"+ "context"+ "fmt"+ "os"+ "os/exec"+ "path/filepath"+ "testing"++ "github.com/feder-cr/git-ai/internal/git/gitcmd"+ "github.com/feder-cr/git-ai/internal/gitaly/config"+ "github.com/feder-cr/git-ai/internal/gitaly/storage/mode"+ "github.com/feder-cr/git-ai/internal/testhelper"+ "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.+const fanOutRemovalShim = `#define _GNU_SOURCE+#include <ctype.h>+#include <dlfcn.h>+#include <string.h>+#include <sys/stat.h>+#include <sys/types.h>+#include <unistd.h>++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]))+ 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 {+ tb.Helper()++ dir := testhelper.TempDir(tb)+ source := filepath.Join(dir, "fanout_removal.c")+ library := filepath.Join(dir, "fanout_removal.so")+ 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)+ })++ // The library must take effect, or a test using it would pass without exercising anything.+ repo, _ := CreateRepository(tb, ctx, cfg, CreateRepositoryConfig{SkipCreationViaService: true})+ var stderr bytes.Buffer+ cmd, err := factory.New(ctx, repo, gitcmd.Command{+ Name: "hash-object",+ Flags: []gitcmd.Option{gitcmd.Flag{Name: "-w"}, gitcmd.Flag{Name: "--stdin"}},+ }, gitcmd.WithStdin(bytes.NewReader([]byte("fan-out removal check"))), gitcmd.WithStderr(&stderr))+ 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")++ return factory+}diff --git a/internal/gitaly/service/operations/apply_patch.go b/internal/gitaly/service/operations/apply_patch.goindex 006b041..028c668 100644--- a/internal/gitaly/service/operations/apply_patch.go+++ b/internal/gitaly/service/operations/apply_patch.go@@ -102,7 +102,16 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyPreturn structerr.NewInvalidArgument("%w", err)}- patchedCommit, err := s.applyPatchesWithIndex(ctx, repo, parentCommitID, committerSignature, stream)+ // The patches are written into a quarantine, which the reference update migrates: a loose object+ // written straight into the repository would race housekeeping's removal of empty loose object+ // directories, which the migration survives.+ quarantineDir, quarantineRepo, cleanup, err := s.quarantinedRepo(ctx, header.GetRepository())+ if err != nil {+ return err+ }+ defer cleanup()++ patchedCommit, err := s.applyPatchesWithIndex(ctx, quarantineRepo, parentCommitID, committerSignature, stream)if err != nil {return err}@@ -131,7 +140,7 @@ func (s *Server) userApplyPatch(ctx context.Context, header *gitalypb.UserApplyP}}- if err := s.updateReferenceWithHooks(ctx, header.GetRepository(), header.GetUser(), nil, targetBranch, patchedCommit, currentCommit); err != nil {+ if err := s.updateReferenceWithHooks(ctx, header.GetRepository(), header.GetUser(), quarantineDir, targetBranch, patchedCommit, currentCommit); err != nil {return fmt.Errorf("update reference: %w", err)}diff --git a/internal/gitaly/service/operations/fanout_removal_test.go b/internal/gitaly/service/operations/fanout_removal_test.gonew file mode 100644index 0000000..9064eae--- /dev/null+++ b/internal/gitaly/service/operations/fanout_removal_test.go@@ -0,0 +1,101 @@+package operations++import (+ "context"+ "fmt"+ "path/filepath"+ "testing"++ "github.com/feder-cr/git-ai/internal/featureflag"+ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/git/localrepo"+ "github.com/feder-cr/git-ai/internal/gitaly/config"+ "github.com/feder-cr/git-ai/internal/testhelper"+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"+ "github.com/feder-cr/git-ai/internal/testhelper/testserver"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+ "github.com/stretchr/testify/require"+ "google.golang.org/protobuf/types/known/timestamppb"+)++// 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) {+ 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)+}++func TestUserApplyPatch_fanOutDirectoryRemoved(t *testing.T) {+ t.Parallel()++ ctx, cfg, client := 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)++ stream, err := client.UserApplyPatch(ctx)+ require.NoError(t, err)+ require.NoError(t, stream.Send(&gitalypb.UserApplyPatchRequest{+ UserApplyPatchRequestPayload: &gitalypb.UserApplyPatchRequest_Header_{+ Header: &gitalypb.UserApplyPatchRequest_Header{+ Repository: repoProto,+ User: gittest.TestUser,+ TargetBranch: []byte(git.DefaultBranch),+ Timestamp: ×tamppb.Timestamp{Seconds: 1234512345},+ },+ },+ }))+ require.NoError(t, stream.Send(&gitalypb.UserApplyPatchRequest{+ UserApplyPatchRequestPayload: &gitalypb.UserApplyPatchRequest_Patches{+ Patches: []byte(fmt.Sprintf(`From %s Mon Sep 17 00:00:00 2001+From: Patch User <[email protected]>+Date: Thu, 18 Oct 2018 13:40:35 +0200+Subject: [PATCH] A commit from a patch++---+ file | 2 +-+ 1 file changed, 1 insertion(+), 1 deletion(-)++diff --git a/file b/file+index 3742e48..e40a3b9 100644+--- a/file++++ b/file+@@ -1 +1 @@+-change me++changed+--+2.19.1+`, parentCommitID)),+ },+ }))++ response, err := stream.CloseAndRecv()+ require.NoError(t, err)++ repo := localrepo.NewTestRepo(t, cfg, repoProto)+ patched, err := repo.ReadCommit(ctx, git.Revision(git.DefaultBranch))+ require.NoError(t, err)+ require.Equal(t, response.GetBranchUpdate().GetCommitId(), patched.GetId())+ require.Equal(t, []string{parentCommitID.String()}, patched.GetParentIds())+ require.Equal(t, []byte("changed\n"), gittest.Exec(t, cfg, "-C", repoPath, "show", git.DefaultBranch+":file"))+ gittest.Exec(t, cfg, "-C", repoPath, "fsck", "--strict")+}