Skip to content

Commit

Permalink
feat(args): add --current flag for processing the current tag (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 2, 2021
1 parent 7b000ad commit 02a6187
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ git-cliff [FLAGS] [OPTIONS] [RANGE]
-v, --verbose Increases the logging verbosity
-i, --init Writes the default configuration file to cliff.toml
-l, --latest Processes the commits starting from the latest tag
--current Processes the commits that belong to the current tag
-u, --unreleased Processes the commits that do not belong to a tag
--topo-order Sorts the tags topologically
-h, --help Prints help information
Expand Down Expand Up @@ -195,6 +196,11 @@ Generate a changelog for a certain part of git history:
# (requires at least 2 tags)
git cliff --latest

# only takes the current tag into account
# useful if you checkout a specific tag (e.g. `git checkout v0.0.1`)
# (requires a tag to be present for the current commit (i.e. HEAD))
git cliff --current

# generate changelog for unreleased commits
git cliff --unreleased
git cliff --unreleased --tag 1.0.0
Expand Down
11 changes: 11 additions & 0 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::error::{
};
use git2::{
Commit,
DescribeOptions,
Repository as GitRepository,
Sort,
};
Expand Down Expand Up @@ -90,6 +91,16 @@ impl Repository {
Ok(commits)
}

/// Returns the current tag.
///
/// It is the same as running `git describe --tags`
pub fn current_tag(&self) -> Option<String> {
self.inner
.describe(DescribeOptions::new().describe_tags())
.ok()
.and_then(|describe| describe.format(None).ok())
}

/// Parses and returns a commit-tag map.
///
/// It collects lightweight and annotated tags.
Expand Down
3 changes: 3 additions & 0 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ pub struct Opt {
/// Processes the commits starting from the latest tag.
#[structopt(short, long)]
pub latest: bool,
/// Processes the commits that belong to the current tag.
#[structopt(long)]
pub current: bool,
/// Processes the commits that do not belong to a tag.
#[structopt(short, long)]
pub unreleased: bool,
Expand Down
27 changes: 22 additions & 5 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,31 @@ pub fn run(mut args: Opt) -> Result<()> {
if let Some(last_tag) = tags.last().map(|(k, _)| k) {
commit_range = Some(format!("{}..HEAD", last_tag));
}
} else if args.latest {
} else if args.latest || args.current {
if tags.len() < 2 {
return Err(Error::ChangelogError(String::from(
"Latest tag cannot be processed",
"Not enough tags exist for processing the latest/current tag",
)));
} else if let (Some(tag1), Some(tag2)) = (
tags.get_index(tags.len() - 2).map(|(k, _)| k),
tags.get_index(tags.len() - 1).map(|(k, _)| k),
}
let mut tag_index = tags.len() - 2;
if args.current {
if let Some(current_tag_index) =
repository.current_tag().as_ref().and_then(|tag| {
tags.iter()
.enumerate()
.find(|(_, (_, v))| v == &tag)
.map(|(i, _)| i)
}) {
tag_index = current_tag_index - 1;
} else {
return Err(Error::ChangelogError(String::from(
"No tag exists for the current commit",
)));
}
}
if let (Some(tag1), Some(tag2)) = (
tags.get_index(tag_index).map(|(k, _)| k),
tags.get_index(tag_index + 1).map(|(k, _)| k),
) {
commit_range = Some(format!("{}..{}", tag1, tag2));
}
Expand Down

0 comments on commit 02a6187

Please sign in to comment.