Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the common ancestor of two branches in SVN?

Tags:

svn

Imagine you've got a big SVN tree with branches all over the place. There is the trunk, there are branches, those branches have branches, etc. So, given two branches in the tree, how can you find the common ancestor?

I know that you could simply take the full log and compare it, but it get's kinda slow if your trunk has 75,000 revisions (and most of the time you do compare trunk with another, possibly faraway, branch).

The process will be automated, so you can suggest things which are not done easily by hand.

Added: Forgot to say, I need to get it done in real time. Not in "real time" as in 10ms, but in "real time" as in "before the person waiting for the output gets annoyed". So it'd be nice if it was under 10s.

like image 704
Vilx- Avatar asked Oct 25 '10 14:10

Vilx-


3 Answers

I guess this is what you need

svn log -v --stop-on-copy

would return the below

r43477 | username | 2010-09-21 13:19:58 +0530 (Tue, 21 Sep 2010) | 1 line Changed paths: A /trunk/re/XXX (from /branches/release/post_XXX/re/XXX:43476)

From this you can identify that this branch is a ancestor of the current branch. If you combine this the logic mentioned by Victor Nicollet you will be able to get the results in real time.

like image 104
Version Control Buddy Avatar answered Sep 23 '22 14:09

Version Control Buddy


Look at the logs at root of the tree. Branching operations will be indicated as copy operations, so you can reconstruct a full ancestor tree of what was copied from where. Leave that code running overnight and you'll have the complete copy tree the next morning, which you can then use to identify common ancestors of your branches.

Next time, you can resume work from the last revision you processed.

like image 35
Victor Nicollet Avatar answered Sep 23 '22 14:09

Victor Nicollet


It's a bit late for the OP, but I needed the same thing and couldn't find an answer. The following Powershell script works for me.

Basically we fetch a list of revision numbers for each of the paths to be compared (up to an optional revision limit to avoid fetching the entire history), then step through the lists looking for a matching revision number. The lists are naturally sorted in descending revision number order.

param ([string]$path1, [string]$path2, [int]$rev = 0)

if ($path1 -eq "" -or $path2 -eq "") { "Specify two svn paths to compare."; break }

$revs1 = new-object System.Collections.Generic.List``1[System.Int32]
svn log -r HEAD:$rev $path1 | %{ if ($_ -match "^r(\d+)") { $revs1.Add($matches[1]) } }

$revs2 = new-object System.Collections.Generic.List``1[System.Int32]
svn log -r HEAD:$rev $path2 | %{ if ($_ -match "^r(\d+)") { $revs2.Add($matches[1]) } }

$i1 = 0
$i2 = 0

while ($true) {
  if ($revs1[$i1] -eq $revs2[$i2]) { $revs1[$i1]; break }
  elseif ($revs1[$i1] -gt $revs2[$i2]) { do { $i1 += 1} until ($i1 -eq $revs1.Count -or $revs1[$i1] -le $revs2[$i2]) }
  else { do { $i2 += 1} until ($i2 -eq $revs2.Count -or $revs2[$i2] -le $revs1[$i1]) }
  if ($i1 -eq $revs1.Count -or $i2 -eq $revs2.Count) { "No common base revision found - try increasing limit."; break }
}
like image 36
Ian Horwill Avatar answered Sep 23 '22 14:09

Ian Horwill