From 5ab13f2f4887fa7e39676811e3eda8cf6b6d98a3 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <jelte.fennema@microsoft.com>
Date: Tue, 19 Dec 2023 16:51:23 +0100
Subject: [PATCH v6 3/4] Add --write flag to pgindent

The --write flag can be used to still write files even if --check or
--diff are provided. This allows for easy and efficient integration into
some pre-commit hook workflows.
---
 src/tools/pgindent/pgindent | 31 +++++++++++++++++--------------
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index 0ae4dcddb1c..10f8cbaeabf 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -23,7 +23,7 @@ my $devnull = File::Spec->devnull;
 
 my ($typedefs_file, $typedef_str, @excludes,
 	$indent, $build, $diff,
-	$check, $help, @commits,);
+	$check, $write, $help, @commits,);
 
 $help = 0;
 
@@ -35,9 +35,15 @@ my %options = (
 	"excludes=s" => \@excludes,
 	"indent=s" => \$indent,
 	"diff" => \$diff,
+	"write" => \$write,
 	"check" => \$check,);
 GetOptions(%options) || usage("bad command line argument");
 
+if (!$check && !$diff)
+{
+	$write = 1;
+}
+
 usage() if $help;
 
 usage("Cannot use --commit with command line file list")
@@ -320,8 +326,9 @@ Options:
 	--list-of-typedefs=STR  string containing typedefs, space separated
 	--excludes=PATH         file containing list of filename patterns to ignore
 	--indent=PATH           path to pg_bsd_indent program
-	--diff                  show the changes that would be made
-	--check                 exit with status 2 if any changes would be made
+	--diff                  show the changes that need to be made
+	--check                 exit with status 2 if any changes need to be made
+	--write                 rewrites files that need changes (default if neither --check/--diff/--write are provided)
 The --excludes and --commit options can be given more than once.
 EOF
 	if ($help)
@@ -415,21 +422,17 @@ foreach my $source_filename (@files)
 
 	if ($source ne $orig_source)
 	{
-		if (!$diff && !$check)
+		if ($write)
 		{
 			write_source($source, $source_filename);
 		}
-		else
+		if ($diff)
+		{
+			print diff($source, $source_filename);
+		}
+		if ($check)
 		{
-			if ($diff)
-			{
-				print diff($source, $source_filename);
-			}
-
-			if ($check)
-			{
-				$status = 2;
-			}
+			$status = 2;
 		}
 	}
 }
-- 
2.34.1

