Skip to content

Commit a88fd8a

Browse files
authored
Merge pull request #10341 from gitbutlerapp/testing-remote-branch-support
Testing CLI: Create stack from remote branch
2 parents 1ccf084 + 62768ab commit a88fd8a

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

crates/but-testing/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ pub enum Subcommands {
215215
/// This is the place where some metadata about the branch can be stored.
216216
#[clap(long, short = 'd')]
217217
description: Option<String>,
218+
/// Whether the branch is a remote branch
219+
#[clap(long, short = 'u', default_value_t = false)]
220+
remote: bool,
218221
},
219222
/// Create a reference at the given position (dependent and independent)
220223
CreateReference {

crates/but-testing/src/command/mod.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ pub mod stacks {
162162
use but_settings::AppSettings;
163163
use but_workspace::{StacksFilter, stack_branches, ui};
164164
use gitbutler_command_context::CommandContext;
165+
use gitbutler_reference::{Refname, RemoteRefname};
165166
use gitbutler_stack::StackId;
166167
use gix::bstr::ByteSlice;
167168
use gix::refs::Category;
168169
use std::path::Path;
170+
use std::str::FromStr;
169171

170172
pub fn list(
171173
current_dir: &Path,
@@ -259,9 +261,40 @@ pub mod stacks {
259261
/// Create a new stack containing only a branch with the given name.
260262
fn create_stack_with_branch(
261263
ctx: &CommandContext,
264+
project: gitbutler_project::Project,
262265
name: &str,
266+
remote: bool,
263267
description: Option<&str>,
264-
) -> anyhow::Result<ui::StackEntryNoOpt> {
268+
) -> anyhow::Result<ui::StackEntry> {
269+
let repo = ctx.gix_repo()?;
270+
let remotes = repo.remote_names();
271+
if remote {
272+
let remote_name = remotes
273+
.first()
274+
.map(|r| r.to_str().unwrap())
275+
.context("No remote found in repository")?;
276+
277+
let ref_name = Refname::from_str(&format!("refs/remotes/{remote_name}/{name}"))?;
278+
let remote_ref_name = RemoteRefname::new(remote_name, name);
279+
280+
let (stack_id, _) = gitbutler_branch_actions::create_virtual_branch_from_branch(
281+
ctx,
282+
&ref_name,
283+
Some(remote_ref_name),
284+
None,
285+
)?;
286+
287+
let stack_entries =
288+
but_workspace::stacks(ctx, &project.gb_dir(), &repo, Default::default())?;
289+
let stack_entry = stack_entries
290+
.into_iter()
291+
.find(|entry| entry.id == Some(stack_id))
292+
.ok_or_else(|| {
293+
anyhow::anyhow!("Failed to find newly created stack with ID: {stack_id}")
294+
})?;
295+
return Ok(stack_entry);
296+
};
297+
265298
let creation_request = gitbutler_branch::BranchCreateRequest {
266299
name: Some(name.to_string()),
267300
..Default::default()
@@ -281,7 +314,7 @@ pub mod stacks {
281314
)?;
282315
}
283316

284-
Ok(stack_entry)
317+
Ok(stack_entry.into())
285318
}
286319

287320
/// Add a branch to an existing stack.
@@ -330,6 +363,7 @@ pub mod stacks {
330363
name: &str,
331364
description: Option<&str>,
332365
current_dir: &Path,
366+
remote: bool,
333367
use_json: bool,
334368
ws3: bool,
335369
) -> anyhow::Result<()> {
@@ -353,7 +387,7 @@ pub mod stacks {
353387

354388
let stack_entry = match id {
355389
Some(id) => add_branch_to_stack(&ctx, id, name, description, project.clone(), &repo)?,
356-
None => create_stack_with_branch(&ctx, name, description)?.into(),
390+
None => create_stack_with_branch(&ctx, project.clone(), name, remote, description)?,
357391
};
358392

359393
if use_json {

crates/but-testing/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ async fn main() -> Result<()> {
184184
id,
185185
branch_name,
186186
description,
187+
remote,
187188
} => match (branch_name, id) {
188189
(Some(branch_name), maybe_id) => command::stacks::create_branch(
189190
*maybe_id,
190191
branch_name,
191192
description.as_deref(),
192193
&args.current_dir,
194+
*remote,
193195
args.json,
194196
args.v3,
195197
),

0 commit comments

Comments
 (0)