diff --git a/src/changelog.rs b/src/changelog.rs index 3d1b0d0..fbcae14 100644 --- a/src/changelog.rs +++ b/src/changelog.rs @@ -547,33 +547,48 @@ fn find_version_tag(repo: &Repository, version: &str) -> Option { None } -/// The commit summaries from HEAD down to (excluding) `boundary`, newest -/// first, capped at `history_limit` entries (0 = unlimited) +/// The commit summaries from HEAD down to the previous entry's boundary, +/// newest first, capped at `history_limit` entries (0 = unlimited) fn commit_summaries_since( repo: &Repository, boundary: Oid, history_limit: usize, ) -> Result, Box> { + // Unsorted, no hide: both revwalk.sorting and revwalk.hide flip + // libgit2's walk into limited mode, where prepare_walk drains the + // whole graph (limit_list) before the first commit is emitted — a + // full-history walk on every run when the boundary is a release tag + // far behind HEAD. The unsorted walk is lazy, so only the collected + // window is visited; the summaries are sorted by committer time + // afterwards. Around the boundary this is approximate: same-second + // commits and merge branches carrying older dates can slip in or out, + // which a draft entry edited before commit absorbs. let mut revwalk = repo.revwalk()?; - revwalk.set_sorting(Sort::TIME)?; + revwalk.set_sorting(Sort::NONE)?; revwalk.push_head()?; - revwalk.hide(boundary)?; - let mut commits = Vec::new(); + let boundary_time = repo.find_commit(boundary)?.time().seconds(); + + let mut collected: Vec<(i64, String)> = 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() { + if id == boundary { continue; } - if history_limit > 0 && commits.len() >= history_limit { - truncated = true; + if commit.time().seconds() < boundary_time { break; } - commits.push(summary); + let message = commit.message().unwrap_or("").trim(); + let summary = message.lines().next().unwrap_or(""); + if !summary.is_empty() { + collected.push((commit.time().seconds(), summary.to_string())); + if history_limit > 0 && collected.len() >= history_limit { + truncated = true; + break; + } + } } if truncated { log::warn!( @@ -581,6 +596,8 @@ fn commit_summaries_since( the entry lists only the newest {history_limit}." ); } + collected.sort_by_key(|&(time, _)| std::cmp::Reverse(time)); + let commits = collected.into_iter().map(|(_, summary)| summary).collect(); Ok(commits) } @@ -602,15 +619,19 @@ fn changelog_in_repo(repo: &Repository, changelog_path: &Path) -> Option( repo: &'a Repository, rel_path: &Path, history_limit: usize, ) -> Result, Box> { + // Unsorted, like commit_summaries_since: any sorting flips the walk + // into libgit2's limited mode, draining the whole graph before the + // first commit is visited. Within the `history_limit` window the + // visit order is walk order rather than newest-first. let mut revwalk = repo.revwalk()?; - revwalk.set_sorting(Sort::TIME)?; + revwalk.set_sorting(Sort::NONE)?; if repo.head().is_err() { // Unborn HEAD: the repository has no commits yet return Ok(Boundary::None);