1

This is my perl script to do svn merge.

$dir = 'D:\Transversal Unit\example\ace__project\branches\UAT';
print $dir."\n";
chdir($dir) or die "Fail $!";
$status = system("svn merge -c39,46 $sourceBranchURL");
print $status;

The problem in this is when the svn merge line is executed, the value of $status is always 0 whether there are conflicts or no. What I want is that if there are svn conflicts then I want to exit from the script and if no conflicts then further commit the code. I am not able to check for conflicts. Can anyone please help me in this?

Thanks in advance...

  • You could try the perl svn library http://stackoverflow.com/questions/2552808/how-do-i-install-perls-svnclient - which OS are you using – KeepCalmAndCarryOn Jun 12 '14 at 10:56
  • I have already tried installing the module Alien::SVN. but some how it is not getting installed. I also read somewhere that this module is not maintained anymore. – user3668231 Jun 12 '14 at 11:29
  • I am trying this option now `$status = qx{svn merge -r39:46 $sourceBranchURL 2>&1};` But when I print $status, it is not having any values. – user3668231 Jun 12 '14 at 11:31

2 Answers2

3

The problem in this is when the svn merge line is executed, the value of $status is always 0 whether there are conflicts or no.

That's not true. svn merge will return a non-zero status if the merge didn't work at all. For example, you didn't have a clean working directory. svn merge returns a non-zero value if the merge worked. And the merge works even if there are conflicts.

Do a --dry-run first and open svn merge as a file handle:

use strict;
use warnings;    # I hope in your program, you're actually using these two pragmas

# No you aren't. :-(
# $dir = 'D:\Transversal Unit\example\ace__project\branches\UAT';

use feature qw(say);
use autodie;

my $dir = 'D:/Traversal Unit/example/ace__project/branches/UAT'; #Use forward slashes
open my $merge, '|-', "svn merge -c39,46 --dry-run $sourceBranchURL";

Now, you can read each line of output from your svn merge command and see if you have problems:

my $conflict_found;
while ( my $line = <$merge> ) {
    chomp $line;
    if ( line =~ /^!/ ) {
       say "Conflict found: $line";
       $conflict_found = 1;
    }
}

if ( $conflict_found ) {
   die qq(Conflicts were found. Do this by hand...);
}

# No conflicts, rerun `svn merge` without `--dry-run`
...

This way, you can check for conflicts before you actually run the merge command. If there are no conflicts, you can (almost) safely do the merge without problems.

Remember that svn if there aren't conflicts doesn't mean the merge worked correctly. There may be a logical conflict even if the lines don't conflict:

Original file

line #1
line #2
line #3
line #4
print "Foo = " + foo

Change on trunk

line #1
line #2
line #3
foo = "bar"
line #4
print "Foo = " + foo

Change on branch

line #1
foo = "boof"
line #2
line #3
line #4
print "Foo = " + foo

I am assuming that setting foo to bar on trunk and boof on the branch is a conflict. However, the merge will happily happen without conflicts:

line #1
foo = "boof"
line #2
line #3
foo = "bar"
line #4
print "Foo = " + foo
David W.
  • 105,218
  • 39
  • 216
  • 337
  • This is a great answer, but I think there's a typo in the line that says "open my $merge, '|-', ...". The '|-' should be '-|' (so we read svn's output rather than sending data into svn). – Dan R. Mar 29 '23 at 17:46
0

With your approach of shelling out the commands, you could add an additional command which runs svn status and then parses the output for conflicts and make a decision based on this.

See Is there a command to list SVN conflicts? for regex examples...

Community
  • 1
  • 1
Josh
  • 401
  • 2
  • 8