Path: blob/main/Tools/scripts/splitpatch.pl
16123 views
#! /usr/bin/env perl1# ----------------------------------------------------------------------------2# "THE BEER-WARE LICENSE" (Revision 42)3# <[email protected]> wrote this file. As long as you retain this notice you4# can do whatever you want with this stuff. If we meet some day, and you think5# this stuff is worth it, you can buy me a beer in return. Anton Berezin6# ----------------------------------------------------------------------------78use strict;9use warnings;1011# good tests:12# /usr/ports/archivers/zoo/files/patch-aa (context diff)13# /usr/ports/astro/xplanet/files/patch-aa (unified with paths)1415my ($in,$fl,$abort,$state,$out);1617if (!@ARGV || $ARGV[0] =~ /^-/) {18print STDERR "Usage:19$0 patchfile ...20"21}2223while (@ARGV) {24$in = shift;25$state = \&nofile;26if (open IN, "< $in") {27$abort = 0;28$out = "";29$fl = "";30while (<IN>) {31$state->();32last if $abort;33}34close IN;35if ($out && !$abort) {36print "Wrote $out\n";37}38} else {39print STDERR "cannot open $in: $!\n";40}41}4243sub nofile44{45if (/^\*\*\*\s+/ && !/^\*\*\*\s+\d+,\d+\s+/) {46$state = \&cstart;47$fl = $_;48} elsif (/^---\s+/ && !/^---\s+\d+,\d+\s+/) {49$state = \&ustart;50$fl = $_;51}52}5354sub cstart55{56if (!/^---\s+\d+,\d+\s+/ && /^---\s+(\S+)\s+/) {57$state = \&body;58$out = $1;59$out =~ s|/|__|g;60$out = "patch-$out";61if (open OUT, "> $out") {62print OUT $fl;63print OUT $_;64} else {65print STDERR "Cannot create $out: $!, aborting\n";66$abort = 1;67}68} else {69print STDERR "Bad context diff in $in, aborting\n";70$abort = 1;71}72}7374sub ustart75{76if (/^\+\+\+\s+(\S+)\s+/) {77$state = \&body;78$out = $1;79$out =~ s|/|__|g;80$out = "patch-$out";81if (open OUT, "> $out") {82print OUT $fl;83print OUT $_;84} else {85print STDERR "Cannot create $out: $!, aborting\n";86$abort = 1;87}88} else {89print STDERR "Bad unified diff in $in, aborting\n";90$abort = 1;91}92}9394sub body95{96if (/^\*\*\*\s+/ && !/^\*\*\*\s+\d+,\d+\s+/) {97close OUT;98print "Wrote $out\n";99$state = \&cstart;100$fl = $_;101} elsif (/^---\s+/ && !/^---\s+\d+,\d+\s+/) {102close OUT;103print "Wrote $out\n";104$state = \&ustart;105$fl = $_;106} else {107print OUT $_;108}109}110111112