Git designed for AI agents.
hook: the compaction the land intents ask for is started by the manager, and tested
Federico Eliadff121981527
diff --git a/internal/gitaly/hook/manager.go b/internal/gitaly/hook/manager.goindex 62aafde..28065e8 100644--- a/internal/gitaly/hook/manager.go+++ b/internal/gitaly/hook/manager.go@@ -87,6 +87,9 @@ type GitLabHookManager struct {observer *eager.Observer// storageVoter commits reference transactions to the Graft storage core, nil when it is off.storageVoter StorageVoter+ // startLandIntentsCompaction runs a compaction of land intents that a prepared transaction+ // asked for, after the hook. Nil runs it in a goroutine; tests set it.+ startLandIntentsCompaction func(compact func())}// ManagerOption configures a GitLabHookManager built by NewManager.diff --git a/internal/gitaly/hook/referencetransaction.go b/internal/gitaly/hook/referencetransaction.goindex 70d5c8d..5d298ca 100644--- a/internal/gitaly/hook/referencetransaction.go+++ b/internal/gitaly/hook/referencetransaction.go@@ -234,11 +234,16 @@ func (m *GitLabHookManager) recordLandIntents(ctx context.Context, payload gitcm// The compaction reads the changelog and rewrites the intents: it runs after the hook, not// under the locks of the references, and its failure leaves the intents as they are.if compact {- go func() {+ run := func() {if err := changelog.CompactLandIntents(repoPath); err != nil {m.logger.WithError(err).WithField("repository", repoPath).Error("graft: compacting the land intents failed")}- }()+ }+ if m.startLandIntentsCompaction != nil {+ m.startLandIntentsCompaction(run)+ } else {+ go run()+ }}return nildiff --git a/internal/gitaly/hook/referencetransaction_intents_test.go b/internal/gitaly/hook/referencetransaction_intents_test.goindex 155ebdd..7838acf 100644--- a/internal/gitaly/hook/referencetransaction_intents_test.go+++ b/internal/gitaly/hook/referencetransaction_intents_test.go@@ -2,6 +2,7 @@ package hookimport ("context"+ "encoding/json""fmt""os""path/filepath"@@ -28,7 +29,7 @@ func TestReferenceTransactionHook_recordsLandIntents(t *testing.T) {// prepare runs the prepared phase of an update of reference whose payload lands land on main,// in a new repository, and returns the repository's path and the error of the hook.- prepare := func(t *testing.T, cfg config.Cfg, reference string, setup func(repoPath string)) (string, error) {+ prepare := func(t *testing.T, cfg config.Cfg, reference string, setup func(repoPath string), startCompaction func(func())) (string, error) {ctx := testhelper.Context(t)repo, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})repoPath, err := config.NewLocator(cfg).GetRepoPath(ctx, repo)@@ -43,13 +44,15 @@ func TestReferenceTransactionHook_recordsLandIntents(t *testing.T) {require.NoError(t, err)changes := fmt.Sprintf("%s %s %s\n", gittest.DefaultObjectHash.EmptyTreeOID, land.Landed, reference)- return repoPath, newMembersTestManager(t, cfg).ReferenceTransactionHook(context.Background(), ReferenceTransactionPrepared, []string{env}, strings.NewReader(changes))+ manager := newMembersTestManager(t, cfg)+ manager.startLandIntentsCompaction = startCompaction+ return repoPath, manager.ReferenceTransactionHook(context.Background(), ReferenceTransactionPrepared, []string{env}, strings.NewReader(changes))}t.Run("an update of the branch of the lands", func(t *testing.T) {t.Parallel()- repoPath, err := prepare(t, testcfg.Build(t), "refs/heads/main", nil)+ repoPath, err := prepare(t, testcfg.Build(t), "refs/heads/main", nil, nil)require.NoError(t, err)records, err := changelog.LookupLand(repoPath, "refs/heads/main", land.Original)@@ -60,7 +63,7 @@ func TestReferenceTransactionHook_recordsLandIntents(t *testing.T) {t.Run("an update of another reference", func(t *testing.T) {t.Parallel()- repoPath, err := prepare(t, testcfg.Build(t), "refs/heads/other", nil)+ repoPath, err := prepare(t, testcfg.Build(t), "refs/heads/other", nil, nil)require.NoError(t, err)require.NoFileExists(t, filepath.Join(repoPath, "graft", "changelog.intents"))})@@ -70,18 +73,58 @@ func TestReferenceTransactionHook_recordsLandIntents(t *testing.T) {cfg := testcfg.Build(t)cfg.Graft.Changelog.Disabled = true- repoPath, err := prepare(t, cfg, "refs/heads/main", nil)+ repoPath, err := prepare(t, cfg, "refs/heads/main", nil, nil)require.NoError(t, err)require.NoFileExists(t, filepath.Join(repoPath, "graft", "changelog.intents"))})+ t.Run("a grown intents file is compacted after the update", func(t *testing.T) {+ t.Parallel()++ // The file holds enough intents whose updates the changelog confirms: the next intent asks+ // for a compaction, which the hook starts after the update.+ var lines strings.Builder+ var records []changelog.Record+ for i := 0; i < 600; i++ {+ confirmed := changelog.Land{Original: fmt.Sprintf("%040x", i+1), Landed: fmt.Sprintf("%040x", i+1)}+ line, err := json.Marshal(map[string]any{"ref": []byte("refs/heads/main"), "original": confirmed.Original, "landed": confirmed.Landed, "invocation": fmt.Sprint("inv-", i)})+ require.NoError(t, err)+ lines.Write(line)+ lines.WriteByte('\n')+ records = append(records, changelog.Record{+ Meta: changelog.Meta{Operation: "land", LandReference: "refs/heads/main", LandInvocation: fmt.Sprint("inv-", i), Lands: []changelog.Land{confirmed}},+ Updates: []changelog.Update{{Reference: "refs/heads/main", OldOID: gittest.DefaultObjectHash.ZeroOID.String(), NewOID: confirmed.Landed}},+ })+ }++ started := 0+ repoPath, err := prepare(t, testcfg.Build(t), "refs/heads/main", func(repoPath string) {+ require.NoError(t, os.MkdirAll(filepath.Join(repoPath, "graft"), 0o755))+ require.NoError(t, os.WriteFile(filepath.Join(repoPath, "graft", "changelog.intents"), []byte(lines.String()), 0o644))+ _, err := changelog.AppendGroup(repoPath, records)+ require.NoError(t, err)+ }, func(compact func()) {+ started+++ compact()+ })+ require.NoError(t, err)+ require.Equal(t, 1, started)++ info, err := os.Stat(filepath.Join(repoPath, "graft", "changelog.intents"))+ require.NoError(t, err)+ require.Less(t, info.Size(), int64(len(lines.String())), "the confirmed intents went")+ records2, err := changelog.LookupLand(repoPath, "refs/heads/main", land.Original)+ require.NoError(t, err)+ require.Equal(t, []changelog.LandRecord{{Landed: land.Landed}}, records2, "the intent of the update stays")+ })+t.Run("a failure to record the intents aborts the update", func(t *testing.T) {t.Parallel()// The intents file cannot be opened for writing: a directory is in its place._, err := prepare(t, testcfg.Build(t), "refs/heads/main", func(repoPath string) {require.NoError(t, os.MkdirAll(filepath.Join(repoPath, "graft", "changelog.intents"), 0o755))- })+ }, nil)require.ErrorContains(t, err, "recording the land intents")})}