Skip to content

Commit 0cc3b50

Browse files
authored
Fix recent_commits(limit=0) returning 1 commit instead of 0 (#7334)
Fixes #7333 This is a small bug fix. This PR fixes an inconsistency in `recent_commits` where `limit == 0` still returns 1 commit due to the use of `limit.max(1)` when constructing the `git log -n` argument. Expected behavior: requesting 0 commits should return an empty list. This PR: - returns an empty `Vec` when `limit == 0` - adds a test for `recent_commits(limit == 0)` that fails before the change and passes afterwards - maintains existing behavior for `limit > 0` This aligns behavior with API expectations and avoids downstream consumers misinterpreting the repository as having commit history when `limit == 0` is used to explicitly request none. Happy to adjust if the current behavior is intentional.
1 parent 8532876 commit 0cc3b50

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

codex-rs/core/src/git_info.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,15 @@ pub async fn recent_commits(cwd: &Path, limit: usize) -> Vec<CommitLogEntry> {
131131
}
132132

133133
let fmt = "%H%x1f%ct%x1f%s"; // <sha> <US> <commit_time> <US> <subject>
134-
let n = limit.max(1).to_string();
135-
let Some(log_out) =
136-
run_git_command_with_timeout(&["log", "-n", &n, &format!("--pretty=format:{fmt}")], cwd)
137-
.await
138-
else {
134+
let limit_arg = (limit > 0).then(|| limit.to_string());
135+
let mut args: Vec<String> = vec!["log".to_string()];
136+
if let Some(n) = &limit_arg {
137+
args.push("-n".to_string());
138+
args.push(n.clone());
139+
}
140+
args.push(format!("--pretty=format:{fmt}"));
141+
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
142+
let Some(log_out) = run_git_command_with_timeout(&arg_refs, cwd).await else {
139143
return Vec::new();
140144
};
141145
if !log_out.status.success() {

0 commit comments

Comments
 (0)