Skip to content

Commit

Permalink
Merge branch 'main' into feat/git-cliff-tui
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 13, 2024
2 parents df9c5da + e85888f commit 4fcc94f
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 11 deletions.
34 changes: 34 additions & 0 deletions .github/fixtures/test-monorepo-include-path/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[changelog]
# template for the changelog footer
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

[git]
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^feat", group = "Features", default_scope = "app" },
{ message = "^fix", group = "Bug Fixes", scope = "cli" },
]
8 changes: 8 additions & 0 deletions .github/fixtures/test-monorepo-include-path/commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -e

git remote add origin https://github.com/orhun/git-cliff
git fetch
mv cliff.toml cliff.toml.bak
git checkout 076feb74b4d8c8634669f57d4e2765c39490d80e
mv cliff.toml.bak cliff.toml
18 changes: 18 additions & 0 deletions .github/fixtures/test-monorepo-include-path/expected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

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

## [unreleased]

### Bug Fixes

- Include the root commit when `--latest` is used with one tag (#901)
- Match PR and release metadata correctly (#907)
- Fix missing commit fields in context (#837) (#920)
- Preserve first time contributors (#925)

### Features

- Allow overriding the remote API URL via config (#896)

<!-- 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 @@ -104,6 +104,8 @@ jobs:
command: --tag v0.2.0
- fixtures-name: test-custom-remote-api-url
command: v1.4.0..v1.4.1
- fixtures-name: test-monorepo-include-path
command: v2.6.1..v2.7.0 --include-path .github/fixtures/

steps:
- name: Checkout
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions git-cliff-core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub enum Error {
/// Error that may occur while handling location of directories.
#[error("Directory error: `{0}`")]
DirsError(String),
/// Error that may occur while constructing patterns.
#[error("Pattern error: `{0}`")]
PatternError(#[from] glob::PatternError),
}

/// Result type of the core library.
Expand Down
11 changes: 10 additions & 1 deletion git-cliff-core/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const CHANGED_FILES_CACHE: &str = "changed_files_cache";
pub struct Repository {
inner: GitRepository,
/// Repository path.
pub path: PathBuf,
path: PathBuf,
/// Cache path for the changed files of the commits.
changed_files_cache_path: PathBuf,
}
Expand Down Expand Up @@ -74,6 +74,15 @@ impl Repository {
}
}

/// Returns the path of the repository.
pub fn path(&self) -> PathBuf {
let mut path = self.inner.path().to_path_buf();
if path.ends_with(".git") {
path.pop();
}
path
}

/// Parses and returns the commits.
///
/// Sorts the commits by their time.
Expand Down
1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ env_logger = "=0.10.2"
pprof = { version = "0.14", optional = true }
rand = { version = "0.8.4", optional = true }
url.workspace = true
pathdiff = "0.2.3"

[dependencies.git-cliff-core]
version = "2.7.0" # managed by release.sh
Expand Down
39 changes: 36 additions & 3 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use git_cliff_core::{
DEFAULT_CONFIG,
IGNORE_FILE,
};
use glob::Pattern;
use std::env;
use std::fs::{
self,
Expand Down Expand Up @@ -229,9 +230,26 @@ fn process_repository<'a>(
}
}
}

// Include only the current directory if not running from the root repository
let mut include_path = args.include_path.clone();
if let Some(mut path_diff) =
pathdiff::diff_paths(env::current_dir()?, repository.path())
{
if include_path.is_none() && path_diff != Path::new("") {
info!(
"Including changes from the current directory: {:?}",
path_diff.display()
);
path_diff.extend(["**", "*"]);
include_path =
Some(vec![Pattern::new(path_diff.to_string_lossy().as_ref())?]);
}
}

let mut commits = repository.commits(
commit_range.as_deref(),
args.include_path.clone(),
include_path,
args.exclude_path.clone(),
)?;
if let Some(commit_limit_value) = config.git.limit_commits {
Expand Down Expand Up @@ -269,7 +287,7 @@ fn process_repository<'a>(
let commit = Commit::from(git_commit);
let commit_id = commit.id.to_string();
release.commits.push(commit);
release.repository = Some(repository.path.to_string_lossy().into_owned());
release.repository = Some(repository.path().to_string_lossy().into_owned());
if let Some(tag) = tags.get(&commit_id) {
release.version = Some(tag.name.to_string());
release.message.clone_from(&tag.message);
Expand Down Expand Up @@ -387,7 +405,7 @@ pub fn generate_changelog(args: &mut Args) -> Result<Changelog<'static>> {
}
}

// Parse the configuration file.
// Set path for the configuration file.
let mut path = args.config.clone();
if !path.exists() {
if let Some(config_path) = dirs::config_dir()
Expand All @@ -397,6 +415,7 @@ pub fn generate_changelog(args: &mut Args) -> Result<Changelog<'static>> {
}
}

// Parse the configuration file.
// Load the default configuration if necessary.
let mut config = if let Ok((config, name)) = builtin_config {
info!("Using built-in configuration file: {name}");
Expand All @@ -405,6 +424,20 @@ pub fn generate_changelog(args: &mut Args) -> Result<Changelog<'static>> {
Config::parse(&path)?
} else if let Some(contents) = Config::read_from_manifest()? {
Config::parse_from_str(&contents)?
} else if let Some(discovered_path) =
env::current_dir()?.ancestors().find_map(|dir| {
let path = dir.join(DEFAULT_CONFIG);
if path.is_file() {
Some(path)
} else {
None
}
}) {
info!(
"Using configuration from parent directory: {}",
discovered_path.display()
);
Config::parse(&discovered_path)?
} else {
if !args.context {
warn!(
Expand Down
12 changes: 7 additions & 5 deletions website/docs/usage/monorepos.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ sidebar_position: 5

# Monorepos

You can generate a changelog scoped to a specific directory via `--include-path` and `--exclude-path`.

This requires changing the current working directory to the target folder. The included/excluded paths must be relative to the repository's root.
You can generate a changelog scoped to a specific directory by just switching to that directory:

```bash
cd packages/some_library
git cliff --include-path "packages/some_library/**/*" --repository "../../"
git cliff
```

To include/exclude specific paths, use the `--include-path` and `--exclude-path` arguments:

```bash
cd packages/some_library
git cliff --include-path "packages/some_library/**/*" --repository "../../" --exclude-path ".github/*"
git cliff --include-path "packages/some_library/**/*" --exclude-path ".github/*"
```

These paths must be relative to the repository's root and should be a valid glob pattern.

0 comments on commit 4fcc94f

Please sign in to comment.