@@ -162,10 +162,12 @@ pub mod stacks {
162
162
use but_settings:: AppSettings ;
163
163
use but_workspace:: { StacksFilter , stack_branches, ui} ;
164
164
use gitbutler_command_context:: CommandContext ;
165
+ use gitbutler_reference:: { Refname , RemoteRefname } ;
165
166
use gitbutler_stack:: StackId ;
166
167
use gix:: bstr:: ByteSlice ;
167
168
use gix:: refs:: Category ;
168
169
use std:: path:: Path ;
170
+ use std:: str:: FromStr ;
169
171
170
172
pub fn list (
171
173
current_dir : & Path ,
@@ -255,9 +257,40 @@ pub mod stacks {
255
257
/// Create a new stack containing only a branch with the given name.
256
258
fn create_stack_with_branch (
257
259
ctx : & CommandContext ,
260
+ project : gitbutler_project:: Project ,
258
261
name : & str ,
262
+ remote : bool ,
259
263
description : Option < & str > ,
260
- ) -> anyhow:: Result < ui:: StackEntryNoOpt > {
264
+ ) -> anyhow:: Result < ui:: StackEntry > {
265
+ let repo = ctx. gix_repo ( ) ?;
266
+ let remotes = repo. remote_names ( ) ;
267
+ if remote {
268
+ let remote_name = remotes
269
+ . first ( )
270
+ . map ( |r| r. to_str ( ) . unwrap ( ) )
271
+ . context ( "No remote found in repository" ) ?;
272
+
273
+ let ref_name = Refname :: from_str ( & format ! ( "refs/remotes/{remote_name}/{name}" ) ) ?;
274
+ let remote_ref_name = RemoteRefname :: new ( remote_name, name) ;
275
+
276
+ let ( stack_id, _) = gitbutler_branch_actions:: create_virtual_branch_from_branch (
277
+ ctx,
278
+ & ref_name,
279
+ Some ( remote_ref_name) ,
280
+ None ,
281
+ ) ?;
282
+
283
+ let stack_entries =
284
+ but_workspace:: stacks ( ctx, & project. gb_dir ( ) , & repo, Default :: default ( ) ) ?;
285
+ let stack_entry = stack_entries
286
+ . into_iter ( )
287
+ . find ( |entry| entry. id == Some ( stack_id) )
288
+ . ok_or_else ( || {
289
+ anyhow:: anyhow!( "Failed to find newly created stack with ID: {stack_id}" )
290
+ } ) ?;
291
+ return Ok ( stack_entry) ;
292
+ } ;
293
+
261
294
let creation_request = gitbutler_branch:: BranchCreateRequest {
262
295
name : Some ( name. to_string ( ) ) ,
263
296
..Default :: default ( )
@@ -277,7 +310,7 @@ pub mod stacks {
277
310
) ?;
278
311
}
279
312
280
- Ok ( stack_entry)
313
+ Ok ( stack_entry. into ( ) )
281
314
}
282
315
283
316
/// Add a branch to an existing stack.
@@ -326,6 +359,7 @@ pub mod stacks {
326
359
name : & str ,
327
360
description : Option < & str > ,
328
361
current_dir : & Path ,
362
+ remote : bool ,
329
363
use_json : bool ,
330
364
ws3 : bool ,
331
365
) -> anyhow:: Result < ( ) > {
@@ -348,7 +382,7 @@ pub mod stacks {
348
382
349
383
let stack_entry = match id {
350
384
Some ( id) => add_branch_to_stack ( & ctx, id, name, description, project. clone ( ) , & repo) ?,
351
- None => create_stack_with_branch ( & ctx, name, description) ?. into ( ) ,
385
+ None => create_stack_with_branch ( & ctx, project . clone ( ) , name, remote , description) ?,
352
386
} ;
353
387
354
388
if use_json {
0 commit comments