Skip to content

Commit c576249

Browse files
authored
Merge pull request #773 from mvanhorn/osc/730-pr-diff-name-only-empty
fix(gh): skip compact_diff for --name-only/--stat in pr diff
2 parents 0bb3eaf + 2ef0690 commit c576249

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

src/gh_cmd.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,18 @@ fn pr_merge(args: &[String], _verbose: u8) -> Result<()> {
11181118
Ok(())
11191119
}
11201120

1121+
/// Flags that change `gh pr diff` output from unified diff to a different format.
1122+
/// When present, compact_diff would produce empty output since it expects diff headers.
1123+
fn has_non_diff_format_flag(args: &[String]) -> bool {
1124+
args.iter().any(|a| {
1125+
a == "--name-only"
1126+
|| a == "--name-status"
1127+
|| a == "--stat"
1128+
|| a == "--numstat"
1129+
|| a == "--shortstat"
1130+
})
1131+
}
1132+
11211133
fn pr_diff(args: &[String], _verbose: u8) -> Result<()> {
11221134
// --no-compact: pass full diff through (gh CLI doesn't know this flag, strip it)
11231135
let no_compact = args.iter().any(|a| a == "--no-compact");
@@ -1127,7 +1139,9 @@ fn pr_diff(args: &[String], _verbose: u8) -> Result<()> {
11271139
.cloned()
11281140
.collect();
11291141

1130-
if no_compact {
1142+
// Passthrough when --no-compact or when a format flag changes output away from
1143+
// unified diff (e.g. --name-only produces a filename list, not diff hunks).
1144+
if no_compact || has_non_diff_format_flag(&gh_args) {
11311145
return run_passthrough_with_extra("gh", &["pr", "diff"], &gh_args);
11321146
}
11331147

@@ -1538,6 +1552,46 @@ mod tests {
15381552
assert!(!should_passthrough_issue_view(&[]));
15391553
}
15401554

1555+
// --- has_non_diff_format_flag tests ---
1556+
1557+
#[test]
1558+
fn test_non_diff_format_flag_name_only() {
1559+
assert!(has_non_diff_format_flag(&["--name-only".into()]));
1560+
}
1561+
1562+
#[test]
1563+
fn test_non_diff_format_flag_stat() {
1564+
assert!(has_non_diff_format_flag(&["--stat".into()]));
1565+
}
1566+
1567+
#[test]
1568+
fn test_non_diff_format_flag_name_status() {
1569+
assert!(has_non_diff_format_flag(&["--name-status".into()]));
1570+
}
1571+
1572+
#[test]
1573+
fn test_non_diff_format_flag_numstat() {
1574+
assert!(has_non_diff_format_flag(&["--numstat".into()]));
1575+
}
1576+
1577+
#[test]
1578+
fn test_non_diff_format_flag_shortstat() {
1579+
assert!(has_non_diff_format_flag(&["--shortstat".into()]));
1580+
}
1581+
1582+
#[test]
1583+
fn test_non_diff_format_flag_absent() {
1584+
assert!(!has_non_diff_format_flag(&[]));
1585+
}
1586+
1587+
#[test]
1588+
fn test_non_diff_format_flag_regular_args() {
1589+
assert!(!has_non_diff_format_flag(&[
1590+
"123".into(),
1591+
"--color=always".into()
1592+
]));
1593+
}
1594+
15411595
// --- filter_markdown_body tests ---
15421596

15431597
#[test]

0 commit comments

Comments
 (0)