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.

【架构优化】合并 gitwork/workbranch.go 中 push/pull 重复的非工作分支严格校验为 verifyOffWorkBranch (#452)

fx-kurumi47818f54b739
diff --git a/internal/gitwork/workbranch.go b/internal/gitwork/workbranch.go
index ffbc1ffd..6d4d96af 100644
--- a/internal/gitwork/workbranch.go
+++ b/internal/gitwork/workbranch.go
@@ -30,17 +30,9 @@ func PushExistingWorkBranch(ctx context.Context, workspace, branch, message stri
func pushWorkBranch(ctx context.Context, workspace, branch, message string, requireExisting bool) (string, error) {
branch = strings.TrimSpace(branch)
if !onWorkBranch(ctx, workspace, branch) {
- if requireExisting {
- if _, err := fetchExistingWorkBranch(ctx, workspace, branch); err != nil {
- return "", err
- }
- if current, _ := C(ctx, workspace, "branch", "--show-current"); strings.TrimSpace(current) != branch {
- return "", fmt.Errorf("工作区未检出已有工作分支 %s", branch)
- }
- }
// A merge/rebase in progress is left for its repair node, just as
- // for generated branches. Strict mode still validated the remote.
- return "", nil
+ // for generated branches. Strict mode still validates the remote.
+ return "", verifyOffWorkBranch(ctx, workspace, branch, requireExisting)
}
var expectedRemote string
if requireExisting {
@@ -145,15 +137,7 @@ func PullExistingWorkBranch(ctx context.Context, workspace, branch string) (stri
func pullWorkBranch(ctx context.Context, workspace, branch string, requireExisting bool) (string, error) {
branch = strings.TrimSpace(branch)
if !onWorkBranch(ctx, workspace, branch) {
- if requireExisting {
- if _, err := fetchExistingWorkBranch(ctx, workspace, branch); err != nil {
- return "", err
- }
- if current, _ := C(ctx, workspace, "branch", "--show-current"); strings.TrimSpace(current) != branch {
- return "", fmt.Errorf("工作区未检出已有工作分支 %s", branch)
- }
- }
- return "", nil
+ return "", verifyOffWorkBranch(ctx, workspace, branch, requireExisting)
}
trackingRef := "refs/remotes/origin/" + branch
if _, err := fetchExistingWorkBranch(ctx, workspace, branch); err != nil {
@@ -194,6 +178,23 @@ func pullWorkBranch(ctx context.Context, workspace, branch string, requireExisti
return head, nil
}
+// verifyOffWorkBranch handles a workspace that is not on the work branch
+// (or has a merge/rebase in progress). Generated branches are skipped; an
+// explicitly chosen existing branch must still exist remotely and be the
+// checked-out branch.
+func verifyOffWorkBranch(ctx context.Context, workspace, branch string, requireExisting bool) error {
+ if !requireExisting {
+ return nil
+ }
+ if _, err := fetchExistingWorkBranch(ctx, workspace, branch); err != nil {
+ return err
+ }
+ if current, _ := C(ctx, workspace, "branch", "--show-current"); strings.TrimSpace(current) != branch {
+ return fmt.Errorf("工作区未检出已有工作分支 %s", branch)
+ }
+ return nil
+}
+
// fetchExistingWorkBranch fetches only refs/heads, never a same-named tag.
// It validates the current remote even if a stale tracking ref already exists.
func fetchExistingWorkBranch(ctx context.Context, workspace, branch string) (string, error) {
diff --git a/internal/gitwork/workbranch_off_branch_test.go b/internal/gitwork/workbranch_off_branch_test.go
new file mode 100644
index 00000000..3b4bcdb9
--- /dev/null
+++ b/internal/gitwork/workbranch_off_branch_test.go
@@ -0,0 +1,59 @@
+package gitwork
+
+import (
+ "context"
+ "strings"
+ "testing"
+)
+
+func TestVerifyOffWorkBranch(t *testing.T) {
+ ctx := context.Background()
+ t.Run("generated branch is skipped", func(t *testing.T) {
+ _, checkout := workBranchFixture(t)
+ if err := verifyOffWorkBranch(ctx, checkout, "feature/missing", false); err != nil {
+ t.Fatalf("non-strict check must pass: %v", err)
+ }
+ })
+ t.Run("strict rejects missing remote", func(t *testing.T) {
+ _, checkout := workBranchFixture(t)
+ err := verifyOffWorkBranch(ctx, checkout, "feature/missing", true)
+ if err == nil || !strings.Contains(err.Error(), "无法读取已有远端分支 feature/missing") {
+ t.Fatalf("err=%v", err)
+ }
+ })
+ t.Run("strict rejects other checkout", func(t *testing.T) {
+ _, checkout := workBranchFixture(t)
+ workBranchGit(t, checkout, "checkout", "-q", "main")
+ err := verifyOffWorkBranch(ctx, checkout, "feature/existing", true)
+ if err == nil || err.Error() != "工作区未检出已有工作分支 feature/existing" {
+ t.Fatalf("err=%v", err)
+ }
+ })
+ t.Run("strict accepts checked-out branch", func(t *testing.T) {
+ _, checkout := workBranchFixture(t)
+ if err := verifyOffWorkBranch(ctx, checkout, "feature/existing", true); err != nil {
+ t.Fatalf("err=%v", err)
+ }
+ })
+}
+
+// Push and pull share the off-branch check, so both must reject an explicit
+// branch that is not checked out and both must skip a generated one.
+func TestWorkBranchSyncOffBranchParity(t *testing.T) {
+ ctx := context.Background()
+ _, checkout := workBranchFixture(t)
+ workBranchGit(t, checkout, "checkout", "-q", "main")
+ want := "工作区未检出已有工作分支 feature/existing"
+ if _, err := PushExistingWorkBranch(ctx, checkout, "feature/existing", "must fail"); err == nil || err.Error() != want {
+ t.Fatalf("strict push err=%v", err)
+ }
+ if _, err := PullExistingWorkBranch(ctx, checkout, "feature/existing"); err == nil || err.Error() != want {
+ t.Fatalf("strict pull err=%v", err)
+ }
+ if head, err := PushWorkBranch(ctx, checkout, "feature/existing", "skip"); err != nil || head != "" {
+ t.Fatalf("generated push head=%q err=%v", head, err)
+ }
+ if head, err := PullWorkBranch(ctx, checkout, "feature/existing"); err != nil || head != "" {
+ t.Fatalf("generated pull head=%q err=%v", head, err)
+ }
+}