fix(worktree): reset retries to pristine base
Retry used to retain ignored and untracked output from an interrupted attempt, which CommitAll could publish later. Refresh now derives the tracked HEAD shape, removes every other worktree entry outside .git, and fails closed when the configured base cannot resolve.
This commit is contained in:
1 parent
dbb6550ab3
commit
196dd02a6d
4 files changed
+184
-16
No files matched your search
@@ -31,6 +31,7 @@ import (
|
||||
gitconfig "github.com/go-git/go-git/v6/config"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
gitclient "github.com/go-git/go-git/v6/plumbing/client"
|
||||
"github.com/go-git/go-git/v6/plumbing/filemode"
|
||||
"github.com/go-git/go-git/v6/plumbing/object"
|
||||
githttp "github.com/go-git/go-git/v6/plumbing/transport/http"
|
||||
)
|
||||
@@ -253,15 +254,93 @@ func (m *Manager) refresh(ctx context.Context, dir, base, branch string) error {
|
||||
}
|
||||
}
|
||||
|
||||
// Reset onto the freshly fetched base. A missing remote base is not
|
||||
// fatal: the branch may exist only locally on a first attempt.
|
||||
// Reset onto the freshly fetched base. This is a prepared clone, so a
|
||||
// missing base is configuration or repository drift and must fail closed.
|
||||
baseHash, err := resolveBase(r, base)
|
||||
if err != nil {
|
||||
return nil //nolint:nilerr // no remote base yet is a normal first-attempt state, not a failure
|
||||
return err
|
||||
}
|
||||
if err := wt.Reset(&git.ResetOptions{Commit: baseHash, Mode: git.HardReset}); err != nil {
|
||||
return fmt.Errorf("reset to %s: %w", remoteBase(base), err)
|
||||
}
|
||||
if err := cleanUntracked(r, dir); err != nil {
|
||||
return fmt.Errorf("clean abandoned worktree files: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cleanUntracked(r *git.Repository, dir string) error {
|
||||
head, err := r.Head()
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve HEAD: %w", err)
|
||||
}
|
||||
commit, err := r.CommitObject(head.Hash())
|
||||
if err != nil {
|
||||
return fmt.Errorf("load HEAD commit: %w", err)
|
||||
}
|
||||
tree, err := commit.Tree()
|
||||
if err != nil {
|
||||
return fmt.Errorf("load HEAD tree: %w", err)
|
||||
}
|
||||
files := make(map[string]struct{})
|
||||
dirs := make(map[string]struct{})
|
||||
opaqueDirs := make(map[string]struct{})
|
||||
if err := collectTrackedPaths(tree, "", files, dirs, opaqueDirs); err != nil {
|
||||
return err
|
||||
}
|
||||
return filepath.WalkDir(dir, func(path string, entry os.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
if path == dir {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(dir, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel = filepath.ToSlash(rel)
|
||||
if rel == ".git" {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if entry.IsDir() {
|
||||
if _, ok := opaqueDirs[rel]; ok {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if _, ok := dirs[rel]; ok {
|
||||
return nil
|
||||
}
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return err
|
||||
}
|
||||
return filepath.SkipDir
|
||||
}
|
||||
if _, ok := files[rel]; ok {
|
||||
return nil
|
||||
}
|
||||
return os.Remove(path)
|
||||
})
|
||||
}
|
||||
|
||||
func collectTrackedPaths(tree *object.Tree, prefix string, files, dirs, opaqueDirs map[string]struct{}) error {
|
||||
for _, entry := range tree.Entries {
|
||||
path := filepath.ToSlash(filepath.Join(prefix, entry.Name))
|
||||
switch entry.Mode {
|
||||
case filemode.Dir:
|
||||
dirs[path] = struct{}{}
|
||||
child, err := tree.Tree(entry.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load tracked directory %s: %w", path, err)
|
||||
}
|
||||
if err := collectTrackedPaths(child, path, files, dirs, opaqueDirs); err != nil {
|
||||
return err
|
||||
}
|
||||
case filemode.Submodule:
|
||||
opaqueDirs[path] = struct{}{}
|
||||
default:
|
||||
files[path] = struct{}{}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/go-git/go-git/v6"
|
||||
gitconfig "github.com/go-git/go-git/v6/config"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
"github.com/go-git/go-git/v6/plumbing/filemode"
|
||||
"github.com/go-git/go-git/v6/plumbing/object"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/container"
|
||||
@@ -198,7 +199,7 @@ func TestPrepareIsIdempotent(t *testing.T) {
|
||||
}
|
||||
// A marker file proves the second call reused the clone instead of
|
||||
// wiping and re-cloning it.
|
||||
marker := filepath.Join(dir1, "reuse-marker")
|
||||
marker := filepath.Join(dir1, ".git", "reuse-marker")
|
||||
if err := os.WriteFile(marker, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -232,6 +233,30 @@ func TestPrepareIsIdempotent(t *testing.T) {
|
||||
func TestPrepareRecoversFromADirtyWorktreeOnRetry(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
seedDir := filepath.Join(t.TempDir(), "ignore-seed")
|
||||
seedRepo, err := git.PlainClone(seedDir, &git.CloneOptions{URL: filepath.Join(host, "acme", "todo.git")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeSeedCommit(t, seedRepo, seedDir, ".gitignore", "bin/\n", "chore: ignore build output")
|
||||
if err := os.Symlink("README.md", filepath.Join(seedDir, "readme-link")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seedWorktree, err := seedRepo.Worktree()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := seedWorktree.Add("readme-link"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sig := testSignature()
|
||||
if _, err := seedWorktree.Commit("chore: add tracked symlink", &git.CommitOptions{Author: sig, Committer: sig}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
baseRef := plumbing.NewBranchReferenceName(testBase)
|
||||
if err := seedRepo.Push(&git.PushOptions{RemoteName: git.DefaultRemoteName, RefSpecs: []gitconfig.RefSpec{gitconfig.RefSpec(baseRef + ":" + baseRef)}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := newManager(t, host)
|
||||
|
||||
dir, branch, err := m.Prepare(ctx, "acme", "todo", testBase, 9, "fix: retry robustness", "", "bug")
|
||||
@@ -247,6 +272,12 @@ func TestPrepareRecoversFromADirtyWorktreeOnRetry(t *testing.T) {
|
||||
if err := os.WriteFile(filepath.Join(dir, "scratch.txt"), []byte("uncommitted new file\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(dir, "bin"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "bin", "stale"), []byte("ignored output\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dir2, branch2, err := m.Prepare(ctx, "acme", "todo", testBase, 9, "fix: retry robustness", "", "bug")
|
||||
if err != nil {
|
||||
@@ -256,12 +287,9 @@ func TestPrepareRecoversFromADirtyWorktreeOnRetry(t *testing.T) {
|
||||
t.Errorf("retry Prepare() = (%q, %q), want the same (%q, %q)", dir2, branch2, dir, branch)
|
||||
}
|
||||
|
||||
// refresh hard-resets to base immediately after checkout succeeds, so
|
||||
// the tracked file's uncommitted edit must be gone -- restored to
|
||||
// base, not merged in. (An untracked leftover like scratch.txt is real
|
||||
// git's job for `git clean`, not `reset --hard`, and is harmless here:
|
||||
// it doesn't block the retry, which is the only thing this test cares
|
||||
// about.)
|
||||
// Retry starts from the base commit: neither tracked edits nor untracked
|
||||
// output abandoned by the previous attempt may survive into the commit
|
||||
// that CommitAll will publish for this attempt.
|
||||
content, err := os.ReadFile(filepath.Join(dir, "README.md"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -269,6 +297,53 @@ func TestPrepareRecoversFromADirtyWorktreeOnRetry(t *testing.T) {
|
||||
if string(content) != "seed\n" {
|
||||
t.Errorf("README.md = %q, want the base commit's content restored", content)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "scratch.txt")); !os.IsNotExist(err) {
|
||||
t.Fatalf("abandoned untracked file survived retry: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "bin", "stale")); !os.IsNotExist(err) {
|
||||
t.Fatalf("ignored abandoned file survived retry: %v", err)
|
||||
}
|
||||
if target, err := os.Readlink(filepath.Join(dir, "readme-link")); err != nil || target != "README.md" {
|
||||
t.Fatalf("tracked symlink = (%q, %v), want README.md", target, err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, preparedSentinel)); err != nil {
|
||||
t.Fatalf("retry removed prepared sentinel: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(dir, "intended.txt"), []byte("new attempt\n"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if changed, err := m.CommitAll(ctx, dir, "feat: intended retry change"); err != nil || !changed {
|
||||
t.Fatalf("CommitAll() = (%v, %v), want a commit", changed, err)
|
||||
}
|
||||
files, err := m.ChangedFiles(ctx, dir, testBase)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(files) != 1 || files[0] != "intended.txt" {
|
||||
t.Fatalf("ChangedFiles() = %v, want only intended.txt", files)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectTrackedPathsClassifiesGitEntries(t *testing.T) {
|
||||
files := make(map[string]struct{})
|
||||
dirs := make(map[string]struct{})
|
||||
opaque := make(map[string]struct{})
|
||||
tree := &object.Tree{Entries: []object.TreeEntry{
|
||||
{Name: "file", Mode: filemode.Regular},
|
||||
{Name: "link", Mode: filemode.Symlink},
|
||||
{Name: "dependency", Mode: filemode.Submodule},
|
||||
}}
|
||||
if err := collectTrackedPaths(tree, "", files, dirs, opaque); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"file", "link"} {
|
||||
if _, ok := files[name]; !ok {
|
||||
t.Errorf("tracked file %q was not classified", name)
|
||||
}
|
||||
}
|
||||
if _, ok := opaque["dependency"]; !ok {
|
||||
t.Error("submodule was not classified as an opaque directory")
|
||||
}
|
||||
}
|
||||
|
||||
// An interrupted clone leaves a directory with no sentinel. Prepare must
|
||||
@@ -323,6 +398,18 @@ func TestPrepareErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareRefreshRejectsUnknownBase(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
host := newLocalRemote(t, "acme", "todo")
|
||||
m := newManager(t, host)
|
||||
if _, _, err := m.Prepare(ctx, "acme", "todo", testBase, 12, "fix: base", "", "bug"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := m.Prepare(ctx, "acme", "todo", "missing-base", 12, "fix: base", "", "bug"); err == nil {
|
||||
t.Fatal("prepared worktree accepted an unknown base")
|
||||
}
|
||||
}
|
||||
|
||||
// Every read-only operation must report a clear error on a directory that
|
||||
// is not a repository, rather than panicking or returning a zero value
|
||||
// that reads as "no changes".
|
||||
@@ -564,7 +651,7 @@ func TestPrepareMigratesMatchingLegacyWorktree(t *testing.T) {
|
||||
if err := os.WriteFile(filepath.Join(legacy, preparedSentinel), nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
marker := filepath.Join(legacy, "legacy-marker")
|
||||
marker := filepath.Join(legacy, ".git", "legacy-marker")
|
||||
if err := os.WriteFile(marker, nil, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -576,7 +663,7 @@ func TestPrepareMigratesMatchingLegacyWorktree(t *testing.T) {
|
||||
if dir != m.Dir("acme", "todo", 44) {
|
||||
t.Fatalf("Prepare dir = %q, want %q", dir, m.Dir("acme", "todo", 44))
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "legacy-marker")); err != nil {
|
||||
if _, err := os.Stat(filepath.Join(dir, ".git", "legacy-marker")); err != nil {
|
||||
t.Fatalf("legacy worktree was not migrated: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(legacy); !os.IsNotExist(err) {
|
||||
|
||||
Reference in new issue
Block a user