11pub mod git;
22pub mod pruning;
3+ pub mod vars;
4+ pub mod worktree;
35
46use std:: {
57 collections:: HashMap ,
@@ -31,7 +33,7 @@ fn styles() -> clap::builder::Styles {
3133 . placeholder ( AnsiColor :: Cyan . on_default ( ) )
3234}
3335
34- const NO_HOOKS : & str = "core.hooksPath=/dev/null" ;
36+ use vars :: { LOKI_NEW_PREFIX , LOKI_REBASE_TARGET , LOKI_WORKTREE_BASE , NO_HOOKS } ;
3537
3638#[ derive( Debug , Parser ) ]
3739struct CommitOptions {
@@ -94,6 +96,53 @@ enum RepoSubcommand {
9496 Stats ( RepoStatsOptions ) ,
9597}
9698
99+ #[ derive( Debug , Subcommand ) ]
100+ enum WorktreeSubcommand {
101+ /// Create a new worktree and branch.
102+ #[ clap( visible_alias = "a" ) ]
103+ Add {
104+ /// Optional prefix to prepend to the branch name.
105+ #[ clap( long, env = LOKI_NEW_PREFIX ) ]
106+ prefix : Option < String > ,
107+
108+ /// Base ref to create the worktree from.
109+ #[ clap( short, long, default_value = "origin/main" , env = LOKI_WORKTREE_BASE ) ]
110+ base : String ,
111+
112+ /// Name parts joined with dashes to form the worktree and branch name.
113+ name : Vec < String > ,
114+ } ,
115+
116+ /// Remove a worktree and its associated branch.
117+ #[ clap( visible_alias = "r" ) ]
118+ Remove {
119+ /// Optional prefix used when the branch was created.
120+ #[ clap( long, env = LOKI_NEW_PREFIX ) ]
121+ prefix : Option < String > ,
122+
123+ /// Force removal of a dirty worktree.
124+ #[ clap( short, long) ]
125+ force : bool ,
126+
127+ /// Worktree name. If omitted, inferred from the current directory.
128+ name : Vec < String > ,
129+ } ,
130+
131+ /// List all worktrees.
132+ #[ clap( visible_alias = "l" ) ]
133+ List ,
134+
135+ /// Print a cd command for switching to a worktree (use with eval).
136+ ///
137+ /// bash/zsh: eval "$(lk w s <name>)"
138+ /// PowerShell: lk w s <name> | Invoke-Expression
139+ #[ clap( visible_alias = "s" ) ]
140+ Switch {
141+ /// Worktree name. If omitted, switches to the main worktree.
142+ name : Vec < String > ,
143+ } ,
144+ }
145+
97146#[ derive( Parser ) ]
98147#[ clap( version, about, author, color = clap:: ColorChoice :: Auto , styles = styles( ) ) ]
99148enum Cli {
@@ -102,7 +151,7 @@ enum Cli {
102151 #[ clap( visible_alias = "n" ) ]
103152 New {
104153 /// Optional prefix to prepend to the generated branch name.
105- #[ clap( long, env = " LOKI_NEW_PREFIX" ) ]
154+ #[ clap( long, env = LOKI_NEW_PREFIX ) ]
106155 prefix : Option < String > ,
107156
108157 /// List of names to join with dashes to form a valid branch name.
@@ -138,7 +187,7 @@ enum Cli {
138187 /// Rebase the current branch onto the target branch after fetching.
139188 Rebase {
140189 /// The branch to rebase onto.
141- #[ clap( default_value = "main" , env = " LOKI_REBASE_TARGET" ) ]
190+ #[ clap( default_value = "main" , env = LOKI_REBASE_TARGET ) ]
142191 target : String ,
143192
144193 /// Start an interactive rebase.
@@ -159,13 +208,18 @@ enum Cli {
159208 command : RepoSubcommand ,
160209 } ,
161210
211+ /// Manage git worktrees.
212+ #[ clap( visible_alias = "w" ) ]
213+ Worktree {
214+ #[ clap( subcommand) ]
215+ command : WorktreeSubcommand ,
216+ } ,
217+
162218 /// Push the main branch to the release branch.
163219 #[ clap( visible_alias = "r" ) ]
164220 Release ,
165221}
166222
167- const LOKI_NEW_PREFIX : & str = "LOKI_NEW_PREFIX" ;
168-
169223fn main ( ) -> Result < ( ) , String > {
170224 let cli = Cli :: parse ( ) ;
171225
@@ -181,6 +235,18 @@ fn main() -> Result<(), String> {
181235 Cli :: Repo {
182236 command : RepoSubcommand :: Stats ( options) ,
183237 } => repo_stats ( options) ,
238+ Cli :: Worktree { command } => match command {
239+ WorktreeSubcommand :: Add { name, base, prefix } => {
240+ worktree:: worktree_add ( name, base, prefix. as_deref ( ) )
241+ }
242+ WorktreeSubcommand :: Remove {
243+ name,
244+ force,
245+ prefix,
246+ } => worktree:: worktree_remove ( name, * force, prefix. as_deref ( ) ) ,
247+ WorktreeSubcommand :: List => worktree:: worktree_list ( ) ,
248+ WorktreeSubcommand :: Switch { name } => worktree:: worktree_switch ( name) ,
249+ } ,
184250 Cli :: Release => release ( ) ,
185251 }
186252}
0 commit comments