Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b90f0b5

Browse filesBrowse files
committed
Add non-destructive modes to pgindent
This adds two modes of running pgindent, neither of which results in any changes being made to the source code. The --show-diff option shows what changes would have been made, and the --silent-diff option just exits with a status of 2 if any changes would be made. The second of these is intended for scripting use in places such as git hooks. Along the way some code cleanup is done, and a --help option is also added. Reviewed by Tom Lane Discussion: https://postgr.es/m/c9c9fa6d-6de6-48c2-4f8b-0fbeef026439@dunslane.net
1 parent 0aa38db commit b90f0b5
Copy full SHA for b90f0b5

File tree

Expand file treeCollapse file tree

2 files changed

+79
-26
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+79
-26
lines changed

‎src/tools/pgindent/pgindent

Copy file name to clipboardExpand all lines: src/tools/pgindent/pgindent
+72-26Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,28 @@ my $indent_opts =
2121

2222
my $devnull = File::Spec->devnull;
2323

24-
my ($typedefs_file, $typedef_str, $code_base, $excludes, $indent, $build);
24+
my ($typedefs_file, $typedef_str, $code_base,
25+
$excludes, $indent, $build,
26+
$show_diff, $silent_diff, $help);
27+
28+
$help = 0;
2529

2630
my %options = (
31+
"help" => \$help,
2732
"typedefs=s" => \$typedefs_file,
2833
"list-of-typedefs=s" => \$typedef_str,
2934
"code-base=s" => \$code_base,
3035
"excludes=s" => \$excludes,
3136
"indent=s" => \$indent,
32-
"build" => \$build,);
33-
GetOptions(%options) || die "bad command line argument\n";
37+
"build" => \$build,
38+
"show-diff" => \$show_diff,
39+
"silent-diff" => \$silent_diff,);
40+
GetOptions(%options) || usage("bad command line argument");
41+
42+
usage() if $help;
43+
44+
usage("Cannot have both --silent-diff and --show-diff")
45+
if $silent_diff && $show_diff;
3446

3547
run_build($code_base) if ($build);
3648

@@ -229,8 +241,7 @@ sub pre_indent
229241

230242
sub post_indent
231243
{
232-
my $source = shift;
233-
my $source_filename = shift;
244+
my $source = shift;
234245

235246
# Restore CATALOG lines
236247
$source =~ s!^/\*(CATALOG\(.*)\*/$!$1!gm;
@@ -280,33 +291,21 @@ sub run_indent
280291
close($src_out);
281292

282293
return $source;
283-
284294
}
285295

286-
287-
# for development diagnostics
288-
sub diff
296+
sub show_diff
289297
{
290-
my $pre = shift;
291-
my $post = shift;
292-
my $flags = shift || "";
293-
294-
print STDERR "running diff\n";
298+
my $indented = shift;
299+
my $source_filename = shift;
295300

296-
my $pre_fh = new File::Temp(TEMPLATE => "pgdiffbXXXXX");
297-
my $post_fh = new File::Temp(TEMPLATE => "pgdiffaXXXXX");
301+
my $post_fh = new File::Temp(TEMPLATE => "pgdiffXXXXX");
298302

299-
print $pre_fh $pre;
300-
print $post_fh $post;
303+
print $post_fh $indented;
301304

302-
$pre_fh->close();
303305
$post_fh->close();
304306

305-
system( "diff $flags "
306-
. $pre_fh->filename . " "
307-
. $post_fh->filename
308-
. " >&2");
309-
return;
307+
my $diff = `diff -upd $source_filename $post_fh->filename 2>&1`;
308+
return $diff;
310309
}
311310

312311

@@ -377,6 +376,34 @@ sub build_clean
377376
return;
378377
}
379378

379+
sub usage
380+
{
381+
my $message = shift;
382+
my $helptext = <<'EOF';
383+
Usage:
384+
pgindent [OPTION]... [FILE]...
385+
Options:
386+
--help show this message and quit
387+
--typedefs=FILE file containing a list of typedefs
388+
--list-of-typedefs=STR string containing typedefs, space separated
389+
--code-base=DIR path to the base of PostgreSQL source code
390+
--excludes=PATH file containing list of filename patterns to ignore
391+
--indent=PATH path to pg_bsd_indent program
392+
--build build the pg_bsd_indent program
393+
--show-diff show the changes that would be made
394+
--silent-diff exit with status 2 if any changes would be made
395+
EOF
396+
if ($help)
397+
{
398+
print $helptext;
399+
exit 0;
400+
}
401+
else
402+
{
403+
print STDERR "$message\n", $helptext;
404+
exit 1;
405+
}
406+
}
380407

381408
# main
382409

@@ -404,6 +431,8 @@ push(@files, @ARGV);
404431

405432
foreach my $source_filename (@files)
406433
{
434+
# ignore anything that's not a .c or .h file
435+
next unless $source_filename =~ /\.[ch]$/;
407436

408437
# Automatically ignore .c and .h files that correspond to a .y or .l
409438
# file. indent tends to get badly confused by Bison/flex output,
@@ -427,9 +456,26 @@ foreach my $source_filename (@files)
427456
next;
428457
}
429458

430-
$source = post_indent($source, $source_filename);
459+
$source = post_indent($source);
460+
461+
if ($source ne $orig_source)
462+
{
463+
if ($silent_diff)
464+
{
465+
exit 2;
466+
}
467+
elsif ($show_diff)
468+
{
469+
print show_diff($source, $source_filename);
470+
}
471+
else
472+
{
473+
write_source($source, $source_filename);
474+
}
475+
}
431476

432-
write_source($source, $source_filename) if $source ne $orig_source;
433477
}
434478

435479
build_clean($code_base) if $build;
480+
481+
exit 0;

‎src/tools/pgindent/pgindent.man

Copy file name to clipboardExpand all lines: src/tools/pgindent/pgindent.man
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ file can be specified using the --excludes command line option. If indenting
3333
a PostgreSQL source tree, this option isn't necessary, as it will find the file
3434
src/tools/pgindent/exclude_file_patterns.
3535

36+
There are also two non-destructive modes of pgindent. If given the --show-diff
37+
option pgindent will show the changes it would make, but doesn't actually make
38+
them. If given instead the --silent-diff option, pgindent will exit with a
39+
status of 2 if it finds any indent changes are required, but will not
40+
make the changes or give any other information. This mode is intended for
41+
possible use in a git pre-commit hook.
42+
3643
Any non-option arguments are taken as the names of files to be indented. In this
3744
case only these files will be changed, and nothing else will be touched. If the
3845
first non-option argument is not a .c or .h file, it is treated as the name

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.