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

后端 未结 4 1205
花落未央
花落未央 2021-01-02 05:14

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

4条回答
  •  自闭症患者
    2021-01-02 05:29

    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 }
    }
    

提交回复
热议问题