From 31d30e866593eb7f1e4311f7700003bb9ced6b3b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Thu, 29 Aug 2024 15:16:01 +0200 Subject: [PATCH] CLI with `branch list-remotes` to list remote branches --- .../gitbutler-branch-actions/src/actions.rs | 6 ++-- crates/gitbutler-branch-actions/src/lib.rs | 2 +- crates/gitbutler-branch-actions/src/remote.rs | 29 +++++++++++-------- .../tests/extra/mod.rs | 2 +- crates/gitbutler-cli/src/args.rs | 2 ++ crates/gitbutler-cli/src/command/vbranch.rs | 4 +++ crates/gitbutler-cli/src/main.rs | 1 + .../gitbutler-tauri/src/virtual_branches.rs | 2 +- 8 files changed, 30 insertions(+), 18 deletions(-) diff --git a/crates/gitbutler-branch-actions/src/actions.rs b/crates/gitbutler-branch-actions/src/actions.rs index 4629682228..801a7a4216 100644 --- a/crates/gitbutler-branch-actions/src/actions.rs +++ b/crates/gitbutler-branch-actions/src/actions.rs @@ -8,7 +8,7 @@ use crate::{ branch::get_uncommited_files, branch_manager::BranchManagerExt, file::RemoteBranchFile, - remote::{get_branch_data, list_remote_branches, RemoteBranch, RemoteBranchData}, + remote::{get_branch_data, list_local_branches, RemoteBranch, RemoteBranchData}, VirtualBranchesExt, }; use anyhow::{Context, Result}; @@ -474,9 +474,9 @@ impl VirtualBranchActions { branch::push(&ctx, branch_id, with_force, &helper, askpass) } - pub fn list_remote_branches(project: Project) -> Result> { + pub fn list_local_branches(project: Project) -> Result> { let ctx = CommandContext::open(&project)?; - list_remote_branches(&ctx) + list_local_branches(&ctx) } pub fn get_remote_branch_data( diff --git a/crates/gitbutler-branch-actions/src/lib.rs b/crates/gitbutler-branch-actions/src/lib.rs index 8ffae0891a..a22d26bd02 100644 --- a/crates/gitbutler-branch-actions/src/lib.rs +++ b/crates/gitbutler-branch-actions/src/lib.rs @@ -18,7 +18,7 @@ mod file; pub use file::{Get, RemoteBranchFile}; mod remote; -pub use remote::{list_remote_branches, RemoteBranch, RemoteBranchData, RemoteCommit}; +pub use remote::{list_local_branches, RemoteBranch, RemoteBranchData, RemoteCommit}; pub mod conflicts; diff --git a/crates/gitbutler-branch-actions/src/remote.rs b/crates/gitbutler-branch-actions/src/remote.rs index b6c49a4d2a..0e4b887ce2 100644 --- a/crates/gitbutler-branch-actions/src/remote.rs +++ b/crates/gitbutler-branch-actions/src/remote.rs @@ -10,15 +10,15 @@ use gitbutler_repo::{LogUntil, RepoActionsExt, RepositoryExt}; use gitbutler_serde::BStringForFrontend; use serde::Serialize; -// this struct is a mapping to the view `RemoteBranch` type in Typescript -// found in src-tauri/src/routes/repo/[project_id]/types.ts -// -// it holds data calculated for presentation purposes of one Git branch -// with comparison data to the Target commit, determining if it is mergeable, -// and how far ahead or behind the Target it is. -// an array of them can be requested from the frontend to show in the sidebar -// Tray and should only contain branches that have not been converted into -// virtual branches yet (ie, we have no `Branch` struct persisted in our data. +/// this struct is a mapping to the view `RemoteBranch` type in Typescript +/// found in src-tauri/src/routes/repo/[project_id]/types.ts +/// +/// it holds data calculated for presentation purposes of one Git branch +/// with comparison data to the Target commit, determining if it is mergeable, +/// and how far ahead or behind the Target it is. +/// an array of them can be requested from the frontend to show in the sidebar +/// Tray and should only contain branches that have not been converted into +/// virtual branches yet (ie, we have no `Branch` struct persisted in our data. #[derive(Debug, Clone, Serialize, PartialEq)] #[serde(rename_all = "camelCase")] pub struct RemoteBranch { @@ -57,9 +57,14 @@ pub struct RemoteCommit { pub parent_ids: Vec, } -// for legacy purposes, this is still named "remote" branches, but it's actually -// a list of all the normal (non-gitbutler) git branches. -pub fn list_remote_branches(ctx: &CommandContext) -> Result> { +/// Return information on all local branches, while skipping gitbutler-specific branches in `refs/heads`. +/// +/// Note to be confused with `list_branches()`, which is used for the new branch listing. +/// +/// # Previous notes +/// For legacy purposes, this is still named "remote" branches, but it's actually +/// a list of all the normal (non-gitbutler) git branches. +pub fn list_local_branches(ctx: &CommandContext) -> Result> { let default_target = default_target(&ctx.project().gb_dir())?; let mut remote_branches = vec![]; diff --git a/crates/gitbutler-branch-actions/tests/extra/mod.rs b/crates/gitbutler-branch-actions/tests/extra/mod.rs index 857adb5949..22407d0583 100644 --- a/crates/gitbutler-branch-actions/tests/extra/mod.rs +++ b/crates/gitbutler-branch-actions/tests/extra/mod.rs @@ -1345,7 +1345,7 @@ fn detect_mergeable_branch() -> Result<()> { vb_state.set_branch(branch4.clone())?; let remotes = - gitbutler_branch_actions::list_remote_branches(ctx).expect("failed to list remotes"); + gitbutler_branch_actions::list_local_branches(ctx).expect("failed to list remotes"); let _remote1 = &remotes .iter() .find(|b| b.name.to_string() == "refs/remotes/origin/remote_branch") diff --git a/crates/gitbutler-cli/src/args.rs b/crates/gitbutler-cli/src/args.rs index 9b94fc0c5f..ece070c8b7 100644 --- a/crates/gitbutler-cli/src/args.rs +++ b/crates/gitbutler-cli/src/args.rs @@ -38,6 +38,8 @@ pub mod vbranch { #[derive(Debug, clap::Subcommand)] pub enum SubCommands { + /// List all local branches that aren't GitButler specific. + ListLocal, /// Provide the current state of all applied virtual branches. Status, /// Make the named branch the default so all worktree or index changes are associated with it automatically. diff --git a/crates/gitbutler-cli/src/command/vbranch.rs b/crates/gitbutler-cli/src/command/vbranch.rs index 4dd7e75d6d..425780e65b 100644 --- a/crates/gitbutler-cli/src/command/vbranch.rs +++ b/crates/gitbutler-cli/src/command/vbranch.rs @@ -18,6 +18,10 @@ pub fn list_all(project: Project) -> Result<()> { debug_print(list_branches(&ctx, None, None)?) } +pub fn list_local(project: Project) -> Result<()> { + debug_print(VirtualBranchActions::list_local_branches(project)?) +} + pub fn details(project: Project, branch_names: Vec) -> Result<()> { let ctx = CommandContext::open(&project)?; debug_print(get_branch_listing_details(&ctx, branch_names)?) diff --git a/crates/gitbutler-cli/src/main.rs b/crates/gitbutler-cli/src/main.rs index a2c1413229..6e7e38d268 100644 --- a/crates/gitbutler-cli/src/main.rs +++ b/crates/gitbutler-cli/src/main.rs @@ -20,6 +20,7 @@ fn main() -> Result<()> { args::Subcommands::Branch(vbranch::Platform { cmd }) => { let project = command::prepare::project_from_path(args.current_dir)?; match cmd { + Some(vbranch::SubCommands::ListLocal) => command::vbranch::list_local(project), Some(vbranch::SubCommands::Status) => command::vbranch::status(project), Some(vbranch::SubCommands::Unapply { name }) => { command::vbranch::unapply(project, name) diff --git a/crates/gitbutler-tauri/src/virtual_branches.rs b/crates/gitbutler-tauri/src/virtual_branches.rs index caf346fd92..2d93429589 100644 --- a/crates/gitbutler-tauri/src/virtual_branches.rs +++ b/crates/gitbutler-tauri/src/virtual_branches.rs @@ -452,7 +452,7 @@ pub mod commands { project_id: ProjectId, ) -> Result, Error> { let project = projects.get(project_id)?; - let branches = VirtualBranchActions::list_remote_branches(project)?; + let branches = VirtualBranchActions::list_local_branches(project)?; Ok(branches) }