config

package
v1.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package config provides configuration management for wt.

Index

Constants

View Source
const ProjectIncludeFile = ".worktreeinclude"

ProjectIncludeFile is the repo-root file naming files to copy into new worktrees, using .gitignore pattern syntax.

Variables

View Source
var ConfigFields = []FieldMeta{
	{
		Key:       "clean",
		Desc:      "When true, 'wt fork' creates clean worktrees without copying changes.",
		ValidOpts: "true, false",
		Parse:     parseBool,
		Get:       func(c GlobalConfig) any { return c.Clean },
		Format:    func(v any) string { return fmt.Sprintf("%v", v) },
	},
	{
		Key:       "merge",
		Desc:      "Default merge mode for 'wt merge'.",
		ValidOpts: "squash, rebase, staged",
		Parse:     func(s string) (any, error) { return ParseMergeMode(s) },
		Get:       func(c GlobalConfig) any { return c.Merge },
		Format:    func(v any) string { m, _ := v.(MergeMode); return string(m) },
	},
	{
		Key:       "direnv",
		Desc:      "When true, automatically run 'direnv allow' in new worktrees.",
		ValidOpts: "true, false",
		Parse:     parseBool,
		Get:       func(c GlobalConfig) any { return c.Direnv },
		Format:    func(v any) string { return fmt.Sprintf("%v", v) },
	},
	{
		Key:       "post_create",
		Desc:      "Shell commands to run in new worktrees after creation.",
		ValidOpts: "shell commands",
		IsList:    true,
		Parse:     parseStringList,
		Get:       func(c GlobalConfig) any { return c.PostCreate },
		Format:    formatStringList,
	},
	{
		Key:       "repos",
		Desc:      "Repositories tracked by wt (auto-populated).",
		ValidOpts: "filesystem paths",
		IsList:    true,
		Parse:     parseStringList,
		Get:       func(c GlobalConfig) any { return c.Repos },
		Format:    formatStringList,
	},
}

ConfigFields defines metadata for all config fields (single source of truth).

View Source
var ErrNotList = errors.New("not a list field")

ErrNotList is returned when a list operation is attempted on a non-list field.

View Source
var ErrUnknownKey = errors.New("unknown config key")

ErrUnknownKey is returned when an unknown config key is requested.

Functions

func ListKeys

func ListKeys() []string

ListKeys returns keys for fields that support add/remove operations.

func SanitizePathComponent

func SanitizePathComponent(name string) string

SanitizePathComponent converts a name to a safe filesystem path component. Replaces "/" with "--" to avoid nested directories.

func UnknownKeyError

func UnknownKeyError(key string) error

UnknownKeyError returns an error wrapping ErrUnknownKey with context.

func UnsanitizePathComponent

func UnsanitizePathComponent(name string) string

UnsanitizePathComponent converts a sanitized path component back to original format. Reverses SanitizePathComponent: "fix--foo" -> "fix/foo"

func ValidKeys

func ValidKeys() []string

ValidKeys returns a slice of all valid config keys.

Types

type FieldMeta

type FieldMeta struct {
	Key       string
	Desc      string
	ValidOpts string
	IsList    bool
	Parse     func(string) (any, error)
	Get       func(GlobalConfig) any
	Format    func(any) string
}

FieldMeta describes a config field for display and file generation.

func GetFieldMeta

func GetFieldMeta(key string) *FieldMeta

GetFieldMeta returns metadata for a config key, or nil if not found.

func (*FieldMeta) FprintValue

func (f *FieldMeta) FprintValue(out io.Writer, cfg GlobalConfig)

FprintValue writes the field's value with description to the given writer.

type GlobalConfig

type GlobalConfig struct {
	Clean      bool      `yaml:"clean"`
	Merge      MergeMode `yaml:"merge,omitempty"`
	Direnv     bool      `yaml:"direnv"`
	PostCreate []string  `yaml:"post_create,omitempty"`
	Repos      []string  `yaml:"repos,omitempty"`
}

GlobalConfig holds the global wt configuration.

func DefaultConfig

func DefaultConfig() GlobalConfig

DefaultConfig returns a GlobalConfig with default values.

func (GlobalConfig) GetListValue

