Skip to content

Commit

Permalink
fix: fix unicode trailing char panic [#101]
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent 738ed8a commit 3de62ba
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 25 deletions.
40 changes: 22 additions & 18 deletions src/conventional/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ impl Commit {
};

let message = git2_message.trim_end();
let commit_message = if message.len() > 80 {
format!("{}{}", &message[0..80], "...").red()
} else {
message.red()
}
.to_string();
let commit_message = Commit::format_summary(message).red().to_string();

let cause = format!("{} {}", "cause:".magenta(), err);
let level = "ERROR".red().bold().to_string();
Expand Down Expand Up @@ -123,21 +118,12 @@ impl Commit {
}

pub fn get_log(&self) -> String {
let message_display = self.message.summary.replace("\n", " ");
let message_display = if message_display.len() > 80 {
format!("{}{}", &message_display[0..80], "...").yellow()
} else {
message_display.yellow()
};

let summary = &self.message.summary;
let message_display = Commit::format_summary(summary).yellow();
let author_format = "Author:".green().bold();
let type_format = "Type:".green().bold();
let scope_format = "Scope:".green().bold();
let breaking_change = if self.message.is_breaking_change {
format!("{} - ", "BREAKING CHANGE".red().bold())
} else {
"".to_string()
};
let breaking_change = self.format_breaking_change();
let now = Utc::now().naive_utc();
let elapsed = now - self.date;
let elapsed = if elapsed.num_weeks() > 0 {
Expand Down Expand Up @@ -193,6 +179,24 @@ impl Commit {
self.message.scope.as_deref().unwrap_or("none"),
)
}

fn format_breaking_change(&self) -> String {
if self.message.is_breaking_change {
format!("{} - ", "BREAKING CHANGE".red().bold())
} else {
"".to_string()
}
}

fn format_summary(summary: &str) -> String {
if summary.len() > 80 {
// display a maximum of 80 char (77 char + ...)
let message = summary.chars().take(77).collect::<String>();
format!("{}{}", message, "...")
} else {
summary.to_string()
}
}
}

impl fmt::Display for Commit {
Expand Down
7 changes: 4 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::git::status::Statuses;
use colored::*;
use std::fmt;
use std::fmt::Formatter;
Expand All @@ -14,8 +13,10 @@ pub(crate) enum ErrorKind {
cause: String,
additional_info: String,
},
#[error("{statuses}")]
NothingToCommit { statuses: Statuses },
#[error("On branch {branch}\nNothing to commit")]
NothingToCommitWithBranch { branch: String },
#[error("Nothing to commit")]
NothingToCommit,
#[error("{level}:\n\t{cause}\n")]
Semver { level: String, cause: String },
#[error("{level}:\n\t{cause}\n")]
Expand Down
9 changes: 6 additions & 3 deletions src/git/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ impl Repository {
.commit(Some("HEAD"), &sig, &sig, message, &tree, &[])
.map_err(|err| anyhow!(err))
} else {
Err(anyhow!(ErrorKind::NothingToCommit {
statuses: self.get_statuses()?
}))
let err = self
.get_branch_shorthand()
.map(|branch| ErrorKind::NothingToCommitWithBranch { branch })
.unwrap_or_else(|| ErrorKind::NothingToCommit);

Err(anyhow!(err))
}
}

Expand Down
File renamed without changes.
File renamed without changes.
21 changes: 20 additions & 1 deletion tests/git_test.rs → tests/cocogitto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use helper::*;

pub mod helper;

use conventional_commit_parser::commit::CommitType;
use helper::run_test_with_context;

#[test]
Expand Down Expand Up @@ -116,3 +115,23 @@ fn check_commit_err_from_latest_tag() -> Result<()> {
Ok(())
})
}

#[test]
fn long_commit_summary_does_not_panic() -> Result<()> {
run_test_with_context(|_| {
helper::git_init()?;
let message =
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa…"
.to_string();

let cocogitto = CocoGitto::get()?;
std::fs::write("file", "Hello")?;
helper::git_add()?;
cocogitto.conventional_commit("feat", None, message, None, None, false)?;

let result = cocogitto.check(false);

assert!(result.is_ok());
Ok(())
})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3de62ba

Please sign in to comment.