@@ -2,7 +2,6 @@ package config
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "os"
87 "path/filepath"
@@ -21,8 +20,8 @@ const (
2120)
2221
2322type Config struct {
24- WorktreeDir string `toml:"worktree_dir"`
25- DefaultSource string `toml:"default_source"`
23+ WorktreeDir string `toml:"worktree_dir,omitempty "`
24+ DefaultSource string `toml:"default_source,omitempty "`
2625 CopyFiles []string `toml:"copy_files"`
2726 PostCreate []string `toml:"post_create,omitempty"`
2827
@@ -44,12 +43,6 @@ type ModuleConfig struct {
4443 WorkDir string `toml:"work_dir,omitempty"`
4544}
4645
47- // Validation error messages for required config fields.
48- const (
49- ErrMsgEmptyWorktreeDir = "worktree_dir must not be empty"
50- ErrMsgEmptyDefaultSource = "default_source must not be empty"
51- )
52-
5346// IsAutoDetectDeps returns whether automatic dependency detection is enabled.
5447// Defaults to true when Deps or AutoDetect is not configured.
5548func (c * Config ) IsAutoDetectDeps () bool {
@@ -59,29 +52,41 @@ func (c *Config) IsAutoDetectDeps() bool {
5952 return * c .Deps .AutoDetect
6053}
6154
62- // Validate checks that required config fields are present .
55+ // Validate checks config fields for consistency .
6356func (c * Config ) Validate () error {
64- var errs []error
57+ return nil
58+ }
59+
60+ // DefaultWorktreeDir returns the conventional worktree directory path for a repo.
61+ func DefaultWorktreeDir (repoName string ) string {
62+ return "../" + repoName + "-worktrees"
63+ }
64+
65+ // DefaultCopyFiles returns the default list of files copied to new worktrees.
66+ func DefaultCopyFiles () []string {
67+ return []string {".env" , ".env.local" , ".envrc" , ".tool-versions" }
68+ }
69+
70+ // FillDefaults fills missing config fields with auto-derived values.
71+ // repoName is used for WorktreeDir; defaultBranch is used for DefaultSource.
72+ func (c * Config ) FillDefaults (repoName , defaultBranch string ) {
6573 if c .WorktreeDir == "" {
66- errs = append ( errs , errors . New ( ErrMsgEmptyWorktreeDir ) )
74+ c . WorktreeDir = DefaultWorktreeDir ( repoName )
6775 }
6876 if c .DefaultSource == "" {
69- errs = append ( errs , errors . New ( ErrMsgEmptyDefaultSource ))
77+ c . DefaultSource = defaultBranch
7078 }
71- if len ( errs ) > 0 {
72- return fmt . Errorf ( "invalid config: %w" , errors . Join ( errs ... ) )
79+ if c . CopyFiles == nil {
80+ c . CopyFiles = DefaultCopyFiles ( )
7381 }
74- return nil
7582}
7683
7784type ctxKey struct {}
7885
7986func DefaultConfig (repoName , defaultBranch string ) * Config {
80- return & Config {
81- WorktreeDir : "../" + repoName + "-worktrees" ,
82- DefaultSource : defaultBranch ,
83- CopyFiles : []string {".env" , ".env.local" , ".envrc" , ".tool-versions" },
84- }
87+ cfg := & Config {}
88+ cfg .FillDefaults (repoName , defaultBranch )
89+ return cfg
8590}
8691
8792func Load (path string ) (* Config , error ) {
0 commit comments