Git designed for AI agents.
localrepo: let callers run the merge and commit helpers without object fsync
Federico Elia25def5878039
diff --git a/internal/git/localrepo/commit_writer.go b/internal/git/localrepo/commit_writer.goindex c0155ab..b2fe523 100644--- a/internal/git/localrepo/commit_writer.go+++ b/internal/git/localrepo/commit_writer.go@@ -55,6 +55,8 @@ type CommitWriter struct {// ctx is the context the writer was created with. Every hash-object process runs under it,// including processes that replace a process that died, so that all of them end with it.ctx context.Context+ // gitOpts are the options of every hash-object process, from the HelperOptions of the writer.+ gitOpts []gitcmd.CmdOpt// process is the running hash-object process, or nil if it must be started before the next// write because the previous one failed.@@ -80,7 +82,7 @@ type commitWriterProcess struct {// NewCommitWriter starts the long-lived process used to write commit objects. The process, and any// process that replaces it, is terminated when ctx is done or when Close is called.-func (repo *Repo) NewCommitWriter(ctx context.Context) (*CommitWriter, error) {+func (repo *Repo) NewCommitWriter(ctx context.Context, opts ...HelperOption) (*CommitWriter, error) {objectHash, err := repo.ObjectHash(ctx)if err != nil {return nil, fmt.Errorf("detecting object hash: %w", err)@@ -96,6 +98,7 @@ func (repo *Repo) NewCommitWriter(ctx context.Context) (*CommitWriter, error) {objectHash: objectHash,tempDir: tempDir,ctx: ctx,+ gitOpts: resolveHelperOptions(opts),}if err := writer.startProcess(); err != nil {@@ -130,8 +133,10 @@ func (w *CommitWriter) startProcess() error {gitcmd.Flag{Name: "--stdin-paths"},},},- gitcmd.WithSetupStdin(),- gitcmd.WithSetupStdout(),+ append([]gitcmd.CmdOpt{+ gitcmd.WithSetupStdin(),+ gitcmd.WithSetupStdout(),+ }, w.gitOpts...)...,)if err != nil {cancel()diff --git a/internal/git/localrepo/merge_tree_stream.go b/internal/git/localrepo/merge_tree_stream.goindex 5508511..34e5c04 100644--- a/internal/git/localrepo/merge_tree_stream.go+++ b/internal/git/localrepo/merge_tree_stream.go@@ -54,6 +54,8 @@ type MergeTreeStream struct {inputs mergeTreeStreamInputs// starts counts the processes the stream started.starts int+ // gitOpts are the options of every process of the stream, from its HelperOptions.+ gitOpts []gitcmd.CmdOptcmd *command.Commandreader *bufio.Reader@@ -74,7 +76,7 @@ type mergeTreeStreamInputs struct {// NewMergeTreeStream starts a `git merge-tree --stdin` process for the repository.// The process lives until Close is called or ctx is done.-func (repo *Repo) NewMergeTreeStream(ctx context.Context) (*MergeTreeStream, error) {+func (repo *Repo) NewMergeTreeStream(ctx context.Context, opts ...HelperOption) (*MergeTreeStream, error) {objectHash, err := repo.ObjectHash(ctx)if err != nil {return nil, structerr.NewInternal("getting object hash %w", err)@@ -90,6 +92,7 @@ func (repo *Repo) NewMergeTreeStream(ctx context.Context) (*MergeTreeStream, errrepoPath: repoPath,objectHash: objectHash,ctx: ctx,+ gitOpts: resolveHelperOptions(opts),}// The inputs are read before the process starts: a change after this point is either@@ -153,9 +156,11 @@ func (s *MergeTreeStream) start(inputs mergeTreeStreamInputs) error {gitcmd.Flag{Name: "--allow-unrelated-histories"},},},- gitcmd.WithSetupStdin(),- gitcmd.WithSetupStdout(),- gitcmd.WithStderr(stderr),+ append([]gitcmd.CmdOpt{+ gitcmd.WithSetupStdin(),+ gitcmd.WithSetupStdout(),+ gitcmd.WithStderr(stderr),+ }, s.gitOpts...)...,)if err != nil {cancel()@@ -353,3 +358,35 @@ func (s *MergeTreeStream) readField() (string, error) {return field[:len(field)-1], nil}++// HelperOption configures the long-lived git processes of a MergeTreeStream or a CommitWriter.+type HelperOption func(*helperConfig)++type helperConfig struct {+ gitOpts []gitcmd.CmdOpt+}++// WithoutObjectFsync starts the helper processes with core.fsync=none. A MergeTreeStream and a+// CommitWriter write nothing but objects, so the option only drops the fsync of the objects they+// write. The caller must make those objects durable before any reference points to them: the batch+// stepper of UserLand packs every loose object of its quarantine into one fsynced pack, and the+// quarantine migration fsyncs whatever else it moves (see doc/graft/PLAN8.md, section 3.2).+//+// The value replaces the core.fsync of the server for the helper. It cannot be derived from it: git+// does not subtract a component from a value set earlier (a later "-loose-object" replaces the+// whole value), and the helpers write no reference, index or derived metadata that the other+// components would cover.+func WithoutObjectFsync() HelperOption {+ return func(cfg *helperConfig) {+ cfg.gitOpts = append(cfg.gitOpts, gitcmd.WithConfig(gitcmd.ConfigPair{Key: "core.fsync", Value: "none"}))+ }+}++// resolveHelperOptions returns the command options of the helper processes configured by opts.+func resolveHelperOptions(opts []HelperOption) []gitcmd.CmdOpt {+ var cfg helperConfig+ for _, opt := range opts {+ opt(&cfg)+ }+ return cfg.gitOpts+}diff --git a/internal/git/localrepo/merge_tree_stream_fsync_test.go b/internal/git/localrepo/merge_tree_stream_fsync_test.gonew file mode 100644index 0000000..8b7b5b9--- /dev/null+++ b/internal/git/localrepo/merge_tree_stream_fsync_test.go@@ -0,0 +1,118 @@+package localrepo++import (+ "strings"+ "testing"+ "time"++ "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/testhelper"+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"+ "github.com/stretchr/testify/require"+)++// effectiveConfig returns the value that git uses for key from the -c switches of args: the last+// one wins.+func effectiveConfig(args []string, key string) (string, bool) {+ value, found := "", false+ for i := 0; i+1 < len(args); i++ {+ if args[i] != "-c" {+ continue+ }+ if v, ok := strings.CutPrefix(args[i+1], key+"="); ok {+ value, found = v, true+ }+ }+ return value, found+}++func TestHelpersWithoutObjectFsync(t *testing.T) {+ t.Parallel()++ cfg := testcfg.Build(t)+ ctx := featureflag.ContextWithFeatureFlag(testhelper.Context(t), featureflag.GPGSigning, false)++ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{+ SkipCreationViaService: true,+ })+ repo := NewTestRepo(t, cfg, repoProto)++ base := gittest.WriteCommit(t, cfg, repoPath, gittest.WithTreeEntries(+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "base\n"},+ ))+ ours := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base), gittest.WithTreeEntries(+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "base\n"},+ gittest.TreeEntry{Mode: "100644", Path: "ours", Content: "ours\n"},+ ))+ theirs := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base), gittest.WithTreeEntries(+ gittest.TreeEntry{Mode: "100644", Path: "a", Content: "base\n"},+ gittest.TreeEntry{Mode: "100644", Path: "theirs", Content: "theirs\n"},+ ))++ t.Run("merge-tree stream", func(t *testing.T) {+ t.Parallel()++ synced, err := repo.NewMergeTreeStream(ctx)+ require.NoError(t, err)+ defer func() { require.NoError(t, synced.Close()) }()++ unsynced, err := repo.NewMergeTreeStream(ctx, WithoutObjectFsync())+ require.NoError(t, err)+ defer func() { require.NoError(t, unsynced.Close()) }()++ // The server's configuration fsyncs objects; the option replaces it for this helper only.+ value, found := effectiveConfig(synced.cmd.Args(), "core.fsync")+ require.True(t, found)+ require.Contains(t, value, "objects")++ value, found = effectiveConfig(unsynced.cmd.Args(), "core.fsync")+ require.True(t, found)+ require.Equal(t, "none", value)++ // Only durability changes: the merge is the same.+ wantOID, wantErr := synced.Merge(ctx, base, ours, theirs)+ gotOID, gotErr := unsynced.Merge(ctx, base, ours, theirs)+ requireSameMergeResult(t, wantOID, wantErr, gotOID, gotErr)+ })++ t.Run("commit writer", func(t *testing.T) {+ t.Parallel()++ synced, err := repo.NewCommitWriter(ctx)+ require.NoError(t, err)+ defer func() { require.NoError(t, synced.Close()) }()++ unsynced, err := repo.NewCommitWriter(ctx, WithoutObjectFsync())+ require.NoError(t, err)+ defer func() { require.NoError(t, unsynced.Close()) }()++ value, found := effectiveConfig(synced.process.cmd.Args(), "core.fsync")+ require.True(t, found)+ require.Contains(t, value, "objects")++ value, found = effectiveConfig(unsynced.process.cmd.Args(), "core.fsync")+ require.True(t, found)+ require.Equal(t, "none", value)++ when := time.Date(2026, time.October, 18, 12, 0, 0, 0, time.UTC)+ writeCfg := WriteCommitConfig{+ TreeID: gittest.ResolveRevision(t, cfg, repoPath, ours.String()+"^{tree}"),+ Parents: []git.ObjectID{base},+ AuthorName: "Ada",+ AuthorEmail: "[email protected]",+ AuthorDate: when,+ CommitterName: "Ada",+ CommitterEmail: "[email protected]",+ CommitterDate: when,+ Message: "same commit\n",+ }++ want, err := synced.Write(ctx, writeCfg)+ require.NoError(t, err)+ got, err := unsynced.Write(ctx, writeCfg)+ require.NoError(t, err)+ require.Equal(t, want, got)+ })+}