diff --git a/README.md b/README.md index 5c8602b..b315dde 100644 --- a/README.md +++ b/README.md @@ -117,9 +117,11 @@ git push xxx user-fork with `--file ` (`-f`). `pkh chlog` walks the commit history back to the previous changelog entry. -Without a version tag, that search is capped at the last 500 commits -(`--limit `, `0` = unlimited): beyond the cap, the entry simply starts -at the oldest commit searched, which keeps the run fast on large histories. +That walk, and the list of commits the entry is built from, are capped at +500 commits (`--limit `, `0` = unlimited): a boundary far behind HEAD — +a stale version tag in a kernel-sized history — truncates the entry to the +newest 500 commits instead of crawling the whole range, which keeps the +run fast on large histories. ### Incremental builds (build records) diff --git a/src/changelog.rs b/src/changelog.rs index 5ee8326..3d1b0d0 100644 --- a/src/changelog.rs +++ b/src/changelog.rs @@ -458,8 +458,10 @@ pub fn parse_changelog_footer(path: &Path) -> Result<(String, String), Box Result, Box> { // Preferred boundary: the tag of the current (previous) version if let Some(tag_id) = find_version_tag(repo, version) { - return commit_summaries_since(repo, tag_id); + return commit_summaries_since(repo, tag_id, history_limit); } // No tag: fall back to the last commit that modified the changelog @@ -493,7 +495,7 @@ fn get_commits_since_version( the entry only covers the commits since then.", history_limit ); - commit_summaries_since(repo, oldest_visited) + commit_summaries_since(repo, oldest_visited, history_limit) } Boundary::Found(boundary) => { // Prefer a tag of the version the committed changelog carries: @@ -502,7 +504,7 @@ fn get_commits_since_version( let boundary_id = version_at_commit(repo, &boundary, rel_path) .and_then(|committed_version| find_version_tag(repo, &committed_version)) .unwrap_or_else(|| boundary.id()); - commit_summaries_since(repo, boundary_id) + commit_summaries_since(repo, boundary_id, history_limit) } } } @@ -545,10 +547,12 @@ fn find_version_tag(repo: &Repository, version: &str) -> Option { None } -/// The commit summaries from HEAD down to (excluding) `boundary` +/// The commit summaries from HEAD down to (excluding) `boundary`, newest +/// first, capped at `history_limit` entries (0 = unlimited) fn commit_summaries_since( repo: &Repository, boundary: Oid, + history_limit: usize, ) -> Result, Box> { let mut revwalk = repo.revwalk()?; revwalk.set_sorting(Sort::TIME)?; @@ -556,14 +560,26 @@ fn commit_summaries_since( revwalk.hide(boundary)?; let mut commits = Vec::new(); + let mut truncated = false; for id in revwalk { let id = id?; let commit = repo.find_commit(id)?; let message = commit.message().unwrap_or("").trim(); let summary = message.lines().next().unwrap_or("").to_string(); - if !summary.is_empty() { - commits.push(summary); + if summary.is_empty() { + continue; } + if history_limit > 0 && commits.len() >= history_limit { + truncated = true; + break; + } + commits.push(summary); + } + if truncated { + log::warn!( + "More than {history_limit} commits since the previous entry; \ + the entry lists only the newest {history_limit}." + ); } Ok(commits) @@ -604,7 +620,7 @@ fn last_commit_touching<'a>( let mut visited = 0; for id in revwalk { let commit = repo.find_commit(id?)?; - if commit_touches_path(repo, &commit, rel_path)? { + if commit_touches_path(&commit, rel_path) { return Ok(Boundary::Found(commit)); } if history_limit > 0 { @@ -619,22 +635,23 @@ fn last_commit_touching<'a>( } /// Whether `commit` modified the file at `rel_path` relative to its first -/// parent (a root commit counts when it adds the file) -fn commit_touches_path( - repo: &Repository, - commit: &git2::Commit<'_>, - rel_path: &Path, -) -> Result> { - let new_tree = commit.tree()?; - let parent_tree = if commit.parent_count() > 0 { - Some(commit.parent(0)?.tree()?) +/// parent (a root commit counts when it adds the file): the tree entry at +/// the path — blob id and mode, so a mode-only change still counts — +/// differs between the two trees. Comparing entries keeps the per-commit +/// cost at two tree lookups, where a full tree diff per commit is what +/// made the boundary search crawl on kernel-sized trees. +fn commit_touches_path(commit: &git2::Commit<'_>, rel_path: &Path) -> bool { + let entry = |commit: Option<&git2::Commit<'_>>| { + let tree = commit?.tree().ok()?; + let entry = tree.get_path(rel_path).ok()?; + Some((entry.id(), entry.filemode())) + }; + let parent = if commit.parent_count() > 0 { + commit.parent(0).ok() } else { None }; - let mut opts = git2::DiffOptions::new(); - opts.pathspec(rel_path.to_string_lossy().to_string()); - let diff = repo.diff_tree_to_tree(parent_tree.as_ref(), Some(&new_tree), Some(&mut opts))?; - Ok(diff.deltas().len() > 0) + entry(Some(commit)) != entry(parent.as_ref()) } /// The version recorded by the changelog content as of `commit` @@ -1409,6 +1426,65 @@ mod tests { assert!(!repo_dir.join("debian/changelog").exists()); } + /// A version tag far behind HEAD bounds nothing by itself: a + /// kernel-sized range since the tag caps the entry at the `--limit` + /// newest summaries instead of walking (and listing) the whole range. + #[tokio::test] + async fn test_generate_entry_summary_cap_since_tag() { + let temp_dir = TempDir::new().unwrap(); + let repo_dir = temp_dir.path(); + setup_repo(repo_dir); + + let changelog_path = repo_dir.join("debian/changelog"); + std::fs::create_dir_all(repo_dir.join("debian")).unwrap(); + std::fs::write( + &changelog_path, + "mypackage (0.1.0-1) unstable; urgency=medium\n\n * Initial release\n\n -- Maintainer Wed, 01 Jan 2020 00:00:00 +0000\n", + ) + .unwrap(); + Command::new("git") + .arg("add") + .arg(".") + .current_dir(repo_dir) + .output() + .unwrap(); + commit_dated(repo_dir, "Initial commit", 0); + tag(repo_dir, "debian/0.1.0-1"); + for i in 1..=10 { + commit_dated(repo_dir, &format!("Fix bug {i}"), i); + } + + let _identity = IDENTITY_LOCK.lock().await; + unsafe { + std::env::set_var("DEBFULLNAME", "Maintainer Maintainer"); + std::env::set_var("DEBEMAIL", "maintainer@maintainer.com"); + } + generate_entry( + "debian/changelog", + Some(repo_dir), + None, + None, + EntryKind::Normal, + 4, + ) + .await + .unwrap(); + unsafe { + std::env::remove_var("DEBFULLNAME"); + std::env::remove_var("DEBEMAIL"); + } + + // The entry carries only the 4 newest summaries: "Fix bug 1" is + // truncated away with everything older than "Fix bug 7" + let content = std::fs::read_to_string(&changelog_path).unwrap(); + assert!(content.contains("mypackage (0.1.0-2) unstable; urgency=medium")); + assert!(content.contains("* Fix bug 10")); + assert!(content.contains("* Fix bug 7")); + assert!(!content.contains("* Fix bug 6")); + assert!(!content.contains("* Fix bug 1\n")); + assert!(!content.contains("* Initial commit")); + } + #[test] fn test_compute_new_version() { // Debian upload diff --git a/src/main.rs b/src/main.rs index 348f190..fd1b6f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -366,7 +366,7 @@ fn main() { .arg(arg!(--rebuild "Number the entry as a no-change rebuild (1.0-1 becomes 1.0-1build1)").required(false)) .arg(arg!(-v --version "Target version (overrides the --backport/--nmu/--rebuild numbering)").required(false)) .arg(arg!(-f --file "Path to the changelog file to generate into and edit (default: debian/changelog)").required(false).default_value("debian/changelog")) - .arg(arg!(--limit "Cap how far back the history is searched for the previous changelog entry when no version tag exists (0 = unlimited)").required(false) + .arg(arg!(--limit "Cap how far back the history is searched for the previous changelog entry, and how many commits the entry lists (0 = unlimited)").required(false) .value_parser(clap::value_parser!(usize)).default_value("500")), ) .subcommand(