#!/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 virt-viewer # - 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 lines # # The only manual step required is to fill in the changelog # use strict; use warnings; my $gitupstream = "git+ssh://git.engineering.redhat.com/srv/git/users/berrange/perl-Sys-Virt-rhel.git"; my $rpmspec = "perl-Sys-Virt.spec"; open SPEC, "$rpmspec" or die "cannot read $rpmspec: $!"; my @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; open GIT, "-|", "git", "branch" or die "cannot run git branch: $!"; my $gitbranch; while () { if (/^\*\s+(\S+)\s*$/) { $gitbranch = $1; } } close GIT; die "cannot find current git branch" unless $gitbranch; die "can only deal with rhel/stream branches" unless $gitbranch =~ /(rhel|stream)/; my $haveupstream; open GIT, "-|", "git", "remote" or die "cannot run git remote: $!"; while () { 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 () { 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; shift @patches if @patches && $patches[0] =~ /0000-/; open SPEC, ">$rpmspec" or die "cannot update $rpmspec: $!"; foreach my $line (@spec) { print SPEC $line unless $line =~ /Patch/ && $line !~ /Patch0:/; my $i; if ($line =~ /Patch0/) { for ($i = 0 ; $i <= $#patches ; $i++) { printf SPEC "Patch%d: %s\n", $i+1, $patches[$i]; } } } close SPEC or die "cannot save $rpmspec: $!"; if (@patches) { `git add *.patch $rpmspec`; }