]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Meta/indent-cpp-directive: C preprocessor directive indentation rules
authorJunio C Hamano <gitster@pobox.com>
Tue, 15 Jul 2025 21:19:20 +0000 (14:19 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 15 Jul 2025 21:19:20 +0000 (14:19 -0700)
indent-cpp-directive.perl [new file with mode: 0755]

diff --git a/indent-cpp-directive.perl b/indent-cpp-directive.perl
new file mode 100755 (executable)
index 0000000..d87180f
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $indent_level = -1;
+
+sub emit {
+       my $indent = $indent_level <= 0 ? "" : " " x $indent_level;
+       printf "#%s%s", $indent, $_;
+}
+
+while (<>) {
+       unless (s/^\s*#\s*//) {
+               print;
+               next;
+       }
+
+       if (/^if/) {
+               emit($_);
+               $indent_level++;
+       } elsif (/^el/) {
+               $indent_level--;
+               emit($_);
+               $indent_level++;
+       } elsif (/^endif/) {
+               $indent_level--;
+               emit($_);
+       } else {
+               emit($_);
+       }
+}