@@ -14,32 +14,59 @@ pub struct CreateCommand {
1414 base : Option < String > ,
1515}
1616
17+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
18+ pub enum CreateOutcome {
19+ AlreadyExists ,
20+ Created ,
21+ }
22+
1723impl CreateCommand {
1824 pub fn new ( name : String , base : Option < String > ) -> Self {
1925 Self { name, base }
2026 }
2127
2228 pub fn execute ( & self , repo : & Repo ) -> color_eyre:: Result < ( ) > {
29+ let outcome = self . create_internal ( repo, false ) ?;
30+ match outcome {
31+ CreateOutcome :: Created | CreateOutcome :: AlreadyExists => self . enter_worktree ( repo) ,
32+ }
33+ }
34+
35+ pub fn create_without_enter (
36+ & self ,
37+ repo : & Repo ,
38+ quiet : bool ,
39+ ) -> color_eyre:: Result < CreateOutcome > {
40+ self . create_internal ( repo, quiet)
41+ }
42+
43+ fn enter_worktree ( & self , repo : & Repo ) -> color_eyre:: Result < ( ) > {
44+ CdCommand :: new ( self . name . clone ( ) , false ) . execute ( repo)
45+ }
46+
47+ fn create_internal ( & self , repo : & Repo , quiet : bool ) -> color_eyre:: Result < CreateOutcome > {
2348 let worktrees_dir = repo. ensure_worktrees_dir ( ) ?;
2449 let worktree_path = worktrees_dir. join ( & self . name ) ;
2550 let target_branch = self . name . as_str ( ) ;
2651 let base_branch = self . base . as_deref ( ) ;
2752
2853 if worktree_path. exists ( ) {
29- let name = format ! (
30- "{}" ,
31- self . name
32- . as_str( )
33- . if_supports_color( Stream :: Stdout , |text| {
34- format!( "{}" , text. cyan( ) . bold( ) )
35- } )
36- ) ;
37- println ! (
38- "Worktree `{}` already exists at `{}`." ,
39- name,
40- worktree_path. display( )
41- ) ;
42- return self . enter_worktree ( repo) ;
54+ if !quiet {
55+ let name = format ! (
56+ "{}" ,
57+ self . name
58+ . as_str( )
59+ . if_supports_color( Stream :: Stdout , |text| {
60+ format!( "{}" , text. cyan( ) . bold( ) )
61+ } )
62+ ) ;
63+ println ! (
64+ "Worktree `{}` already exists at `{}`." ,
65+ name,
66+ worktree_path. display( )
67+ ) ;
68+ }
69+ return Ok ( CreateOutcome :: AlreadyExists ) ;
4370 }
4471
4572 if let Some ( parent) = worktree_path. parent ( ) {
@@ -63,36 +90,34 @@ impl CreateCommand {
6390 )
6491 } ) ?;
6592
66- let name = format ! (
67- "{}" ,
68- target_branch. if_supports_color( Stream :: Stdout , |text| {
69- format!( "{}" , text. green( ) . bold( ) )
70- } )
71- ) ;
72- let path_raw = format ! ( "{}" , worktree_path. display( ) ) ;
73- let path = format ! (
74- "{}" ,
75- path_raw
76- . as_str( )
77- . if_supports_color( Stream :: Stdout , |text| { format!( "{}" , text. blue( ) ) } )
78- ) ;
79- if let Some ( base) = base_branch {
80- let base = format ! (
93+ if !quiet {
94+ let name = format ! (
8195 "{}" ,
82- base . if_supports_color( Stream :: Stdout , |text| {
83- format!( "{}" , text. magenta ( ) . bold( ) )
96+ target_branch . if_supports_color( Stream :: Stdout , |text| {
97+ format!( "{}" , text. green ( ) . bold( ) )
8498 } )
8599 ) ;
86- println ! ( "Created worktree `{}` at `{}` from `{}`." , name, path, base) ;
87- } else {
88- println ! ( "Created worktree `{}` at `{}`." , name, path) ;
100+ let path_raw = format ! ( "{}" , worktree_path. display( ) ) ;
101+ let path = format ! (
102+ "{}" ,
103+ path_raw
104+ . as_str( )
105+ . if_supports_color( Stream :: Stdout , |text| { format!( "{}" , text. blue( ) ) } )
106+ ) ;
107+ if let Some ( base) = base_branch {
108+ let base = format ! (
109+ "{}" ,
110+ base. if_supports_color( Stream :: Stdout , |text| {
111+ format!( "{}" , text. magenta( ) . bold( ) )
112+ } )
113+ ) ;
114+ println ! ( "Created worktree `{}` at `{}` from `{}`." , name, path, base) ;
115+ } else {
116+ println ! ( "Created worktree `{}` at `{}`." , name, path) ;
117+ }
89118 }
90119
91- self . enter_worktree ( repo)
92- }
93-
94- fn enter_worktree ( & self , repo : & Repo ) -> color_eyre:: Result < ( ) > {
95- CdCommand :: new ( self . name . clone ( ) , false ) . execute ( repo)
120+ Ok ( CreateOutcome :: Created )
96121 }
97122}
98123
0 commit comments