v2-0001-Fix-pgindent-truncates-last-line-of-files.patch

application/octet-stream

Filename: v2-0001-Fix-pgindent-truncates-last-line-of-files.patch
Type: application/octet-stream
Part: 0
Message: Re: [PATCH] pgindent truncates last line of files missing a trailing newline

Patch

Format: format-patch
Series: patch v2-0001
Subject: Fix pgindent to ensure files end with a trailing newline.
File+
src/tools/pg_bsd_indent/lexi.c 23 6
src/tools/pg_bsd_indent/tests/no_trailing_newline.0 4 0
src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout 6 0
From f1f4c2886e07d8a376c34cd958eea1534ee5f96e Mon Sep 17 00:00:00 2001
From: Akshay Joshi <akshay.joshi@enterprisedb.com>
Date: Mon, 16 Feb 2026 16:39:08 +0530
Subject: [PATCH] Fix pgindent to ensure files end with a trailing newline.

If a source file is missing a trailing newline, pg_bsd_indent now adds
one. The fill_buffer() function already adds a synthetic newline at EOF;
this change ensures that newline is emitted to the output rather than
being suppressed.

Author: Akshay Joshi <akshay.joshi@enterprisedb.com>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
---
 src/tools/pg_bsd_indent/lexi.c                | 29 +++++++++++++++----
 .../pg_bsd_indent/tests/no_trailing_newline.0 |  4 +++
 .../tests/no_trailing_newline.0.stdout        |  6 ++++
 3 files changed, 33 insertions(+), 6 deletions(-)
 create mode 100644 src/tools/pg_bsd_indent/tests/no_trailing_newline.0
 create mode 100644 src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout

diff --git a/src/tools/pg_bsd_indent/lexi.c b/src/tools/pg_bsd_indent/lexi.c
index 943bf7ce6b0..b026fef6fca 100644
--- a/src/tools/pg_bsd_indent/lexi.c
+++ b/src/tools/pg_bsd_indent/lexi.c
@@ -453,12 +453,29 @@ lexi(struct parser_state *state)
     case '\n':
 	unary_delim = state->last_u_d;
 	state->last_nl = true;	/* remember that we just had a newline */
-	code = (had_eof ? 0 : newline);
-
-	/*
-	 * if data has been exhausted, the newline is a dummy, and we should
-	 * return code to stop
-	 */
+	{
+		/*
+		* Ensure we output exactly one newline at EOF: either the file's real
+		* trailing newline, or the synthetic one added by fill_buffer() if the
+		* file was missing one.
+		*/
+		static bool final_newline_done = false;
+
+		if (had_eof)
+		{
+			if (final_newline_done)
+				code = 0;			/* already emitted final newline, stop now */
+			else
+			{
+				code = newline;
+				final_newline_done = true;
+			}
+		}
+		else
+		{
+			code = newline;
+		}
+	}
 	break;
 
     case '\'':			/* start of quoted character */
diff --git a/src/tools/pg_bsd_indent/tests/no_trailing_newline.0 b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0
new file mode 100644
index 00000000000..3233990e11f
--- /dev/null
+++ b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0
@@ -0,0 +1,4 @@
+/* Test: file without trailing newline */
+void test(void) {
+int x = 1;
+}
\ No newline at end of file
diff --git a/src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout
new file mode 100644
index 00000000000..c632d62f16c
--- /dev/null
+++ b/src/tools/pg_bsd_indent/tests/no_trailing_newline.0.stdout
@@ -0,0 +1,6 @@
+/* Test: file without trailing newline */
+void
+test(void)
+{
+	int		x = 1;
+}
-- 
2.51.0