Skip to content

Commit a1c4cd6

Browse files
committed
Allow multiple --excludes options in pgindent
This includes a unification of the logic used to find the excludes file and the typedefs file. Also, remove the dangerous and deprecated feature where the first non-option argument was taken as a typdefs file if it wasn't a .c or .h file, remove some extraneous blank lines, and improve the documentation somewhat.
1 parent 8f68580 commit a1c4cd6

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/tools/pgindent/pgindent

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ my $indent_opts =
2222
my $devnull = File::Spec->devnull;
2323

2424
my ($typedefs_file, $typedef_str, $code_base,
25-
$excludes, $indent, $build,
25+
@excludes, $indent, $build,
2626
$show_diff, $silent_diff, $help);
2727

2828
$help = 0;
@@ -32,7 +32,7 @@ my %options = (
3232
"typedefs=s" => \$typedefs_file,
3333
"list-of-typedefs=s" => \$typedef_str,
3434
"code-base=s" => \$code_base,
35-
"excludes=s" => \$excludes,
35+
"excludes=s" => \@excludes,
3636
"indent=s" => \$indent,
3737
"build" => \$build,
3838
"show-diff" => \$show_diff,
@@ -46,10 +46,8 @@ usage("Cannot have both --silent-diff and --show-diff")
4646

4747
run_build($code_base) if ($build);
4848

49-
# command line option wins, then first non-option arg,
50-
# then environment (which is how --build sets it) ,
49+
# command line option wins, then environment (which is how --build sets it) ,
5150
# then locations. based on current dir, then default location
52-
$typedefs_file ||= shift if @ARGV && $ARGV[0] !~ /\.[ch]$/;
5351
$typedefs_file ||= $ENV{PGTYPEDEFS};
5452

5553
# build mode sets PGINDENT
@@ -58,14 +56,15 @@ $indent ||= $ENV{PGINDENT} || $ENV{INDENT} || "pg_bsd_indent";
5856
# no non-option arguments given. so do everything in the current directory
5957
$code_base ||= '.' unless @ARGV;
6058

59+
my $sourcedir = locate_sourcedir();
60+
6161
# if it's the base of a postgres tree, we will exclude the files
6262
# postgres wants excluded
63-
$excludes ||= "$code_base/src/tools/pgindent/exclude_file_patterns"
64-
if $code_base && -f "$code_base/src/tools/pgindent/exclude_file_patterns";
65-
66-
# also look under the current directory for the exclude patterns file
67-
$excludes ||= "src/tools/pgindent/exclude_file_patterns"
68-
if -f "src/tools/pgindent/exclude_file_patterns";
63+
if ($sourcedir)
64+
{
65+
my $exclude_candidate = "$sourcedir/exclude_file_patterns";
66+
push (@excludes, $exclude_candidate) if -f $exclude_candidate;
67+
}
6968

7069
# The typedef list that's mechanically extracted by the buildfarm may omit
7170
# some names we want to treat like typedefs, e.g. "bool" (which is a macro
@@ -85,7 +84,6 @@ my %excluded = map { +"$_\n" => 1 } qw(
8584
my @files;
8685
my $filtered_typedefs_fh;
8786

88-
8987
sub check_indent
9088
{
9189
system("$indent -? < $devnull > $devnull 2>&1");
@@ -114,26 +112,34 @@ sub check_indent
114112
return;
115113
}
116114

115+
sub locate_sourcedir
116+
{
117+
# try fairly hard to locate the sourcedir
118+
my $where = $code_base || '.';
119+
my $sub = "$where/src/tools/pgindent";
120+
return $sub if -d $sub;
121+
# try to find it from an ancestor directory
122+
$sub = "../src/tools/pgindent";
123+
foreach (1..4)
124+
{
125+
return $sub if -d $sub;
126+
$sub = "../$sub";
127+
}
128+
return; # undef if nothing found
129+
}
117130

118131
sub load_typedefs
119132
{
120-
121133
# try fairly hard to find the typedefs file if it's not set
122134

123-
foreach my $try ('.', 'src/tools/pgindent', '/usr/local/etc')
135+
foreach my $try ('.', $sourcedir, '/usr/local/etc')
124136
{
125-
$typedefs_file ||= "$try/typedefs.list"
137+
last if $typedefs_file;
138+
next unless defined $try;
139+
$typedefs_file = "$try/typedefs.list"
126140
if (-f "$try/typedefs.list");
127141
}
128142

129-
# try to find typedefs by moving up directory levels
130-
my $tdtry = "..";
131-
foreach (1 .. 5)
132-
{
133-
$typedefs_file ||= "$tdtry/src/tools/pgindent/typedefs.list"
134-
if (-f "$tdtry/src/tools/pgindent/typedefs.list");
135-
$tdtry = "$tdtry/..";
136-
}
137143
die "cannot locate typedefs file \"$typedefs_file\"\n"
138144
unless $typedefs_file && -f $typedefs_file;
139145

@@ -166,13 +172,13 @@ sub load_typedefs
166172
return $filter_typedefs_fh;
167173
}
168174

169-
170175
sub process_exclude
171176
{
172-
if ($excludes && @files)
177+
foreach my $excl (@excludes)
173178
{
174-
open(my $eh, '<', $excludes)
175-
|| die "cannot open exclude file \"$excludes\"\n";
179+
last unless @files;
180+
open(my $eh, '<', $excl)
181+
|| die "cannot open exclude file \"$excl\"\n";
176182
while (my $line = <$eh>)
177183
{
178184
chomp $line;
@@ -185,7 +191,6 @@ sub process_exclude
185191
return;
186192
}
187193

188-
189194
sub read_source
190195
{
191196
my $source_filename = shift;
@@ -200,7 +205,6 @@ sub read_source
200205
return $source;
201206
}
202207

203-
204208
sub write_source
205209
{
206210
my $source = shift;
@@ -213,7 +217,6 @@ sub write_source
213217
return;
214218
}
215219

216-
217220
sub pre_indent
218221
{
219222
my $source = shift;
@@ -242,7 +245,6 @@ sub pre_indent
242245
return $source;
243246
}
244247

245-
246248
sub post_indent
247249
{
248250
my $source = shift;
@@ -270,7 +272,6 @@ sub post_indent
270272
return $source;
271273
}
272274

273-
274275
sub run_indent
275276
{
276277
my $source = shift;
@@ -313,7 +314,6 @@ sub show_diff
313314
return $diff;
314315
}
315316

316-
317317
sub run_build
318318
{
319319
eval "use LWP::Simple;"; ## no critic (ProhibitStringyEval);
@@ -359,7 +359,6 @@ sub run_build
359359
return;
360360
}
361361

362-
363362
sub build_clean
364363
{
365364
my $code_base = shift || '.';
@@ -397,6 +396,7 @@ Options:
397396
--build build the pg_bsd_indent program
398397
--show-diff show the changes that would be made
399398
--silent-diff exit with status 2 if any changes would be made
399+
The --excludes option can be given more than once.
400400
EOF
401401
if ($help)
402402
{
@@ -479,7 +479,6 @@ foreach my $source_filename (@files)
479479
write_source($source, $source_filename);
480480
}
481481
}
482-
483482
}
484483

485484
build_clean($code_base) if $build;

src/tools/pgindent/pgindent.man

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ the PostgreSQL project. It needs several things to run, and tries to locate
33
or build them if possible. They can also be specified via command line switches
44
or the environment.
55

6+
You can see all the options by running:
7+
8+
pgindent --help
9+
610
In its simplest form, if all the required objects are installed, simply run
711
it without any parameters at the top of the source tree you want to process.
812

@@ -30,18 +34,18 @@ you can specify it via the --code-base command line option.
3034
We don't want to indent certain files in the PostgreSQL source. pgindent
3135
will honor a file containing a list of patterns of files to avoid. This
3236
file can be specified using the --excludes command line option. If indenting
33-
a PostgreSQL source tree, this option isn't necessary, as it will find the file
34-
src/tools/pgindent/exclude_file_patterns.
37+
a PostgreSQL source tree, this option is usually not necessary, as it will
38+
find the file src/tools/pgindent/exclude_file_patterns. The --excludes option
39+
can be used more than once to specify multiple files containing exclusion
40+
patterns.
3541

3642
There are also two non-destructive modes of pgindent. If given the --show-diff
3743
option pgindent will show the changes it would make, but doesn't actually make
3844
them. If given instead the --silent-diff option, pgindent will exit with a
3945
status of 2 if it finds any indent changes are required, but will not
4046
make the changes or give any other information. This mode is intended for
41-
possible use in a git pre-commit hook.
47+
possible use in a git pre-commit hook. An example of its use in a git hook
48+
can be seen at https://wiki.postgresql.org/wiki/Working_with_Git#Using_git_hooks
4249

4350
Any non-option arguments are taken as the names of files to be indented. In this
44-
case only these files will be changed, and nothing else will be touched. If the
45-
first non-option argument is not a .c or .h file, it is treated as the name
46-
of a typedefs file for legacy reasons, but this use is deprecated - use the
47-
--typedefs option instead.
51+
case only these files will be changed, and nothing else will be touched.

0 commit comments

Comments
 (0)