Skip to content

Commit

Permalink
feat(cli): add custom commit type help generation and fix cli help di…
Browse files Browse the repository at this point in the history
…splay order
  • Loading branch information
oknozor committed Sep 12, 2020
1 parent bea5a2d commit fe0e143
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 39 deletions.
3 changes: 1 addition & 2 deletions coco.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
changelog_file = "CHANGELOG.md"
sort_commit = "by_type"
semver = true
sort_commit = "by_type"
44 changes: 25 additions & 19 deletions src/bin/cog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const CHANGELOG: &str = "changelog";

fn main() -> Result<()> {
let cocogitto = CocoGitto::get().unwrap_or_else(|err| panic!("{}", err));
let commit_types = cocogitto.commit_types();

let commit_subcommands = commit_types
let commit_subcommands = cocogitto
.allowed_commits
.iter()
.map(|commit_type| {
SubCommand::with_name(commit_type)
SubCommand::with_name(commit_type.0)
.settings(SUBCOMMAND_SETTINGS)
.about("Create prefixed commit")
.about(&*commit_type.1.help_message)
.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"))
Expand Down Expand Up @@ -63,6 +63,7 @@ fn main() -> Result<()> {
.short("e")
.long("edit")
)
.display_order(1)
)
.subcommand(SubCommand::with_name(LOG)
.settings(SUBCOMMAND_SETTINGS)
Expand Down Expand Up @@ -93,13 +94,30 @@ fn main() -> Result<()> {
.help("omit error on the commit log")
.short("e")
.long("no-error"))
.display_order(2)
)

.subcommand(
SubCommand::with_name(VERIFY)
.settings(SUBCOMMAND_SETTINGS)
.about("Verify a single commit message")
.arg(Arg::with_name("message").help("The commit message"))
.display_order(3)
)
.subcommand(SubCommand::with_name(CHANGELOG)
.settings(SUBCOMMAND_SETTINGS)
.about("Display a changelog for a given commit oid range")
.arg(Arg::with_name("from")
.help("Generate the changelog from this commit or tag ref, default latest tag")
.short("f")
.long("from")
.takes_value(true)
)
.arg(Arg::with_name("to")
.help("Generate the changelog to this commit or tag ref, default HEAD")
.short("t")
.long("to")
.takes_value(true))
.display_order(4)
)
.subcommand(
SubCommand::with_name(BUMP)
Expand Down Expand Up @@ -136,22 +154,10 @@ fn main() -> Result<()> {
.long("minor")
.required_unless_one(&["version", "auto", "patch", "major"])
)
.display_order(5)
)
.subcommand(SubCommand::with_name(CHANGELOG)
.settings(SUBCOMMAND_SETTINGS)
.about("Display a changelog for a given commit oid range")
.arg(Arg::with_name("from")
.help("Generate the changelog from this commit or tag ref, default latest tag")
.short("f")
.long("from")
.takes_value(true)
)
.arg(Arg::with_name("to")
.help("Generate the changelog to this commit or tag ref, default HEAD")
.short("t")
.long("to")
.takes_value(true)))
.subcommands(commit_subcommands)
.display_order(6)
.get_matches();

if let Some(subcommand) = matches.subcommand_name() {
Expand Down
86 changes: 70 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ use crate::changelog::Changelog;
use crate::commit::{CommitMessage, CommitType};
use crate::error::CocoGittoError::SemverError;
use crate::repository::Repository;
use crate::settings::Settings;
use crate::settings::{CommitTypeSetting, Settings};
use anyhow::Result;
use chrono::Utc;
use colored::*;
use commit::Commit;
use git2::{Commit as Git2Commit, Oid, RebaseOptions, Repository as Git2Repository};
use semver::Version;
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::process::{Command, Stdio};
use tempdir::TempDir;

pub struct CocoGitto {
pub settings: Settings,
pub allowed_commits: HashMap<String, CommitTypeSetting>,
repository: Repository,
}

Expand Down Expand Up @@ -132,33 +133,86 @@ impl CocoGitto {
let settings = Settings::get(&repository)?;

Ok(CocoGitto {
settings,
allowed_commits: CocoGitto::commit_types(&settings),
repository,
})
}

pub fn commit_types(&self) -> Vec<String> {
let mut commit_types: Vec<String> = self
.settings
.commit_types
.iter()
.map(|(key, _)| key)
.cloned()
.collect();
fn commit_types(settings: &Settings) -> HashMap<String, CommitTypeSetting> {
let mut commit_types = settings.commit_types.clone();

commit_types.extend_from_slice(&[
let mut default_types = HashMap::new();
default_types.insert(
"feat".to_string(),
CommitTypeSetting::new(
CommitType::Feature.get_markdown_title(),
"create a feature commit",
),
);
default_types.insert(
"fix".to_string(),
CommitTypeSetting::new(
CommitType::BugFix.get_markdown_title(),
"create a bug fix commit",
),
);
default_types.insert(
"chore".to_string(),
CommitTypeSetting::new(
CommitType::Chore.get_markdown_title(),
"create a chore commit",
),
);
default_types.insert(
"revert".to_string(),
CommitTypeSetting::new(
CommitType::Revert.get_markdown_title(),
"create a revert commit",
),
);
default_types.insert(
"perf".to_string(),
CommitTypeSetting::new(
CommitType::Performances.get_markdown_title(),
"create a performance commit",
),
);
default_types.insert(
"docs".to_string(),
CommitTypeSetting::new(
CommitType::Documentation.get_markdown_title(),
"create a documentation commit",
),
);
default_types.insert(
"style".to_string(),
CommitTypeSetting::new(
CommitType::Style.get_markdown_title(),
"create a style commit",
),
);
default_types.insert(
"refactor".to_string(),
CommitTypeSetting::new(
CommitType::Refactoring.get_markdown_title(),
"create a refactor commit",
),
);
default_types.insert(
"test".to_string(),
"build".to_string(),
CommitTypeSetting::new(
CommitType::Test.get_markdown_title(),
"create a test commit",
),
);
default_types.insert(
"ci".to_string(),
]);
CommitTypeSetting::new(
CommitType::Ci.get_markdown_title(),
"create a continuous integration commit",
),
);
commit_types.extend(default_types);

commit_types
}
Expand Down Expand Up @@ -283,9 +337,9 @@ impl CocoGitto {
println!(
"{}",
Commit {
shorthand: "".to_string(),
shorthand: "not committed".to_string(),
message: commit_message,
author: "".to_string(),
author: " ".to_string(),
date: Utc::now().naive_utc(),
}
)
Expand Down
20 changes: 18 additions & 2 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,26 @@ pub struct Settings {
#[serde(default)]
pub hooks: Vec<String>,
#[serde(default)]
pub commit_types: HashMap<String, String>,
pub commit_types: HashMap<String, CommitTypeSetting>,
// TODO : default impl
pub changelog_file: PathBuf,
// TODO : default impl
pub sort_commit: SortCommit,
pub semver: bool,
}

#[derive(Debug, Deserialize, Clone)]
pub struct CommitTypeSetting {
pub changelog_title: String,
pub help_message: String,
}

impl CommitTypeSetting {
pub(crate) fn new(changelog_title: &str, help_message: &str) -> Self {
CommitTypeSetting {
changelog_title: changelog_title.to_string(),
help_message: help_message.to_string(),
}
}
}

impl Settings {
Expand Down

0 comments on commit fe0e143

Please sign in to comment.