Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions app/cli/internal/trace/git/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,6 @@ func (c *ExecClient) StagedFiles(repoRoot string) ([]string, error) {
return strings.Split(strings.TrimRight(out, "\n"), "\n"), nil
}

// SnapshotWorktree returns a signature of the working tree (repo-relative path
// → content hash). The walk is git-binary-independent, so it shares the
// implementation used by GoGitClient.
func (c *ExecClient) SnapshotWorktree(repoRoot string) (map[string]string, error) {
return snapshotWorktree(repoRoot)
}

// IsMergeCommit reports whether the commit at sha has more than one parent.
func (c *ExecClient) IsMergeCommit(repoRoot, sha string) (bool, error) {
cmd := exec.Command("git", "rev-list", "--no-walk", "--parents", sha)
Expand Down
5 changes: 0 additions & 5 deletions app/cli/internal/trace/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ type Client interface {
GeneratedMatcher(repoRoot string) func(path string) bool
// StagedFiles returns repo-relative paths of files staged for the current commit.
StagedFiles(repoRoot string) ([]string, error)
// SnapshotWorktree returns a signature of the working tree as a map of
// repo-relative path (forward slashes) → content hash, skipping the .git
// directory and any .gitignore'd paths. Used to detect files changed by an
// agent-run shell command by diffing a before/after signature.
SnapshotWorktree(repoRoot string) (map[string]string, error)
// SnapshotIndex returns staged regular-file paths and their blob hashes.
SnapshotIndex(repoRoot string) (map[string]string, error)
// IndexFile returns the staged content for a repo-relative path.
Expand Down
108 changes: 0 additions & 108 deletions app/cli/internal/trace/git/gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@
package git

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
iofs "io/fs"
"os"
"path/filepath"
"sort"
"strings"
"time"
Expand All @@ -32,7 +26,6 @@ import (
gogit "github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing"
"github.com/go-git/go-git/v6/plumbing/format/gitattributes"
"github.com/go-git/go-git/v6/plumbing/format/gitignore"
"github.com/go-git/go-git/v6/plumbing/object"
"github.com/go-git/go-git/v6/utils/merkletrie"
)
Expand Down Expand Up @@ -295,107 +288,6 @@ func (c *GoGitClient) StagedFiles(repoRoot string) ([]string, error) {
return files, nil
}

// SnapshotWorktree returns a signature of the working tree (repo-relative path
// → SHA-256 content hash), honoring .gitignore and skipping the .git directory.
func (c *GoGitClient) SnapshotWorktree(repoRoot string) (map[string]string, error) {
return snapshotWorktree(repoRoot)
}

// snapshotWorktree walks repoRoot and hashes every regular, non-ignored file.
// gitignore patterns are read in-process via go-git's parser (best-effort: an
// unreadable .gitignore yields no patterns rather than an error). Symlinks and
// unreadable files are skipped. The walk never fails the caller — attribution
// is best-effort and must not block the agent.
func snapshotWorktree(repoRoot string) (map[string]string, error) {
wtfs := osfs.New(repoRoot)
patterns, _ := gitignore.ReadPatterns(wtfs, nil)
matcher := gitignore.NewMatcher(patterns)
tracked := trackedPaths(repoRoot)

sig := make(map[string]string)
err := filepath.WalkDir(repoRoot, func(p string, d iofs.DirEntry, err error) error {
if err != nil {
return nil // skip unreadable entries, keep walking
}

rel, relErr := filepath.Rel(repoRoot, p)
if relErr != nil || rel == "." {
return nil
}
slashRel := filepath.ToSlash(rel)
parts := strings.Split(slashRel, "/")

if d.IsDir() {
if d.Name() == ".git" || matcher.Match(parts, true) {
return filepath.SkipDir
}

return nil
}

if !d.Type().IsRegular() {
return nil
}
// Ignore rules apply only to untracked files, matching git: a tracked
// file that also matches a .gitignore pattern (e.g. force-added
// generated code) is still part of the working tree and must be
// snapshotted so edits to it are attributed. (Tracked files nested
// inside an ignored directory are the one gap — that directory is
// pruned above for walk performance.)
if matcher.Match(parts, false) && !tracked[slashRel] {
return nil
}

h, herr := hashFileContent(p)
if herr != nil {
return nil
}
sig[slashRel] = h

return nil
})

return sig, err
}

// trackedPaths returns the set of repo-relative paths (forward-slash separated)
// recorded in the git index. Best-effort: returns nil when the repository or
// its index cannot be read, in which case ignore rules apply to every file.
func trackedPaths(repoRoot string) map[string]bool {
repo, err := openRepo(repoRoot)
if err != nil {
return nil
}

idx, err := repo.Storer.Index()
if err != nil {
return nil
}

tracked := make(map[string]bool, len(idx.Entries))
for _, e := range idx.Entries {
tracked[e.Name] = true
}

return tracked
}

// hashFileContent returns the hex-encoded SHA-256 of the file's content.
func hashFileContent(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer func() { _ = f.Close() }()

h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return "", err
}

return hex.EncodeToString(h.Sum(nil)), nil
}

// IsMergeCommit reports whether the commit at sha has more than one parent.
func (c *GoGitClient) IsMergeCommit(repoRoot, sha string) (bool, error) {
repo, err := openRepo(repoRoot)
Expand Down
11 changes: 5 additions & 6 deletions app/cli/internal/trace/git/gogit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func TestGoGitClient_Worktree(t *testing.T) {
})
}

