Skip to content

Commit

Permalink
feat(clog): auto increment version with --major, --minor, or --patch
Browse files Browse the repository at this point in the history
Only compatible with semver at the moment

Closes #19
  • Loading branch information
kbknapp authored and cburgdorf committed Apr 26, 2015
1 parent 00f2662 commit b71db54
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
18 changes: 12 additions & 6 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ description = "A conventional changelog for the rest of us"
regex = "*"
time = "*"
clap = "*"
semver = "*"
18 changes: 15 additions & 3 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use common:: { LogEntry };
use common::CommitType;
use std::borrow::ToOwned;

use semver;

#[derive(Debug)]
pub struct LogReaderConfig {
pub grep: String,
Expand All @@ -16,17 +18,27 @@ pub fn get_latest_tag () -> String {
.arg("rev-list")
.arg("--tags")
.arg("--max-count=1")
.output().unwrap_or_else(|e| panic!("Failed to run git rev-list with error: {}",e));
.output().unwrap_or_else(|e| panic!("Failed to run 'git rev-list' with error: {}",e));
let buf = String::from_utf8_lossy(&output.stdout);

buf.trim_matches('\n').to_owned()
}

pub fn get_latest_tag_ver () -> Result<semver::Version, semver::ParseError> {
let output = Command::new("git")
.arg("describe")
.arg("--tags")
.arg("--abbrev=0")
.output().unwrap_or_else(|e| panic!("Failed to run 'git describe' with error: {}",e));

semver::Version::parse(&String::from_utf8_lossy(&output.stdout)[..])
}

pub fn get_last_commit () -> String {
let output = Command::new("git")
.arg("rev-parse")
.arg("HEAD")
.output().unwrap_or_else(|e| panic!("Failed to run git rev-parse with error: {}", e));
.output().unwrap_or_else(|e| panic!("Failed to run 'git rev-parse' with error: {}", e));

String::from_utf8_lossy(&output.stdout).into_owned()
}
Expand All @@ -44,7 +56,7 @@ pub fn get_log_entries (config:LogReaderConfig) -> Vec<LogEntry>{
.arg(&format!("--grep={}",config.grep))
.arg(&format!("--format={}", "%H%n%s%n%b%n==END=="))
.arg(&range)
.output().unwrap_or_else(|e| panic!("Failed to run git log with error: {}", e));
.output().unwrap_or_else(|e| panic!("Failed to run 'git log' with error: {}", e));

String::from_utf8_lossy(&output.stdout)
.split("\n==END==\n")
Expand Down
41 changes: 35 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

extern crate regex;
extern crate time;
extern crate semver;

#[macro_use]
extern crate clap;
Expand Down Expand Up @@ -32,14 +33,18 @@ fn main () {
.version(&crate_version!()[..])
.about("a conventional changelog for the rest of us")
.args_from_usage("-r --repository=[repository] 'e.g. https://github.com/thoughtram/clog'
--setversion=[setversion] 'e.g. 1.0.1'
--from=[from] 'e.g. 12a8546'
--major 'Increment major version by one (Sets minor and patch to 0)'
--minor 'Increment minor version by one (Sets patch to 0)'
--patch 'Increment patch version by one'
--subtitle=[subtitle] 'e.g. crazy-release-title'
--to=[to] 'e.g. 8057684 (Defaults to HEAD when omitted)'")
// Because --from-latest-tag can't be used with --from, we add it seperately so we can
// specify a .mutually_excludes()
.arg(Arg::from_usage("--from-latest-tag 'use latest tag as start (instead of --from)'")
.mutually_excludes("from"))
.arg(Arg::from_usage("--setversion=[setversion] 'e.g. 1.0.1'")
.mutually_excludes_all(vec!["major", "minor", "patch"]))
.get_matches();

let start_nsec = time::get_time().nsec;
Expand All @@ -51,6 +56,34 @@ fn main () {
to: matches.value_of("to").unwrap_or("").to_owned()
};

// compute version early, so we can exit on error
let version = {
// less typing later...
let (major, minor, patch) = (matches.is_present("major"), matches.is_present("minor"), matches.is_present("patch"));
if matches.is_present("setversion") {
matches.value_of("setversion").unwrap().to_owned()
} else if major || minor || patch {
match git::get_latest_tag_ver() {
Ok(ref mut v) => {
// if-else may be quicker, but it's longer mentally, and this isn't slow
match (major, minor, patch) {
(true,_,_) => { v.major += 1; v.minor = 0; v.patch = 0; },
(_,true,_) => { v.minor += 1; v.patch = 0; },
(_,_,true) => { v.patch += 1; },
_ => unreachable!()
}
format!("{}", v)
},
Err(e) => {
println!("Error parsing latest version: {}\nTry setting the version manually with --setversion=[version]", e );
std::process::exit(1);
}
}
} else {
format_util::get_short_hash(&git::get_last_commit()[..]).to_owned()
}
};

let commits = git::get_log_entries(log_reader_config);

let sections = section_builder::build_sections(commits.clone());
Expand All @@ -62,11 +95,7 @@ fn main () {
let mut file = File::create(&Path::new("changelog.md")).ok().unwrap();
let mut writer = LogWriter::new(&mut file, LogWriterOptions {
repository_link: matches.value_of("repository").unwrap_or(""),
version: if matches.is_present("setversion") {
matches.value_of("setversion").unwrap().to_owned()
} else {
format_util::get_short_hash(&git::get_last_commit()[..]).to_owned()
},
version: version,
subtitle: matches.value_of("subtitle").unwrap_or("").to_owned()
});

Expand Down

0 comments on commit b71db54

Please sign in to comment.