arche / internal/cli/cmd_worktree.go

commit 154431fd
 1package cli
 2
 3import (
 4	"fmt"
 5
 6	"github.com/spf13/cobra"
 7)
 8
 9var worktreeCmd = &cobra.Command{
10	Use:   "worktree",
11	Short: "Manage multiple working trees sharing one store",
12	Long: `Multiple working trees let you check out different bookmarks into
13separate directories while sharing the same store.db and issue tracker.
14
15Each linked worktree has its own HEAD and working copy, but all read and write
16objects from the central .arche/ directory of the main repository.`,
17}
18
19var worktreeAddBookmark string
20
21var worktreeAddCmd = &cobra.Command{
22	Use:   "add <path> [--bookmark|-b <name>]",
23	Short: "Create a new linked worktree",
24	Long: `Creates a new working tree at <path> and materializes the tip of
25<bookmark> (or HEAD if no bookmark is given) into that directory.
26
27A .arche-wt sentinel file is written into <path> so that arche commands
28run from inside that directory automatically use the shared store.`,
29	Args: cobra.ExactArgs(1),
30	RunE: func(cmd *cobra.Command, args []string) error {
31		r := openRepo()
32		defer r.Close()
33
34		path := args[0]
35		if err := r.AddWorktree(path, worktreeAddBookmark); err != nil {
36			return err
37		}
38
39		fmt.Printf("Worktree created at %s\n", path)
40		if worktreeAddBookmark != "" {
41			fmt.Printf("  bookmark: %s\n", worktreeAddBookmark)
42		}
43		return nil
44	},
45}
46
47var worktreeListCmd = &cobra.Command{
48	Use:   "list",
49	Short: "List all linked worktrees",
50	RunE: func(cmd *cobra.Command, args []string) error {
51		r := openRepo()
52		defer r.Close()
53
54		wts, err := r.ListWorktrees()
55		if err != nil {
56			return err
57		}
58
59		if len(wts) == 0 {
60			fmt.Println("No linked worktrees.")
61			return nil
62		}
63
64		for _, wt := range wts {
65			fmt.Printf("%-20s  %s\n", wt.Name, wt.Path)
66		}
67		return nil
68	},
69}
70
71var worktreeRemoveCmd = &cobra.Command{
72	Use:   "remove <name>",
73	Short: "Unregister a linked worktree",
74	Long: `Removes the worktree registration and its .arche-wt sentinel file.
75The working-copy files at the worktree path are left in place.`,
76	Args: cobra.ExactArgs(1),
77	RunE: func(cmd *cobra.Command, args []string) error {
78		r := openRepo()
79		defer r.Close()
80
81		name := args[0]
82		if err := r.RemoveWorktree(name); err != nil {
83			return err
84		}
85
86		fmt.Printf("Worktree %q removed.\n", name)
87		return nil
88	},
89}
90
91func init() {
92	worktreeAddCmd.Flags().StringVarP(&worktreeAddBookmark, "bookmark", "b", "",
93		"bookmark to check out into the new worktree (default: HEAD)")
94	worktreeCmd.AddCommand(worktreeAddCmd, worktreeListCmd, worktreeRemoveCmd)
95}