Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Structured example (`t repo add nnutter/timber` defaults to the structured name;
Rename with `timber repo rename <old> <new>` to move between simple and structured names; standard-layout worktrees move with the repo.
Sorting `timber list --sort repo` orders by the full structured name, so the grouping prefix controls grouping.

Listing a GitHub owner with `timber config append strip_github_owners <login>` registers that owner's repositories under the short name instead: `timber repo add nnutter/timber` then becomes repo name `timber` at `~/worktrees/timber/.git`.
Explicit `--name` always wins, owner matching ignores case, and other hosts keep their full grouping.

Use `timber repo import <path>` to register an existing clean clone and recreate its worktrees (including the former main checkout) in this layout.
When invoked through the shell wrapper (`t repo import <path>`), the shell also `cd`s to `$HOME` after success.

Expand Down Expand Up @@ -97,21 +100,28 @@ npx skills add https://github.com/nnutter/timber

## Configuration

Run `timber config` to show settings and create the config file if absent.
`timber config show` prints every setting and creates the config file if absent.
The file is `$XDG_CONFIG_HOME/timber/config.toml` (fallback: `~/.config/timber/config.toml`).
The other subcommands read and change one setting by name: `get` prints its values one per line, `set` replaces the value, `append` adds values to a list setting, and `remove` (alias `rm`) resets a setting or drops matching list values.
Setting names complete in the shell, as do the values of boolean settings and the stored values of list settings when they are removed.

```bash
timber config show
timber config get auto_mise_trust
timber config set auto_mise_trust true
timber config append strip_github_owners nnutter acme
timber config remove strip_github_owners nnutter
timber config rm strip_github_owners
```

Automatic mise trust and automatic hk hook installation are disabled by default.
On the first eligible worktree creation in an interactive terminal, Timber offers to enable each feature.
The prompts default to No and save your answers, so Timber does not ask again.
After No, a prompt shows the matching `timber config` flag so you can enable the feature later.
An explicit `--no-auto-mise-trust` or `--no-auto-hk-install` also suppresses its prompt.
After No, a prompt shows the matching `timber config set ...` command so you can enable the feature later.
Setting either setting records the choice explicitly, which also suppresses its prompt.
Non-interactive creation does not prompt or record a choice.

```bash
timber config --auto-mise-trust
timber config --no-auto-mise-trust
timber config --auto-hk-install
timber config --no-auto-hk-install
```
`strip_github_owners` lists the owners whose GitHub repositories register under the short name; `repo add` and `repo import` drop a listed owner from names they derive from GitHub remotes.

Enabling automatic trust permits mise configuration in eligible worktrees to execute code.
Eligibility requires a root-level, regular `mise.toml` and a GitHub upstream owned by you or one of your organizations.
Expand Down Expand Up @@ -423,7 +433,7 @@ For full flags and examples, see `timber --help` and `timber <command> --help`.
| `repo fix` | Move bare repositories from the old data directory into the worktree root, repair worktree links, and set `push.default=current` |
| `repo remove` | Unregister a bare repository once its worktrees are gone |
| `repo rename` | Rename a repository and move its worktree directories |
| `config` | Show settings or change them with `--auto-mise-trust` / `--no-auto-mise-trust` / `--auto-hk-install` / `--no-auto-hk-install` |
| `config` | Show settings or change them with `show`, `get`, `set`, `append`, and `remove` (`rm`); keys and values complete in the shell |
| `create` | Create a worktree for a branch (`--no-herdr` skips the Herdr space) |
| `switch` | Shell-only: `cd` into a worktree, creating it first with `-c` |
| `tui` | Interactively open an existing worktree or create a new one |
Expand Down
89 changes: 89 additions & 0 deletions internal/cli/complete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"

"github.com/spf13/cobra"

coreconfig "github.com/nnutter/timber/internal/timber/config"
)

// Queries supplies the completion candidates.
Expand Down Expand Up @@ -177,3 +179,90 @@ func canonicalName(command *cobra.Command, word string) (string, bool) {
}
return "", false
}

// ConfigReader supplies stored settings for config value completion.
type ConfigReader interface {
ReadConfig() (coreconfig.Settings, error)
}

// ConfigValueMode selects which values a config command offers.
type ConfigValueMode int

const (
// ConfigValueSet offers true and false for boolean settings.
ConfigValueSet ConfigValueMode = iota
// ConfigValueRemove offers the stored values of a list setting.
ConfigValueRemove
// ConfigValueNone offers no value candidates.
ConfigValueNone
)

