@@ -3,28 +3,19 @@ use but_settings::AppSettings;
33use but_workspace:: { StackId , ui:: StackEntry } ;
44use gitbutler_command_context:: CommandContext ;
55use gitbutler_project:: Project ;
6- use serde:: Serialize ;
76use std:: io:: { self , Write } ;
87
98mod list;
109
11- #[ derive( Debug , Serialize ) ]
12- #[ serde( untagged) ]
13- enum BranchNewResponse {
14- Success ( BranchNewOutput ) ,
15- Error ( BranchNewError ) ,
16- }
17-
18- #[ derive( Debug , Serialize ) ]
19- struct BranchNewOutput {
20- branch : String ,
21- #[ serde( skip_serializing_if = "Option::is_none" ) ]
22- anchor : Option < String > ,
23- }
10+ mod json {
11+ use serde:: Serialize ;
2412
25- #[ derive( Debug , Serialize ) ]
26- struct BranchNewError {
27- error : String ,
13+ #[ derive( Debug , Serialize ) ]
14+ pub struct BranchNewOutput {
15+ pub branch : String ,
16+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
17+ pub anchor : Option < String > ,
18+ }
2819}
2920
3021#[ derive( Debug , clap:: Parser ) ]
@@ -82,23 +73,9 @@ pub async fn handle(cmd: Option<Subcommands>, project: &Project, json: bool) ->
8273 let ctx =
8374 CommandContext :: open ( project, AppSettings :: load_from_default_path_creating ( ) ?) ?;
8475 // Get branch name or use canned name
85- let branch_name = if let Some ( name) = branch_name {
86- let repo = ctx. gix_repo ( ) ?;
87- if repo. try_find_reference ( name. as_str ( ) ) ?. is_some ( ) {
88- if json {
89- let response = BranchNewResponse :: Error ( BranchNewError {
90- error : format ! ( "Branch '{}' already exists" , name) ,
91- } ) ;
92- println ! ( "{}" , serde_json:: to_string_pretty( & response) ?) ;
93- } else {
94- println ! ( "Branch '{name}' already exists" ) ;
95- }
96- return Ok ( ( ) ) ;
97- }
98- name. clone ( )
99- } else {
100- but_api:: workspace:: canned_branch_name ( project. id ) ?
101- } ;
76+ let branch_name = branch_name
77+ . map ( Ok :: < _ , but_api:: error:: Error > )
78+ . unwrap_or_else ( || but_api:: workspace:: canned_branch_name ( project. id ) ) ?;
10279
10380 // Store anchor string for JSON output
10481 let anchor_for_json = anchor. clone ( ) ;
@@ -155,10 +132,10 @@ pub async fn handle(cmd: Option<Subcommands>, project: &Project, json: bool) ->
155132 ) ?;
156133
157134 if json {
158- let response = BranchNewResponse :: Success ( BranchNewOutput {
135+ let response = json :: BranchNewOutput {
159136 branch : branch_name,
160137 anchor : anchor_for_json,
161- } ) ;
138+ } ;
162139 println ! ( "{}" , serde_json:: to_string_pretty( & response) ?) ;
163140 } else if atty:: is ( Stream :: Stdout ) {
164141 println ! ( "Created branch {branch_name}" ) ;
0 commit comments