Git designed for AI agents.
gitops: pack a closure, look up and check reachability (PLAN7 WP74)
Federico Eliadeb5fd6615dd
diff --git a/internal/graft/corestore/gitops/gitops.go b/internal/graft/corestore/gitops/gitops.goindex 4d043a2..1feb079 100644--- a/internal/graft/corestore/gitops/gitops.go+++ b/internal/graft/corestore/gitops/gitops.go@@ -92,42 +92,180 @@ func (p Pack) Remove() error {// tips may be empty; zero OIDs are ignored. An empty result returns an empty// Pack and no file.func BuildPack(ctx context.Context, repo *localrepo.Repo, tips []git.ObjectID, tmpDir string) (Pack, error) {- hash, err := repo.ObjectHash(ctx)- if err != nil {- return Pack{}, fmt.Errorf("object hash: %w", err)- }-// Tips read from stdin are interesting even though --not is given on the// command line: git only applies --not to the revisions it parses from// the arguments.+ stdin, positive, err := tipsInput(ctx, repo, tips)+ if err != nil {+ return Pack{}, err+ }+ if positive == 0 {+ return Pack{}, nil+ }++ // The --not flag makes --all excluded; the tips on stdin are included.+ return buildPack(ctx, repo, gitcmd.Command{+ Name: "rev-list",+ Flags: []gitcmd.Option{+ gitcmd.Flag{Name: "--objects"},+ gitcmd.Flag{Name: "--stdin"},+ gitcmd.Flag{Name: "--not"},+ gitcmd.Flag{Name: "--all"},+ },+ }, &stdin, tmpDir)+}++// BuildPackFrom writes a self-contained (non-thin) pack of every object+// reachable from tips, whatever the references of the repository: the+// consolidated pack of a checkpoint (doc/graft/PLAN7.md). The tips are fed to+// `git rev-list --objects --stdin`, piped into `git pack-objects --stdout` and+// written to a new file in tmpDir while hashing. Zero tips are ignored; without+// any tip the result is an empty Pack and no file. A tip that is not in the+// repository is an error: callers check the tips with MissingObjects first, so+// that a missing object is reported precisely and never yields a partial pack.+func BuildPackFrom(ctx context.Context, repo *localrepo.Repo, tips []git.ObjectID, tmpDir string) (Pack, error) {+ stdin, positive, err := tipsInput(ctx, repo, tips)+ if err != nil {+ return Pack{}, err+ }+ if positive == 0 {+ return Pack{}, nil+ }++ return buildPack(ctx, repo, gitcmd.Command{+ Name: "rev-list",+ Flags: []gitcmd.Option{+ gitcmd.Flag{Name: "--objects"},+ gitcmd.Flag{Name: "--stdin"},+ },+ }, &stdin, tmpDir)+}++// tipsInput returns the standard input of a rev-list that walks from tips: one+// validated object ID per line, zero OIDs left out, and how many there are.+func tipsInput(ctx context.Context, repo *localrepo.Repo, tips []git.ObjectID) (bytes.Buffer, int, error) {var stdin bytes.Buffer++ hash, err := repo.ObjectHash(ctx)+ if err != nil {+ return stdin, 0, fmt.Errorf("object hash: %w", err)+ }+positive := 0for _, tip := range tips {if hash.IsZeroOID(tip) {continue}if err := hash.ValidateHex(tip.String()); err != nil {- return Pack{}, fmt.Errorf("tip: %w", err)+ return stdin, 0, fmt.Errorf("tip: %w", err)}stdin.WriteString(tip.String())stdin.WriteByte('\n')positive++}+ return stdin, positive, nil+}- if positive == 0 {- return Pack{}, nil+// MissingObjects returns the objects among oids that are not in the+// repository, in the order given, with one `git cat-file --batch-check` run.+// Every object ID is validated against the repository's hash first.+func MissingObjects(ctx context.Context, repo *localrepo.Repo, oids []git.ObjectID) ([]git.ObjectID, error) {+ if len(oids) == 0 {+ return nil, nil}- // The --not flag makes --all excluded; the tips on stdin are included.- return buildPack(ctx, repo, gitcmd.Command{- Name: "rev-list",- Flags: []gitcmd.Option{- gitcmd.Flag{Name: "--objects"},- gitcmd.Flag{Name: "--stdin"},- gitcmd.Flag{Name: "--not"},- gitcmd.Flag{Name: "--all"},+ hash, err := repo.ObjectHash(ctx)+ if err != nil {+ return nil, fmt.Errorf("object hash: %w", err)+ }++ var stdin bytes.Buffer+ for _, oid := range oids {+ if err := hash.ValidateHex(oid.String()); err != nil {+ return nil, fmt.Errorf("object %q: %w", oid, err)+ }+ stdin.WriteString(oid.String())+ stdin.WriteByte('\n')+ }++ var stdout, stderr bytes.Buffer+ if err := repo.ExecAndWait(ctx,+ gitcmd.Command{+ Name: "cat-file",+ Flags: []gitcmd.Option{gitcmd.Flag{Name: "--batch-check=%(objectname) %(objecttype)"}},},- }, &stdin, tmpDir)+ gitcmd.WithStdin(&stdin),+ gitcmd.WithStdout(&stdout),+ gitcmd.WithStderr(&stderr),+ ); err != nil {+ return nil, structerr.New("cat-file: %w", err).WithMetadata("stderr", stderr.String())+ }++ // One output line per input line: "<oid> <type>", or "<oid> missing".+ lines := strings.Split(strings.TrimSuffix(stdout.String(), "\n"), "\n")+ if len(lines) != len(oids) {+ return nil, fmt.Errorf("cat-file: %d answers for %d objects", len(lines), len(oids))+ }++ var missing []git.ObjectID+ for i, line := range lines {+ name, kind, ok := strings.Cut(line, " ")+ if !ok || name != oids[i].String() {+ return nil, fmt.Errorf("cat-file: unexpected answer %q for %s", line, oids[i])+ }+ if kind == "missing" {+ missing = append(missing, oids[i])+ }+ }+ return missing, nil+}++// IsReachable reports whether commit is reachable from at least one of tips+// (the object IDs that the references of a repository point to): `git+// rev-list --stdin` from commit with every tip negated prints nothing exactly+// when every commit it would walk is reachable from a tip, which holds exactly+// when commit is. commit must be in the repository; zero tips are ignored, and+// no tip at all reaches nothing.+func IsReachable(ctx context.Context, repo *localrepo.Repo, commit git.ObjectID, tips []git.ObjectID) (bool, error) {+ hash, err := repo.ObjectHash(ctx)+ if err != nil {+ return false, fmt.Errorf("object hash: %w", err)+ }+ if err := hash.ValidateHex(commit.String()); err != nil {+ return false, fmt.Errorf("commit: %w", err)+ }++ var stdin bytes.Buffer+ stdin.WriteString(commit.String())+ stdin.WriteByte('\n')+ for _, tip := range tips {+ if hash.IsZeroOID(tip) {+ continue+ }+ if err := hash.ValidateHex(tip.String()); err != nil {+ return false, fmt.Errorf("tip: %w", err)+ }+ stdin.WriteString("^" + tip.String())+ stdin.WriteByte('\n')+ }++ var stdout, stderr bytes.Buffer+ if err := repo.ExecAndWait(ctx,+ gitcmd.Command{+ Name: "rev-list",+ Flags: []gitcmd.Option{+ gitcmd.Flag{Name: "--stdin"},+ gitcmd.Flag{Name: "--max-count=1"},+ },+ },+ gitcmd.WithStdin(&stdin),+ gitcmd.WithStdout(&stdout),+ gitcmd.WithStderr(&stderr),+ ); err != nil {+ return false, structerr.New("rev-list: %w", err).WithMetadata("stderr", stderr.String())+ }++ return stdout.Len() == 0, nil}// BuildFullPack writes a pack of every object reachable from any referencediff --git a/internal/graft/corestore/gitops/gitops_checkpoint_test.go b/internal/graft/corestore/gitops/gitops_checkpoint_test.gonew file mode 100644index 0000000..e2aab8a--- /dev/null+++ b/internal/graft/corestore/gitops/gitops_checkpoint_test.go@@ -0,0 +1,148 @@+package gitops_test++import (+ "bytes"+ "strings"+ "testing"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/graft/corestore"+ "github.com/feder-cr/git-ai/internal/graft/corestore/gitops"+ "github.com/stretchr/testify/require"+)++// tagObject writes an annotated tag object of target, not referenced.+func (f fixture) tagObject(t *testing.T, target git.ObjectID) git.ObjectID {+ t.Helper()++ mktag := gittest.NewCommand(t, f.cfg, "-C", f.path, "mktag")+ mktag.Stdin = strings.NewReader("object " + target.String() + "\ntype commit\ntag v1\ntagger T <[email protected]> 1700000000 +0000\n\nv1\n")+ out, err := mktag.Output()+ require.NoError(t, err)+ return git.ObjectID(strings.TrimSpace(string(out)))+}++func TestBuildPackFromIsSelfContained(t *testing.T) {+ t.Parallel()++ for _, format := range objectFormats {+ t.Run(format, func(t *testing.T) {+ t.Parallel()++ source := newFixture(t, format)+ c1 := source.commit(t, nil, "first")+ gittest.WriteRef(t, source.cfg, source.path, "refs/heads/main", c1)+ c2 := source.commit(t, []git.ObjectID{c1}, "second")++ // Unlike BuildPack, the objects that a reference already reaches (c1)+ // are included: the pack is the whole closure of the tips.+ pack, err := gitops.BuildPackFrom(source.ctx, source.repo, []git.ObjectID{c2, source.hash.ZeroOID}, t.TempDir())+ require.NoError(t, err)+ require.Equal(t, 6, pack.Objects, "two commits, two trees and two blobs")+ data := readPack(t, pack)++ target := newFixture(t, format)+ require.NoError(t, gitops.IndexPack(target.ctx, target.repo, bytes.NewReader(data)))+ require.NoError(t, gitops.ApplyRefs(target.ctx, target.repo, map[string]corestore.Target{+ "refs/heads/main": corestore.Target(c2.String()),+ }))+ gittest.Exec(t, target.cfg, "-C", target.path, "fsck", "--connectivity-only")+ })+ }+}++func TestBuildPackFromWithoutTips(t *testing.T) {+ t.Parallel()++ f := newFixture(t, "sha1")+ for _, tips := range [][]git.ObjectID{nil, {f.hash.ZeroOID}} {+ pack, err := gitops.BuildPackFrom(f.ctx, f.repo, tips, t.TempDir())+ require.NoError(t, err)+ require.True(t, pack.Empty())+ }+}++func TestBuildPackFromRejectsBadTips(t *testing.T) {+ t.Parallel()++ f := newFixture(t, "sha1")++ _, err := gitops.BuildPackFrom(f.ctx, f.repo, []git.ObjectID{"--all"}, t.TempDir())+ require.Error(t, err, "a tip that is not an object ID")++ absent := git.ObjectID(strings.Repeat("ab", 20))+ _, err = gitops.BuildPackFrom(f.ctx, f.repo, []git.ObjectID{absent}, t.TempDir())+ require.Error(t, err, "a tip that is not in the repository")+}++func TestMissingObjects(t *testing.T) {+ t.Parallel()++ for _, format := range objectFormats {+ t.Run(format, func(t *testing.T) {+ t.Parallel()++ f := newFixture(t, format)+ c1 := f.commit(t, nil, "present")+ blob := gittest.WriteBlob(t, f.cfg, f.path, []byte("a blob"))+ absent1 := git.ObjectID(strings.Repeat("1", f.hash.EncodedLen()))+ absent2 := git.ObjectID(strings.Repeat("2", f.hash.EncodedLen()))++ missing, err := gitops.MissingObjects(f.ctx, f.repo, []git.ObjectID{absent2, c1, blob, absent1})+ require.NoError(t, err)+ require.Equal(t, []git.ObjectID{absent2, absent1}, missing, "in the order given")++ missing, err = gitops.MissingObjects(f.ctx, f.repo, []git.ObjectID{c1})+ require.NoError(t, err)+ require.Empty(t, missing)++ missing, err = gitops.MissingObjects(f.ctx, f.repo, nil)+ require.NoError(t, err)+ require.Empty(t, missing)++ _, err = gitops.MissingObjects(f.ctx, f.repo, []git.ObjectID{"HEAD"})+ require.Error(t, err, "only object IDs are looked up")+ })+ }+}++func TestIsReachable(t *testing.T) {+ t.Parallel()++ for _, format := range objectFormats {+ t.Run(format, func(t *testing.T) {+ t.Parallel()++ f := newFixture(t, format)+ c1 := f.commit(t, nil, "base")+ c2 := f.commit(t, []git.ObjectID{c1}, "tip")+ unrelated := f.commit(t, nil, "unrelated")+ tag := f.tagObject(t, c2)+ blob := gittest.WriteBlob(t, f.cfg, f.path, []byte("a reference may point to a blob"))++ for _, tc := range []struct {+ desc string+ commit git.ObjectID+ tips []git.ObjectID+ want bool+ }{+ {desc: "ancestor of a tip", commit: c1, tips: []git.ObjectID{c2}, want: true},+ {desc: "the tip itself", commit: c2, tips: []git.ObjectID{c2}, want: true},+ {desc: "through an annotated tag", commit: c1, tips: []git.ObjectID{tag}, want: true},+ {desc: "next to a tip that is a blob", commit: c1, tips: []git.ObjectID{blob, c2}, want: true},+ {desc: "unrelated history", commit: unrelated, tips: []git.ObjectID{c2, tag, blob}, want: false},+ {desc: "a descendant is not reachable from its ancestor", commit: c2, tips: []git.ObjectID{c1}, want: false},+ {desc: "no tips", commit: c1, tips: nil, want: false},+ {desc: "zero tips are ignored", commit: c1, tips: []git.ObjectID{f.hash.ZeroOID}, want: false},+ } {+ got, err := gitops.IsReachable(f.ctx, f.repo, tc.commit, tc.tips)+ require.NoError(t, err, tc.desc)+ require.Equal(t, tc.want, got, tc.desc)+ }++ _, err := gitops.IsReachable(f.ctx, f.repo, "--all", []git.ObjectID{c2})+ require.Error(t, err, "the commit must be an object ID")+ })+ }+}