Skip to content

Commit

Permalink
fix(changelog): use root commit when --latest and there is only one t…
Browse files Browse the repository at this point in the history
…ag (#59)

* test: add a test for --latest with one tag

Signed-off-by: Kenji Miyake <[email protected]>

* chore: add a test script

Signed-off-by: Kenji Miyake <[email protected]>

* fix: use root commit when --latest and there is only one tag

Signed-off-by: Kenji Miyake <[email protected]>

* docs: remove "requires at least 2 tags"

Signed-off-by: Kenji Miyake <[email protected]>

* add newlines

Signed-off-by: Kenji Miyake <[email protected]>

* apply review to test-fixtures-locally.sh

Signed-off-by: Kenji Miyake <[email protected]>

* apply review to lib.rs

Signed-off-by: Kenji Miyake <[email protected]>

* chore(fixtures): apply shellcheck suggestion

* chore(ci): checkout pull request HEAD commit

Co-authored-by: Orhun Parmaksız <[email protected]>
  • Loading branch information
kenji-miyake and orhun authored Feb 20, 2022
1 parent ff1d981 commit 3ccec7f
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 26 deletions.
22 changes: 22 additions & 0 deletions .github/fixtures/test-fixtures-locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -e

SCRIPT_DIR=$(readlink -f "$(dirname "$0")")

if [ -z "$1" ]; then
echo "Please input a fixture name."
exit 1
fi

export FIXTURES_DIR="$SCRIPT_DIR/$1"

# Set up a temporary repository
cd "$(mktemp -d)"
git init

# Commit
"$FIXTURES_DIR/commit.sh"

# Show results
echo -e "\n---Run git-cliff---"
cargo run --manifest-path "$SCRIPT_DIR/../../Cargo.toml" -- --config "$FIXTURES_DIR/cliff.toml" $2
2 changes: 1 addition & 1 deletion .github/fixtures/test-ignore-tags/commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ GIT_COMMITTER_DATE="2021-01-23 01:23:48" git commit --allow-empty -m "feat: add
git tag v0.2.0-beta.1

GIT_COMMITTER_DATE="2021-01-23 01:23:49" git commit --allow-empty -m "feat: add feature 3"
git tag v0.2.0
git tag v0.2.0
27 changes: 27 additions & 0 deletions .github/fixtures/test-latest-with-one-tag/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[changelog]
# changelog header
header = """
# Changelog\n
All notable changes to this project will be documented in this file.\n
"""
# template for the changelog body
# https://tera.netlify.app/docs/#introduction
body = """
{% if version %}\
## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }}
{% else %}\
## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
### {{ group | upper_first }}
{% for commit in commits %}
- {% if commit.breaking %}[**breaking**] {% endif %}{{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# remove the leading and trailing whitespaces from the template
trim = true
# changelog footer
footer = """
<!-- generated by git-cliff -->
"""
7 changes: 7 additions & 0 deletions .github/fixtures/test-latest-with-one-tag/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -e

GIT_COMMITTER_DATE="2021-01-23 01:23:45" git commit --allow-empty -m "Initial commit"

GIT_COMMITTER_DATE="2021-01-23 01:23:46" git commit --allow-empty -m "feat: add feature 1"
git tag v0.1.0
11 changes: 11 additions & 0 deletions .github/fixtures/test-latest-with-one-tag/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

## [0.1.0] - 2021-01-23

### Feat

- Add feature 1

<!-- generated by git-cliff -->
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,16 @@ jobs:
toolchain: nightly
override: true
- name: Checkout
if: github.event_name != 'pull_request'
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Checkout
if: github.event_name == 'pull_request'
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Run tests
run: |
export CARGO_INCREMENTAL=0
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test-fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
command: --latest
- fixtures-name: test-date-order-arg
command: --latest --date-order
- fixtures-name: test-latest-with-one-tag
command: --latest
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ Generate a changelog for a certain part of git history:

```sh
# only takes the latest tag into account
# (requires at least 2 tags)
git cliff --latest

# only takes the current tag into account
Expand Down
53 changes: 29 additions & 24 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,36 @@ pub fn run(mut args: Opt) -> Result<()> {
}
} else if args.latest || args.current {
if tags.len() < 2 {
return Err(Error::ChangelogError(String::from(
"Not enough tags exist for processing the latest/current tag",
)));
}
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",
)));
let commits = repository.commits(None, None, None)?;
if let (Some(tag1), Some(tag2)) = (
commits.last().map(|c| c.id().to_string()),
tags.get_index(0).map(|(k, _)| k),
) {
commit_range = Some(format!("{}..{}", tag1, tag2));
}
} else {
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));
}
}
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));
}
}
let commits =
Expand Down

0 comments on commit 3ccec7f

Please sign in to comment.