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

feat: add option for github remote repository #121

Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,20 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]
**Options:**

```
-c, --config <PATH> Sets the configuration file [env: GIT_CLIFF_CONFIG=] [default: cliff.toml]
-w, --workdir <PATH> Sets the working directory [env: GIT_CLIFF_WORKDIR=]
-r, --repository <PATH> Sets the git repository [env: GIT_CLIFF_REPOSITORY=]
--include-path <PATTERN>... Sets the path to include related commits [env: GIT_CLIFF_INCLUDE_PATH=]
--exclude-path <PATTERN>... Sets the path to exclude related commits [env: GIT_CLIFF_EXCLUDE_PATH=]
--with-commit <MSG>... Sets custom commit messages to include in the changelog [env: GIT_CLIFF_WITH_COMMIT=]
-p, --prepend <PATH> Prepends entries to the given changelog file [env: GIT_CLIFF_PREPEND=]
-o, --output <PATH> Writes output to the given file [env: GIT_CLIFF_OUTPUT=]
-t, --tag <TAG> Sets the tag for the latest version [env: GIT_CLIFF_TAG=]
-b, --body <TEMPLATE> Sets the template for the changelog body [env: GIT_CLIFF_TEMPLATE=]
-s, --strip <PART> Strips the given parts from the changelog [possible values: header, footer, all]
--sort <SORT> Sets sorting of the commits inside sections [default: oldest] [possible values: oldest, newest]
-c, --config <PATH> Sets the configuration file [env: GIT_CLIFF_CONFIG=] [default: cliff.toml]
-w, --workdir <PATH> Sets the working directory [env: GIT_CLIFF_WORKDIR=]
-r, --repository <PATH> Sets the git repository [env: GIT_CLIFF_REPOSITORY=]
--include-path <PATTERN>... Sets the path to include related commits [env: GIT_CLIFF_INCLUDE_PATH=]
--exclude-path <PATTERN>... Sets the path to exclude related commits [env: GIT_CLIFF_EXCLUDE_PATH=]
--with-commit <MSG>... Sets custom commit messages to include in the changelog [env: GIT_CLIFF_WITH_COMMIT=]
-p, --prepend <PATH> Prepends entries to the given changelog file [env: GIT_CLIFF_PREPEND=]
-o, --output <PATH> Writes output to the given file [env: GIT_CLIFF_OUTPUT=]
-t, --tag <TAG> Sets the tag for the latest version [env: GIT_CLIFF_TAG=]
-b, --body <TEMPLATE> Sets the template for the changelog body [env: GIT_CLIFF_TEMPLATE=]
-s, --strip <PART> Strips the given parts from the changelog [possible values: header, footer, all]
--sort <SORT> Sets sorting of the commits inside sections [default: oldest] [possible values: oldest, newest]
--github-repository-url <GITHUB_REPOSITORY_URL> Adds [CommitPreprocessor] steps for github issues (#i) & pull requests (@i) [env: GIT_CLIFF_GITHUB_REPOSITORY_URL=]

```

**Args:**
Expand Down Expand Up @@ -309,6 +311,32 @@ git cliff --body $template --strip footer

Also, see the [release script](./release.sh) of this project which sets the changelog as a message of an annotated tag.

Add commit preprocessors to create markdown links for github issues & pull requests:

Assuming we have a git commit like ...

```sh
feat: #2 adds a new feature as suggested by #1
```

```sh
git-cliff --github-repository-url github.com/example-repo
```

Will generate the following changelog entry for the above commit:

