Skip to content

Commit

Permalink
feat(args): add --no-exec flag for skipping command execution (#458)
Browse files Browse the repository at this point in the history
* feat(args): add `--no-exec` flag for skipping command execution

* refactor(args): reorder arguments
  • Loading branch information
orhun committed Jan 20, 2024
1 parent 722efd6 commit 7ae77ff
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 2 deletions.
42 changes: 42 additions & 0 deletions .github/fixtures/test-no-exec/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[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://keats.github.io/tera/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 %}
- {{ commit.message | upper_first }}\
{% endfor %}
{% endfor %}\n
"""
# template for the changelog footer
footer = """
<!-- generated by git-cliff -->
"""
# remove the leading and trailing whitespace from the templates
trim = true
# postprocessors
postprocessors = [
{ pattern = '.*', replace_command = 'this_command_does_not_exist' },
]

[git]
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features", default_scope = "app" },
{ message = "^fix", group = "Bug Fixes", scope = "cli" },
]
# regex for preprocessing the commit messages
commit_preprocessors = [
{ pattern = '.*', replace_command = "this_command_does_not_exist" },
]
11 changes: 11 additions & 0 deletions .github/fixtures/test-no-exec/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
set -e

GIT_COMMITTER_DATE="2022-04-06 01:25:08" git commit --allow-empty -m "Initial commit"
GIT_COMMITTER_DATE="2022-04-06 01:25:09" git commit --allow-empty -m "feat: add feature 1"
GIT_COMMITTER_DATE="2022-04-06 01:25:10" git commit --allow-empty -m "fix: fix feature 1"
git tag v0.1.0
GIT_COMMITTER_DATE="2022-04-06 01:25:11" git commit --allow-empty -m "feat(gui): add feature 2"
GIT_COMMITTER_DATE="2022-04-06 01:25:12" git commit --allow-empty -m "fix(gui): fix feature 2"
git tag v0.2.0
GIT_COMMITTER_DATE="2022-04-06 01:25:13" git commit --allow-empty -m "test: add tests"
31 changes: 31 additions & 0 deletions .github/fixtures/test-no-exec/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Changelog

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

## [unreleased]

### Test

- Add tests

## [0.2.0] - 2022-04-06

### Bug Fixes

- Fix feature 2

### Features

- Add feature 2

## [0.1.0] - 2022-04-06

### Bug Fixes

- Fix feature 1

### Features

- Add feature 1

<!-- generated by git-cliff -->
2 changes: 2 additions & 0 deletions .github/workflows/test-fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
command: --bump
- fixtures-name: test-skip-commits
command: --skip-commit ad27b43e8032671afb4809a1a3ecf12f45c60e0e
- fixtures-name: test-no-exec
command: --no-exec
steps:
- name: Checkout
uses: actions/checkout@v4
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 @@ -201,6 +201,9 @@ pub struct Opt {
/// Sorts the tags topologically.
#[arg(long, help_heading = Some("FLAGS"))]
pub topo_order: bool,
/// Disables the external command execution.
#[arg(long, help_heading = Some("FLAGS"))]
pub no_exec: bool,
/// Prints changelog context as JSON.
#[arg(short = 'x', long, help_heading = Some("FLAGS"))]
pub context: bool,
Expand Down
12 changes: 12 additions & 0 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ pub fn run(mut args: Opt) -> Result<()> {
config.remote.github.owner = remote.0.owner.to_string();
config.remote.github.repo = remote.0.repo.to_string();
}
if args.no_exec {
if let Some(ref mut preprocessors) = config.git.commit_preprocessors {
preprocessors
.iter_mut()
.for_each(|v| v.replace_command = None);
}
if let Some(ref mut postprocessors) = config.changelog.postprocessors {
postprocessors
.iter_mut()
.for_each(|v| v.replace_command = None);
}
}
config.git.skip_tags = config.git.skip_tags.filter(|r| !r.as_str().is_empty());

// Process the repositories.
Expand Down
2 changes: 1 addition & 1 deletion website/docs/usage/args.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
sidebar_position: 1
---

# Command-line Arguments

```
Expand All @@ -21,6 +20,7 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]
-u, --unreleased Processes the commits that do not belong to a tag
--topo-order Sorts the tags topologically
-x, --context Prints changelog context as JSON
--no-exec Disables the external command execution
```

## Options
Expand Down
8 changes: 7 additions & 1 deletion website/docs/usage/examples.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
sidebar_position: 3
---

# Examples

To simply create a changelog at your projects git root directory:
Expand Down Expand Up @@ -81,3 +80,10 @@ Set/remove the changelog parts:
```bash
git cliff --body $template --strip footer
```

Skip running the commands defined in [pre](/docs/configuration/git#commit_preprocessors)/[postprocessors](/docs/configuration/changelog#postprocessors).

```bash
# No external command execution
git cliff --no-exec
```

0 comments on commit 7ae77ff

Please sign in to comment.