Subject: Add --check option to pgindent

From: "Tristan Partin" <[email-masked]>
Date: Mon, 11 Dec 2023 18:09:21 -0600

Not sold on the name, but --check is a combination of --silent-diff and 
--show-diff. I envision --check mostly being used in CI environments. 
I recently came across a situation where this behavior would have been 
useful. Without --check, you're left to capture the output of 
--show-diff and exit 2 if the output isn't empty by yourself.

-- 
Tristan Partin
Neon (https://neon.tech)
From 1001252d49a47e660045cee3d2ba5abd87e925d9 Mon Sep 17 00:00:00 2001
From: Tristan Partin <[email-masked]>
Date: Mon, 11 Dec 2023 17:34:17 -0600
Subject: [PATCH v1] Add --check option to pgindent

The option is a combination of --show-diff and --silent-diff. It is
useful for situations such as CI checks where the diff can be logged,
but will also cause the build to fail. Without such an option, people
are left to re-implement the same logic over and over again.
---
 src/tools/pgindent/pgindent | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index bce63d95da..a285015c76 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -23,7 +23,8 @@ my $devnull = File::Spec->devnull;
 
 my ($typedefs_file, $typedef_str, @excludes,
 	$indent, $build, $show_diff,
-	$silent_diff, $help, @commits,);
+	$silent_diff, $help, @commits,
+	$check,);
 
 $help = 0;
 
@@ -35,7 +36,8 @@ my %options = (
 	"excludes=s" => \@excludes,
 	"indent=s" => \$indent,
 	"show-diff" => \$show_diff,
-	"silent-diff" => \$silent_diff,);
+	"silent-diff" => \$silent_diff,
+	"check" => \$check,);
 GetOptions(%options) || usage("bad command line argument");
 
 usage() if $help;
@@ -43,6 +45,12 @@ usage() if $help;
 usage("Cannot have both --silent-diff and --show-diff")
   if $silent_diff && $show_diff;
 
+usage("Cannot have both --check and --show-diff")
+  if $check && $show_diff;
+
+usage("Cannot have both --check and --silent-diff")
+  if $check && $silent_diff;
+
 usage("Cannot use --commit with command line file list")
   if (@commits && @ARGV);
 
@@ -325,6 +333,7 @@ Options:
 	--indent=PATH           path to pg_bsd_indent program
 	--show-diff             show the changes that would be made
 	--silent-diff           exit with status 2 if any changes would be made
+	--check                 combination of --show-diff and --silent-diff
 The --excludes and --commit options can be given more than once.
 EOF
 	if ($help)
@@ -417,7 +426,12 @@ foreach my $source_filename (@files)
 
 	if ($source ne $orig_source)
 	{
-		if ($silent_diff)
+		if ($check)
+		{
+			print show_diff($source, $source_filename);
+			exit 2;
+		}
+		elsif ($silent_diff)
 		{
 			exit 2;
 		}
-- 
Tristan Partin
Neon (https://neon.tech)

---

From: Daniel Gustafsson <[email-masked]>
Date: Tue, 12 Dec 2023 01:21:36 +0100

> On 12 Dec 2023, at 01:09, Tristan Partin <[email-masked]> wrote:
> 
> Not sold on the name, but --check is a combination of --silent-diff and --show-diff. I envision --check mostly being used in CI environments. I recently came across a situation where this behavior would have been useful. Without --check, you're left to capture the output of --show-diff and exit 2 if the output isn't empty by yourself.

I haven’t studied the patch but I can see that being useful.

./daniel