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
This commit is contained in:
Eric Davis
2009-08-15 22:41:40 +00:00
parent a39bc8f1f4
commit c28b044d68
62 changed files with 699 additions and 440 deletions
+179 -177
View File
@@ -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
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
# 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
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
return mvector
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
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
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
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
end
def match(ai, bi)
@diffs.push @curdiffs unless @curdiffs.empty?
@curdiffs = []
end
def discarda(i, elem)
@curdiffs.push ['-', i, elem]
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]
end
diffs.push curdiff
}
return diffs
end
def discardb(i, elem)
@curdiffs.push ['+', i, elem]
end
attr_reader :diffs, :difftype
def compact
return Diff.new(compactdiffs)
end
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 = []
end
def compact!
@diffs = compactdiffs
end
def discarda(i, elem)
@curdiffs.push ['-', i, elem]
end
def inspect
@diffs.inspect
end
def discardb(i, elem)
@curdiffs.push ['+', i, elem]
end
def compact
return Diff.new(compactdiffs)
end
def compact!
@diffs = compactdiffs
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
+32 -1
View File
@@ -100,6 +100,18 @@ module Redmine
def entries(path=nil, identifier=nil)
return nil
end
def branches
return nil
end
def tags
return nil
end
def default_branch
return nil
end
def properties(path, identifier=nil)
return nil
@@ -260,6 +272,7 @@ module Redmine
class Revision
attr_accessor :identifier, :scmid, :name, :author, :time, :message, :paths, :revision, :branch
def initialize(attributes={})
self.identifier = attributes[:identifier]
self.scmid = attributes[:scmid]
@@ -271,7 +284,25 @@ module Redmine
self.revision = attributes[:revision]
self.branch = attributes[:branch]
end
def save(repo)
if repo.changesets.find_by_scmid(scmid.to_s).nil?
changeset = Changeset.create!(
:repository => repo,
:revision => identifier,
:scmid => scmid,
:committer => author,
:committed_on => time,
:comments => message)
paths.each do |file|
Change.create!(
:changeset => changeset,
:action => file[:action],
:path => file[:path])
end
end
end
end
class Annotate
+104 -111
View File
@@ -21,90 +21,38 @@ module Redmine
module Scm
module Adapters
class GitAdapter < AbstractAdapter
# Git executable name
GIT_BIN = "git"
# Get the revision of a particuliar file
def get_rev (rev,path)
if rev != 'latest' && !rev.nil?
cmd="#{GIT_BIN} --git-dir #{target('')} show --date=iso --pretty=fuller #{shell_quote rev} -- #{shell_quote path}"
else
@branch ||= shellout("#{GIT_BIN} --git-dir #{target('')} branch") { |io| io.grep(/\*/)[0].strip.match(/\* (.*)/)[1] }
cmd="#{GIT_BIN} --git-dir #{target('')} log --date=iso --pretty=fuller -1 #{@branch} -- #{shell_quote path}"
end
rev=[]
i=0
shellout(cmd) do |io|
files=[]
changeset = {}
parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
io.each_line do |line|
if line =~ /^commit ([0-9a-f]{40})$/
key = "commit"
value = $1
if (parsing_descr == 1 || parsing_descr == 2)
parsing_descr = 0
rev = Revision.new({:identifier => changeset[:commit],
:scmid => changeset[:commit],
:author => changeset[:author],
:time => Time.parse(changeset[:date]),
:message => changeset[:description],
:paths => files
})
changeset = {}
files = []
end
changeset[:commit] = $1
elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
key = $1
value = $2
if key == "Author"
changeset[:author] = value
elsif key == "CommitDate"
changeset[:date] = value
end
elsif (parsing_descr == 0) && line.chomp.to_s == ""
parsing_descr = 1
changeset[:description] = ""
elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
parsing_descr = 2
fileaction = $1
filepath = $2
files << {:action => fileaction, :path => filepath}
elsif (parsing_descr == 1) && line.chomp.to_s == ""
parsing_descr = 2
elsif (parsing_descr == 1)
changeset[:description] << line
end
end
rev = Revision.new({:identifier => changeset[:commit],
:scmid => changeset[:commit],
:author => changeset[:author],
:time => (changeset[:date] ? Time.parse(changeset[:date]) : nil),
:message => changeset[:description],
:paths => files
})
end
get_rev('latest',path) if rev == []
return nil if $? && $?.exitstatus != 0
return rev
end
def info
revs = revisions(url,nil,nil,{:limit => 1})
if revs && revs.any?
Info.new(:root_url => url, :lastrev => revs.first)
else
begin
Info.new(:root_url => url, :lastrev => lastrev('',nil))
rescue
nil
end
rescue Errno::ENOENT => e
return nil
end
def branches
branches = []
cmd = "#{GIT_BIN} --git-dir #{target('')} branch"
shellout(cmd) do |io|
io.each_line do |line|
branches << line.match('\s*\*?\s*(.*)$')[1]
end
end
branches.sort!
end
def tags
tags = []
cmd = "#{GIT_BIN} --git-dir #{target('')} tag"
shellout(cmd) do |io|
io.readlines.sort!.map{|t| t.strip}
end
end
def default_branch
branches.include?('master') ? 'master' : branches.first
end
def entries(path=nil, identifier=nil)
@@ -121,27 +69,63 @@ module Redmine
sha = $2
size = $3
name = $4
full_path = path.empty? ? name : "#{path}/#{name}"
entries << Entry.new({:name => name,
:path => (path.empty? ? name : "#{path}/#{name}"),
:kind => ((type == "tree") ? 'dir' : 'file'),
:size => ((type == "tree") ? nil : size),
:lastrev => get_rev(identifier,(path.empty? ? name : "#{path}/#{name}"))
}) unless entries.detect{|entry| entry.name == name}
:path => full_path,
:kind => (type == "tree") ? 'dir' : 'file',
:size => (type == "tree") ? nil : size,
:lastrev => lastrev(full_path,identifier)
}) unless entries.detect{|entry| entry.name == name}
end
end
end
return nil if $? && $?.exitstatus != 0
entries.sort_by_name
end
def lastrev(path,rev)
return nil if path.nil?
cmd = "#{GIT_BIN} --git-dir #{target('')} log --pretty=fuller --no-merges -n 1 "
cmd << " #{shell_quote rev} " if rev
cmd << "-- #{path} " unless path.empty?
shellout(cmd) do |io|
begin
id = io.gets.split[1]
author = io.gets.match('Author:\s+(.*)$')[1]
2.times { io.gets }
time = io.gets.match('CommitDate:\s+(.*)$')[1]
Revision.new({
:identifier => id,
:scmid => id,
:author => author,
:time => time,
:message => nil,
:paths => nil
})
rescue NoMethodError => e
logger.error("The revision '#{path}' has a wrong format")
return nil
end
end
end
def num_revisions
cmd = "#{GIT_BIN} --git-dir #{target('')} log --all --pretty=format:'' | wc -l"
shellout(cmd) {|io| io.gets.chomp.to_i + 1}
end
def revisions(path, identifier_from, identifier_to, options={})
revisions = Revisions.new
cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw --date=iso --pretty=fuller"
cmd = "#{GIT_BIN} --git-dir #{target('')} log --find-copies-harder --raw --date=iso --pretty=fuller"
cmd << " --reverse" if options[:reverse]
cmd << " -n #{options[:limit].to_i} " if (!options.nil?) && options[:limit]
cmd << " --all" if options[:all]
cmd << " -n #{options[:limit]} " if options[:limit]
cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from
cmd << " #{shell_quote identifier_to} " if identifier_to
cmd << " -- #{path}" if path && !path.empty?
shellout(cmd) do |io|
files=[]
changeset = {}
@@ -154,13 +138,14 @@ module Redmine
value = $1
if (parsing_descr == 1 || parsing_descr == 2)
parsing_descr = 0
revision = Revision.new({:identifier => changeset[:commit],
:scmid => changeset[:commit],
:author => changeset[:author],
:time => Time.parse(changeset[:date]),
:message => changeset[:description],
:paths => files
})
revision = Revision.new({
:identifier => changeset[:commit],
:scmid => changeset[:commit],
:author => changeset[:author],
:time => Time.parse(changeset[:date]),
:message => changeset[:description],
:paths => files
})
if block_given?
yield revision
else
@@ -182,26 +167,35 @@ module Redmine
elsif (parsing_descr == 0) && line.chomp.to_s == ""
parsing_descr = 1
changeset[:description] = ""
elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
elsif (parsing_descr == 1 || parsing_descr == 2) \
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
parsing_descr = 2
fileaction = $1
filepath = $2
files << {:action => fileaction, :path => filepath}
elsif (parsing_descr == 1 || parsing_descr == 2) \
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\s+(.+)$/
parsing_descr = 2
fileaction = $1
filepath = $3
files << {:action => fileaction, :path => filepath}
elsif (parsing_descr == 1) && line.chomp.to_s == ""
parsing_descr = 2
elsif (parsing_descr == 1)
changeset[:description] << line[4..-1]
end
end
end
if changeset[:commit]
revision = Revision.new({:identifier => changeset[:commit],
:scmid => changeset[:commit],
:author => changeset[:author],
:time => Time.parse(changeset[:date]),
:message => changeset[:description],
:paths => files
})
revision = Revision.new({
:identifier => changeset[:commit],
:scmid => changeset[:commit],
:author => changeset[:author],
:time => Time.parse(changeset[:date]),
:message => changeset[:description],
:paths => files
})
if block_given?
yield revision
else
@@ -213,15 +207,16 @@ module Redmine
return nil if $? && $?.exitstatus != 0
revisions
end
def diff(path, identifier_from, identifier_to=nil)
path ||= ''
if !identifier_to
identifier_to = nil
if identifier_to
cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}"
else
cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}"
end
cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" if identifier_to.nil?
cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" if !identifier_to.nil?
cmd << " -- #{shell_quote path}" unless path.empty?
diff = []
shellout(cmd) do |io|
@@ -265,6 +260,4 @@ module Redmine
end
end
end
end