Rafael Sanches

May 10, 2008

simple script to merge commits from a bugzilla id

Filed under: maintainability, programming — Tags: , , , , — mufumbo @ 9:15 pm

Today i have made my first PERL script!

For me it is very painful when it arrives the time to merge, into another branch, all the commits that i have done in the “trunk”. I have searched a little and did not find anything that could magically solve all my problems. I know that it’s better to create a separated branch when there are lot’s of commits, but there are some cases that a super-simple functionality can explode into a big ball of mud.

Practically the script merge all the commits of a bugzilla id to another branch. If someone knows a standard way to do this; please tell me!

The script take three inputs:

  1. The starting revision ID to filter the search.
  2. The SVN address of the source.
  3. The search string to filter the results. Here you put your bugzilla bug id.

Commands that are executed when you launch the script:

  1. Go to the directory of the destination branch.
  2. To execute the script simply do:
  3. svn_search_merge.pl 0 https://svn.example.com/main/trunk/ “1: “
  4. Note that “1: ” is the bugzilla bug id. What happens next is:
  5. svn log -r 1:HEAD https://svn.example.com/main/trunk/
  6. With that command we get the log of all commits from the revision 1 to the HEAD. After it’s just matter of check if the string “1: ” is inside the log. Then we simply execute:
  7. svn merge -r (ACTUAL_REVISION-1):ACTUAL_REVISION https://svn.example.com/main/trunk/

Source code of the script:

#!/usr/bin/perl

# Simple script to merge commits from a source branch to the current destination directory.
# https://mufumbo.wordpress.com/2008/05/10/simple-script-to-merge-commits-from-a-bugzilla-id/
#
# Example:
# $ cd my-branch-destination/
# $ svn_search_merge.pl 3000 https://svn.example.com/main/trunk/ "bug 673"
# Where 3000 is the starting revision and "bug 673" is the string to match in the comments.
#
use strict;
use warnings;

my $prev_revision = shift;
my $svnHost = shift;
my $searchStr = shift;

print "Starting Revision: $prev_revision\n";
print "SVN addr: $svnHost\n";
print "Search pattern: $searchStr\n";

my $buffer;
$buffer = `svn log -r $prev_revision:HEAD $svnHost`;
my $shouldContinue = "y";
LOGS: foreach my $changelog_entry (split(/----+/m, $buffer)) {
    if($changelog_entry =~ m/($searchStr)/) {
            #my (undef, $info, undef, $comment) = split(/\n/, $changelog_entry);
            #next unless $info =~ m/^r/;

        print "\n--------------------------------------------------";
        print $changelog_entry;
        my $revisionId = substr($changelog_entry, 2, 5);
        $revisionId =~ s/^\s+//;
        $revisionId =~ s/\s+$//;

        if ($shouldContinue ne 'a') {
            PROMPT: while(1) {
                print "\nShould continue with merge of revision '$revisionId'? (Yes,Always,Skip,Exit): ";
                $shouldContinue = <>;
                chomp($shouldContinue);

                last PROMPT if $shouldContinue eq 'y';
                last PROMPT if $shouldContinue eq 'a';
                next LOGS if $shouldContinue eq 's';
                die("User requested to stop.") if $shouldContinue eq 'e';
            }
        }
        else {
            print "\nAuto merging '$revisionId'\n";
        }

        my $pRevisionId = $revisionId-1;
        my $mergeBuffer = `svn merge -r $pRevisionId:$revisionId $svnHost`;
        print $mergeBuffer;
    }
}
Advertisements

2 Comments »

  1. Hi Rafael,
    That is a very great piece of code . I am knew to Perl and I got this requirement :

    Step 1: Developers will checkin the files in TRUNK folder. They will be use proper comments ( 1234:This is the test file). In the comments , the characters before : will be JIRA ID. Once they done with the development they will inform BUILD team to promote the file to QA with the details of JIRA(in this case its 1234).
    Step 2: Build team will run the script which will copy the files from Trunk to QA( only the files which are applicable for 1234).

    It seems your code is merging at the same branch not from the trunk to the branch .

    Please , let me know if you have any suggestions.

    Thanks,

    Comment by Aladdin — July 1, 2009 @ 10:40 pm

    • Hi Aladdin,

      At the time, I had a very similar requirement as you have!

      We were making commits to the trunk, and when the feature was properly tested in the development (trunk based), I was needing to merge the changes to the production branch.

      This script works with trunk -> production-branch or any other “branch -> trunk” or, in another words, any-branch -> any-branch

      In your example you just need to:
      1 – download the branch that you want in some directory
      2 – merge the changes of the trunk into the branch directory executing: svn_search_merge.pl 0 https://svn.example.com/main/trunk/ “1234:“
      3 – commit the changes that you have in your branch directory

      That’s what you need? let me know.. 🙂

      regards, rafael

      Comment by mufumbo — July 2, 2009 @ 10:35 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: