Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 978fe6f

Browse files
JelteFCommitfest Bot
authored andcommitted
pgindent: Use git ls-files to discover files
When running pgindent on the whole source tree, it would often also indent build artifacts. This changes the pgindent directory search logic to become git-aware, and only indent files that are actually tracked by git. Any files that are explicitly part of the pgindent its arguments, are always indented though, even if they are not tracked by git. This can be useful to force indentation of an untracked file and/or for editor integration.
1 parent 9d8cdcb commit 978fe6f

1 file changed

Lines changed: 44 additions & 10 deletions

File tree

src/tools/pgindent/pgindent

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use strict;
1313
use warnings FATAL => 'all';
1414

1515
use Cwd qw(abs_path getcwd);
16-
use File::Find;
1716
use File::Spec;
1817
use File::Temp;
1918
use IO::Handle;
@@ -383,6 +382,49 @@ sub diff
383382
return $diff;
384383
}
385384

385+
sub discover_files
386+
{
387+
my @paths = @_;
388+
my @discovered;
389+
390+
# Separate individual files from directories
391+
my @dirs;
392+
foreach my $path (@paths)
393+
{
394+
if (-f $path)
395+
{
396+
push(@discovered, $path);
397+
}
398+
elsif (-d $path)
399+
{
400+
push(@dirs, $path);
401+
}
402+
else
403+
{
404+
warn "Could not find $path";
405+
}
406+
}
407+
408+
# Use git ls-files for directories to avoid searching build trees etc.
409+
if (@dirs)
410+
{
411+
# Use -z to handle files with newlines in their names, and split on \0
412+
# afterward.
413+
open(my $git_ls, '-|', 'git', 'ls-files', '-z', '--', @dirs)
414+
|| die "could not open git ls-files: $!";
415+
binmode($git_ls);
416+
# Slurp all output into a single string by temporarily unsetting
417+
# the line separator.
418+
my $git_output = do { local $/; <$git_ls> };
419+
close($git_ls);
420+
die "git ls-files error" if $?;
421+
my @git_files = split(/\0/, $git_output);
422+
push(@discovered, grep { /\.[ch]$/ } @git_files);
423+
}
424+
425+
return @discovered;
426+
}
427+
386428
sub usage
387429
{
388430
my $message = shift;
@@ -418,16 +460,8 @@ $filtered_typedefs_fh = load_typedefs();
418460

419461
check_indent();
420462

421-
my $wanted = sub {
422-
my ($dev, $ino, $mode, $nlink, $uid, $gid);
423-
(($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))
424-
&& -f _
425-
&& /^.*\.[ch]\z/s
426-
&& push(@files, $File::Find::name);
427-
};
428-
429463
# any non-option arguments are files or directories to be processed
430-
File::Find::find({ wanted => $wanted }, @ARGV) if @ARGV;
464+
push(@files, discover_files(@ARGV)) if @ARGV;
431465

432466
# commit file locations are relative to the source root
433467
chdir "$sourcedir/../../.." if @commits && $sourcedir;

0 commit comments

Comments
 (0)