Skip to content

Commit

Permalink
Update perl script to remove add/deleted file perms
Browse files Browse the repository at this point in the history
Also cleanup modified file perms
  • Loading branch information
Scott Baker committed Mar 3, 2016
1 parent 1113a59 commit e10a860
Showing 1 changed file with 73 additions and 21 deletions.
94 changes: 73 additions & 21 deletions libs/header_clean/header_clean.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,93 @@
use strict;
use warnings;

my $remove_file_add_header = 1;
my $remove_file_delete_header = 1;
my $clean_permission_changes = 1;

#################################################################################

my $ansi_sequence_regex = qr/(\e\[([0-9]{1,3}(;[0-9]{1,3}){0,3})[mK])?/;

my ($f1,$f2);
while (my $l = <>) {
# Find the first file: --- a/README.md
if ($l =~ /^$ansi_sequence_regex--- (a\/)?(.+?)(\e|$)/) {
$f1 = $5;
# Find the second file: +++ b/README.md
} elsif ($l =~ /^$ansi_sequence_regex\+\+\+ (b\/)?(.+?)(\e|$)/) {
my ($file_1,$file_2,$last_file_seen);
my @input = <>;
strip_empty_first_line(\@input);

for (my $i = 0; $i <= $#input; $i++) {
my $line = $input[$i];

#print "RAW: $line";

#########################
# Look for the filename #
#########################
if ($line =~ /^${ansi_sequence_regex}diff --git a\/(.+?) b\/(.+)/) {
$last_file_seen = $4;
########################################
# Find the first file: --- a/README.md #
########################################
} elsif ($line =~ /^$ansi_sequence_regex--- (a\/)?(.+?)(\e|$)/) {
$file_1 = $5;

# Find the second file on the next line: +++ b/README.md
my $next = $input[++$i];
$next =~ /^$ansi_sequence_regex\+\+\+ (b\/)?(.+?)(\e|$)/;
if ($1) {
print $1; # Print out whatever color we're using
}
$f2 = $5;
$file_2 = $5;

# If they're the same it's a modify
if ($f1 eq $f2) {
print "modified: $f1\n";
if ($file_1 eq $file_2) {
print "modified: $file_1\n";
# If the first is /dev/null it's a new file
} elsif ($f1 eq "/dev/null") {
print "added: $f2\n";
} elsif ($file_1 eq "/dev/null") {
print "added: $file_2\n";
# If the second is /dev/null it's a deletion
} elsif ($f2 eq "/dev/null") {
print "deleted: $f1\n";
} elsif ($file_2 eq "/dev/null") {
print "deleted: $file_1\n";
# If the files aren't the same it's a rename
} elsif ($f1 ne $f2) {
print "renamed: $f1 to $f2\n";
} elsif ($file_1 ne $file_2) {
print "renamed: $file_1 to $file_2\n";
# Something we haven't thought of yet
} else {
print "$f1 -> $f2\n";
print "$file_1 -> $file_2\n";
}
###################################
# Remove any new file permissions #
###################################
} elsif ($remove_file_add_header && $line =~ /^${ansi_sequence_regex}new file mode/) {
# Don't print the line (i.e. remove it from the output);
######################################
# Remove any delete file permissions #
######################################
} elsif ($remove_file_delete_header && $line =~ /^${ansi_sequence_regex}deleted file mode/) {
# Don't print the line (i.e. remove it from the output);
#####################################################
# Check if we're changing the permissions of a file #
#####################################################
} elsif ($clean_permission_changes && $line =~ /^${ansi_sequence_regex}old mode (\d+)/) {
my $next = $input[++$i];

if ($1) {
print $1; # Print out whatever color we're using
}

# Reset the vars
my ($f1,$f2);
# Just a regular line, print it out
my ($new_mode) = $next =~ m/new mode (\d+)/;
print "$last_file_seen changed file mode to $new_mode\n";
#####################################
# Just a regular line, print it out #
#####################################
} else {
print $l;
print $line;
}
}

sub strip_empty_first_line {
my $foo = shift(); # Array passed in by reference

# If the first line is just whitespace remove it
if ($foo->[0] =~ /^\s*$/) {
shift($foo);
}
}

0 comments on commit e10a860

Please sign in to comment.