func TestGoGitClient_SnapshotWorktree(t *testing.T) {
func TestSnapshotWorktree(t *testing.T) {
root := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(root, "keep.txt"), []byte("hello\n"), 0600))
require.NoError(t, os.MkdirAll(filepath.Join(root, "sub"), 0755))
Expand All @@ -485,8 +485,7 @@ func TestGoGitClient_SnapshotWorktree(t *testing.T) {
require.NoError(t, os.MkdirAll(filepath.Join(root, ".git"), 0755))
require.NoError(t, os.WriteFile(filepath.Join(root, ".git", "config"), []byte("[core]\n"), 0600))

c := NewGoGitClient()
sig, err := c.SnapshotWorktree(root)
sig, _, err := SnapshotWorktreeCached(root, nil)
require.NoError(t, err)

// Non-ignored files present (forward-slash keys), ignored + .git excluded.
Expand All @@ -500,13 +499,13 @@ func TestGoGitClient_SnapshotWorktree(t *testing.T) {
// Modifying a file changes its signature; adding one adds a key.
require.NoError(t, os.WriteFile(filepath.Join(root, "keep.txt"), []byte("hello world\n"), 0600))
require.NoError(t, os.WriteFile(filepath.Join(root, "new.txt"), []byte("new\n"), 0600))
sig2, err := c.SnapshotWorktree(root)
sig2, _, err := SnapshotWorktreeCached(root, nil)
require.NoError(t, err)
assert.NotEqual(t, sig["keep.txt"], sig2["keep.txt"])
assert.Contains(t, sig2, "new.txt")
}

func TestGoGitClient_SnapshotWorktree_TrackedIgnoredFile(t *testing.T) {
func TestSnapshotWorktree_TrackedIgnoredFile(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git binary not found")
}
Expand All @@ -530,7 +529,7 @@ func TestGoGitClient_SnapshotWorktree_TrackedIgnoredFile(t *testing.T) {
run("add", "normal.txt", ".gitignore")
run("commit", "-m", "init")

sig, err := NewGoGitClient().SnapshotWorktree(root)
sig, _, err := SnapshotWorktreeCached(root, nil)
require.NoError(t, err)

assert.Contains(t, sig, "tracked.gen", "force-added tracked file must be snapshotted despite matching .gitignore")
Expand Down
Loading
Loading