// ConfigArgs completes config setting names, then the value that suits the
// selected setting and command.
func ConfigArgs(reader ConfigReader, mode ConfigValueMode) func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
return func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
candidates := make([]string, 0)
for _, key := range coreconfig.Keys() {
candidates = append(candidates, key.Name+" "+key.Description)
}
return matchingPrefix(candidates, toComplete), cobra.ShellCompDirectiveNoFileComp
}
switch mode {
case ConfigValueSet:
if len(args) > 1 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return matchingPrefix(boolValues(args[0]), toComplete), cobra.ShellCompDirectiveNoFileComp
case ConfigValueRemove:
return listValues(reader, args[0], args[1:], toComplete)
default:
return nil, cobra.ShellCompDirectiveNoFileComp
}
}
}

func boolValues(name string) []string {
key, err := coreconfig.LookupKey(name)
if err != nil {
return nil
}
if key.Kind != coreconfig.KindBool {
return nil
}
return []string{"true", "false"}
}

func listValues(reader ConfigReader, name string, selected []string, toComplete string) ([]string, cobra.ShellCompDirective) {
key, err := coreconfig.LookupKey(name)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
if key.Kind != coreconfig.KindList {
return nil, cobra.ShellCompDirectiveNoFileComp
}
settings, err := reader.ReadConfig()
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
if len(selected) > 0 {
if err := coreconfig.Remove(&settings, name, selected); err != nil {
return nil, cobra.ShellCompDirectiveError
}
}
values, err := coreconfig.Get(settings, name)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
return matchingPrefix(values, toComplete), cobra.ShellCompDirectiveNoFileComp
}

func matchingPrefix(candidates []string, prefix string) []string {
matches := make([]string, 0, len(candidates))
for _, candidate := range candidates {
if strings.HasPrefix(candidate, prefix) {
matches = append(matches, candidate)
}
}
return matches
}
26 changes: 26 additions & 0 deletions internal/cli/completion_functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ func TestWorktreeCompletionAddsAtWhenNameIsAmbiguous(t *testing.T) {
assert.Contains(t, qualified, at(secondaryName, "feature/login"))
}

func TestConfigCompletionOffersSettingsAndValues(t *testing.T) {
t.Parallel()
runtime := testRuntime(t)
require.NoError(t, runTimberCommandWithRuntime(t, runtime, "config", "append", "strip_github_owners", "nnutter", "acme").err)

keys := runCompleteWithRuntime(t, runtime, "config", "get", "")
assert.Contains(t, keys, "auto_mise_trust\t")
assert.Contains(t, keys, "strip_github_owners\t")
assert.NotContains(t, keys, "auto_mise_trust_decided")

values := runCompleteWithRuntime(t, runtime, "config", "set", "auto_mise_trust", "")
assert.Contains(t, values, "true")
assert.Contains(t, values, "false")

owners := runCompleteWithRuntime(t, runtime, "config", "remove", "strip_github_owners", "")
assert.Contains(t, owners, "nnutter")

remaining := runCompleteWithRuntime(t, runtime, "config", "remove", "strip_github_owners", "ACME", "")
assert.Contains(t, remaining, "nnutter")
assert.NotContains(t, remaining, "acme")
assert.NotContains(t, runCompleteWithRuntime(t, runtime, "config", "set", "auto_mise_trust", "true", ""), "false")

// Appending takes values that do not exist yet, so there is nothing to offer.
assert.NotContains(t, runCompleteWithRuntime(t, runtime, "config", "append", "auto_mise_trust", ""), "true")
}

