diff --git a/crates/git/src/git.rs b/crates/git/src/git.rs index d965605fe0..debb7649ab 100644 --- a/crates/git/src/git.rs +++ b/crates/git/src/git.rs @@ -105,6 +105,8 @@ actions!( ViewCommit, /// Adds a file to .gitignore. AddToGitignore, + /// Copies the current branch name to the clipboard. + CopyBranchName, ] ); diff --git a/crates/git_ui/src/git_ui.rs b/crates/git_ui/src/git_ui.rs index 0b44b1c517..5aa6ffe782 100644 --- a/crates/git_ui/src/git_ui.rs +++ b/crates/git_ui/src/git_ui.rs @@ -17,8 +17,8 @@ use git::{ status::{FileStatus, StatusCode, UnmergedStatus, UnmergedStatusCode}, }; use gpui::{ - App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, SharedString, - Subscription, Task, Window, + App, ClipboardItem, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, + SharedString, Subscription, Task, Window, }; use menu::{Cancel, Confirm}; use project::git_store::Repository; @@ -270,6 +270,9 @@ pub fn init(cx: &mut App) { workspace.register_action(|workspace, _: &git::RenameBranch, window, cx| { rename_current_branch(workspace, window, cx); }); + workspace.register_action(|workspace, _: &git::CopyBranchName, _, cx| { + copy_branch_name(workspace, cx); + }); workspace.register_action(show_ref_picker); workspace.register_action( |workspace, action: &DiffClipboardWithSelectionData, window, cx| { @@ -465,6 +468,20 @@ fn rename_current_branch( }); } +fn copy_branch_name(workspace: &mut Workspace, cx: &mut Context) { + let Some(panel) = workspace.panel::(cx) else { + return; + }; + let branch_name = panel.update(cx, |panel, cx| { + let repo = panel.active_repository.as_ref()?; + let repo = repo.read(cx); + repo.branch.as_ref().map(|branch| branch.name().to_string()) + }); + if let Some(name) = branch_name { + cx.write_to_clipboard(ClipboardItem::new_string(name)); + } +} + struct RefPickerModal { editor: Entity, repo: Entity,