Skip to content

Commit fa051ef

Browse files
feat: add branch name validation on renaming (#2081)
Closes #2062 --------- Co-authored-by: extrawurst <[email protected]>
1 parent a50a478 commit fa051ef

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ These defaults require some adoption from existing users but feel more natural t
2929
* support `prepare-commit-msg` hook ([#1873](https://github.com/extrawurst/gitui/issues/1873))
3030
* new style `block_title_focused` to allow customizing title text of focused frame/block ([#2052](https://github.com/extrawurst/gitui/issues/2052)).
3131
* allow `fetch` command in both tabs of branchlist popup ([#2067](https://github.com/extrawurst/gitui/issues/2067))
32+
* check branch name validity while typing [[@sainad2222](https://github.com/sainad2222)] ([#2062](https://github.com/extrawurst/gitui/issues/2062))
3233

3334
### Changed
3435
* do not allow tagging when `tag.gpgsign` enabled until gpg-signing is [supported](https://github.com/extrawurst/gitui/issues/97) [[@TeFiLeDo](https://github.com/TeFiLeDo)] ([#1915](https://github.com/extrawurst/gitui/pull/1915))

src/popups/rename_branch.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::components::{
22
visibility_blocking, CommandBlocking, CommandInfo, Component,
33
DrawableComponent, EventState, InputType, TextInputComponent,
44
};
5+
use crate::ui::style::SharedTheme;
56
use crate::{
67
app::Environment,
78
keys::{key_match, SharedKeyConfig},
@@ -11,20 +12,24 @@ use crate::{
1112
use anyhow::Result;
1213
use asyncgit::sync::{self, RepoPathRef};
1314
use crossterm::event::Event;
14-
use ratatui::{layout::Rect, Frame};
15+
use easy_cast::Cast;
16+
use ratatui::{layout::Rect, widgets::Paragraph, Frame};
1517

1618
pub struct RenameBranchPopup {
1719
repo: RepoPathRef,
1820
input: TextInputComponent,
1921
branch_ref: Option<String>,
2022
queue: Queue,
2123
key_config: SharedKeyConfig,
24+
theme: SharedTheme,
2225
}
2326

2427
impl DrawableComponent for RenameBranchPopup {
2528
fn draw(&self, f: &mut Frame, rect: Rect) -> Result<()> {
26-
self.input.draw(f, rect)?;
27-
29+
if self.is_visible() {
30+
self.input.draw(f, rect)?;
31+
self.draw_warnings(f);
32+
}
2833
Ok(())
2934
}
3035
}
@@ -97,6 +102,7 @@ impl RenameBranchPopup {
97102
.with_input_type(InputType::Singleline),
98103
branch_ref: None,
99104
key_config: env.key_config.clone(),
105+
theme: env.theme.clone(),
100106
}
101107
}
102108

@@ -148,4 +154,35 @@ impl RenameBranchPopup {
148154

149155
self.input.clear();
150156
}
157+
158+
fn draw_warnings(&self, f: &mut Frame) {
159+
let current_text = self.input.get_text();
160+
161+
if !current_text.is_empty() {
162+
let valid = sync::validate_branch_name(current_text)
163+
.unwrap_or_default();
164+
165+
if !valid {
166+
let msg = strings::branch_name_invalid();
167+
let msg_length: u16 = msg.len().cast();
168+
let w = Paragraph::new(msg)
169+
.style(self.theme.text_danger());
170+
171+
let rect = {
172+
let mut rect = self.input.get_area();
173+
rect.y += rect.height.saturating_sub(1);
174+
rect.height = 1;
175+
let offset =
176+
rect.width.saturating_sub(msg_length + 1);
177+
rect.width =
178+
rect.width.saturating_sub(offset + 1);
179+
rect.x += offset;
180+
181+
rect
182+
};
183+
184+
f.render_widget(w, rect);
185+
}
186+
}
187+
}
151188
}

0 commit comments

Comments
 (0)