func TestAliasCompletionExpandsToCommandName(t *testing.T) {
t.Parallel()
testRepository := newTestRepository(t)
Expand Down
126 changes: 100 additions & 26 deletions internal/cli/config/config.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,133 @@
package config

import (
"fmt"

"github.com/BurntSushi/toml"
"github.com/spf13/cobra"

"github.com/nnutter/timber/internal/cli/complete"
coreconfig "github.com/nnutter/timber/internal/timber/config"
)

// Store loads and saves Timber settings.
type Store interface {
LoadConfig() (coreconfig.Settings, error)
ReadConfig() (coreconfig.Settings, error)
SaveConfig(coreconfig.Settings) error
}

// New constructs the config command and its subcommands.
func New(store Store) *cobra.Command {
var enable, disable, enableHK, disableHK bool
command := &cobra.Command{
Use: "config",
Short: "Show or change Timber settings",
Args: cobra.NoArgs,
RunE: func(command *cobra.Command, _ []string) error {
return command.Help()
},
}
command.AddCommand(
showCommand(store),
getCommand(store),
setCommand(store),
appendCommand(store),
removeCommand(store),
)
return command
}

func showCommand(store Store) *cobra.Command {
return &cobra.Command{
Use: "show",
Short: "Show all settings",
Args: cobra.NoArgs,
RunE: func(command *cobra.Command, _ []string) error {
settings, err := store.LoadConfig()
if err != nil {
return err
}
changed := false
if command.Flags().Changed("auto-mise-trust") || command.Flags().Changed("no-auto-mise-trust") {
settings.AutoMiseTrust = enable
if command.Flags().Changed("no-auto-mise-trust") {
settings.AutoMiseTrust = !disable
}
settings.AutoMiseTrustDecided = true
changed = true
return toml.NewEncoder(command.OutOrStdout()).Encode(settings)
},
}
}

func getCommand(store Store) *cobra.Command {
return &cobra.Command{
Use: "get <setting>",
Short: "Print a setting's value",
Args: cobra.ExactArgs(1),
ValidArgsFunction: complete.ConfigArgs(store, complete.ConfigValueNone),
RunE: func(command *cobra.Command, args []string) error {
settings, err := store.ReadConfig()
if err != nil {
return err
}
if command.Flags().Changed("auto-hk-install") || command.Flags().Changed("no-auto-hk-install") {
settings.AutoHKInstall = enableHK
if command.Flags().Changed("no-auto-hk-install") {
settings.AutoHKInstall = !disableHK
}
settings.AutoHKInstallDecided = true
changed = true
values, err := coreconfig.Get(settings, args[0])
if err != nil {
return err
}
if changed {
if err := store.SaveConfig(settings); err != nil {
for _, value := range values {
if _, err := fmt.Fprintln(command.OutOrStdout(), value); err != nil {
return err
}
}
return toml.NewEncoder(command.OutOrStdout()).Encode(settings)
return nil
},
}
command.Flags().BoolVar(&enable, "auto-mise-trust", false, "Trust mise.toml in worktrees owned by you or your GitHub organizations")
command.Flags().BoolVar(&disable, "no-auto-mise-trust", false, "Disable automatic mise trust")
command.MarkFlagsMutuallyExclusive("auto-mise-trust", "no-auto-mise-trust")
command.Flags().BoolVar(&enableHK, "auto-hk-install", false, "Install hk hooks in worktrees owned by you or your GitHub organizations")
command.Flags().BoolVar(&disableHK, "no-auto-hk-install", false, "Disable automatic hk hook installation")
command.MarkFlagsMutuallyExclusive("auto-hk-install", "no-auto-hk-install")
return command
}

func setCommand(store Store) *cobra.Command {
return &cobra.Command{
Use: "set <setting> <value>",
Short: "Set a setting's value",
Args: cobra.ExactArgs(2),
ValidArgsFunction: complete.ConfigArgs(store, complete.ConfigValueSet),
RunE: func(_ *cobra.Command, args []string) error {
return changeSettings(store, func(settings *coreconfig.Settings) error {
return coreconfig.Set(settings, args[0], args[1:])
})
},
}
}

func appendCommand(store Store) *cobra.Command {
return &cobra.Command{
Use: "append <setting> <value>...",
Short: "Append values to a list setting",
Args: cobra.MinimumNArgs(2),
ValidArgsFunction: complete.ConfigArgs(store, complete.ConfigValueNone),
RunE: func(_ *cobra.Command, args []string) error {
return changeSettings(store, func(settings *coreconfig.Settings) error {
return coreconfig.Append(settings, args[0], args[1:])
})
},
}
}

func removeCommand(store Store) *cobra.Command {
return &cobra.Command{
Use: "remove <setting> [value...]",
Aliases: []string{"rm"},
Short: "Reset a setting or remove list values",
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: complete.ConfigArgs(store, complete.ConfigValueRemove),
RunE: func(_ *cobra.Command, args []string) error {
return changeSettings(store, func(settings *coreconfig.Settings) error {
return coreconfig.Remove(settings, args[0], args[1:])
})
},
}
}

// changeSettings loads the settings, applies change, and saves the result.
func changeSettings(store Store, change func(*coreconfig.Settings) error) error {
settings, err := store.LoadConfig()
if err != nil {
return err
}
if err := change(&settings); err != nil {
return err
}
return store.SaveConfig(settings)
}
Loading
Loading