git_ui: Normalize git branch name when renaming (#65734)

## Summary

Normalize git branch name in action `git::RenameBranch` to make its
behavior similar to action `git::Branch`.

### Before

<img width="1237" height="169" alt="image"
src="https://github.com/user-attachments/assets/dedc1e6a-5573-4202-b7f3-e6c4744fb000"
/>

<img width="768" height="481" alt="image"
src="https://github.com/user-attachments/assets/a937709f-4947-44c4-9c54-d53841347ccc"
/>

### After

<img width="1255" height="241" alt="image"
src="https://github.com/user-attachments/assets/9501ba89-f8b1-4b49-b622-cc5f88703f8b"
/>

## Testing

Tested in Linux Wayland.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Normalize branch name in action: `git::RenameBranch`.

---------

Co-authored-by: Anthony Eid <56899983+Anthony-Eid@users.noreply.github.com>
This commit is contained in:
Sha1rholder
2026-11-10 21:59:40 +00:00
committed by GitHub
parent 3def8bad0b
commit 0d1756eb10
3 changed files with 38 additions and 18 deletions
+12
View File
@@ -162,6 +162,11 @@ pub struct RenameBranch {
pub branch: Option<String>,
}
/// 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<Oid> 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::<Oid>().expect("failed to parse oid");
+1 -15
View File
@@ -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);
+25 -3
View File
@@ -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<Editor>,
repo: Entity<Repository>,
_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<Self>) {
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<Self>) -> 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(),
),
)
}
}