Skip to content

check branch name validity while typing #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## Added
- check branch name validity while typing ([#559](https://github.com/extrawurst/gitui/issues/559))

## Fixed
- do not allow to ignore .gitignore files ([#825](https://github.com/extrawurst/gitui/issues/825))
- crash in shallow repo ([#836](https://github.com/extrawurst/gitui/issues/836))
Expand Down
11 changes: 10 additions & 1 deletion asyncgit/src/sync/branch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
error::{Error, Result},
sync::{utils, CommitId},
};
use git2::{BranchType, Repository};
use git2::{Branch, BranchType, Repository};
use scopetime::scope_time;
use utils::get_head_repo;

Expand Down Expand Up @@ -90,6 +90,15 @@ impl BranchInfo {
}
}

///
pub fn validate_branch_name(name: &str) -> Result<bool> {
scope_time!("validate_branch_name");

let valid = Branch::name_is_valid(name)?;

Ok(valid)
}

/// returns a list of `BranchInfo` with a simple summary on each branch
/// `local` filters for local branches otherwise remote branches will be returned
pub fn get_branches_info(
Expand Down
2 changes: 1 addition & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub use branch::{
get_branches_info, merge_commit::merge_upstream_commit,
merge_ff::branch_merge_upstream_fastforward,
merge_rebase::merge_upstream_rebase, rename::rename_branch,
BranchCompare, BranchInfo,
validate_branch_name, BranchCompare, BranchInfo,
};
pub use commit::{amend, commit, tag};
pub use commit_details::{
Expand Down
45 changes: 42 additions & 3 deletions src/components/create_branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ use crate::{
use anyhow::Result;
use asyncgit::{sync, CWD};
use crossterm::event::Event;
use tui::{backend::Backend, layout::Rect, Frame};
use easy_cast::Cast;
use tui::{
backend::Backend, layout::Rect, widgets::Paragraph, Frame,
};

pub struct CreateBranchComponent {
input: TextInputComponent,
queue: Queue,
key_config: SharedKeyConfig,
theme: SharedTheme,
}

impl DrawableComponent for CreateBranchComponent {
Expand All @@ -26,7 +30,10 @@ impl DrawableComponent for CreateBranchComponent {
f: &mut Frame<B>,
rect: Rect,
) -> Result<()> {
self.input.draw(f, rect)?;
if self.is_visible() {
self.input.draw(f, rect)?;
self.draw_warnings(f);
}

Ok(())
}
Expand Down Expand Up @@ -95,12 +102,13 @@ impl CreateBranchComponent {
Self {
queue,
input: TextInputComponent::new(
theme,
theme.clone(),
key_config.clone(),
&strings::create_branch_popup_title(&key_config),
&strings::create_branch_popup_msg(&key_config),
true,
),
theme,
key_config,
}
}
Expand Down Expand Up @@ -134,4 +142,35 @@ impl CreateBranchComponent {
}
}
}

fn draw_warnings<B: Backend>(&self, f: &mut Frame<B>) {
let current_text = self.input.get_text();

if !current_text.is_empty() {
let valid = sync::validate_branch_name(current_text)
.unwrap_or_default();

if !valid {
let msg = strings::branch_name_invalid();
let msg_length: u16 = msg.len().cast();
let w = Paragraph::new(msg)
.style(self.theme.text_danger());

let rect = {
let mut rect = self.input.get_area();
rect.y += rect.height.saturating_sub(1);
rect.height = 1;
let offset =
rect.width.saturating_sub(msg_length + 1);
rect.width =
rect.width.saturating_sub(offset + 1);
rect.x += offset;

rect
};

f.render_widget(w, rect);
}
}
}
}
3 changes: 3 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub fn commit_msg(_key_config: &SharedKeyConfig) -> String {
pub fn commit_first_line_warning(count: usize) -> String {
format!("[subject length: {}]", count)
}
pub const fn branch_name_invalid() -> &'static str {
"[invalid name]"
}
pub fn commit_editor_msg(_key_config: &SharedKeyConfig) -> String {
r##"
# Edit your commit message
Expand Down