Update title tool to reject branch names that already exist
Added a check to the title tool to verify that branches do not already exist
before setting them, which prevents potential issues with duplicate branch names.
Includes a new unit test to validate this behavior.
Co-Authored-By: sketch <hello@sketch.dev>
diff --git a/loop/agent.go b/loop/agent.go
index a130c96..d0f1ce7 100644
--- a/loop/agent.go
+++ b/loop/agent.go
@@ -704,6 +704,23 @@
return convo
}
+// branchExists reports whether branchName exists, either locally or in well-known remotes.
+func branchExists(dir, branchName string) bool {
+ refs := []string{
+ "refs/heads/",
+ "refs/remotes/origin/",
+ "refs/remotes/sketch-host/",
+ }
+ for _, ref := range refs {
+ cmd := exec.Command("git", "show-ref", "--verify", "--quiet", ref+branchName)
+ cmd.Dir = dir
+ if cmd.Run() == nil { // exit code 0 means branch exists
+ return true
+ }
+ }
+ return false
+}
+
func (a *Agent) titleTool() *ant.Tool {
title := &ant.Tool{
Name: "title",
@@ -745,6 +762,10 @@
}
branchName := "sketch/" + cleanBranchName(params.BranchName)
+ if branchExists(a.workingDir, branchName) {
+ return "", fmt.Errorf("branch %q already exists; please choose a different branch name", branchName)
+ }
+
a.SetTitleBranch(params.Title, branchName)
response := fmt.Sprintf("Title set to %q, branch name set to %q", params.Title, branchName)