Git designed for AI agents.
revrange: exact revision ranges under committer-date skew
Federico Elia04c0776463e1
diff --git a/internal/git/revrange/revrange.go b/internal/git/revrange/revrange.gonew file mode 100644index 0000000..477af06--- /dev/null+++ b/internal/git/revrange/revrange.go@@ -0,0 +1,378 @@+// Package revrange computes revision ranges exactly: the commits reachable from one commit and not+// from another.+//+// git rev-list base..head is not exact. Its walk stops once every commit it still has to visit is+// reachable from base and older, by committer date, than the last commit it listed (revision.c,+// limit_list and still_interesting, after a few more steps). A commit that the walk reached from+// head before the walk from base reached it is then listed although base reaches it. That happens+// when committer dates are skewed: commits dated before the history they build on, for example+// because the clock of their author is behind or because their dates were set explicitly. The+// listing never misses a commit of the range, because only the ancestors of base are ever marked+// as excluded, but it may hold extra commits, and every one of them is an ancestor of base.+//+// Exact builds the exact range from such a listing with exact primitives (git merge-base+// --is-ancestor and git merge-base --all, which walk until the answer is certain). It relies on+// two facts about the extra commits X of a listing L of head excluding base:+//+// - X is closed downwards in L: the parents of an extra commit are ancestors of base too, so+// those of them that are in L are extra as well.+// - Hence X is not empty exactly when some bottom of L, a commit of L none of whose parents is in+// L, is an ancestor of base: going down from an extra commit through parents that are in L+// ends at a bottom, which is extra.+//+// Without skew a listing has one bottom per point where the range joins the history of base,+// usually one, and checking the bottoms costs one git process each. Under skew Exact lists again+// below the exact merge bases, which bound the walk on every path that runs through them, and then+// removes the remaining extra commits bottom by bottom with one check each.+package revrange++import (+ "bytes"+ "context"+ "errors"+ "fmt"+ "sort"+ "strings"++ "github.com/feder-cr/git-ai/internal/command"+ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/gitcmd"+ "github.com/prometheus/client_golang/prometheus"+ "github.com/prometheus/client_golang/prometheus/promauto"+)++var (+ skewedListingsTotal = promauto.NewCounter(prometheus.CounterOpts{+ Name: "graft_revrange_skewed_listings_total",+ Help: "Listings of a revision range that held commits outside of the range, which happens when " ++ "committer dates are skewed, and that were made exact.",+ })++ skewChecksTotal = promauto.NewCounter(prometheus.CounterOpts{+ Name: "graft_revrange_skew_checks_total",+ Help: "Ancestry checks spent removing the commits outside of the range from skewed listings.",+ })+)++// Commit is a commit of a range with all of its parents in order, as git rev-list --parents prints+// it.+type Commit struct {+ ID git.ObjectID+ Parents []git.ObjectID+}++// Walker answers the questions that Exact builds a range from.+type Walker interface {+ // List returns every commit that is reachable from head and from none of the commits of+ // exclude, each with all of its parents, in no particular order. The listing may also hold+ // commits that are reachable from a commit of exclude (see the package documentation), never+ // fewer commits. Only Exact and Refine make it exact.+ List(ctx context.Context, head git.ObjectID, exclude []git.ObjectID) ([]Commit, error)+ // IsAncestor reports whether ancestor is reachable from descendant, the same commit included.+ // The answer is exact.+ IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error)+ // MergeBases returns the best common ancestors of a and b, as git merge-base --all prints them,+ // and none when a and b share no history. The answer is exact.+ MergeBases(ctx context.Context, a, b git.ObjectID) ([]git.ObjectID, error)+}++// Exact returns the commits reachable from head and not from base, exactly, in no particular order.+// It is empty when head is reachable from base.+func Exact(ctx context.Context, w Walker, base, head git.ObjectID) ([]Commit, error) {+ listed, err := w.List(ctx, head, []git.ObjectID{base})+ if err != nil {+ return nil, err+ }++ return Refine(ctx, w, base, head, listed)+}++// Refine returns the exact range of the commits reachable from head and not from base, given+// listed, which w.List returned for head excluding base.+//+// When the listing is exact, which it is unless committer dates are skewed, Refine checks each of+// its bottoms once. Otherwise it lists again below the merge bases of base and head and removes the+// commits of that listing that base reaches, checking one bottom at a time: at most one check for+// each such commit plus one for each bottom of the range.+func Refine(ctx context.Context, w Walker, base, head git.ObjectID, listed []Commit) ([]Commit, error) {+ if len(listed) == 0 {+ return nil, nil+ }++ rng, err := newRangeSet(listed)+ if err != nil {+ return nil, fmt.Errorf("listing of %s excluding %s: %w", head, base, err)+ }++ extra := false+ for _, bottom := range rng.bottoms() {+ reachable, err := w.IsAncestor(ctx, bottom, base)+ if err != nil {+ return nil, err+ }+ if reachable {+ extra = true+ break+ }+ }+ if !extra {+ return listed, nil+ }++ skewedListingsTotal.Inc()++ // Every common ancestor of base and head is an ancestor of one of their merge bases, so the+ // range is also what head reaches without reaching a merge base, and a walk that excludes the+ // merge bases stops at them.+ bases, err := w.MergeBases(ctx, base, head)+ if err != nil {+ return nil, err+ }+ if len(bases) == 0 {+ // A bottom of the listing is an ancestor of base, and every commit of the listing is an+ // ancestor of head, so base and head share history.+ return nil, fmt.Errorf("%s and %s have no merge base although they share history", base, head)+ }++ relisted, err := w.List(ctx, head, bases)+ if err != nil {+ return nil, err+ }++ rng, err = newRangeSet(relisted)+ if err != nil {+ return nil, fmt.Errorf("listing of %s excluding the merge bases: %w", head, err)+ }++ // A bottom that base does not reach is in the range for good: the commits of the range are+ // exactly those that base does not reach, whatever else the walk removes.+ inRange := make(map[git.ObjectID]bool)+ checks := 0+ for removed := true; removed; {+ removed = false+ for _, bottom := range rng.bottoms() {+ if inRange[bottom] {+ continue+ }++ checks+++ reachable, err := w.IsAncestor(ctx, bottom, base)+ if err != nil {+ skewChecksTotal.Add(float64(checks))+ return nil, err+ }++ if reachable {+ rng.remove(bottom)+ removed = true+ } else {+ inRange[bottom] = true+ }+ }+ }+ skewChecksTotal.Add(float64(checks))++ return rng.commits(), nil+}++// Boundary returns the parents of the commits of rng that are not themselves in rng, sorted. For+// the exact range of head excluding base, they are the commits where the range joins the history+// of base: every path from head into that history runs through one of them, so a walk of head that+// also excludes them cannot list a commit outside of the range.+func Boundary(rng []Commit) []git.ObjectID {+ in := make(map[git.ObjectID]bool, len(rng))+ for _, c := range rng {+ in[c.ID] = true+ }++ seen := make(map[git.ObjectID]bool)+ var boundary []git.ObjectID+ for _, c := range rng {+ for _, parent := range c.Parents {+ if !in[parent] && !seen[parent] {+ seen[parent] = true+ boundary = append(boundary, parent)+ }+ }+ }++ sort.Slice(boundary, func(i, j int) bool { return boundary[i] < boundary[j] })+ return boundary+}++// rangeSet is a listing of commits that Refine removes commits from.+type rangeSet struct {+ // order is the order of the listing.+ order []git.ObjectID+ // byID holds the commits that are still in the set.+ byID map[git.ObjectID]Commit+}++func newRangeSet(listed []Commit) (*rangeSet, error) {+ s := &rangeSet{byID: make(map[git.ObjectID]Commit, len(listed))}+ for _, c := range listed {+ if _, ok := s.byID[c.ID]; ok {+ return nil, fmt.Errorf("commit %s listed twice", c.ID)+ }+ s.byID[c.ID] = c+ s.order = append(s.order, c.ID)+ }+ return s, nil+}++// bottoms returns the commits of the set none of whose parents is in the set, in listing order.+func (s *rangeSet) bottoms() []git.ObjectID {+ var bottoms []git.ObjectID+ for _, id := range s.order {+ c, ok := s.byID[id]+ if !ok {+ continue+ }++ bottom := true+ for _, parent := range c.Parents {+ if _, ok := s.byID[parent]; ok {+ bottom = false+ break+ }+ }+ if bottom {+ bottoms = append(bottoms, id)+ }+ }+ return bottoms+}++func (s *rangeSet) remove(id git.ObjectID) {+ delete(s.byID, id)+}++// commits returns the commits of the set in listing order.+func (s *rangeSet) commits() []Commit {+ commits := make([]Commit, 0, len(s.byID))+ for _, id := range s.order {+ if c, ok := s.byID[id]; ok {+ commits = append(commits, c)+ }+ }+ return commits+}++// Git returns a Walker that runs git in repo for each question.+func Git(repo gitcmd.RepositoryExecutor) Walker {+ return gitWalker{repo: repo}+}++type gitWalker struct {+ repo gitcmd.RepositoryExecutor+}++// List runs git rev-list --parents.+func (w gitWalker) List(ctx context.Context, head git.ObjectID, exclude []git.ObjectID) ([]Commit, error) {+ objectHash, err := w.repo.ObjectHash(ctx)+ if err != nil {+ return nil, fmt.Errorf("detecting object hash: %w", err)+ }++ args := make([]string, 0, len(exclude)+1)+ args = append(args, head.String())+ for _, id := range exclude {+ args = append(args, "^"+id.String())+ }++ var stdout, stderr bytes.Buffer+ if err := w.repo.ExecAndWait(ctx,+ gitcmd.Command{+ Name: "rev-list",+ Flags: []gitcmd.Option{gitcmd.Flag{Name: "--parents"}},+ Args: args,+ },+ gitcmd.WithStdout(&stdout),+ gitcmd.WithStderr(&stderr),+ ); err != nil {+ return nil, fmt.Errorf("rev-list: %w, stderr: %q", err, stderr.String())+ }++ var commits []Commit+ for _, line := range strings.Split(stdout.String(), "\n") {+ fields := strings.Fields(line)+ if len(fields) == 0 {+ continue+ }++ ids, err := parseIDs(objectHash, fields)+ if err != nil {+ return nil, fmt.Errorf("parsing rev-list output %q: %w", line, err)+ }+ commits = append(commits, Commit{ID: ids[0], Parents: ids[1:]})+ }++ return commits, nil+}++// IsAncestor runs git merge-base --is-ancestor, which exits with 1 when ancestor is not reachable+// from descendant.+func (w gitWalker) IsAncestor(ctx context.Context, ancestor, descendant git.ObjectID) (bool, error) {+ var stderr bytes.Buffer+ if err := w.repo.ExecAndWait(ctx,+ gitcmd.Command{+ Name: "merge-base",+ Flags: []gitcmd.Option{gitcmd.Flag{Name: "--is-ancestor"}},+ Args: []string{ancestor.String(), descendant.String()},+ },+ gitcmd.WithStderr(&stderr),+ ); err != nil {+ if status, ok := command.ExitStatus(err); ok && status == 1 && stderr.Len() == 0 {+ return false, nil+ }+ return false, fmt.Errorf("merge-base --is-ancestor: %w, stderr: %q", err, stderr.String())+ }++ return true, nil+}++// MergeBases runs git merge-base --all, which exits with 1 and prints nothing when a and b share no+// history.+func (w gitWalker) MergeBases(ctx context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {+ objectHash, err := w.repo.ObjectHash(ctx)+ if err != nil {+ return nil, fmt.Errorf("detecting object hash: %w", err)+ }++ var stdout, stderr bytes.Buffer+ if err := w.repo.ExecAndWait(ctx,+ gitcmd.Command{+ Name: "merge-base",+ Flags: []gitcmd.Option{gitcmd.Flag{Name: "--all"}},+ Args: []string{a.String(), b.String()},+ },+ gitcmd.WithStdout(&stdout),+ gitcmd.WithStderr(&stderr),+ ); err != nil {+ if status, ok := command.ExitStatus(err); ok && status == 1 && stdout.Len() == 0 && stderr.Len() == 0 {+ return nil, nil+ }+ return nil, fmt.Errorf("merge-base --all: %w, stderr: %q", err, stderr.String())+ }++ bases, err := parseIDs(objectHash, strings.Fields(stdout.String()))+ if err != nil {+ return nil, fmt.Errorf("parsing merge-base output: %w", err)+ }+ return bases, nil+}++func parseIDs(objectHash git.ObjectHash, fields []string) ([]git.ObjectID, error) {+ ids := make([]git.ObjectID, 0, len(fields))+ for _, field := range fields {+ id, err := objectHash.FromHex(field)+ if err != nil {+ return nil, err+ }+ ids = append(ids, id)+ }+ if len(ids) == 0 {+ return nil, errors.New("no object ID")+ }+ return ids, nil+}diff --git a/internal/git/revrange/revrange_test.go b/internal/git/revrange/revrange_test.gonew file mode 100644index 0000000..ae3cc31--- /dev/null+++ b/internal/git/revrange/revrange_test.go@@ -0,0 +1,249 @@+package revrange++import (+ "context"+ "fmt"+ "math/rand"+ "sort"+ "testing"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/stretchr/testify/require"+)++// modelWalker answers from a history held in memory. Its List adds to the exact range a random part+// of the commits that the excluded commits reach, as git rev-list may under committer-date skew.+type modelWalker struct {+ parents map[git.ObjectID][]git.ObjectID+ rnd *rand.Rand+ // extra is the probability that List adds a commit that an excluded commit reaches.+ extra float64++ lists, isAncestors, mergeBases int+ // checksAfterMergeBases counts the IsAncestor calls made after the first MergeBases call.+ checksAfterMergeBases int+ // lastExtra is the number of extra commits of the last List.+ lastExtra int+}++func (m *modelWalker) ancestors(ids ...git.ObjectID) map[git.ObjectID]bool {+ seen := map[git.ObjectID]bool{}+ stack := append([]git.ObjectID(nil), ids...)+ for _, id := range ids {+ seen[id] = true+ }+ for len(stack) > 0 {+ id := stack[len(stack)-1]+ stack = stack[:len(stack)-1]+ for _, p := range m.parents[id] {+ if !seen[p] {+ seen[p] = true+ stack = append(stack, p)+ }+ }+ }+ return seen+}++func (m *modelWalker) List(_ context.Context, head git.ObjectID, exclude []git.ObjectID) ([]Commit, error) {+ m.lists+++ excluded := m.ancestors(exclude...)+ m.lastExtra = 0++ var commits []Commit+ for _, id := range sortedKeys(m.ancestors(head)) {+ if excluded[id] {+ if m.rnd.Float64() >= m.extra {+ continue+ }+ m.lastExtra+++ }+ commits = append(commits, Commit{ID: id, Parents: m.parents[id]})+ }+ m.rnd.Shuffle(len(commits), func(i, j int) { commits[i], commits[j] = commits[j], commits[i] })+ return commits, nil+}++func (m *modelWalker) IsAncestor(_ context.Context, ancestor, descendant git.ObjectID) (bool, error) {+ m.isAncestors+++ if m.mergeBases > 0 {+ m.checksAfterMergeBases+++ }+ return m.ancestors(descendant)[ancestor], nil+}++func (m *modelWalker) MergeBases(_ context.Context, a, b git.ObjectID) ([]git.ObjectID, error) {+ m.mergeBases+++ aAncestors, bAncestors := m.ancestors(a), m.ancestors(b)+ var common []git.ObjectID+ for id := range aAncestors {+ if bAncestors[id] {+ common = append(common, id)+ }+ }++ var best []git.ObjectID+ for _, id := range common {+ redundant := false+ for _, other := range common {+ if other != id && m.ancestors(other)[id] {+ redundant = true+ break+ }+ }+ if !redundant {+ best = append(best, id)+ }+ }+ sort.Slice(best, func(i, j int) bool { return best[i] < best[j] })+ return best, nil+}++func sortedKeys(set map[git.ObjectID]bool) []git.ObjectID {+ keys := make([]git.ObjectID, 0, len(set))+ for id := range set {+ keys = append(keys, id)+ }+ sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })+ return keys+}++// randomHistory returns n commits named c0..c<n-1> whose parents have lower numbers: chains, merges+// and a few further roots.+func randomHistory(rnd *rand.Rand, n int) ([]git.ObjectID, map[git.ObjectID][]git.ObjectID) {+ ids := make([]git.ObjectID, n)+ parents := map[git.ObjectID][]git.ObjectID{}+ for i := range ids {+ ids[i] = git.ObjectID(fmt.Sprintf("c%03d", i))+ if i == 0 || rnd.Intn(30) == 0 {+ continue+ }+ first := i - 1+ if rnd.Intn(3) == 0 {+ first = rnd.Intn(i)+ }+ parents[ids[i]] = []git.ObjectID{ids[first]}+ if rnd.Intn(5) == 0 {+ if second := rnd.Intn(i); second != first {+ parents[ids[i]] = append(parents[ids[i]], ids[second])+ }+ }+ }+ return ids, parents+}++func rangeOf(commits []Commit) map[git.ObjectID][]git.ObjectID {+ set := make(map[git.ObjectID][]git.ObjectID, len(commits))+ for _, c := range commits {+ set[c.ID] = c.Parents+ }+ return set+}++func bottomsOf(set map[git.ObjectID][]git.ObjectID) int {+ bottoms := 0+ for _, parents := range set {+ bottom := true+ for _, p := range parents {+ if _, ok := set[p]; ok {+ bottom = false+ }+ }+ if bottom {+ bottoms+++ }+ }+ return bottoms+}++// TestExactOnRandomHistories checks Exact against the exact range on random histories, with+// listings that hold no extra commit, some, and every commit that the base reaches. It also checks+// the costs that the package documents: without extra commits one check per bottom of the listing+// and nothing else; with them, at most one check per extra commit of the second listing and per+// bottom of the range.+func TestExactOnRandomHistories(t *testing.T) {+ t.Parallel()++ ctx := context.Background()+ for _, extra := range []float64{0, 0.3, 1} {+ for seed := int64(1); seed <= 3; seed++ {+ t.Run(fmt.Sprintf("extra %v seed %d", extra, seed), func(t *testing.T) {+ t.Parallel()++ rnd := rand.New(rand.NewSource(seed))+ ids, parents := randomHistory(rnd, 150)++ skewed := 0+ for k := 0; k < 300; k++ {+ base, head := ids[rnd.Intn(len(ids))], ids[rnd.Intn(len(ids))]+ m := &modelWalker{parents: parents, rnd: rnd, extra: extra}++ baseAncestors := m.ancestors(base)+ want := map[git.ObjectID][]git.ObjectID{}+ for id := range m.ancestors(head) {+ if !baseAncestors[id] {+ want[id] = parents[id]+ }+ }++ got, err := Exact(ctx, m, base, head)+ require.NoError(t, err)+ require.Equal(t, want, rangeOf(got), "range %s..%s", base, head)++ if m.mergeBases == 0 {+ // The listing was exact: one check per bottom at most, one listing.+ require.Equal(t, 1, m.lists)+ require.Equal(t, bottomsOf(want), m.isAncestors)+ continue+ }++ skewed+++ require.Equal(t, 2, m.lists)+ require.Equal(t, 1, m.mergeBases)+ require.LessOrEqual(t, m.checksAfterMergeBases, m.lastExtra+bottomsOf(want),+ "range %s..%s: %d checks for %d extra commits and %d bottoms", base, head,+ m.checksAfterMergeBases, m.lastExtra, bottomsOf(want))+ }++ if extra == 0 {+ require.Zero(t, skewed)+ } else {+ require.Positive(t, skewed)+ }+ })+ }+ }+}++// TestRefineOfAnEmptyListing needs no question.+func TestRefineOfAnEmptyListing(t *testing.T) {+ t.Parallel()++ m := &modelWalker{rnd: rand.New(rand.NewSource(1))}+ got, err := Refine(context.Background(), m, "a", "b", nil)+ require.NoError(t, err)+ require.Empty(t, got)+ require.Zero(t, m.isAncestors+m.lists+m.mergeBases)+}++// TestRefineRejectsListingsThatAreNotHistories reports a listing that lists a commit twice.+func TestRefineRejectsListingsThatAreNotHistories(t *testing.T) {+ t.Parallel()++ m := &modelWalker{rnd: rand.New(rand.NewSource(1))}+ _, err := Refine(context.Background(), m, "a", "b", []Commit{{ID: "b"}, {ID: "b"}})+ require.ErrorContains(t, err, "listed twice")+}++func TestBoundary(t *testing.T) {+ t.Parallel()++ rng := []Commit{+ {ID: "d", Parents: []git.ObjectID{"c", "x"}},+ {ID: "c", Parents: []git.ObjectID{"b"}},+ {ID: "b", Parents: []git.ObjectID{"y"}},+ {ID: "e", Parents: []git.ObjectID{"x"}},+ }+ require.Equal(t, []git.ObjectID{"x", "y"}, Boundary(rng))+ require.Empty(t, Boundary(nil))+}