diff --git a/crates/git/src/git.rs b/crates/git/src/git.rs index 6749bbf0101..d12f0011aeb 100644 --- a/crates/git/src/git.rs +++ b/crates/git/src/git.rs @@ -162,6 +162,11 @@ pub struct RenameBranch { pub branch: Option, } +/// Trim. Then replace ` ` with `-`. +pub fn normalize_branch_name(query: &str) -> String { + query.trim().replace(' ', "-") +} + /// Restores a file to its last committed state, discarding local changes. #[derive(Clone, Debug, Default, PartialEq, Deserialize, JsonSchema, Action)] #[action(namespace = git, deprecated_aliases = ["editor::RevertFile"])] @@ -376,6 +381,13 @@ impl From for usize { mod tests { use super::*; + #[test] + fn test_normalize_branch_name() { + assert_eq!(normalize_branch_name(" branch-name "), "branch-name"); + assert_eq!(normalize_branch_name("branch name"), "branch-name"); + assert_eq!(normalize_branch_name(" branch name "), "branch--name"); + } + #[test] fn parses_short_sha1_oid_by_padding_trailing_nibbles() { let oid = "abc1234".parse::().expect("failed to parse oid"); diff --git a/crates/git_ui/src/branch_picker.rs b/crates/git_ui/src/branch_picker.rs index 6df10530e35..35aac3f0cd0 100644 --- a/crates/git_ui/src/branch_picker.rs +++ b/crates/git_ui/src/branch_picker.rs @@ -4,7 +4,7 @@ use fuzzy_nucleo::StringMatchCandidate; use collections::{HashMap, HashSet}; use git::repository::{Branch, delete_branch_flag}; -use git::{GitHostingProviderRegistry, parse_git_remote_url}; +use git::{GitHostingProviderRegistry, normalize_branch_name, parse_git_remote_url}; use gpui::http_client::Url; use gpui::{ Action, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Global, @@ -946,13 +946,6 @@ fn branch_matches_ref(branch: &Branch, branch_ref: &SharedString) -> bool { branch.ref_name.as_ref() == branch_ref.as_ref() || branch.name() == branch_ref.as_ref() } -// Git branch names can't contain whitespace, so we replace spaces with dashes, -// but we need to first trim because a branch name can't start or end with a -// dash. -fn normalize_branch_name(query: &str) -> String { - query.trim().replace(' ', "-") -} - fn branch_remote_name(branch: &Branch) -> Option<&str> { branch.remote_name().or_else(|| { branch @@ -3285,13 +3278,6 @@ mod tests { ); } - #[test] - fn test_normalize_branch_name() { - assert_eq!(normalize_branch_name(" branch-name "), "branch-name"); - assert_eq!(normalize_branch_name("branch name"), "branch-name"); - assert_eq!(normalize_branch_name(" branch name "), "branch--name"); - } - #[gpui::test] async fn test_remote_url_detection_https(cx: &mut TestAppContext) { init_test(cx); diff --git a/crates/git_ui/src/git_ui.rs b/crates/git_ui/src/git_ui.rs index f0b8469028a..b6cfa80bd34 100644 --- a/crates/git_ui/src/git_ui.rs +++ b/crates/git_ui/src/git_ui.rs @@ -8,7 +8,7 @@ mod blame_ui; pub mod clone; use git::{ - Oid, + Oid, normalize_branch_name, repository::{Branch, CommitDetails, Upstream, UpstreamTracking, UpstreamTrackingStatus}, status::{FileStatus, StatusCode, UnmergedStatus, UnmergedStatusCode}, }; @@ -453,6 +453,7 @@ struct RenameBranchModal { current_branch: SharedString, editor: Entity, repo: Entity, + _editor_subscription: Subscription, } impl RenameBranchModal { @@ -467,10 +468,16 @@ impl RenameBranchModal { editor.set_text(current_branch.clone(), window, cx); editor }); + let editor_subscription = cx.subscribe(&editor, |_, _, event, cx| { + if let editor::EditorEvent::BufferEdited = event { + cx.notify(); + } + }); Self { current_branch: current_branch.into(), editor, repo, + _editor_subscription: editor_subscription, } } @@ -479,7 +486,7 @@ impl RenameBranchModal { } fn confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context) { - let new_name = self.editor.read(cx).text(cx); + let new_name = normalize_branch_name(&self.editor.read(cx).text(cx)); if new_name.is_empty() || new_name == self.current_branch.as_ref() { cx.emit(DismissEvent); return; @@ -514,6 +521,7 @@ impl Focusable for RenameBranchModal { impl Render for RenameBranchModal { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let new_name = normalize_branch_name(&self.editor.read(cx).text(cx)); v_flex() .key_context("RenameBranchModal") .on_action(cx.listener(Self::cancel)) @@ -533,7 +541,21 @@ impl Render for RenameBranchModal { .size(HeadlineSize::XSmall), ), ) - .child(div().px_3().pb_3().w_full().child(self.editor.clone())) + .child( + v_flex() + .px_3() + .pb_3() + .w_full() + .gap_2() + .child(self.editor.clone()) + .child( + Label::new(new_name) + .size(LabelSize::Small) + .color(Color::Muted) + .single_line() + .truncate(), + ), + ) } }