-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcitus_indent
executable file
·133 lines (91 loc) · 3.44 KB
/
citus_indent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl -wT
use strict;
use warnings;
use Getopt::Long qw(:config no_auto_abbrev no_ignore_case prefix=--);
use POSIX qw(setlocale LC_ALL);
# untaint environment
local $ENV{'PATH'} = '/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
my ($quiet, $check, $diff);
exit 64 unless GetOptions ('quiet' => \$quiet, 'check' => \$check, 'diff' => \$diff);
my $null_delimited_triples = ($diff ?
`git diff --cached --name-only --diff-filter=ACMR -z | git check-attr --stdin -z citus-style` :
`git ls-files -z | git check-attr --stdin -z citus-style`
);
my @flattened_triples = split(/\x00/, $null_delimited_triples);
my $exit_code = $? >> 8;
die "could not list files under source control\n" if $exit_code;
# exit if there are no files to check
print "no files to check" if !@flattened_triples && !$quiet;
exit if !@flattened_triples;
my @files_to_format = ();
my @formatter_args = qw(-c /usr/local/etc/citustools/citus-style.cfg);
push @formatter_args, '-q' if $quiet;
if ($check)
{
push @formatter_args, '--check';
}
else
{
push @formatter_args, '--no-backup', '--replace';
}
while(my ($filename, $attrname, $attrvalue) = splice(@flattened_triples, 0, 3)) {
next unless $attrvalue eq 'set';
# untaint filename and error if '..' is present
if ($filename =~ /^(.*(?!\.\.)$)/) {
push @files_to_format, $1;
} else {
die "illegal filename\n";
}
}
push @formatter_args, @files_to_format;
system('uncrustify', @formatter_args);
if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
$exit_code = $? >> 8;
die "formatting check failed. see output for details.\n" if ($check && $exit_code);
}
__END__
=head1 NAME
citus_indent - apply Citus formatting conventions to a C codebase
=head1 SYNOPSIS
B<citus_indent> [I<options>]
=head1 DESCRIPTION
Applies (or enforces) the C formatting conventions used within Citus Data to
all C files within the working directory. To be subject to formatting rules,
files must be under L<git(1)> source control and have the I<citus-style> file
attribute applied (see L<gitattributes(5)>).
By default, all files with this attribute beneath the working directory will
be formatted in-place (combined with source control, this in-place formatting
is generally safe). A bundled L<uncrustify(1)> configuration file specifies all
formatting options.
=head1 OPTIONS
=over 4
=item B<--quiet>
Quiet Mode: do not show progress as files are being processed.
=item B<--check>
Do not modify any files, instead simply verify that all files eligible for
formatting are compliant with Citus Data style. The exit code I<EXIT_SUCCESS>
signals that a codebase is compliant, otherwise I<EXIT_FAILURE> is used.
=item B<--diff>
Only check files that are staged for commit. This is primarily useful when used
in a pre-commit hook as it is generally faster to only check the changed files.
=back
=head1 FILES
=over 4
=item C</usr/local/etc/citustools/citus-style.cfg>
This configuration file defines Citus Data's C style and will be passed to
L<uncrustify(1)> to apply (or enforce) coding style.
=back
=head1 TODO
It might be nice to be able to invoke our style on arbitrary files.
=head1 SEE ALSO
L<git(1)>, L<gitattributes(5)>, L<uncrustify(1)>
=head1 AUTHOR
Jason Petersen L<E<lt>[email protected]<gt>>