Skip to content

Commit

Permalink
feat(monorepo): automatically set include-path for current directory
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Dec 4, 2024
1 parent 98453b3 commit c1c8b79
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
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
23 changes: 21 additions & 2 deletions git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use git_cliff_core::{
DEFAULT_CONFIG,
IGNORE_FILE,
};
use glob::Pattern;
use std::env;
use std::fs::{
self,
Expand Down Expand Up @@ -210,9 +211,27 @@ 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().to_string(),
)?]);
}
}

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 @@ -250,7 +269,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
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 c1c8b79

Please sign in to comment.