Skip to content

Commit

Permalink
Merge pull request #21 from sdttttt/develop
Browse files Browse the repository at this point in the history
docs: Add Comments.
  • Loading branch information
sdttttt authored Nov 3, 2020
2 parents 02376a1 + 90754a0 commit 2a7fd5e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "grc"
version = "0.9.0-rc.1"
authors = ["sdttttt <sdttttt@outlook.com>"]
description = "Similar to git-cz, gcr will help you to provide a better Git experience."
readme = "README.md"
exclude = ["/.github/", "/rustfmt.toml"]
edition = "2018"
license = "MIT"
repository = "https://github.com/sdttttt/gcr.git"
Expand All @@ -11,7 +13,7 @@ categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.release]
lto = "fat"
lto = true

[dependencies]
clap = "2.33.3"
Expand Down
4 changes: 2 additions & 2 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use git2::Error;
use crate::metadata::*;
use crate::util::*;

/// Parse the behavior and extra parameters of GRC by entering commands.
pub struct Arguments {
mode: Mode,
params: Vec<String>,
Expand All @@ -14,7 +15,6 @@ impl Arguments {
// get the external parameter.
pub fn collect() -> Result<Self, Error> {
let matches = Self::cli().get_matches();

Self::resolve_command(matches)
}

Expand Down Expand Up @@ -59,7 +59,7 @@ impl Arguments {
.takes_value(true)
}

// Construct the behavior according to the input parameters.
/// Construct the behavior according to the input parameters.
fn resolve_command(matches: ArgMatches) -> Result<Self, Error> {
let arg: Self;
if matches.is_present(ADD_PARAMS) {
Expand Down
9 changes: 8 additions & 1 deletion src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,33 @@ use serde::Deserialize;

use crate::metadata::GRC_CONFIG_FILE_NAME;

/// Extensions is GRC future config.
#[derive(Deserialize)]
pub struct Extensions {
#[serde(rename = "type")]
typ: Vec<String>,
}

impl Extensions {
pub fn from_agreement() -> Result<Self, Error> {

/// Read Extension from the configuration file in the convention name.
pub fn from_agreement() -> Result<Self, Error> {
let file_str = Self::read_config_file(GRC_CONFIG_FILE_NAME)?;
Ok(Self::deserialize(file_str)?)
}

/// Read Extension from the configuration file in the Specified name.
pub fn from(filename: &str) -> Result<Self, Error> {
let file_str = Self::read_config_file(filename)?;
Ok(Self::deserialize(file_str)?)
}

/// got All Types in configuration file.
pub fn types(&self) -> &Vec<String> {
&self.typ
}

/// deserialize toml configuration file to struct.
fn deserialize(file_str: String) -> Result<Self, Error> {
if file_str.len() == 0 || file_str == "" {
return Ok(Self { typ: vec![] });
Expand All @@ -35,6 +41,7 @@ impl Extensions {
Ok(config)
}

/// read config file convert std::string::String
fn read_config_file(filename: &str) -> Result<String, Error> {
match fs::read_to_string(filename) {
Ok(content) => Ok(content),
Expand Down
36 changes: 17 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,43 @@ use util::*;

fn main() {
// input parameters.
let arg = {
match Arguments::collect() {
let arg = match Arguments::collect() {
Ok(a) => a,
Err(e) => {
gcr_err_println(e.message());
return;
}
}
};
};

// repository path.
let path = current_path();

// repository Object instance.
let repo = {
match Repository::new(path, arg) {
let repo = match Repository::new(path, arg) {
Ok(r) => r,
Err(e) => {
gcr_err_println(e.message());
return;
}
}
};

// before commit hook.
if let Err(e) = repo.pre_commit() {
gcr_err_println(e.message());
return;
}

let mut types: Vec<String> = vec![];

};

// extends types.
let mut types: Vec<String> = vec![];

// parse configuration file to Extensions struct.
if let Ok(extends) = Extensions::from_agreement() {
types = extends.types().clone();
types = extends.types().clone();
}

// commit message.
let message = Messager::new().load_ext_td(&types).ask().build();
gcr_println(&message);

// before commit hook.
if let Err(e) = repo.pre_commit() {
gcr_err_println(e.message());
return;
}

// Git commit
if let Err(e) = repo.commit(message.as_str()) {
Expand Down
16 changes: 11 additions & 5 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::util::remove_pound_prefix;

struct CommitTD(String, String);

// Messsager is Commit Message struct.
/// Messsager is Commit Message struct.
pub struct Messager {
commit_type_descript: Vec<CommitTD>,

Expand Down Expand Up @@ -35,6 +35,7 @@ impl Messager {
Self { commit_type_descript, typ, scope, subject, body }
}

/// Load externally provided extension types.
pub fn load_ext_td(mut self, t: &Vec<String>) -> Self {
let mut td = t
.iter()
Expand All @@ -48,6 +49,7 @@ impl Messager {
self
}

// got commit message for self.
pub fn ask(mut self) -> Self {
self.ask_type();
self.ask_scope();
Expand All @@ -56,7 +58,7 @@ impl Messager {
self
}

// generate commit message.
/// generate commit message.
pub fn build(&self) -> String {
let header = if self.scope.trim().len() == 0 {
format!("{}: {}", self.typ, self.subject)
Expand All @@ -75,7 +77,7 @@ impl Messager {
self.commit_type_descript.push(CommitTD::from("<~>", "Define your commit type."))
}

// generate commit long description.
/// generate commit long description.
fn build_body(&mut self) {
let description = self.ask_description();
let closes = self.ask_close();
Expand All @@ -93,7 +95,7 @@ impl Messager {
self.body = body
}

// type of commit message.
/// type of commit message.
fn ask_type(&mut self) {
let selection = Select::with_theme(&ColorfulTheme::default())
.items(&self.type_list())
Expand All @@ -119,7 +121,7 @@ impl Messager {
}
}

// scope of commit scope.
/// scope of commit message.
fn ask_scope(&mut self) {
let scope = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("GRC: Which scope? (Optional)")
Expand All @@ -130,6 +132,7 @@ impl Messager {
self.scope = String::from(scope.trim())
}

/// subject of commit message.
fn ask_subject(&mut self) {
self.subject = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("GRC: Commit Message ?")
Expand All @@ -144,6 +147,7 @@ impl Messager {
.unwrap()
}

/// description of commit message.
fn ask_description(&self) -> String {
let description = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("GRC: Provide a longer description? (Optional)")
Expand All @@ -154,6 +158,7 @@ impl Messager {
String::from(description.trim())
}

// close PR or issue of commit message.
fn ask_close(&self) -> String {
let closes = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("GRC: PR & Issues this commit closes, e.g 123: (Optional)")
Expand All @@ -164,6 +169,7 @@ impl Messager {
String::from(remove_pound_prefix(closes.trim()))
}

// self.commit_type_descript { Vec<CommitTD> } convert to { Vec<String> }.
fn type_list(&self) -> Vec<String> {
self.commit_type_descript
.iter()
Expand Down

0 comments on commit 2a7fd5e

Please sign in to comment.