d00b508
#!/usr/bin/perl
d00b508
#
d00b508
# Update the local patches and RPM spec with patches from
d00b508
# an upstream tree with matching branch name.
d00b508
#
d00b508
# For example
d00b508
#
d00b508
#   - Checkout upstream GIT repo for libvirt-sandbox
d00b508
#   - Create a branch name matching current RHEL (eg rhel-6.4)
d00b508
#   - Populate the branch by cherry-picking patches from master
d00b508
#
d00b508
# This script will then
d00b508
#
d00b508
#   - Setup the upstream GIT repo as a remote named 'upstream'
d00b508
#   - Extract version number from RPM spec
d00b508
#   - Look for a tag 'v$VERSION' in upstream GIT
d00b508
#   - Run 'git format-patches v$VERSION..upstream/rhel-6.4'
d00b508
#   - Re-write the RPM spec to update all PatchNNN and %patchNNN lines
d00b508
#
d00b508
# The only manual step required is to fill in the changelog
d00b508
#
d00b508
d00b508
d00b508
use strict;
d00b508
use warnings;
d00b508
d00b508
my $gitupstream = "git://libvirt.org/libvirt-sandbox.git";
d00b508
my $rpmspec = "libvirt-sandbox.spec";
d00b508
d00b508
open SPEC, "$rpmspec" or die "cannot read $rpmspec: $!";
d00b508
my @spec = <SPEC>;
d00b508
close SPEC;
d00b508
d00b508
my $version;
d00b508
d00b508
foreach my $line (@spec) {
d00b508
    if ($line =~ /^Version:\s*(\S+)\s*$/) {
d00b508
	$version = $1;
d00b508
    }
d00b508
}
d00b508
d00b508
die "cannot find Version: line in RPM spec"
d00b508
    unless $version;
d00b508
d00b508
my $gittag = "v" . $version;
d00b508
my $gitbranch = $gittag . "-maint";
d00b508
d00b508
my $haveupstream;
d00b508
d00b508
open GIT, "-|", "git", "remote" or die "cannot run git remote: $!";
d00b508
while (<GIT>) {
d00b508
    if (/upstream/) {
d00b508
	$haveupstream = 1;
d00b508
    }
d00b508
}
d00b508
d00b508
close GIT;
d00b508
d00b508
unless ($haveupstream) {
d00b508
    `git remote add upstream $gitupstream`;
d00b508
}
d00b508
d00b508
`git fetch upstream`;
d00b508
d00b508
d00b508
$haveupstream = 0;
d00b508
d00b508
open GIT, "-|", "git", "branch", "-a" or die "cannot find git branch -a: $!";
d00b508
while (<GIT>) {
d00b508
    if (m,upstream/$gitbranch,) {
d00b508
	$haveupstream = 1;
d00b508
    }
d00b508
}
d00b508
close GIT;
d00b508
d00b508
die "cannot find upstream/$gitbranch" unless $haveupstream;
d00b508
d00b508
`git format-patch --no-signature -N $gittag..upstream/$gitbranch`;
d00b508
d00b508
opendir DH, "." or die "cannot read current directory: $!";
d00b508
d00b508
my @patches
d00b508
    = grep {
d00b508
	/^\d\d\d.*\.patch/
d00b508
    } readdir(DH);
d00b508
d00b508
closedir DH;
d00b508
d00b508
@patches = sort @patches;
d00b508
d00b508
open SPEC, ">$rpmspec" or die "cannot update $rpmspec: $!";
d00b508
d00b508
foreach my $line (@spec) {
d00b508
    print SPEC $line unless $line =~ /(Patch|%patch)/;
d00b508
d00b508
    my $i;
d00b508
    if ($line =~ /Source0/) {
d00b508
	for ($i = 0 ; $i <= $#patches ; $i++) {
d00b508
	    printf SPEC "Patch%d: %s\n", $i+1, $patches[$i];
d00b508
	}
d00b508
    } elsif ($line =~ /%setup/) {
d00b508
	for ($i = 0 ; $i <= $#patches ; $i++) {
d00b508
	    printf SPEC "%%patch%d -p1\n", $i+1;
d00b508
	}
d00b508
    }
d00b508
}
d00b508
d00b508
close SPEC or die "cannot save $rpmspec: $!";
d00b508
d00b508
`git add *.patch $rpmspec`;