Skip to content

Commit acff31e

Browse files
authored
patch: allow configuring the "No newline at end of file" message (#37)
Allow configuring the "No newline at end of file" message from being printed when formatting a patch.
1 parent c7df5fb commit acff31e

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/diff/tests.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
diff::{Diff, DiffRange},
55
patch::Patch,
66
range::Range,
7+
PatchFormatter,
78
};
89

910
// Helper macros are based off of the ones used in [dissimilar](https://docs.rs/dissimilar)
@@ -518,6 +519,35 @@ fn no_newline_at_eof() {
518519
assert_patch!(old, new, expected);
519520
}
520521

522+
#[test]
523+
fn without_no_newline_at_eof_message() {
524+
let old = "old line";
525+
let new = "new line";
526+
let expected = "\
527+
--- original
528+
+++ modified
529+
@@ -1 +1 @@
530+
-old line
531+
+new line
532+
";
533+
534+
let f = PatchFormatter::new().missing_newline_message(false);
535+
let patch = create_patch(old, new);
536+
let bpatch = create_patch_bytes(old.as_bytes(), new.as_bytes());
537+
let patch_str = format!("{}", f.fmt_patch(&patch));
538+
let mut patch_bytes = Vec::new();
539+
f.write_patch_into(&bpatch, &mut patch_bytes).unwrap();
540+
541+
assert_eq!(patch_str, expected);
542+
assert_eq!(patch_bytes, patch_str.as_bytes());
543+
assert_eq!(patch_bytes, expected.as_bytes());
544+
assert_eq!(apply(old, &patch).unwrap(), new);
545+
assert_eq!(
546+
crate::apply_bytes(old.as_bytes(), &bpatch).unwrap(),
547+
new.as_bytes()
548+
);
549+
}
550+
521551
#[test]
522552
fn myers_diffy_vs_git() {
523553
let original = "\

src/patch/format.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{
99
#[derive(Debug)]
1010
pub struct PatchFormatter {
1111
with_color: bool,
12+
with_missing_newline_message: bool,
1213

1314
context: Style,
1415
delete: Style,
@@ -23,6 +24,7 @@ impl PatchFormatter {
2324
pub fn new() -> Self {
2425
Self {
2526
with_color: false,
27+
with_missing_newline_message: true,
2628

2729
context: Style::new(),
2830
delete: Color::Red.normal(),
@@ -39,6 +41,19 @@ impl PatchFormatter {
3941
self
4042
}
4143

44+
/// Sets whether to format a patch with a "No newline at end of file" message.
45+
///
46+
/// Default is `true`.
47+
///
48+
/// Note: If this is disabled by setting to `false`, formatted patches will no longer contain
49+
/// sufficient information to determine if a file ended with a newline character (`\n`) or not
50+
/// and the patch will be formatted as if both the original and modified files ended with a
51+
/// newline character (`\n`).
52+
pub fn missing_newline_message(mut self, enable: bool) -> Self {
53+
self.with_missing_newline_message = enable;
54+
self
55+
}
56+
4257
/// Returns a `Display` impl which can be used to print a Patch
4358
pub fn fmt_patch<'a>(&'a self, patch: &'a Patch<'a, str>) -> impl Display + 'a {
4459
PatchDisplay { f: self, patch }
@@ -238,7 +253,9 @@ impl<T: AsRef<[u8]> + ?Sized> LineDisplay<'_, T> {
238253

239254
if !line.ends_with(b"\n") {
240255
writeln!(w)?;
241-
writeln!(w, "{}", NO_NEWLINE_AT_EOF)?;
256+
if self.f.with_missing_newline_message {
257+
writeln!(w, "{}", NO_NEWLINE_AT_EOF)?;
258+
}
242259
}
243260

244261
Ok(())
@@ -269,7 +286,9 @@ impl Display for LineDisplay<'_, str> {
269286

270287
if !line.ends_with('\n') {
271288
writeln!(f)?;
272-
writeln!(f, "{}", NO_NEWLINE_AT_EOF)?;
289+
if self.f.with_missing_newline_message {
290+
writeln!(f, "{}", NO_NEWLINE_AT_EOF)?;
291+
}
273292
}
274293

275294
Ok(())

0 commit comments

Comments
 (0)