Blob Blame History Raw
#!/usr/bin/perl
#
# Update the local patches and RPM spec with patches from
# an upstream tree with matching branch name.
#
# For example
#
#   - Checkout upstream GIT repo for libvirt-sandbox
#   - Create a branch name matching current RHEL (eg rhel-6.4)
#   - Populate the branch by cherry-picking patches from master
#
# This script will then
#
#   - Setup the upstream GIT repo as a remote named 'upstream'
#   - Extract version number from RPM spec
#   - Look for a tag 'v$VERSION' in upstream GIT
#   - Run 'git format-patches v$VERSION..upstream/rhel-6.4'
#   - Re-write the RPM spec to update all PatchNNN and %patchNNN lines
#
# The only manual step required is to fill in the changelog
#


use strict;
use warnings;

my $gitupstream = "git://libvirt.org/libvirt-sandbox.git";
my $rpmspec = "libvirt-sandbox.spec";

open SPEC, "$rpmspec" or die "cannot read $rpmspec: $!";
my @spec = <SPEC>;
close SPEC;

my $version;

foreach my $line (@spec) {
    if ($line =~ /^Version:\s*(\S+)\s*$/) {
	$version = $1;
    }
}

die "cannot find Version: line in RPM spec"
    unless $version;

my $gittag = "v" . $version;
my $gitbranch = $gittag . "-maint";

my $haveupstream;

open GIT, "-|", "git", "remote" or die "cannot run git remote: $!";
while (<GIT>) {
    if (/upstream/) {
	$haveupstream = 1;
    }
}

close GIT;

unless ($haveupstream) {
    `git remote add upstream $gitupstream`;
}

`git fetch upstream`;


$haveupstream = 0;

open GIT, "-|", "git", "branch", "-a" or die "cannot find git branch -a: $!";
while (<GIT>) {
    if (m,upstream/$gitbranch,) {
	$haveupstream = 1;
    }
}
close GIT;

die "cannot find upstream/$gitbranch" unless $haveupstream;

`git format-patch --no-signature -N $gittag..upstream/$gitbranch`;

opendir DH, "." or die "cannot read current directory: $!";

my @patches
    = grep {
	/^\d\d\d.*\.patch/
    } readdir(DH);

closedir DH;

@patches = sort @patches;

open SPEC, ">$rpmspec" or die "cannot update $rpmspec: $!";

foreach my $line (@spec) {
    print SPEC $line unless $line =~ /(Patch|%patch)/;

    my $i;
    if ($line =~ /Source0/) {
	for ($i = 0 ; $i <= $#patches ; $i++) {
	    printf SPEC "Patch%d: %s\n", $i+1, $patches[$i];
	}
    } elsif ($line =~ /%setup/) {
	for ($i = 0 ; $i <= $#patches ; $i++) {
	    printf SPEC "%%patch%d -p1\n", $i+1;
	}
    }
}

close SPEC or die "cannot save $rpmspec: $!";

`git add *.patch $rpmspec`;