@@ -71,27 +71,43 @@ pub(crate) fn create_branch(
71
71
// Find the target stack and get its current head commit
72
72
let stacks = crate :: log:: stacks ( & ctx) ?;
73
73
let target_stack = stacks. iter ( ) . find ( |s| {
74
- s. heads . iter ( ) . any ( |head| head. name . to_string ( ) == target_branch_name)
74
+ s. heads
75
+ . iter ( )
76
+ . any ( |head| head. name . to_string ( ) == target_branch_name)
75
77
} ) ;
76
-
78
+
77
79
let target_stack = match target_stack {
78
80
Some ( s) => s,
79
- None => return Err ( anyhow:: anyhow!( "No stack found for branch '{}'" , target_branch_name) ) ,
81
+ None => {
82
+ return Err ( anyhow:: anyhow!(
83
+ "No stack found for branch '{}'" ,
84
+ target_branch_name
85
+ ) ) ;
86
+ }
80
87
} ;
81
-
82
- let target_stack_id = target_stack. id . ok_or_else ( || anyhow:: anyhow!( "Target stack has no ID" ) ) ?;
83
-
88
+
89
+ let target_stack_id = target_stack
90
+ . id
91
+ . ok_or_else ( || anyhow:: anyhow!( "Target stack has no ID" ) ) ?;
92
+
84
93
// Get the stack details to find the head commit
85
94
let target_stack_details = crate :: log:: stack_details ( & ctx, target_stack_id) ?;
86
95
if target_stack_details. branch_details . is_empty ( ) {
87
96
return Err ( anyhow:: anyhow!( "Target stack has no branch details" ) ) ;
88
97
}
89
-
98
+
90
99
// Find the target branch in the stack details
91
- let target_branch_details = target_stack_details. branch_details . iter ( ) . find ( |b|
92
- b. name == target_branch_name
93
- ) . ok_or_else ( || anyhow:: anyhow!( "Target branch '{}' not found in stack details" , target_branch_name) ) ?;
94
-
100
+ let target_branch_details = target_stack_details
101
+ . branch_details
102
+ . iter ( )
103
+ . find ( |b| b. name == target_branch_name)
104
+ . ok_or_else ( || {
105
+ anyhow:: anyhow!(
106
+ "Target branch '{}' not found in stack details" ,
107
+ target_branch_name
108
+ )
109
+ } ) ?;
110
+
95
111
// Get the head commit of the target branch
96
112
let target_head_oid = if !target_branch_details. commits . is_empty ( ) {
97
113
// Use the last local commit
@@ -100,10 +116,13 @@ pub(crate) fn create_branch(
100
116
// If no local commits, use the last upstream commit
101
117
target_branch_details. upstream_commits . last ( ) . unwrap ( ) . id
102
118
} else {
103
- return Err ( anyhow:: anyhow!( "Target branch '{}' has no commits" , target_branch_name) ) ;
119
+ return Err ( anyhow:: anyhow!(
120
+ "Target branch '{}' has no commits" ,
121
+ target_branch_name
122
+ ) ) ;
104
123
} ;
105
-
106
- // Create a new virtual branch
124
+
125
+ // Create a new virtual branch
107
126
let mut guard = project. exclusive_worktree_access ( ) ;
108
127
let create_request = BranchCreateRequest {
109
128
name : Some ( branch_name. to_string ( ) ) ,
@@ -112,12 +131,13 @@ pub(crate) fn create_branch(
112
131
selected_for_changes : None ,
113
132
} ;
114
133
115
- let new_stack_id = create_virtual_branch ( & ctx, & create_request, guard. write_permission ( ) ) ?;
116
-
134
+ let new_stack_id =
135
+ create_virtual_branch ( & ctx, & create_request, guard. write_permission ( ) ) ?;
136
+
117
137
// Now set up the new branch to start from the target branch's head
118
138
let vb_state = VirtualBranchesHandle :: new ( ctx. project ( ) . gb_dir ( ) ) ;
119
139
let mut new_stack = vb_state. get_stack ( new_stack_id. id ) ?;
120
-
140
+
121
141
// Set the head of the new stack to be the target branch's head
122
142
// This creates the stacking relationship
123
143
let gix_repo = ctx. repo ( ) . to_gix ( ) ?;
@@ -158,38 +178,35 @@ pub(crate) fn create_branch(
158
178
Ok ( ( ) )
159
179
}
160
180
161
- pub ( crate ) fn unapply_branch (
162
- repo_path : & Path ,
163
- _json : bool ,
164
- branch_id : & str ,
165
- ) -> anyhow:: Result < ( ) > {
181
+ pub ( crate ) fn unapply_branch ( repo_path : & Path , _json : bool , branch_id : & str ) -> anyhow:: Result < ( ) > {
166
182
let project = Project :: from_path ( repo_path) ?;
167
183
let mut ctx = CommandContext :: open ( & project, AppSettings :: load_from_default_path_creating ( ) ?) ?;
168
-
184
+
169
185
// Try to resolve the branch ID
170
186
let cli_ids = CliId :: from_str ( & mut ctx, branch_id) ?;
171
-
187
+
172
188
if cli_ids. is_empty ( ) {
173
189
return Err ( anyhow:: anyhow!(
174
190
"Branch '{}' not found. Try using a branch CLI ID or full branch name." ,
175
191
branch_id
176
192
) ) ;
177
193
}
178
-
194
+
179
195
if cli_ids. len ( ) > 1 {
180
- let matches: Vec < String > = cli_ids. iter ( ) . map ( |id| {
181
- match id {
196
+ let matches: Vec < String > = cli_ids
197
+ . iter ( )
198
+ . map ( |id| match id {
182
199
CliId :: Branch { name } => format ! ( "{} (branch '{}')" , id. to_string( ) , name) ,
183
- _ => format ! ( "{} ({})" , id. to_string( ) , id. kind( ) )
184
- }
185
- } ) . collect ( ) ;
200
+ _ => format ! ( "{} ({})" , id. to_string( ) , id. kind( ) ) ,
201
+ } )
202
+ . collect ( ) ;
186
203
return Err ( anyhow:: anyhow!(
187
204
"Branch '{}' is ambiguous. Matches: {}. Try using more characters or the full branch name." ,
188
205
branch_id,
189
206
matches. join( ", " )
190
207
) ) ;
191
208
}
192
-
209
+
193
210
let cli_id = & cli_ids[ 0 ] ;
194
211
let stack_id = match cli_id {
195
212
CliId :: Branch { .. } => {
@@ -204,7 +221,7 @@ pub(crate) fn unapply_branch(
204
221
}
205
222
} )
206
223
} ) ;
207
-
224
+
208
225
match stack {
209
226
Some ( s) => s. id . ok_or_else ( || anyhow:: anyhow!( "Stack has no ID" ) ) ?,
210
227
None => return Err ( anyhow:: anyhow!( "No stack found for branch '{}'" , branch_id) ) ,
@@ -218,25 +235,25 @@ pub(crate) fn unapply_branch(
218
235
) ) ;
219
236
}
220
237
} ;
221
-
238
+
222
239
let branch_name = match cli_id {
223
240
CliId :: Branch { name } => name,
224
241
_ => unreachable ! ( ) ,
225
242
} ;
226
-
243
+
227
244
println ! (
228
245
"Unapplying branch '{}' ({})" ,
229
246
branch_name. yellow( ) . bold( ) ,
230
247
branch_id. blue( ) . underline( )
231
248
) ;
232
-
249
+
233
250
unapply_stack ( & ctx, stack_id, Vec :: new ( ) ) ?;
234
-
251
+
235
252
println ! (
236
253
"{} Branch '{}' unapplied successfully!" ,
237
254
"✓" . green( ) . bold( ) ,
238
255
branch_name. yellow( ) . bold( )
239
256
) ;
240
-
257
+
241
258
Ok ( ( ) )
242
259
}
0 commit comments