Automatically convert spaces to dashes in branch names (#2916)

* Automatically convert spaces to dashes in branch names

* cargo fmt

* docs(changelog): add an entry for the new feature

---------

Co-authored-by: rusticorn <[email protected]>
This commit is contained in:
Pierre Bouillon
2026-05-26 12:54:49 +02:00
committed by GitHub
co-authored by rusticorn
parent 8619c07f3f
commit 4f97fb5b1c
4 changed files with 66 additions and 6 deletions
+1
View File
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
* use [tombi](https://github.com/tombi-toml/tombi) for all toml file formatting
* open the external editor from the status diff view [[@WaterWhisperer](https://github.com/WaterWhisperer)] ([#2805](https://github.com/gitui-org/gitui/issues/2805))
* automatically convert spaces to dashes when creating or renaming a branch [[@pbouillon]](https//pbouillon.github.io)] ([#2916](https://github.com/gitui-org/gitui/pull/2916))
### Fixes
* crash when opening submodule ([#2895](https://github.com/gitui-org/gitui/issues/2895))
+21 -3
View File
@@ -11,9 +11,10 @@ use crate::{
};
use anyhow::Result;
use asyncgit::sync::{self, RepoPathRef};
use crossterm::event::Event;
use crossterm::event::{Event, KeyCode, KeyEvent};
use easy_cast::Cast;
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
use std::borrow::Cow;
pub struct CreateBranchPopup {
repo: RepoPathRef,
@@ -57,11 +58,28 @@ impl Component for CreateBranchPopup {
fn event(&mut self, ev: &Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)?.is_consumed() {
let ev = match ev {
Event::Key(KeyEvent {
code: KeyCode::Char(c),
modifiers,
kind,
state,
}) => Cow::Owned(Event::Key(KeyEvent {
code: KeyCode::Char(
strings::normalize_branch_name_char(*c),
),
modifiers: *modifiers,
kind: *kind,
state: *state,
})),
_ => Cow::Borrowed(ev),
};
if self.input.event(&ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
if let Event::Key(e) = ev.as_ref() {
if key_match(e, self.key_config.keys.enter) {
self.create_branch();
}
+21 -3
View File
@@ -11,9 +11,10 @@ use crate::{
};
use anyhow::Result;
use asyncgit::sync::{self, RepoPathRef};
use crossterm::event::Event;
use crossterm::event::{Event, KeyCode, KeyEvent};
use easy_cast::Cast;
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
use std::borrow::Cow;
pub struct RenameBranchPopup {
repo: RepoPathRef,
@@ -57,11 +58,28 @@ impl Component for RenameBranchPopup {
fn event(&mut self, ev: &Event) -> Result<EventState> {
if self.is_visible() {
if self.input.event(ev)?.is_consumed() {
let ev = match ev {
Event::Key(KeyEvent {
code: KeyCode::Char(c),
modifiers,
kind,
state,
}) => Cow::Owned(Event::Key(KeyEvent {
code: KeyCode::Char(
strings::normalize_branch_name_char(*c),
),
modifiers: *modifiers,
kind: *kind,
state: *state,
})),
_ => Cow::Borrowed(ev),
};
if self.input.event(&ev)?.is_consumed() {
return Ok(EventState::Consumed);
}
if let Event::Key(e) = ev {
if let Event::Key(e) = ev.as_ref() {
if key_match(e, self.key_config.keys.enter) {
self.rename_branch();
}
+23
View File
@@ -438,6 +438,13 @@ pub fn ellipsis_trim_start(s: &str, width: usize) -> Cow<'_, str> {
}
}
pub const fn normalize_branch_name_char(c: char) -> char {
match c {
' ' => '-',
c => c,
}
}
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum CheckoutOptions {
KeepLocalChanges,
@@ -1934,3 +1941,19 @@ pub mod commands {
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
let input = "feature/auto replace spaces in branch name";
let output: String =
input.chars().map(normalize_branch_name_char).collect();
assert_eq!(
output,
"feature/auto-replace-spaces-in-branch-name"
);
}
}