Skip to content

Commit 4f97fb5

Browse files
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 <mail@rusticorn.com>
1 parent 8619c07 commit 4f97fb5

4 files changed

Lines changed: 66 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Changed
1111
* use [tombi](https://github.com/tombi-toml/tombi) for all toml file formatting
1212
* open the external editor from the status diff view [[@WaterWhisperer](https://github.com/WaterWhisperer)] ([#2805](https://github.com/gitui-org/gitui/issues/2805))
13+
* 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))
1314

1415
### Fixes
1516
* crash when opening submodule ([#2895](https://github.com/gitui-org/gitui/issues/2895))

src/popups/create_branch.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use crate::{
1111
};
1212
use anyhow::Result;
1313
use asyncgit::sync::{self, RepoPathRef};
14-
use crossterm::event::Event;
14+
use crossterm::event::{Event, KeyCode, KeyEvent};
1515
use easy_cast::Cast;
1616
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
17+
use std::borrow::Cow;
1718

1819
pub struct CreateBranchPopup {
1920
repo: RepoPathRef,
@@ -57,11 +58,28 @@ impl Component for CreateBranchPopup {
5758

5859
fn event(&mut self, ev: &Event) -> Result<EventState> {
5960
if self.is_visible() {
60-
if self.input.event(ev)?.is_consumed() {
61+
let ev = match ev {
62+
Event::Key(KeyEvent {
63+
code: KeyCode::Char(c),
64+
modifiers,
65+
kind,
66+
state,
67+
}) => Cow::Owned(Event::Key(KeyEvent {
68+
code: KeyCode::Char(
69+
strings::normalize_branch_name_char(*c),
70+
),
71+
modifiers: *modifiers,
72+
kind: *kind,
73+
state: *state,
74+
})),
75+
_ => Cow::Borrowed(ev),
76+
};
77+
78+
if self.input.event(&ev)?.is_consumed() {
6179
return Ok(EventState::Consumed);
6280
}
6381

64-
if let Event::Key(e) = ev {
82+
if let Event::Key(e) = ev.as_ref() {
6583
if key_match(e, self.key_config.keys.enter) {
6684
self.create_branch();
6785
}

src/popups/rename_branch.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use crate::{
1111
};
1212
use anyhow::Result;
1313
use asyncgit::sync::{self, RepoPathRef};
14-
use crossterm::event::Event;
14+
use crossterm::event::{Event, KeyCode, KeyEvent};
1515
use easy_cast::Cast;
1616
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
17+
use std::borrow::Cow;
1718

1819
pub struct RenameBranchPopup {
1920
repo: RepoPathRef,
@@ -57,11 +58,28 @@ impl Component for RenameBranchPopup {
5758

5859
fn event(&mut self, ev: &Event) -> Result<EventState> {
5960
if self.is_visible() {
60-
if self.input.event(ev)?.is_consumed() {
61+
let ev = match ev {
62+
Event::Key(KeyEvent {
63+
code: KeyCode::Char(c),
64+
modifiers,
65+
kind,
66+
state,
67+
}) => Cow::Owned(Event::Key(KeyEvent {
68+
code: KeyCode::Char(
69+
strings::normalize_branch_name_char(*c),
70+
),
71+
modifiers: *modifiers,
72+
kind: *kind,
73+
state: *state,
74+
})),
75+
_ => Cow::Borrowed(ev),
76+
};
77+
78+
if self.input.event(&ev)?.is_consumed() {
6179
return Ok(EventState::Consumed);
6280
}
6381

64-
if let Event::Key(e) = ev {
82+
if let Event::Key(e) = ev.as_ref() {
6583
if key_match(e, self.key_config.keys.enter) {
6684
self.rename_branch();
6785
}

src/strings.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ pub fn ellipsis_trim_start(s: &str, width: usize) -> Cow<'_, str> {
438438
}
439439
}
440440

441+
pub const fn normalize_branch_name_char(c: char) -> char {
442+
match c {
443+
' ' => '-',
444+
c => c,
445+
}
446+
}
447+
441448
#[derive(PartialEq, Eq, Clone, Copy)]
442449
pub enum CheckoutOptions {
443450
KeepLocalChanges,
@@ -1934,3 +1941,19 @@ pub mod commands {
19341941
)
19351942
}
19361943
}
1944+
1945+
#[cfg(test)]
1946+
mod tests {
1947+
use super::*;
1948+
1949+
#[test]
1950+
fn test_spaces_are_replaced_by_dashes_in_branch_name() {
1951+
let input = "feature/auto replace spaces in branch name";
1952+
let output: String =
1953+
input.chars().map(normalize_branch_name_char).collect();
1954+
assert_eq!(
1955+
output,
1956+
"feature/auto-replace-spaces-in-branch-name"
1957+
);
1958+
}
1959+
}

0 commit comments

Comments
 (0)