- [#2](https://github.com/example-repo/pull/1) adds a new feature as suggested by [#1](https://github.com/example-repo/issues/1)

<details>
<summary>Raw Output</summary>

```
- [#2](https://github.com/example-repo/pull/1) adds a new feature as suggested by [#1](https://github.com/example-repo/issues/1)
```

</details>


## Docker

The easiest way of running **git-cliff** (in the git root directory with [configuration file](#configuration-file) present) is to use the [available tags](https://hub.docker.com/repository/docker/orhunp/git-cliff/tags) from [Docker Hub](https://hub.docker.com/repository/docker/orhunp/git-cliff):
Expand Down
1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ log = "0.4.17"
dirs-next = "2.0.0"
clap_complete = "3.2.5"
clap_mangen = "0.1.11"
regex = "1.6.0"

[dependencies.git-cliff-core]
version = "0.9.2" # managed by release.sh
Expand Down
44 changes: 24 additions & 20 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,46 @@ pub enum Sort {
pub struct Opt {
/// Increases the logging verbosity.
#[clap(short, long, parse(from_occurrences), alias = "debug", help_heading = Some("FLAGS"))]
pub verbose: u8,
pub verbose: u8,
/// Sets the configuration file.
#[clap(short, long, env = "GIT_CLIFF_CONFIG", value_name = "PATH", default_value = DEFAULT_CONFIG)]
pub config: PathBuf,
pub config: PathBuf,
/// Sets the working directory.
#[clap(short, long, env = "GIT_CLIFF_WORKDIR", value_name = "PATH")]
pub workdir: Option<PathBuf>,
pub workdir: Option<PathBuf>,
/// Sets the git repository.
#[clap(short, long, env = "GIT_CLIFF_REPOSITORY", value_name = "PATH")]
pub repository: Option<PathBuf>,
pub repository: Option<PathBuf>,
/// Sets the path to include related commits.
#[clap(
long,
env = "GIT_CLIFF_INCLUDE_PATH",
value_name = "PATTERN",
multiple_values = true
)]
pub include_path: Option<Vec<Pattern>>,
pub include_path: Option<Vec<Pattern>>,
/// Sets the path to exclude related commits.
#[clap(
long,
env = "GIT_CLIFF_EXCLUDE_PATH",
value_name = "PATTERN",
multiple_values = true
)]
pub exclude_path: Option<Vec<Pattern>>,
pub exclude_path: Option<Vec<Pattern>>,
/// Sets custom commit messages to include in the changelog.
#[clap(
long,
env = "GIT_CLIFF_WITH_COMMIT",
value_name = "MSG",
multiple_values = true
)]
pub with_commit: Option<Vec<String>>,
pub with_commit: Option<Vec<String>>,
/// Prepends entries to the given changelog file.
#[clap(short, long, env = "GIT_CLIFF_PREPEND", value_name = "PATH")]
pub prepend: Option<PathBuf>,
pub prepend: Option<PathBuf>,
/// Writes output to the given file.
#[clap(short, long, env = "GIT_CLIFF_OUTPUT", value_name = "PATH")]
pub output: Option<PathBuf>,
pub output: Option<PathBuf>,
/// Sets the tag for the latest version.
#[clap(
short,
Expand All @@ -84,7 +84,7 @@ pub struct Opt {
value_name = "TAG",
allow_hyphen_values = true
)]
pub tag: Option<String>,
pub tag: Option<String>,
/// Sets the template for the changelog body.
#[clap(
short,
Expand All @@ -93,36 +93,40 @@ pub struct Opt {
value_name = "TEMPLATE",
allow_hyphen_values = true
)]
pub body: Option<String>,
pub body: Option<String>,
/// Writes the default configuration file to cliff.toml
#[clap(short, long, help_heading = Some("FLAGS"))]
pub init: bool,
pub init: bool,
/// Processes the commits starting from the latest tag.
#[clap(short, long, help_heading = Some("FLAGS"))]
pub latest: bool,
pub latest: bool,
/// Processes the commits that belong to the current tag.
#[clap(long, help_heading = Some("FLAGS"))]
pub current: bool,
pub current: bool,
/// Processes the commits that do not belong to a tag.
#[clap(short, long, help_heading = Some("FLAGS"))]
pub unreleased: bool,
pub unreleased: bool,
/// Sorts the tags chronologically.
#[clap(long, help_heading = Some("FLAGS"))]
pub date_order: bool,
pub date_order: bool,
/// Prints changelog context as JSON.
#[clap(long, help_heading = Some("FLAGS"))]
pub context: bool,
pub context: bool,
/// Strips the given parts from the changelog.
#[clap(short, long, value_name = "PART", arg_enum)]
pub strip: Option<Strip>,
pub strip: Option<Strip>,
/// Sets sorting of the commits inside sections.
#[clap(
long,
arg_enum,
default_value_t = Sort::Oldest
)]
pub sort: Sort,
pub sort: Sort,
/// Sets the commit range to process.
#[clap(value_name = "RANGE", help_heading = Some("ARGS"))]
pub range: Option<String>,
pub range: Option<String>,
/// Adds [CommitPreprocessor] steps for github issue & pull request
/// references (#i)
#[clap(long, env = "GIT_CLIFF_GITHUB_REPOSITORY_URL")]
pub github_repository_url: Option<String>,
}
31 changes: 30 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use args::{
use changelog::Changelog;
use clap::ArgEnum;
use git_cliff_core::commit::Commit;
use git_cliff_core::config::Config;
use git_cliff_core::config::{
CommitPreprocessor,
Config,
};
use git_cliff_core::embed::EmbeddedConfig;
use git_cliff_core::error::{
Error,
Expand Down Expand Up @@ -101,6 +104,12 @@ pub fn run(mut args: Opt) -> Result<()> {
config.changelog.body = EmbeddedConfig::parse()?.changelog.body;
}

// Add custom git_cliff_core::config::LinkParser for github issues & pull
// requests
if let Some(ref url) = args.github_repository_url {
add_github_link_preprocessors(&mut config, url.clone())?;
}

// Update the configuration based on command line arguments and vice versa.
match args.strip {
Some(Strip::Header) => {
Expand Down Expand Up @@ -301,3 +310,23 @@ pub fn run(mut args: Opt) -> Result<()> {
changelog.generate(&mut io::stdout())
}
}

fn add_github_link_preprocessors(
config: &mut Config,
repository_url: String,
) -> Result<()> {
let github_commit_preprocessor = CommitPreprocessor {
pattern: regex::Regex::new("#(\\d+)")?,
replace: Some(format!(
"[#${{1}}](https://{repository_url}/issues/${{1}})"
)),
replace_command: None,
};
if let Some(v) = config.git.commit_preprocessors.as_mut() {
v.push(github_commit_preprocessor);
} else {
config.git.commit_preprocessors =
Some([github_commit_preprocessor].into_iter().collect());
}
Ok(())
}