On 2023-02-07 Tu 11:32, Jelte Fennema wrote:
On Tue, 7 Feb 2023 at 17:11, Robert Haas<robertmh...@gmail.com>  wrote:
I don't know if that works or not, but it does seem plausible, at
least. My idea would have been to use the --name-status option, which
works for both git diff and git show. You just look and see which
lines in the output start with M or A and then take the file names
from those lines.
If you add `--diff-filter=ACMR`, then git diff/show will only show
Added, Copied, Modified, and Renamed files.

The pre-commit hook that Andrew added to the wiki uses that in
combination with --name-only to get the list of files that you want to
check on commit:
https://wiki.postgresql.org/wiki/Working_with_Git#Using_git_hooks


OK, here's a patch based on Robert's and Jelte's ideas.


cheers


andrew

--
Andrew Dunstan
EDB:https://www.enterprisedb.com
diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index 07970f758c..c9f42302c9 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -24,13 +24,15 @@ my $devnull = File::Spec->devnull;
 my ($typedefs_file, $typedef_str, $code_base,
 	@excludes,      $indent,      $build,
 	$show_diff,     $silent_diff, $help,
-	@commits,);
+	@commits, $dirty, $cached);
 
 $help = 0;
 
 my %options = (
 	"help"               => \$help,
 	"commit=s"           => \@commits,
+	"dirty"              => \$dirty,
+	"cached"             => \$cached,
 	"typedefs=s"         => \$typedefs_file,
 	"list-of-typedefs=s" => \$typedef_str,
 	"code-base=s"        => \$code_base,
@@ -46,8 +48,11 @@ usage() if $help;
 usage("Cannot have both --silent-diff and --show-diff")
   if $silent_diff && $show_diff;
 
-usage("Cannot use --commit with --code-base or command line file list")
-  if (@commits && ($code_base || @ARGV));
+usage("Cannot use --commit, --dirty or --cached with --code-base or command line file list")
+  if (($dirty || $cached || @commits) && ($code_base || @ARGV));
+
+usage( "Cannot use --dirty or --cached with --commit")
+  if (($dirty || $cached) && @commits);
 
 run_build($code_base) if ($build);
 
@@ -395,6 +400,8 @@ pgindent [OPTION]... [FILE]...
 Options:
 	--help                  show this message and quit
 	--commit=gitref         use files modified by the named commit
+	--dirty                 use modified files under current directory
+	--cached                use staged files under current directory
 	--typedefs=FILE         file containing a list of typedefs
 	--list-of-typedefs=STR  string containing typedefs, space separated
 	--code-base=DIR         path to the base of PostgreSQL source code
@@ -450,6 +457,19 @@ foreach my $commit (@commits)
 	push(@files,@affected);
 }
 
+# look for dirty / cached files under the CWD
+if ($dirty || $cached)
+{
+	my $gitcmd = "git diff --diff-filter=ACMR --name-only";
+	$gitcmd .= " --cached" if ($cached && !$dirty);
+	$gitcmd .= " HEAD" if ($dirty && $cached);
+	$gitcmd .= " -- .";
+	my @affected=`$gitcmd`;
+	die "git error" if $?;
+	chomp(@affected);
+	push(@files,@affected);
+}
+
 # remove excluded files from the file list
 process_exclude();
 

Reply via email to