Skip to content

Commit

Permalink
feat(commit): add commit optional args {body} {footer} and {breaking-…
Browse files Browse the repository at this point in the history
…change}

scope comes after the message in the cli in order to use positional args only
  • Loading branch information
oknozor committed Sep 11, 2020
1 parent 2248c90 commit 819a04d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
21 changes: 19 additions & 2 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ fn main() -> Result<()> {
.help("Create a pre-formatted commit")
.arg(Arg::with_name("message").help("The commit message"))
.arg(Arg::with_name("scope").help("The scope of the commit message"))
.arg(Arg::with_name("body").help("The body of the commit message"))
.arg(Arg::with_name("footer").help("The footer of the commit message"))
.arg(
Arg::with_name("breaking-change")
.help("BREAKING CHANGE commit")
.short("B")
.long("breaking-change"),
)
})
.collect::<Vec<App>>();

Expand Down Expand Up @@ -171,8 +179,17 @@ fn main() -> Result<()> {
if let Some(args) = matches.subcommand_matches(commit_type) {
let message = args.value_of("message").unwrap().to_string();
let scope = args.value_of("scope").map(|scope| scope.to_string());

cocogitto.conventional_commit(commit_type, scope, message)?;
let body = args.value_of("body").map(|body| body.to_string());
let footer = args.value_of("footer").map(|footer| footer.to_string());
let breaking_change = args.is_present("breaking-change");
cocogitto.conventional_commit(
commit_type,
scope,
message,
body,
footer,
breaking_change,
)?;
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,32 @@ impl CommitType {
}
}

impl ToString for CommitMessage {
fn to_string(&self) -> String {
let mut message = String::new();
message.push_str(&self.commit_type.get_key_str());

if let Some(scope) = &self.scope {
message.push_str(&format!("({})", scope));
}

if self.is_breaking_change {
message.push('!');
}

message.push_str(&format!(": {}", &self.description));

if let Some(body) = &self.body {
message.push_str(&format!("\n\n{}", body));
}

if let Some(footer) = &self.footer {
message.push_str(&format!("\n\n{}", footer));
}

message
}
}
impl From<&str> for CommitType {
fn from(commit_type: &str) -> Self {
match commit_type {
Expand Down
25 changes: 18 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub mod repository;
pub mod settings;

use crate::changelog::Changelog;
use crate::commit::CommitType;
use crate::commit::{CommitMessage, CommitType};
use crate::error::CocoGittoError::SemverError;
use crate::repository::Repository;
use crate::settings::Settings;
Expand Down Expand Up @@ -203,18 +203,29 @@ impl CocoGitto {
&self,
commit_type: &str,
scope: Option<String>,
message: String,
description: String,
body: Option<String>,
footer: Option<String>,
is_breaking_change: bool,
) -> Result<()> {
let commit_type = CommitType::from(commit_type);
let message = match scope {
Some(scope) => format!("{}({}): {}", commit_type, scope, message,),
None => format!("{}: {}", commit_type, message,),
};

let message = CommitMessage {
commit_type,
scope,
body,
footer,
description,
is_breaking_change,
}
.to_string();

let oid = self.repository.commit(message)?;
let commit = self.repository.0.find_commit(oid)?;
let commit = Commit::from_git_commit(&commit)?;
Ok(println!("{}", commit))
println!("{}", commit);

Ok(())
}

pub fn create_version(&self, increment: VersionIncrement) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::CocoGittoError::GitError;
use anyhow::{Error, Result};
use anyhow::Result;
use colored::Colorize;
use git2::{
Commit as Git2Commit, DiffOptions, Object, ObjectType, Oid, Repository as Git2Repository,
Expand Down

0 comments on commit 819a04d

Please sign in to comment.