Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds functionality of #19 #26

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@

#Others
.DS_Store

# Temporary files
.*~
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 = "*"
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ USAGE:
clog [FLAGS] [OPTIONS]

FLAGS:
--from-latest-tag uses the latest tag as starting point (ignores other --from parameters)
--from-latest-tag use latest tag as start (instead of --from)
-h, --help Prints help information
--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
-v, --version Prints version information
-h, --help Prints this message

OPTIONS:
--subtitle=subtitle e.g. crazy-release-title
--from=from e.g. 12a8546
-r, --repository=repository e.g. https://github.com/thoughtram/clog
--setversion=setversion e.g. 1.0.1
--subtitle=subtitle e.g. crazy-release-title
--to=to e.g. 8057684 (Defaults to HEAD when omitted)
--from=from e.g. 12a8546

```

Try it!
Expand Down
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