diff options
author | Eric Davis <edavis@littlestreamsoftware.com> | 2009-08-15 22:41:40 +0000 |
---|---|---|
committer | Eric Davis <edavis@littlestreamsoftware.com> | 2009-08-15 22:41:40 +0000 |
commit | c28b044d6802559a9a2a07af1b7661a1122e5f48 (patch) | |
tree | 393dda01ed0fcd20ed98b2154f471a3a33440644 /lib/diff.rb | |
parent | a39bc8f1f4ec7c3b3ce2f27a02467cf497cef03a (diff) | |
download | redmine-c28b044d6802559a9a2a07af1b7661a1122e5f48.tar.gz redmine-c28b044d6802559a9a2a07af1b7661a1122e5f48.zip |
Added branch and tag support to the git repository viewer. (#1406)
Many thanks to Adam Soltys and everyone else who tested this patch.
* Updated git test repository so it has a branch with some differences from the master branch
* Moved redmine diff class into a module so as not to clash with diff-lcs gem which is required by grit
* Find changesets from all branches, not just master
* Got revision browsing working
* Got file actions working properly
* Allow browsing by short form of commit identifier
* Added a method to retrieve repository branches
* Allow browsing by branch names as well as commit numbers
* Handle the case where a git repository has no master branch
* Expand revision box and handle finding revisions by first 8 characters
* Added branches dropdown to repository show page
* Combined repository browse and show into a single action. Moved branch/revision navigation into a partial.
* Renamed partial navigation -> breadcrumbs
* Made it so latest revisions list uses branch and path context
* Preserve current path when changing branch or revision
* Perform slightly more graceful error handling in the case of invalid repository URLs
* Allow branch names to contain periods
* Allow dashes in branch names
* Sort branches by name
* Adding tags dropdown
* Need to disable both branches and tags dropdowns before submitting revision form
* Support underscores in revision (branch/tag) names
* Making file history sensitive to current branch/tag/revision, adding common navigation to changes page
* Updated translation files to include labels for 'branch', 'tag', and 'view all revisions'
* Reenable fields after submit so they don't look disabled and don't stay disabled on browser back button
* Instead of dashes just use empty string for default dropdown value
* Individual entry views now sport the upgraded revision navigation
* Don't display dropdowns with no entries
* Consider all revisions when doing initial load
* Fixed bug grabbing changesets. Thanks to Bernhard Furtmueller for catching.
* Always check the entire log to find new revisions, rather than trying to go forward from the latest known one
* Added some cleverness to avoid selecting the whole changesets table any time someone views the repository root
* File copies and renames being detected properly
* Return gracefully if no revisions are found in the git log
* Applied patch from Babar Le Lapin to improve Windows compatibility
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2840 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib/diff.rb')
-rw-r--r-- | lib/diff.rb | 350 |
1 files changed, 176 insertions, 174 deletions
diff --git a/lib/diff.rb b/lib/diff.rb index 646f91bae..f88e7fbb1 100644 --- a/lib/diff.rb +++ b/lib/diff.rb @@ -1,153 +1,155 @@ -class Diff +module RedmineDiff + class Diff - VERSION = 0.3 + VERSION = 0.3 - def Diff.lcs(a, b) - astart = 0 - bstart = 0 - afinish = a.length-1 - bfinish = b.length-1 - mvector = [] - - # First we prune off any common elements at the beginning - while (astart <= afinish && bstart <= afinish && a[astart] == b[bstart]) - mvector[astart] = bstart - astart += 1 - bstart += 1 - end - - # now the end - while (astart <= afinish && bstart <= bfinish && a[afinish] == b[bfinish]) - mvector[afinish] = bfinish - afinish -= 1 - bfinish -= 1 - end + def Diff.lcs(a, b) + astart = 0 + bstart = 0 + afinish = a.length-1 + bfinish = b.length-1 + mvector = [] + + # First we prune off any common elements at the beginning + while (astart <= afinish && bstart <= afinish && a[astart] == b[bstart]) + mvector[astart] = bstart + astart += 1 + bstart += 1 + end + + # now the end + while (astart <= afinish && bstart <= bfinish && a[afinish] == b[bfinish]) + mvector[afinish] = bfinish + afinish -= 1 + bfinish -= 1 + end - bmatches = b.reverse_hash(bstart..bfinish) - thresh = [] - links = [] - - (astart..afinish).each { |aindex| - aelem = a[aindex] - next unless bmatches.has_key? aelem - k = nil - bmatches[aelem].reverse.each { |bindex| - if k && (thresh[k] > bindex) && (thresh[k-1] < bindex) - thresh[k] = bindex - else - k = thresh.replacenextlarger(bindex, k) - end - links[k] = [ (k==0) ? nil : links[k-1], aindex, bindex ] if k + bmatches = b.reverse_hash(bstart..bfinish) + thresh = [] + links = [] + + (astart..afinish).each { |aindex| + aelem = a[aindex] + next unless bmatches.has_key? aelem + k = nil + bmatches[aelem].reverse.each { |bindex| + if k && (thresh[k] > bindex) && (thresh[k-1] < bindex) + thresh[k] = bindex + else + k = thresh.replacenextlarger(bindex, k) + end + links[k] = [ (k==0) ? nil : links[k-1], aindex, bindex ] if k + } } - } - if !thresh.empty? - link = links[thresh.length-1] - while link - mvector[link[1]] = link[2] - link = link[0] + if !thresh.empty? + link = links[thresh.length-1] + while link + mvector[link[1]] = link[2] + link = link[0] + end end - end - return mvector - end - - def makediff(a, b) - mvector = Diff.lcs(a, b) - ai = bi = 0 - while ai < mvector.length - bline = mvector[ai] - if bline - while bi < bline - discardb(bi, b[bi]) - bi += 1 - end - match(ai, bi) - bi += 1 - else - discarda(ai, a[ai]) - end - ai += 1 - end - while ai < a.length - discarda(ai, a[ai]) - ai += 1 + return mvector end - while bi < b.length + + def makediff(a, b) + mvector = Diff.lcs(a, b) + ai = bi = 0 + while ai < mvector.length + bline = mvector[ai] + if bline + while bi < bline discardb(bi, b[bi]) bi += 1 end match(ai, bi) - 1 - end - - def compactdiffs - diffs = [] - @diffs.each { |df| - i = 0 - curdiff = [] - while i < df.length - whot = df[i][0] - s = @isstring ? df[i][2].chr : [df[i][2]] - p = df[i][1] - last = df[i][1] - i += 1 - while df[i] && df[i][0] == whot && df[i][1] == last+1 - s << df[i][2] - last = df[i][1] - i += 1 - end - curdiff.push [whot, p, s] + bi += 1 + else + discarda(ai, a[ai]) + end + ai += 1 end - diffs.push curdiff - } - return diffs - end + while ai < a.length + discarda(ai, a[ai]) + ai += 1 + end + while bi < b.length + discardb(bi, b[bi]) + bi += 1 + end + match(ai, bi) + 1 + end - attr_reader :diffs, :difftype + def compactdiffs + diffs = [] + @diffs.each { |df| + i = 0 + curdiff = [] + while i < df.length + whot = df[i][0] + s = @isstring ? df[i][2].chr : [df[i][2]] + p = df[i][1] + last = df[i][1] + i += 1 + while df[i] && df[i][0] == whot && df[i][1] == last+1 + s << df[i][2] + last = df[i][1] + i += 1 + end + curdiff.push [whot, p, s] + end + diffs.push curdiff + } + return diffs + end - def initialize(diffs_or_a, b = nil, isstring = nil) - if b.nil? - @diffs = diffs_or_a - @isstring = isstring - else - @diffs = [] + attr_reader :diffs, :difftype + + def initialize(diffs_or_a, b = nil, isstring = nil) + if b.nil? + @diffs = diffs_or_a + @isstring = isstring + else + @diffs = [] + @curdiffs = [] + makediff(diffs_or_a, b) + @difftype = diffs_or_a.class + end + end + + def match(ai, bi) + @diffs.push @curdiffs unless @curdiffs.empty? @curdiffs = [] - makediff(diffs_or_a, b) - @difftype = diffs_or_a.class end - end - - def match(ai, bi) - @diffs.push @curdiffs unless @curdiffs.empty? - @curdiffs = [] - end - def discarda(i, elem) - @curdiffs.push ['-', i, elem] - end + def discarda(i, elem) + @curdiffs.push ['-', i, elem] + end - def discardb(i, elem) - @curdiffs.push ['+', i, elem] - end + def discardb(i, elem) + @curdiffs.push ['+', i, elem] + end - def compact - return Diff.new(compactdiffs) - end + def compact + return Diff.new(compactdiffs) + end - def compact! - @diffs = compactdiffs - end + def compact! + @diffs = compactdiffs + end - def inspect - @diffs.inspect - end + def inspect + @diffs.inspect + end + end end module Diffable def diff(b) - Diff.new(self, b) + RedmineDiff::Diff.new(self, b) end # Create a hash that maps elements of the array to arrays of indices @@ -158,9 +160,9 @@ module Diffable range.each { |i| elem = self[i] if revmap.has_key? elem - revmap[elem].push i + revmap[elem].push i else - revmap[elem] = [i] + revmap[elem] = [i] end } return revmap @@ -179,9 +181,9 @@ module Diffable found = self[index] return nil if value == found if value > found - low = index + 1 + low = index + 1 else - high = index + high = index end end @@ -204,25 +206,25 @@ module Diffable bi = 0 diff.diffs.each { |d| d.each { |mod| - case mod[0] - when '-' - while ai < mod[1] - newary << self[ai] - ai += 1 - bi += 1 - end - ai += 1 - when '+' - while bi < mod[1] - newary << self[ai] - ai += 1 - bi += 1 - end - newary << mod[2] - bi += 1 - else - raise "Unknown diff action" - end + case mod[0] + when '-' + while ai < mod[1] + newary << self[ai] + ai += 1 + bi += 1 + end + ai += 1 + when '+' + while bi < mod[1] + newary << self[ai] + ai += 1 + bi += 1 + end + newary << mod[2] + bi += 1 + else + raise "Unknown diff action" + end } } while ai < self.length @@ -243,38 +245,38 @@ class String end =begin -= Diff -(({diff.rb})) - computes the differences between two arrays or -strings. Copyright (C) 2001 Lars Christensen + = Diff + (({diff.rb})) - computes the differences between two arrays or + strings. Copyright (C) 2001 Lars Christensen -== Synopsis + == Synopsis - diff = Diff.new(a, b) - b = a.patch(diff) + diff = Diff.new(a, b) + b = a.patch(diff) -== Class Diff -=== Class Methods ---- Diff.new(a, b) ---- a.diff(b) - Creates a Diff object which represent the differences between - ((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays - of any objects, strings, or object of any class that include - module ((|Diffable|)) + == Class Diff + === Class Methods + --- Diff.new(a, b) + --- a.diff(b) + Creates a Diff object which represent the differences between + ((|a|)) and ((|b|)). ((|a|)) and ((|b|)) can be either be arrays + of any objects, strings, or object of any class that include + module ((|Diffable|)) -== Module Diffable -The module ((|Diffable|)) is intended to be included in any class for -which differences are to be computed. Diffable is included into String -and Array when (({diff.rb})) is (({require}))'d. + == Module Diffable + The module ((|Diffable|)) is intended to be included in any class for + which differences are to be computed. Diffable is included into String + and Array when (({diff.rb})) is (({require}))'d. -Classes including Diffable should implement (({[]})) to get element at -integer indices, (({<<})) to append elements to the object and -(({ClassName#new})) should accept 0 arguments to create a new empty -object. + Classes including Diffable should implement (({[]})) to get element at + integer indices, (({<<})) to append elements to the object and + (({ClassName#new})) should accept 0 arguments to create a new empty + object. -=== Instance Methods ---- Diffable#patch(diff) - Applies the differences from ((|diff|)) to the object ((|obj|)) - and return the result. ((|obj|)) is not changed. ((|obj|)) and - can be either an array or a string, but must match the object - from which the ((|diff|)) was created. + === Instance Methods + --- Diffable#patch(diff) + Applies the differences from ((|diff|)) to the object ((|obj|)) + and return the result. ((|obj|)) is not changed. ((|obj|)) and + can be either an array or a string, but must match the object + from which the ((|diff|)) was created. =end |