From b5155b27a1ee35289183d9021941db3be741d664 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <postgres@jeltef.nl>
Date: Tue, 3 Mar 2026 23:46:52 +0100
Subject: [PATCH v4 4/7] pgindent: Try to find pg_bsd_indent binary in common
 locations

To run pgindent you need to to have the right version of pg_bsd_indent
in your PATH, or specify it manually. This is a bit of a hassle,
especially for newcomers or when working on backbranches. So this
chnages pgindent to search for a pg_bsd_indent that's built from the
current sources, both in-tree (for in-tree autoconf builds) and in a
build directory (for meson or autoconf vpath builds).
---
 src/tools/pgindent/pgindent | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index ca56758ffac..27ebdbdbf25 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -83,11 +83,33 @@ usage("Cannot use --commit with command line file list")
 # dir, then default location
 $typedefs_file ||= $ENV{PGTYPEDEFS};
 
-# get indent location for environment or default
-$indent ||= $ENV{PGINDENT} || $ENV{INDENT} || "pg_bsd_indent";
-
 my $sourcedir = locate_sourcedir();
 
+# get indent location: command line wins, then environment, then try to find
+# a compiled pg_bsd_indent in the source tree, then fall back to PATH.
+$indent ||= $ENV{PGINDENT} || $ENV{INDENT};
+if (!$indent && $sourcedir)
+{
+	my $srcroot = "$sourcedir/../../..";
+	my $bsd_indent_subdir = "src/tools/pg_bsd_indent/pg_bsd_indent";
+
+	# Look for pg_bsd_indent: first in-tree (autoconf in-tree build),
+	# then in a "build" directory (meson or autoconf vpath),
+	# then any "build*" directory.
+	foreach my $candidate (
+		"$srcroot/$bsd_indent_subdir",
+		"$srcroot/build/$bsd_indent_subdir",
+		glob("$srcroot/build*/$bsd_indent_subdir"))
+	{
+		if (-x $candidate)
+		{
+			$indent = $candidate;
+			last;
+		}
+	}
+}
+$indent ||= "pg_bsd_indent";
+
 # if it's the base of a postgres tree, we will exclude the files
 # postgres wants excluded
 if ($sourcedir)
-- 
2.53.0

