fix(config): improve cross-platform path handling and regex efficiency
Code review fixes for bd config validate: - Use filepath.Join instead of string concatenation for paths - Use filepath.Dir instead of strings.LastIndex for parent directory - Move gitSSHRemotePattern regex to package-level var (compile once) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -14,6 +15,9 @@ import (
|
|||||||
"github.com/steveyegge/beads/internal/syncbranch"
|
"github.com/steveyegge/beads/internal/syncbranch"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// gitSSHRemotePattern matches standard git SSH remote URLs (user@host:path)
|
||||||
|
var gitSSHRemotePattern = regexp.MustCompile(`^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9._-]*:.+$`)
|
||||||
|
|
||||||
var configCmd = &cobra.Command{
|
var configCmd = &cobra.Command{
|
||||||
Use: "config",
|
Use: "config",
|
||||||
GroupID: "setup",
|
GroupID: "setup",
|
||||||
@@ -347,7 +351,7 @@ func validateSyncConfig(repoPath string) []string {
|
|||||||
var issues []string
|
var issues []string
|
||||||
|
|
||||||
// Load config.yaml directly from the repo path
|
// Load config.yaml directly from the repo path
|
||||||
configPath := repoPath + "/.beads/config.yaml"
|
configPath := filepath.Join(repoPath, ".beads", "config.yaml")
|
||||||
v := viper.New()
|
v := viper.New()
|
||||||
v.SetConfigType("yaml")
|
v.SetConfigType("yaml")
|
||||||
v.SetConfigFile(configPath)
|
v.SetConfigFile(configPath)
|
||||||
@@ -433,22 +437,19 @@ func isValidRemoteURL(url string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Also allow standard git remote patterns (user@host:path)
|
// Also allow standard git remote patterns (user@host:path)
|
||||||
// The host must have at least one character before the colon
|
return gitSSHRemotePattern.MatchString(url)
|
||||||
// Pattern: username@hostname:path where hostname has at least 2 chars
|
|
||||||
gitSSHPattern := regexp.MustCompile(`^[a-zA-Z0-9._-]+@[a-zA-Z0-9][a-zA-Z0-9._-]*:.+$`)
|
|
||||||
return gitSSHPattern.MatchString(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// findBeadsRepoRoot walks up from the given path to find the repo root (containing .beads)
|
// findBeadsRepoRoot walks up from the given path to find the repo root (containing .beads)
|
||||||
func findBeadsRepoRoot(startPath string) string {
|
func findBeadsRepoRoot(startPath string) string {
|
||||||
path := startPath
|
path := startPath
|
||||||
for {
|
for {
|
||||||
beadsDir := path + "/.beads"
|
beadsDir := filepath.Join(path, ".beads")
|
||||||
if info, err := os.Stat(beadsDir); err == nil && info.IsDir() {
|
if info, err := os.Stat(beadsDir); err == nil && info.IsDir() {
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
parent := path[:strings.LastIndex(path, "/")]
|
parent := filepath.Dir(path)
|
||||||
if parent == path || parent == "" {
|
if parent == path {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
path = parent
|
path = parent
|
||||||
|
|||||||
Reference in New Issue
Block a user