Git designed for AI agents.
graft: the CLI prints changeset members without linking the log
Loris Salsicfaf85b0fac0
diff --git a/cmd/graft/commands.go b/cmd/graft/commands.goindex 9da6439..74b70a6 100644--- a/cmd/graft/commands.go+++ b/cmd/graft/commands.go@@ -8,7 +8,7 @@ import ("strings""time"- "github.com/feder-cr/git-ai/internal/graft/changelog"+ "github.com/feder-cr/git-ai/internal/graft/changelog/member""github.com/feder-cr/git-ai/internal/graft/workspace""github.com/feder-cr/git-ai/sdk")@@ -531,29 +531,29 @@ func memberNote(m workspace.ChangesetMember) string {// changesetJSON is the --json form of a changeset, one object per line. It is// the event format of graft-superfan (superfan.Event). Object IDs are full.-// Members have the form of the changeset log, see changelog.Member.+// Members have the form of the changeset log, see member.Member.type changesetJSON struct {- Sequence uint64 `json:"sequence"`- Reference string `json:"reference"`- OldOID string `json:"old_oid"`- NewOID string `json:"new_oid"`- Operation string `json:"operation"`- Actor string `json:"actor"`- RecordedAt time.Time `json:"recorded_at"`- Rewritten bool `json:"rewritten"`- Members []changelog.Member `json:"members,omitempty"`+ Sequence uint64 `json:"sequence"`+ Reference string `json:"reference"`+ OldOID string `json:"old_oid"`+ NewOID string `json:"new_oid"`+ Operation string `json:"operation"`+ Actor string `json:"actor"`+ RecordedAt time.Time `json:"recorded_at"`+ Rewritten bool `json:"rewritten"`+ Members []member.Member `json:"members,omitempty"`}// logMembers converts the members of a changeset to the form of the changeset// log, which --json writes. It returns nil when there are none.-func logMembers(members []workspace.ChangesetMember) []changelog.Member {+func logMembers(members []workspace.ChangesetMember) []member.Member {if len(members) == 0 {return nil}- out := make([]changelog.Member, len(members))+ out := make([]member.Member, len(members))for i, m := range members {- out[i] = changelog.Member{+ out[i] = member.Member{OriginalCommit: m.OriginalCommit,Commit: m.Commit,Actor: m.Actor,diff --git a/internal/graft/changelog/member/member.go b/internal/graft/changelog/member/member.gonew file mode 100644index 0000000..d37f473--- /dev/null+++ b/internal/graft/changelog/member/member.go@@ -0,0 +1,133 @@+// Package member holds Member, one change that a batched land folded into a changeset, and its+// JSON form. The changeset log records members (package changelog, which aliases the type), and the+// graft CLI prints them in the same form (graft log --json, graft watch --json). The CLI imports+// this package rather than changelog: the log locks its files with flock, so changelog builds on+// Unix only, and the CLI builds on Windows too.+package member++import (+ "encoding/base64"+ "encoding/json"+ "fmt"+ "strings"+ "unicode/utf8"+)++// Member is one change that a batched land folded into a changeset. A batch+// lands several changes with a single reference update, so the changeset of the+// batch has one member per change. Members keep the per-change attribution that+// the single update would otherwise hide.+type Member struct {+ // OriginalCommit is the commit that the change submitted, before any rewrite.+ OriginalCommit string `json:"original_commit"`+ // Commit is the commit of the change inside the batch's chain of+ // commits, which is the rewritten commit when Rewritten is true.+ Commit string `json:"commit"`+ // Actor identifies who submitted the change. It is valid UTF-8.+ Actor string `json:"actor,omitempty"`+ // Rewritten is true when the change was rebased or merged onto the running tip+ // of the batch, so that Commit differs from OriginalCommit.+ Rewritten bool `json:"rewritten,omitempty"`+ // ResolvedPaths lists the paths that a resolver changed to land the change. It+ // is empty when the change landed without conflicts. Paths are kept byte for+ // byte, see MarshalJSON.+ ResolvedPaths []string `json:"resolved_paths,omitempty"`+}++// memberJSON is the JSON form of a Member.+type memberJSON struct {+ OriginalCommit string `json:"original_commit"`+ Commit string `json:"commit"`+ Actor string `json:"actor,omitempty"`+ Rewritten bool `json:"rewritten,omitempty"`+ ResolvedPaths []string `json:"resolved_paths,omitempty"`+ // ResolvedPathsBase64 holds every resolved path, base64 encoded and in the same+ // order as ResolvedPaths. It is only set when a path is not valid UTF-8, which+ // JSON strings cannot hold. ResolvedPaths then holds a readable but lossy form+ // of the paths, in which the invalid bytes are replaced with U+FFFD.+ ResolvedPathsBase64 []string `json:"resolved_paths_b64,omitempty"`+}++// MarshalJSON encodes the member with the keys original_commit, commit, actor,+// rewritten and resolved_paths. encoding/json would replace the invalid bytes of+// a resolved path with U+FFFD, so such paths are also encoded in+// resolved_paths_b64, which decodes them exactly.+func (m Member) MarshalJSON() ([]byte, error) {+ encoded := memberJSON{+ OriginalCommit: m.OriginalCommit,+ Commit: m.Commit,+ Actor: m.Actor,+ Rewritten: m.Rewritten,+ ResolvedPaths: m.ResolvedPaths,+ }++ if !allValidUTF8(m.ResolvedPaths) {+ encoded.ResolvedPaths = make([]string, len(m.ResolvedPaths))+ encoded.ResolvedPathsBase64 = make([]string, len(m.ResolvedPaths))+ for i, p := range m.ResolvedPaths {+ encoded.ResolvedPaths[i] = toValidUTF8(p)+ encoded.ResolvedPathsBase64[i] = base64.StdEncoding.EncodeToString([]byte(p))+ }+ }++ return json.Marshal(encoded)+}++// UnmarshalJSON decodes a member encoded by MarshalJSON.+func (m *Member) UnmarshalJSON(data []byte) error {+ var decoded memberJSON+ if err := json.Unmarshal(data, &decoded); err != nil {+ return err+ }++ paths := decoded.ResolvedPaths+ if len(decoded.ResolvedPathsBase64) > 0 {+ if len(decoded.ResolvedPathsBase64) != len(decoded.ResolvedPaths) {+ return fmt.Errorf("resolved_paths_b64 has %d paths, resolved_paths has %d",+ len(decoded.ResolvedPathsBase64), len(decoded.ResolvedPaths))+ }++ paths = make([]string, len(decoded.ResolvedPathsBase64))+ for i, encoded := range decoded.ResolvedPathsBase64 {+ p, err := base64.StdEncoding.DecodeString(encoded)+ if err != nil {+ return fmt.Errorf("decoding resolved_paths_b64: %w", err)+ }+ paths[i] = string(p)+ }+ }++ *m = Member{+ OriginalCommit: decoded.OriginalCommit,+ Commit: decoded.Commit,+ Actor: decoded.Actor,+ Rewritten: decoded.Rewritten,+ ResolvedPaths: paths,+ }++ return nil+}++// Resolved reports whether the member was landed with resolved conflicts.+func (m Member) Resolved() bool {+ return len(m.ResolvedPaths) > 0+}++// allValidUTF8 reports whether every string of ss is valid UTF-8.+func allValidUTF8(ss []string) bool {+ for _, s := range ss {+ if !utf8.ValidString(s) {+ return false+ }+ }++ return true+}++// toValidUTF8 replaces invalid bytes with U+FFFD, as the changeset log does for actors.+func toValidUTF8(s string) string {+ if utf8.ValidString(s) {+ return s+ }+ return strings.ToValidUTF8(s, string(utf8.RuneError))+}diff --git a/internal/graft/changelog/members.go b/internal/graft/changelog/members.goindex 24606fa..e7fb8d4 100644--- a/internal/graft/changelog/members.go+++ b/internal/graft/changelog/members.go@@ -1,117 +1,22 @@package changelogimport (- "encoding/base64"- "encoding/json""errors""fmt""strings""unicode/utf8"++ "github.com/feder-cr/git-ai/internal/graft/changelog/member")// shortOIDLength is the number of characters of an object ID that FormatMembers// shows.const shortOIDLength = 8-// Member is one change that a batched land folded into a changeset. A batch-// lands several changes with a single reference update, so the changeset of the-// batch has one member per change. Members keep the per-change attribution that-// the single update would otherwise hide.-type Member struct {- // OriginalCommit is the commit that the change submitted, before any rewrite.- OriginalCommit string `json:"original_commit"`- // Commit is the commit of the change inside the batch's chain of- // commits, which is the rewritten commit when Rewritten is true.- Commit string `json:"commit"`- // Actor identifies who submitted the change. It is valid UTF-8.- Actor string `json:"actor,omitempty"`- // Rewritten is true when the change was rebased or merged onto the running tip- // of the batch, so that Commit differs from OriginalCommit.- Rewritten bool `json:"rewritten,omitempty"`- // ResolvedPaths lists the paths that a resolver changed to land the change. It- // is empty when the change landed without conflicts. Paths are kept byte for- // byte, see MarshalJSON.- ResolvedPaths []string `json:"resolved_paths,omitempty"`-}--// memberJSON is the JSON form of a Member.-type memberJSON struct {- OriginalCommit string `json:"original_commit"`- Commit string `json:"commit"`- Actor string `json:"actor,omitempty"`- Rewritten bool `json:"rewritten,omitempty"`- ResolvedPaths []string `json:"resolved_paths,omitempty"`- // ResolvedPathsBase64 holds every resolved path, base64 encoded and in the same- // order as ResolvedPaths. It is only set when a path is not valid UTF-8, which- // JSON strings cannot hold. ResolvedPaths then holds a readable but lossy form- // of the paths, in which the invalid bytes are replaced with U+FFFD.- ResolvedPathsBase64 []string `json:"resolved_paths_b64,omitempty"`-}--// MarshalJSON encodes the member with the keys original_commit, commit, actor,-// rewritten and resolved_paths. encoding/json would replace the invalid bytes of-// a resolved path with U+FFFD, so such paths are also encoded in-// resolved_paths_b64, which decodes them exactly.-func (m Member) MarshalJSON() ([]byte, error) {- encoded := memberJSON{- OriginalCommit: m.OriginalCommit,- Commit: m.Commit,- Actor: m.Actor,- Rewritten: m.Rewritten,- ResolvedPaths: m.ResolvedPaths,- }-- if !allValidUTF8(m.ResolvedPaths) {- encoded.ResolvedPaths = make([]string, len(m.ResolvedPaths))- encoded.ResolvedPathsBase64 = make([]string, len(m.ResolvedPaths))- for i, p := range m.ResolvedPaths {- encoded.ResolvedPaths[i] = toValidUTF8(p)- encoded.ResolvedPathsBase64[i] = base64.StdEncoding.EncodeToString([]byte(p))- }- }-- return json.Marshal(encoded)-}--// UnmarshalJSON decodes a member encoded by MarshalJSON.-func (m *Member) UnmarshalJSON(data []byte) error {- var decoded memberJSON- if err := json.Unmarshal(data, &decoded); err != nil {- return err- }-- paths := decoded.ResolvedPaths- if len(decoded.ResolvedPathsBase64) > 0 {- if len(decoded.ResolvedPathsBase64) != len(decoded.ResolvedPaths) {- return fmt.Errorf("resolved_paths_b64 has %d paths, resolved_paths has %d",- len(decoded.ResolvedPathsBase64), len(decoded.ResolvedPaths))- }-- paths = make([]string, len(decoded.ResolvedPathsBase64))- for i, encoded := range decoded.ResolvedPathsBase64 {- p, err := base64.StdEncoding.DecodeString(encoded)- if err != nil {- return fmt.Errorf("decoding resolved_paths_b64: %w", err)- }- paths[i] = string(p)- }- }-- *m = Member{- OriginalCommit: decoded.OriginalCommit,- Commit: decoded.Commit,- Actor: decoded.Actor,- Rewritten: decoded.Rewritten,- ResolvedPaths: paths,- }-- return nil-}--// Resolved reports whether the member was landed with resolved conflicts.-func (m Member) Resolved() bool {- return len(m.ResolvedPaths) > 0-}+// Member is one change that a batched land folded into a changeset, see+// member.Member. The type and its JSON form live in a package of their own so+// that the graft CLI prints members without linking the log.+type Member = member.Member// ValidateMembers checks that every member names both its original and its// landed commit. It returns the first problem found, with the member's index.@@ -181,17 +86,6 @@ func normalizeMembers(members []Member) []Member {return out}-// allValidUTF8 reports whether every string of ss is valid UTF-8.-func allValidUTF8(ss []string) bool {- for _, s := range ss {- if !utf8.ValidString(s) {- return false- }- }-- return true-}-// toValidUTF8 replaces invalid bytes with U+FFFD, like Append does for actors.func toValidUTF8(s string) string {if utf8.ValidString(s) {