Skip to content

Commit

Permalink
feat(changelog): add --topo-order flag for sorting tags (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Oct 29, 2021
1 parent dc7e91b commit cc09d63
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ git-cliff [FLAGS] [OPTIONS] [RANGE]
-i, --init Writes the default configuration file to cliff.toml
-l, --latest Processes the commits starting from the latest tag
-u, --unreleased Processes the commits that do not belong to a tag
--topo-order Sorts the tags topologically
-h, --help Prints help information
-V, --version Prints version information
```
Expand Down Expand Up @@ -218,6 +219,13 @@ git cliff --sort oldest
git cliff --sort newest
```

Sort the tags in topological order:

```sh
# Process in topological order instead of chronological.
git cliff --topo-order
```

Save the changelog file to the specified file:

```sh
Expand Down
7 changes: 5 additions & 2 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl Repository {
pub fn tags(
&self,
pattern: &Option<String>,
topo_order: bool,
) -> Result<IndexMap<String, String>> {
let mut tags: Vec<(Commit, String)> = Vec::new();
let tag_names = self.inner.tag_names(pattern.as_deref())?;
Expand All @@ -100,7 +101,9 @@ impl Repository {
}
}
}
tags.sort_by(|a, b| a.0.time().seconds().cmp(&b.0.time().seconds()));
if !topo_order {
tags.sort_by(|a, b| a.0.time().seconds().cmp(&b.0.time().seconds()));
}
Ok(tags
.into_iter()
.map(|(a, b)| (a.id().to_string(), b))
Expand Down Expand Up @@ -164,7 +167,7 @@ mod test {
}
}
}
let tags = repository.tags(&None)?;
let tags = repository.tags(&None, false)?;
assert_eq!(&get_last_tag()?, tags.last().unwrap().1);
Ok(())
}
Expand Down
6 changes: 4 additions & 2 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub struct Opt {
/// Processes the commits that do not belong to a tag.
#[structopt(short, long)]
pub unreleased: bool,
/// Sorts the tags topologically.
#[structopt(long)]
pub topo_order: bool,
/// Strips the given parts from the changelog.
#[structopt(
short,
Expand All @@ -77,12 +80,11 @@ pub struct Opt {
/// Sets the commit range to process.
#[structopt(value_name = "RANGE")]
pub range: Option<String>,

/// Sets sorting of the commits inside sections.
#[structopt(
long,
possible_values = &["oldest", "newest"],
default_value = "oldest"
)]
pub sort: String,
pub sort: String,
}
2 changes: 1 addition & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub fn run(mut args: Opt) -> Result<()> {
Repository::init(args.repository.unwrap_or(env::current_dir()?))?;

// Parse tags.
let mut tags = repository.tags(&config.git.tag_pattern)?;
let mut tags = repository.tags(&config.git.tag_pattern, args.topo_order)?;

// Parse commits.
let mut commit_range = args.range;
Expand Down

0 comments on commit cc09d63

Please sign in to comment.