Skip to content

Instantly share code, notes, and snippets.

@hikiko hikiko/check-mesa-diff.pl
Last active Mar 19, 2018

Embed
What would you like to do?
A script that checks if a mesa patch indentation uses 3 spaces and no tabs. Run it with: ./check-mesa-diff.pl <patch-filename>
#!/usr/bin/perl
use strict;
use warnings;
use Term::ANSIColor;
local $Term::ANSIColor::AUTOLOCAL = 1;
my $fname = $ARGV[0];
if(not defined $fname) {
die "You must set the patch filename.\n";
}
my $full_line;
my $lnum = 0;
my $llen = 0;
my $counter = 0;
open(my $fh, '<', $fname) or die "Couldn't open file $fname.\n";
while(!eof($fh)) {
defined(my $line = readline $fh) or die "Failed to read line.\n";
$lnum++;
# inserted lines (diff)
$full_line = $line;
my $line_printed = 0;
if($line =~ m/^\+/) {
# remove the insert (+) character at the beginning
substr($line, 0, 1) = "";
$llen = length($line);
# if the first characters are tabs die with message
if($line =~ m/(^(\t+))/) {
close($fh);
goto TAB_DEATH;
}
# if the line begins with a space count spaces
if($line =~ /^( +).*$/) {
my $spaces = $1;
my $num_spaces = length($spaces);
if(($num_spaces % 3) != 0) {
my $next_char = substr($line, $num_spaces, 1);
if(!($next_char eq '*')) {
my $string = qq[$lnum:\t$full_line];
print colored($string, 'yellow on_black');
print color('reset');
$line_printed++;
}
$counter++;
}
if($num_spaces == ($llen - 1)) {
close($fh);
die "Diff line $lnum contains only spaces:\n$full_line";
}
my $next_char = substr($line, $num_spaces, 1);
if($next_char eq "\t") {
close($fh);
goto TAB_DEATH;
}
}
}
if(!$line_printed) {
print color('reset');
print "$lnum:\t$full_line";
}
}
close($fh);
if($counter) {
print "\nSome lines start with a number of spaces that is not a ";
print "multiple of 3.\nPlease check your indentation.\n\n";
}
exit(0);
TAB_DEATH:
die "In line: $lnum you use tab instead of space:\n$full_line\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.