Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(engine): extract prepareLocalSnapshotRepository from prepareRepository (#405)

fx-kurumi1447979d3134
diff --git a/internal/engine/local_snapshot_repository_test.go b/internal/engine/local_snapshot_repository_test.go
new file mode 100644
index 00000000..2f759a61
--- /dev/null
+++ b/internal/engine/local_snapshot_repository_test.go
@@ -0,0 +1,67 @@
+package engine
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestPrepareLocalSnapshotRepositoryCommitsBaseline(t *testing.T) {
+ t.Parallel()
+ source := t.TempDir()
+ if err := os.WriteFile(filepath.Join(source, "app.txt"), []byte("hello"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ workspace := t.TempDir()
+ repository := domain.RepositoryContext{RepositoryID: "repo", LocalPath: source, DefaultBranch: "master", BaseBranch: "master"}
+ if err := prepareLocalSnapshotRepository(context.Background(), workspace, source, repository); err != nil {
+ t.Fatal(err)
+ }
+ if content, err := os.ReadFile(filepath.Join(workspace, "app.txt")); err != nil || string(content) != "hello" {
+ t.Fatalf("copied content = %q, %v", content, err)
+ }
+ branch, err := gitC(context.Background(), workspace, "branch", "--show-current")
+ if err != nil || strings.TrimSpace(branch) != "agentflow-base" {
+ t.Fatalf("branch = %q, %v", branch, err)
+ }
+ subject, err := gitC(context.Background(), workspace, "log", "-1", "--format=%s")
+ if err != nil || strings.TrimSpace(subject) != "LoopForge baseline" {
+ t.Fatalf("baseline commit = %q, %v", subject, err)
+ }
+}
+
+func TestPrepareLocalSnapshotRepositoryRejectsBranchRequests(t *testing.T) {
+ t.Parallel()
+ for name, repository := range map[string]domain.RepositoryContext{
+ "continue work branch": {RepositoryID: "repo", WorkBranch: "feature/x", RequireExistingWorkBranch: true},
+ "custom base branch": {RepositoryID: "repo", DefaultBranch: "master", BaseBranch: "release"},
+ "legacy custom base": {RepositoryID: "repo", BaseBranch: "develop"},
+ } {
+ source, workspace := t.TempDir(), t.TempDir()
+ err := prepareLocalSnapshotRepository(context.Background(), workspace, source, repository)
+ if err == nil || !strings.Contains(err.Error(), "local source is not a Git repository") {
+ t.Fatalf("%s: err = %v", name, err)
+ }
+ if _, statErr := os.Stat(filepath.Join(workspace, ".git")); !os.IsNotExist(statErr) {
+ t.Fatalf("%s: workspace was initialized before rejection", name)
+ }
+ }
+}
+
+func TestPrepareLocalSnapshotRepositoryAcceptsLegacyDefaultBase(t *testing.T) {
+ t.Parallel()
+ for _, base := range []string{"", "main", "master"} {
+ source, workspace := t.TempDir(), t.TempDir()
+ if err := os.WriteFile(filepath.Join(source, "app.txt"), []byte(base), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ repository := domain.RepositoryContext{RepositoryID: "repo", BaseBranch: base}
+ if err := prepareLocalSnapshotRepository(context.Background(), workspace, source, repository); err != nil {
+ t.Fatalf("base %q: %v", base, err)
+ }
+ }
+}
diff --git a/internal/engine/repository_workspace.go b/internal/engine/repository_workspace.go
index 913d4691..0f0ba4d0 100644
--- a/internal/engine/repository_workspace.go
+++ b/internal/engine/repository_workspace.go
@@ -134,29 +134,7 @@ func prepareRepository(ctx context.Context, workspace string, repository domain.
}
}
if localIsDir && !localIsGit && cloneURL == "" {
- if repository.RequireExistingWorkBranch {
- return fmt.Errorf("cannot continue branch %q: local source is not a Git repository", repository.WorkBranch)
- }
- base := strings.TrimSpace(repository.BaseBranch)
- defaultBranch := strings.TrimSpace(repository.DefaultBranch)
- legacyDefault := defaultBranch == "" && (base == "master" || base == "main")
- if base != "" && base != defaultBranch && !legacyDefault {
- return fmt.Errorf("cannot check out base branch %q: local source is not a Git repository", base)
- }
- cmd := exec.CommandContext(ctx, "cp", "-a", filepath.Clean(localPath)+"/.", workspace)
- if output, err := cmd.CombinedOutput(); err != nil {
- return fmt.Errorf("copy local source: %w: %s", err, strings.TrimSpace(string(output)))
- }
- for _, args := range [][]string{{"init", "-b", "agentflow-base"}, {"config", "user.email", "loopforge@local"}, {"config", "user.name", "LoopForge"}, {"add", "-A"}, {"commit", "-m", "LoopForge baseline", "--quiet"}} {
- cmd = exec.CommandContext(ctx, "git", append([]string{"-C", workspace}, args...)...)
- if output, err := cmd.CombinedOutput(); err != nil {
- return fmt.Errorf("snapshot local source: %w: %s", err, strings.TrimSpace(string(output)))
- }
- }
- if err := applyWorkspaceGitIdentity(ctx, workspace); err != nil {
- return err
- }
- return recordRepositoryBaseline(workspace, "")
+ return prepareLocalSnapshotRepository(ctx, workspace, localPath, repository)
}
remote := cloneURL
if remote == "" {
@@ -283,6 +261,36 @@ func prepareRepository(ctx context.Context, workspace string, repository domain.
return recordRepositoryBaseline(workspace, baseBranch)
}
+// prepareLocalSnapshotRepository copies a non-Git local source directory into
+// workspace and commits it as a single baseline on agentflow-base. Such a
+// source has no branches, so continuing a work branch or checking out a custom
+// base branch is refused.
+func prepareLocalSnapshotRepository(ctx context.Context, workspace, localPath string, repository domain.RepositoryContext) error {
+ if repository.RequireExistingWorkBranch {
+ return fmt.Errorf("cannot continue branch %q: local source is not a Git repository", repository.WorkBranch)
+ }
+ base := strings.TrimSpace(repository.BaseBranch)
+ defaultBranch := strings.TrimSpace(repository.DefaultBranch)
+ legacyDefault := defaultBranch == "" && (base == "master" || base == "main")
+ if base != "" && base != defaultBranch && !legacyDefault {
+ return fmt.Errorf("cannot check out base branch %q: local source is not a Git repository", base)
+ }
+ cmd := exec.CommandContext(ctx, "cp", "-a", filepath.Clean(localPath)+"/.", workspace)
+ if output, err := cmd.CombinedOutput(); err != nil {
+ return fmt.Errorf("copy local source: %w: %s", err, strings.TrimSpace(string(output)))
+ }
+ for _, args := range [][]string{{"init", "-b", "agentflow-base"}, {"config", "user.email", "loopforge@local"}, {"config", "user.name", "LoopForge"}, {"add", "-A"}, {"commit", "-m", "LoopForge baseline", "--quiet"}} {
+ cmd = exec.CommandContext(ctx, "git", append([]string{"-C", workspace}, args...)...)
+ if output, err := cmd.CombinedOutput(); err != nil {
+ return fmt.Errorf("snapshot local source: %w: %s", err, strings.TrimSpace(string(output)))
+ }
+ }
+ if err := applyWorkspaceGitIdentity(ctx, workspace); err != nil {
+ return err
+ }
+ return recordRepositoryBaseline(workspace, "")
+}
+
// seedEmptyRepository handles a freshly created Gongfeng repository with no
// commits: clone has no branch to check out, so neither an assistant
// workspace nor an SOP run could write the first file (the owner had to make an