2023 09 05 19 02 [git] 去除多餘空白


以下是去除多餘空白的perl程式:

#!/usr/bin/perl

for ($i=0;$i<=$#ARGV;$i++) {
    open($file,$ARGV[$i]);
    open($fout,"> $ARGV[$i].new");
    while ($line=<$file>) {
        if ($line =~ /^(.*)\s+(\r?\n)$/) {
            print $fout $1,$2;
        } else {
            print $fout $line;
        }
    }
    close($fout);
    close($file);
    unlink("$ARGV[$i].backup");
    rename($ARGV[$i],"$ARGV[$i].backup");
    rename("$ARGV[$i].new",$ARGV[$i]);
}

這個則是列出git repo中有哪些行尾有空白字元的檔案名稱:

#!/usr/bin/perl

$git_dir = `git rev-parse --show-toplevel`;
chomp($git_dir);
open($fin,"git ls-tree --full-tree --name-only -r HEAD |");

while (chomp($filename=<$fin>))
{
#    print "x $git_dir/$filename\n";
    open($file,"$git_dir/$filename");
    $found = 0;
    $lineno = 0;
    while (chomp($line=<$file>)) {
        $lineno++;
        if ($line =~ /\s+$/) {
#            print "$lineno: \"$line\"\n";
            $found = 1;
            last;
        }
    }
    if ($found) {
        print "$git_dir/$filename:$lineno\n";
    }
}