Skip to content

Commit d42b003

Browse files
author
Stephan Dilly
authored
check branch name validity while typing (#842)
closes #559
1 parent e16dfca commit d42b003

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## Added
11+
- check branch name validity while typing ([#559](https://github.com/extrawurst/gitui/issues/559))
12+
1013
## Fixed
1114
- do not allow to ignore .gitignore files ([#825](https://github.com/extrawurst/gitui/issues/825))
1215
- crash in shallow repo ([#836](https://github.com/extrawurst/gitui/issues/836))

asyncgit/src/sync/branch/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
error::{Error, Result},
1313
sync::{utils, CommitId},
1414
};
15-
use git2::{BranchType, Repository};
15+
use git2::{Branch, BranchType, Repository};
1616
use scopetime::scope_time;
1717
use utils::get_head_repo;
1818

@@ -90,6 +90,15 @@ impl BranchInfo {
9090
}
9191
}
9292

93+
///
94+
pub fn validate_branch_name(name: &str) -> Result<bool> {
95+
scope_time!("validate_branch_name");
96+
97+
let valid = Branch::name_is_valid(name)?;
98+
99+
Ok(valid)
100+
}
101+
93102
/// returns a list of `BranchInfo` with a simple summary on each branch
94103
/// `local` filters for local branches otherwise remote branches will be returned
95104
pub fn get_branches_info(

asyncgit/src/sync/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use branch::{
3535
get_branches_info, merge_commit::merge_upstream_commit,
3636
merge_ff::branch_merge_upstream_fastforward,
3737
merge_rebase::merge_upstream_rebase, rename::rename_branch,
38-
BranchCompare, BranchInfo,
38+
validate_branch_name, BranchCompare, BranchInfo,
3939
};
4040
pub use commit::{amend, commit, tag};
4141
pub use commit_details::{

src/components/create_branch.rs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ use crate::{
1212
use anyhow::Result;
1313
use asyncgit::{sync, CWD};
1414
use crossterm::event::Event;
15-
use tui::{backend::Backend, layout::Rect, Frame};
15+
use easy_cast::Cast;
16+
use tui::{
17+
backend::Backend, layout::Rect, widgets::Paragraph, Frame,
18+
};
1619

1720
pub struct CreateBranchComponent {
1821
input: TextInputComponent,
1922
queue: Queue,
2023
key_config: SharedKeyConfig,
24+
theme: SharedTheme,
2125
}
2226

2327
impl DrawableComponent for CreateBranchComponent {
@@ -26,7 +30,10 @@ impl DrawableComponent for CreateBranchComponent {
2630
f: &mut Frame<B>,
2731
rect: Rect,
2832
) -> Result<()> {
29-
self.input.draw(f, rect)?;
33+
if self.is_visible() {
34+
self.input.draw(f, rect)?;
35+
self.draw_warnings(f);
36+
}
3037

3138
Ok(())
3239
}
@@ -95,12 +102,13 @@ impl CreateBranchComponent {
95102
Self {
96103
queue,
97104
input: TextInputComponent::new(
98-
theme,
105+
theme.clone(),
99106
key_config.clone(),
100107
&strings::create_branch_popup_title(&key_config),
101108
&strings::create_branch_popup_msg(&key_config),
102109
true,
103110
),
111+
theme,
104112
key_config,
105113
}
106114
}
@@ -134,4 +142,35 @@ impl CreateBranchComponent {
134142
}
135143
}
136144
}
145+
146+
fn draw_warnings<B: Backend>(&self, f: &mut Frame<B>) {
147+
let current_text = self.input.get_text();
148+
149+
if !current_text.is_empty() {
150+
let valid = sync::validate_branch_name(current_text)
151+
.unwrap_or_default();
152+
153+
if !valid {
154+
let msg = strings::branch_name_invalid();
155+
let msg_length: u16 = msg.len().cast();
156+
let w = Paragraph::new(msg)
157+
.style(self.theme.text_danger());
158+
159+
let rect = {
160+
let mut rect = self.input.get_area();
161+
rect.y += rect.height.saturating_sub(1);
162+
rect.height = 1;
163+
let offset =
164+
rect.width.saturating_sub(msg_length + 1);
165+
rect.width =
166+
rect.width.saturating_sub(offset + 1);
167+
rect.x += offset;
168+
169+
rect
170+
};
171+
172+
f.render_widget(w, rect);
173+
}
174+
}
175+
}
137176
}

src/strings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub fn commit_msg(_key_config: &SharedKeyConfig) -> String {
8383
pub fn commit_first_line_warning(count: usize) -> String {
8484
format!("[subject length: {}]", count)
8585
}
86+
pub const fn branch_name_invalid() -> &'static str {
87+
"[invalid name]"
88+
}
8689
pub fn commit_editor_msg(_key_config: &SharedKeyConfig) -> String {
8790
r##"
8891
# Edit your commit message

0 commit comments

Comments
 (0)