func (c GlobalConfig) GetListValue(key string) []string

GetListValue returns the []string value for a list field.

type GlobalStore

type GlobalStore struct {
	// contains filtered or unexported fields
}

GlobalStore manages global configuration persistence at ~/.wt.

func DefaultGlobalStore

func DefaultGlobalStore() *GlobalStore

DefaultGlobalStore returns a GlobalStore using WT_HOME or ~/.wt as the root directory.

func NewGlobalStore

func NewGlobalStore(rootDir string) *GlobalStore

NewGlobalStore creates a GlobalStore with the given root directory.

func (*GlobalStore) AddToConfigList

func (s *GlobalStore) AddToConfigList(key, item string) error

AddToConfigList appends an item to a list config field.

func (*GlobalStore) ConfigPath

func (s *GlobalStore) ConfigPath() string

ConfigPath returns the path to the config file.

func (*GlobalStore) IsManagedPath

func (s *GlobalStore) IsManagedPath(path string) bool

IsManagedPath reports whether the given path falls under the managed worktree directory.

func (*GlobalStore) ListWorktreeDirs

func (s *GlobalStore) ListWorktreeDirs() ([]WorktreeDir, error)

ListWorktreeDirs walks the worktree base directory two levels deep, returning one entry per <repo>/<wt> directory found.

func (*GlobalStore) LoadConfig

func (s *GlobalStore) LoadConfig() (cfg GlobalConfig, isNew bool, err error)

LoadConfig reads the global config from disk. Creates default if missing.

func (*GlobalStore) RegisterRepo

func (s *GlobalStore) RegisterRepo(repoDir string) error

RegisterRepo adds a repository path to the tracked repos list. Resolves symlinks before storing to avoid duplicates.

func (*GlobalStore) RemoveFromConfigList

func (s *GlobalStore) RemoveFromConfigList(key, item string) (string, error)

RemoveFromConfigList removes an item from a list config field by exact match.

func (*GlobalStore) RemoveFromConfigListByIndex

func (s *GlobalStore) RemoveFromConfigListByIndex(key string, index int) (string, error)

RemoveFromConfigListByIndex removes an item from a list config field by 0-based index.

func (*GlobalStore) RootDir

func (s *GlobalStore) RootDir() string

RootDir returns the store's root directory.

func (*GlobalStore) SaveDefaultConfig

func (s *GlobalStore) SaveDefaultConfig(cfg GlobalConfig) error

SaveDefaultConfig writes config with comments derived from ConfigFields metadata.

func (*GlobalStore) SetConfigValue

func (s *GlobalStore) SetConfigValue(key string, value any) error

SetConfigValue updates a single key in the config file, preserving comments.

func (*GlobalStore) SetConfigValueFromString

func (s *GlobalStore) SetConfigValueFromString(key, input string) error

SetConfigValueFromString parses a string input and updates the config file.

func (*GlobalStore) UserIncludePath

func (s *GlobalStore) UserIncludePath() string

UserIncludePath returns the path to the user-level worktreeinclude file.

func (*GlobalStore) WorktreePath

func (s *GlobalStore) WorktreePath(repoName, wtName string) string

WorktreePath returns the filesystem path for a worktree directory.

type MergeMode

type MergeMode string

MergeMode specifies how to merge worktree changes back to the parent branch.

const (
	// MergeModeSquash squashes all commits into one on the parent branch.
	MergeModeSquash MergeMode = "squash"

	// MergeModeRebase rebases commits onto the parent branch (preserves individual commits).
	MergeModeRebase MergeMode = "rebase"

	// MergeModeStaged applies changes as staged (no commit).
	MergeModeStaged MergeMode = "staged"
)

func ParseMergeMode

func ParseMergeMode(s string) (MergeMode, error)

ParseMergeMode converts a string to MergeMode, returning error for invalid values.

func (*MergeMode) UnmarshalYAML

func (m *MergeMode) UnmarshalYAML(unmarshal func(any) error) error

UnmarshalYAML validates MergeMode when reading from config.

type WorktreeDir

type WorktreeDir struct {
	RepoName string
	Name     string
	Path     string
}

WorktreeDir describes a worktree directory on disk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL