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

Add local changelog duplicate detection #149

Merged
merged 4 commits into from
Dec 3, 2023
Merged
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
3 changes: 3 additions & 0 deletions .changelog/unreleased/features/149-local-dup-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Add CLI subcommand `find-duplicates` to assist in finding changelog entries
that are duplicated across releases - see the README for more details
([\#149](https://github.com/informalsystems/unclog/pull/149))
105 changes: 105 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ simplelog = { version = "0.12", optional = true }
clap = { version = "4.4", features = ["derive", "env"], optional = true }
tempfile = { version = "3.8", optional = true }
chrono = "0.4.31"
comfy-table = "7.1.0"

[dev-dependencies]
env_logger = "0.10"
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ file (see below) prior to attempting to add an entry that references any of your
components. Otherwise `unclog` will fail. This is to ensure that people don't
add entries for incorrectly named or non-existent components.

### Duplicate detection

`unclog` has a convenience method to assist in finding duplicate entries across
releases on your local branch (as per [\#81], a future version aims to provide
this _across_ Git repository branches).

```bash
# List all the duplicate entries across releases.
unclog find-duplicates
```

### Configuration

Certain `unclog` settings can be overridden through the use of a configuration
Expand Down Expand Up @@ -386,3 +397,4 @@ limitations under the License.
[license-image]: https://img.shields.io/badge/license-Apache2.0-blue.svg
[license-link]: https://github.com/informalsystems/unclog/blob/main/LICENSE
[rustc-image]: https://img.shields.io/badge/rustc-stable-blue.svg
[\#81]: https://github.com/informalsystems/unclog/issues/81
47 changes: 46 additions & 1 deletion src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `unclog` helps you build your changelog.

use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
use log::error;
use simplelog::{ColorChoice, LevelFilter, TermLogger, TerminalMode};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -121,6 +121,12 @@ enum Command {
#[arg(name = "message", short, long)]
maybe_message: Option<String>,
},
/// Searches for duplicate entries across releases in this changelog.
FindDuplicates {
/// The format to use when writing the duplicates to stdout.
#[arg(value_enum, short, long, default_value = "simple")]
format: DuplicatesOutputFormat,
},
/// Build the changelog from the input path and write the output to stdout.
Build {
/// Render all changes, including released and unreleased ones.
Expand All @@ -141,6 +147,15 @@ enum Command {
},
}

#[derive(Debug, Clone, Default, Copy, ValueEnum)]
enum DuplicatesOutputFormat {
/// A simple table with no borders.
#[default]
Simple,
/// A table with borders made of ASCII characters.
AsciiTable,
}

fn main() {
let opt: Opt = Opt::parse();
TermLogger::init(
Expand Down Expand Up @@ -230,6 +245,7 @@ fn main() {
&id,
),
},
Command::FindDuplicates { format } => find_duplicates(&config, &opt.path, format),
Command::Release { editor, version } => {
prepare_release(&config, &editor, &opt.path, &version)
}
Expand Down Expand Up @@ -319,6 +335,35 @@ fn add_unreleased_entry_with_editor(
Changelog::add_unreleased_entry(config, path, section, component, id, &tmpfile_content)
}

fn find_duplicates(
config: &Config,
path: &Path,
output_format: DuplicatesOutputFormat,
) -> Result<()> {
let changelog = Changelog::read_from_dir(config, path)?;
let dups = changelog.find_duplicates_across_releases();
if dups.is_empty() {
log::info!("No duplicates found");
return Ok(());
}
log::info!("Found {} duplicate(s)", dups.len());

let mut table = comfy_table::Table::new();
table.load_preset(match output_format {
DuplicatesOutputFormat::Simple => comfy_table::presets::NOTHING,
DuplicatesOutputFormat::AsciiTable => comfy_table::presets::ASCII_FULL,
});

for (path_a, path_b) in dups {
table.add_row(vec![
path.join(path_a.as_path(config)).display(),
path.join(path_b.as_path(config)).display(),
]);
}
println!("{table}");
Ok(())
}

fn prepare_release(config: &Config, editor: &Path, path: &Path, version: &str) -> Result<()> {
// Add the summary to the unreleased folder, since we'll be moving it to
// the new release folder
Expand Down
Loading