Skip to content

Commit 9f582c7

Browse files
author
nightcityblade
committed
fix(git): preserve status trailing newline
1 parent 8a24ce2 commit 9f582c7

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

src/cmds/git/git.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::core::utils::{
1212
exit_code_from_output, exit_code_from_status, join_with_overflow, resolved_command, strip_ansi,
1313
};
1414
use anyhow::{Context, Result};
15+
use std::borrow::Cow;
1516
use std::ffi::OsString;
1617
use std::process::Command;
1718
use std::process::Stdio;
@@ -821,6 +822,14 @@ fn filter_status_with_args(output: &str) -> String {
821822
}
822823
}
823824

825+
fn ensure_trailing_newline(output: &str) -> Cow<'_, str> {
826+
if output.is_empty() || output.ends_with('\n') {
827+
Cow::Borrowed(output)
828+
} else {
829+
Cow::Owned(format!("{output}\n"))
830+
}
831+
}
832+
824833
fn run_status(args: &[String], verbose: u8, global_args: &[String]) -> Result<i32> {
825834
let timer = tracking::TimedExecution::start();
826835

@@ -849,14 +858,15 @@ fn run_status(args: &[String], verbose: u8, global_args: &[String]) -> Result<i3
849858

850859
// Apply minimal filtering: strip ANSI, remove hints, empty lines
851860
let filtered = filter_status_with_args(&result.stdout);
852-
let filtered = never_worse(&result.stdout, &filtered).to_string();
853-
print!("{}", filtered);
861+
let filtered = never_worse(&result.stdout, &filtered);
862+
let emitted = ensure_trailing_newline(filtered);
863+
print!("{}", emitted);
854864

855865
timer.track(
856866
&format!("git status {}", args.join(" ")),
857867
&format!("rtk git status {}", args.join(" ")),
858868
&result.stdout,
859-
&filtered,
869+
&emitted,
860870
);
861871

862872
return Ok(0);
@@ -2784,6 +2794,13 @@ no changes added to commit (use "git add" and/or "git commit -a")
27842794
assert!(result.contains("nothing to commit"));
27852795
}
27862796

2797+
#[test]
2798+
fn test_ensure_trailing_newline() {
2799+
assert_eq!(ensure_trailing_newline(""), "");
2800+
assert_eq!(ensure_trailing_newline("?? new.py"), "?? new.py\n");
2801+
assert_eq!(ensure_trailing_newline("?? new.py\n"), "?? new.py\n");
2802+
}
2803+
27872804
#[test]
27882805
fn test_filter_log_output_multibyte() {
27892806
// Thai characters: each is 3 bytes. A line with >80 bytes but few chars

0 commit comments

Comments
 (0)