Skip to content

Commit

Permalink
feat(sections): allows users to use empty components in commit subjects
Browse files Browse the repository at this point in the history
Users can now use "feat: some message" or "feat(): some message" style
commit subjects

Closes #2
  • Loading branch information
kbknapp committed May 21, 2015
1 parent 159b76c commit 71b32ee
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn get_latest_tag_ver() -> String {
.arg("--tags")
.arg("--abbrev=0")
.output().unwrap_or_else(|e| panic!("Failed to run 'git describe' with error: {}",e));

String::from_utf8_lossy(&output.stdout).into_owned()
}

Expand Down Expand Up @@ -61,7 +61,7 @@ fn parse_raw_commit<'a>(commit_str:&str, config: &'a ClogConfig) -> LogEntry<'a>

let hash = lines.next().unwrap_or("").to_owned();

let commit_pattern = regex!(r"^(.*)\((.*)\):(.*)");
let commit_pattern = regex!(r"^(.*?)(?:\((.*)?\))?:(.*)");
let (subject, component, commit_type) =
match lines.next().and_then(|s| commit_pattern.captures(s)) {
Some(caps) => {
Expand All @@ -74,13 +74,13 @@ fn parse_raw_commit<'a>(commit_str:&str, config: &'a ClogConfig) -> LogEntry<'a>
};
let closes_pattern = regex!(r"(?:Closes|Fixes|Resolves)\s((?:#(\d+)(?:,\s)?)+)");
let closes = lines.filter_map(|line| closes_pattern.captures(line))
.map(|caps| caps.at(2).unwrap().to_owned())
.map(|caps| caps.at(2).unwrap_or("").to_owned())
.collect();

LogEntry {
hash: hash,
subject: subject.unwrap().to_owned(),
component: component.unwrap().to_owned(),
component: component.unwrap_or("").to_owned(),
closes: closes,
breaks: vec![],
commit_type: commit_type
Expand Down
11 changes: 6 additions & 5 deletions src/log_writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::io::{Write, Result};

use time;
Expand Down Expand Up @@ -44,21 +44,22 @@ impl<'a, 'cc> LogWriter<'a, 'cc> {
}
}

pub fn write_section(&mut self, title: &str, section: &HashMap<String, Vec<LogEntry>>)
pub fn write_section(&mut self, title: &str, section: &BTreeMap<&String, &Vec<LogEntry>>)
-> Result<()> {
if section.len() == 0 { return Ok(()) }

try!(self.writer.write(&format!("\n#### {}\n\n", title)[..].as_bytes()));

for (component, entries) in section.iter() {
let nested = entries.len() > 1;
let nested = (entries.len() > 1) && !component.is_empty();

//TODO: implement the empty component stuff
let prefix = if nested {
try!(write!(self.writer, "* **{}**\n", component));
" *".to_owned()
} else {
} else if !component.is_empty() {
format!("* **{}**", component)
} else {
format!("* ")
};

for entry in entries.iter() {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate clap;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::collections::BTreeMap;

use clap::{App, Arg, ArgGroup};

Expand Down Expand Up @@ -73,7 +74,7 @@ fn main () {

writer.write_header().ok().expect("failed to write header");
for (sec, secmap) in sm.sections {
writer.write_section(&sec[..], &secmap).ok().expect(&format!("failed to write {}", sec)[..]);
writer.write_section(&sec[..], &secmap.iter().collect::<BTreeMap<_,_>>()).ok().expect(&format!("failed to write {}", sec)[..]);
}
// writer.write_section("Bug Fixes", &sections.fixes).ok().expect("failed to write bugfixes");
// writer.write_section("Features", &sections.features).ok().expect("failed to write features");
Expand Down
7 changes: 4 additions & 3 deletions src/sectionmap.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::collections::HashMap;
use std::collections::BTreeMap;

use logentry::LogEntry;

pub type ComponentMap<'a> = HashMap<String, Vec<LogEntry<'a>>>;
pub type ComponentMap<'a> = BTreeMap<String, Vec<LogEntry<'a>>>;

pub struct SectionMap<'a> {
pub sections: HashMap<String, ComponentMap<'a>>
Expand All @@ -15,11 +16,11 @@ impl<'a> SectionMap<'a> {
};

log_entries.into_iter().map(|entry| {
let comp_map = sm.sections.entry(entry.commit_type.clone()).or_insert(HashMap::new());
let comp_map = sm.sections.entry(entry.commit_type.clone()).or_insert(BTreeMap::new());
let sec_map = comp_map.entry(entry.component.clone()).or_insert(vec![]);
sec_map.push(entry);
}).collect::<Vec<_>>();

sm
}
}
}

0 comments on commit 71b32ee

Please sign in to comment.