Skip to content

Commit

Permalink
feat(args): add --exclude-path argument for excluding related commits
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 1, 2021
1 parent edb343a commit 25a1d49
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 28 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ git-cliff [FLAGS] [OPTIONS] [RANGE]
-c, --config <PATH> Sets the configuration file [env: CONFIG=] [default: cliff.toml]
-w, --workdir <PATH> Sets the working directory [env: WORKDIR=]
-r, --repository <PATH> Sets the git repository [env: REPOSITORY=]
--commit-path <PATTERN>... Sets the directory to parse commits from [env: COMMIT_PATH=]
--commit-path <PATTERN>... Sets the path to include related commits [env: COMMIT_PATH=]
--exclude-path <PATTERN>... Sets the path to exclude related commits [env: EXCLUDE_PATH=]
-p, --prepend <PATH> Prepends entries to the given changelog file [env: PREPEND=
-o, --output <PATH> Writes output to the given file [env: OUTPUT=]
-t, --tag <TAG> Sets the tag for the latest version [env: TAG=]
Expand Down Expand Up @@ -208,6 +209,7 @@ Generate a changelog scoped to a specific directory (useful for monorepos):

```sh
git cliff --commit-path "**/*.toml" --commit-path "*.md"
git cliff --exclude-path ".github/*"
```

Sort the commits inside sections:
Expand Down
29 changes: 20 additions & 9 deletions git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ impl Repository {
pub fn commits(
&self,
range: Option<String>,
path: Option<Vec<Pattern>>,
include_path: Option<Vec<Pattern>>,
exclude_path: Option<Vec<Pattern>>,
) -> Result<Vec<Commit>> {
let mut revwalk = self.inner.revwalk()?;
revwalk.set_sorting(Sort::TIME | Sort::TOPOLOGICAL)?;
Expand All @@ -53,7 +54,7 @@ impl Repository {
.filter_map(|id| id.ok())
.filter_map(|id| self.inner.find_commit(id).ok())
.collect();
if let Some(commit_path) = path {
if include_path.is_some() || exclude_path.is_some() {
commits = commits
.into_iter()
.filter(|commit| {
Expand All @@ -63,13 +64,23 @@ impl Repository {
prev_commit.tree().ok().as_ref(),
None,
) {
return diff.deltas().any(|delta| {
delta.new_file().path().map_or(false, |path| {
commit_path
.iter()
.any(|glob| glob.matches_path(path))
})
});
return diff
.deltas()
.filter_map(|delta| delta.new_file().path())
.any(|new_file_path| {
if let Some(include_path) = &include_path {
include_path.iter().any(|glob| {
glob.matches_path(new_file_path)
})
} else if let Some(exclude_path) = &exclude_path
{
!exclude_path.iter().any(|glob| {
glob.matches_path(new_file_path)
})
} else {
false
}
});
}
}
false
Expand Down
37 changes: 20 additions & 17 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use structopt::StructOpt;
pub struct Opt {
/// Increases the logging verbosity.
#[structopt(short, long, parse(from_occurrences), alias = "debug")]
pub verbose: u8,
pub verbose: u8,
/// Sets the configuration file.
#[structopt(
short,
Expand All @@ -30,25 +30,28 @@ pub struct Opt {
value_name = "PATH",
default_value = DEFAULT_CONFIG,
)]
pub config: PathBuf,
pub config: PathBuf,
/// Sets the working directory.
#[structopt(short, long, env, value_name = "PATH")]
pub workdir: Option<PathBuf>,
pub workdir: Option<PathBuf>,
/// Sets the git repository.
#[structopt(short, long, env, value_name = "PATH")]
pub repository: Option<PathBuf>,
/// Sets the directory to parse commits from.
pub repository: Option<PathBuf>,
/// Sets the path to include related commits.
#[structopt(long, env, value_name = "PATTERN")]
pub commit_path: Option<Vec<Pattern>>,
pub commit_path: Option<Vec<Pattern>>,
/// Sets the path to exclude related commits.
#[structopt(long, env, value_name = "PATTERN")]
pub exclude_path: Option<Vec<Pattern>>,
/// Prepends entries to the given changelog file.
#[structopt(short, long, env, value_name = "PATH")]
pub prepend: Option<PathBuf>,
pub prepend: Option<PathBuf>,
/// Writes output to the given file.
#[structopt(short, long, env, value_name = "PATH")]
pub output: Option<PathBuf>,
pub output: Option<PathBuf>,
/// Sets the tag for the latest version.
#[structopt(short, long, env, value_name = "TAG", allow_hyphen_values = true)]
pub tag: Option<String>,
pub tag: Option<String>,
/// Sets the template for the changelog body.
#[structopt(
short,
Expand All @@ -57,35 +60,35 @@ 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
#[structopt(short, long)]
pub init: bool,
pub init: bool,
/// Processes the commits starting from the latest tag.
#[structopt(short, long)]
pub latest: bool,
pub latest: bool,
/// Processes the commits that do not belong to a tag.
#[structopt(short, long)]
pub unreleased: bool,
pub unreleased: bool,
/// Sorts the tags topologically.
#[structopt(long)]
pub topo_order: bool,
pub topo_order: bool,
/// Strips the given parts from the changelog.
#[structopt(
short,
long,
value_name = "PART",
possible_values = &["header", "footer", "all"]
)]
pub strip: Option<String>,
pub strip: Option<String>,
/// Sets the commit range to process.
#[structopt(value_name = "RANGE")]
pub range: Option<String>,
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,
}
4 changes: 3 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ pub fn run(mut args: Opt) -> Result<()> {
commit_range = Some(format!("{}..{}", tag1, tag2));
}
}
let commits = repository.commits(commit_range, args.commit_path)?;
let commits =
repository.commits(commit_range, args.commit_path, args.exclude_path)?;

// Update tags.
if let Some(tag) = args.tag {
if let Some(commit_id) = commits.first().map(|c| c.id().to_string()) {
Expand Down

0 comments on commit 25a1d49

Please sign in to comment.