public inbox for [email protected]  
help / color / mirror / Atom feed
From: Akshay Joshi <[email protected]>
To: Ashutosh Bapat <[email protected]>
Cc: Tom Lane <[email protected]>
Cc: Andrew Dunstan <[email protected]>
Cc: pgsql-hackers <[email protected]>
Subject: Re: [PATCH] pgindent truncates last line of files missing a trailing newline
Date: Tue, 17 Feb 2026 13:24:05 +0530
Message-ID: <CANxoLDdbhQbap5vd3=01ad67EGbGNKDsvZdYVT3KRUiy-onYqA@mail.gmail.com> (raw)
In-Reply-To: <CAExHW5uSuG9qcGd=o6o6TiwWUKE4ie7K7nmgKzDzUKxrQ9rYDw@mail.gmail.com>
References: <CANxoLDfca8O5SkeDxB_j6SVNXd+pNKaDmVmEW+2yyicdU8fy0w@mail.gmail.com>
	<[email protected]>
	<[email protected]>
	<CAExHW5uSuG9qcGd=o6o6TiwWUKE4ie7K7nmgKzDzUKxrQ9rYDw@mail.gmail.com>

I have addressed the review comments from Andrew.
Attached is the v2 patch, ready for review.

On Tue, Feb 17, 2026 at 7:50 AM Ashutosh Bapat
<[email protected]> wrote:
>
> On Mon, Feb 16, 2026 at 10:20 PM Tom Lane <[email protected]> wrote:
> >
> > Andrew Dunstan <[email protected]> writes:
> > > Yeah. I wonder if we shouldn't be just ensuring that there is a line
> > > feed at the end of the buffer, i.e. add one if it's not there. We
> > > shouldn't be adding files without a trailing LF, and ensuring there is
> > > one seems like a reasonable thing to do in pg_bsd_indent.
> >
> > +1
>
> +1. That will allow me to remove some code from my patch preparation
> script. Ref. [1]
>
> [1] https://www.postgresql.org/message-id/flat/CAExHW5v8u7-2H2LqWP3ybhh5GnAVVeCOYuTfkg9pmdnrLwAtNA%40mai...
>
> --
> Best Wishes,
> Ashutosh Bapat


Attachments:

  [application/octet-stream] v2-0001-Fix-pgindent-truncates-last-line-of-files.patch (2.7K, 2-v2-0001-Fix-pgindent-truncates-last-line-of-files.patch)
  download | inline diff:
From f1f4c2886e07d8a376c34cd958eea1534ee5f96e Mon Sep 17 00:00:00 2001
From: Akshay Joshi <[email protected]>
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 <[email protected]>
Reviewed-by: Andrew Dunstan <[email protected]>
---
 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



view thread (7+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected], [email protected]
  Subject: Re: [PATCH] pgindent truncates last line of files missing a trailing newline
  In-Reply-To: <CANxoLDdbhQbap5vd3=01ad67EGbGNKDsvZdYVT3KRUiy-onYqA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox