Created
January 13, 2011 23:08
-
-
Save jasonmoo/778812 to your computer and use it in GitHub Desktop.
A perl script for hooking into a Mercurial hook event.
-In it's current form it will make a backup, convert indenting to tabs, and lint check php files.
-Allows for anonymous subs to be passed as adhoc filters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
### HG Hook script | |
### - allows filtering and other actions to be | |
### run at a designated hook. | |
### | |
# a copy of each file along with it's directory structure | |
# is placed here when the script is initially run. | |
$BACKUP_DIR = "~/_hg_modified"; | |
## initializing stuff | |
mkdir $BACKUP_DIR; | |
$CWD = `pwd`; | |
chomp $CWD; | |
@FILES = map { /^. (.+)$/; "$CWD/$1"; } split "\n", `hg st`; | |
exit unless @FILES; | |
## end initializing stuff | |
######################################################### | |
######################################################### | |
######################################################### | |
## User defined filters here | |
# first backup each file | |
filter(sub { | |
(undef,$file,$filename,$dir) = @_; | |
`mkdir -p $BACKUP_DIR$dir`; | |
`cp $file $BACKUP_DIR$dir/$filename`; | |
return; | |
}); | |
# process tabs and spaces | |
filter(sub { | |
return unless $_[1] =~ /\.(php|js|html|pl)$/i; | |
return map { s/(^|\G)( |\t)/\t/g; $_; } @{$_[0]}; | |
}); | |
# output the post processing diff | |
print `hg diff` || "<<NO DIFF>>\n"; | |
# lint check php files | |
filter(sub { | |
$file = $_[1]; | |
print `php -l $file` if $file =~ /\.php$/i; | |
return; | |
}); | |
######################################################### | |
######################################################### | |
######################################################### | |
## Final actions | |
print `\nread -p "Continue? [Enter|CTRL+C]"`; | |
## end Final actions | |
# filter handler | |
# -takes a single function | |
# --params available to function: ref @data, $file (full path), $filename, $dir | |
# -must return void to disable writing output | |
sub filter { | |
my $func = shift; | |
foreach my $file (@FILES) { | |
my ($filename) = ($file =~ m|([^/]+)$|); | |
my ($dir) = ($file =~ m|^(.+)/[^/]+$|); | |
# modify the file | |
open IN, $file or die "Cannot open for reading $file: $!"; | |
my @data = <IN>; | |
close IN; | |
my @newdata = &$func(\@data,$file,$filename,$dir); | |
if (@newdata) { | |
open OUT, ">$file" or die "Cannot open for writing $file: $!"; | |
print OUT @newdata; | |
close OUT; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment