Redmine 3.4.4

This commit is contained in:
Manuel Cillero 2018-02-02 22:19:29 +01:00
commit 64924a6376
2112 changed files with 259028 additions and 0 deletions

View file

@ -0,0 +1,434 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'cgi'
require 'redmine/scm/adapters'
module Redmine
module Scm
module Adapters
class AbstractAdapter #:nodoc:
include Redmine::Utils::Shell
# raised if scm command exited with error, e.g. unknown revision.
class ScmCommandAborted < ::Redmine::Scm::Adapters::CommandFailed; end
class << self
def client_command
""
end
def shell_quote(str)
Redmine::Utils::Shell.shell_quote str
end
def shell_quote_command
Redmine::Utils::Shell.shell_quote_command client_command
end
# Returns the version of the scm client
# Eg: [1, 5, 0] or [] if unknown
def client_version
[]
end
# Returns the version string of the scm client
# Eg: '1.5.0' or 'Unknown version' if unknown
def client_version_string
v = client_version || 'Unknown version'
v.is_a?(Array) ? v.join('.') : v.to_s
end
# Returns true if the current client version is above
# or equals the given one
# If option is :unknown is set to true, it will return
# true if the client version is unknown
def client_version_above?(v, options={})
((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
end
def client_available
true
end
end
def initialize(url, root_url=nil, login=nil, password=nil,
path_encoding=nil)
@url = url
@login = login if login && !login.empty?
@password = (password || "") if @login
@root_url = root_url.blank? ? retrieve_root_url : root_url
end
def adapter_name
'Abstract'
end
def supports_cat?
true
end
def supports_annotate?
respond_to?('annotate')
end
def root_url
@root_url
end
def url
@url
end
def path_encoding
nil
end
# get info about the svn repository
def info
return nil
end
# Returns the entry identified by path and revision identifier
# or nil if entry doesn't exist in the repository
def entry(path=nil, identifier=nil)
parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
search_path = parts[0..-2].join('/')
search_name = parts[-1]
if search_path.blank? && search_name.blank?
# Root entry
Entry.new(:path => '', :kind => 'dir')
else
# Search for the entry in the parent directory
es = entries(search_path, identifier)
es ? es.detect {|e| e.name == search_name} : nil
end
end
# Returns an Entries collection
# or nil if the given path doesn't exist in the repository
def entries(path=nil, identifier=nil, options={})
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
end
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
return nil
end
def diff(path, identifier_from, identifier_to=nil)
return nil
end
def cat(path, identifier=nil)
return nil
end
def with_leading_slash(path)
path ||= ''
(path[0,1]!="/") ? "/#{path}" : path
end
def with_trailling_slash(path)
path ||= ''
(path[-1,1] == "/") ? path : "#{path}/"
end
def without_leading_slash(path)
path ||= ''
path.gsub(%r{^/+}, '')
end
def without_trailling_slash(path)
path ||= ''
(path[-1,1] == "/") ? path[0..-2] : path
end
private
def retrieve_root_url
info = self.info
info ? info.root_url : nil
end
def target(path, sq=true)
path ||= ''
base = path.match(/^\//) ? root_url : url
str = "#{base}/#{path}".gsub(/[?<>\*]/, '')
if sq
str = shell_quote(str)
end
str
end
def logger
self.class.logger
end
def shellout(cmd, options = {}, &block)
self.class.shellout(cmd, options, &block)
end
def self.logger
Rails.logger
end
# Path to the file where scm stderr output is logged
# Returns nil if the log file is not writable
def self.stderr_log_file
if @stderr_log_file.nil?
writable = false
path = Redmine::Configuration['scm_stderr_log_file'].presence
path ||= Rails.root.join("log/#{Rails.env}.scm.stderr.log").to_s
if File.exists?(path)
if File.file?(path) && File.writable?(path)
writable = true
else
logger.warn("SCM log file (#{path}) is not writable")
end
else
begin
File.open(path, "w") {}
writable = true
rescue => e
logger.warn("SCM log file (#{path}) cannot be created: #{e.message}")
end
end
@stderr_log_file = writable ? path : false
end
@stderr_log_file || nil
end
def self.shellout(cmd, options = {}, &block)
if logger && logger.debug?
logger.debug "Shelling out: #{strip_credential(cmd)}"
# Capture stderr in a log file
if stderr_log_file
cmd = "#{cmd} 2>>#{shell_quote(stderr_log_file)}"
end
end
begin
mode = "r+"
IO.popen(cmd, mode) do |io|
io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
io.close_write unless options[:write_stdin]
block.call(io) if block_given?
end
## If scm command does not exist,
## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException
## in production environment.
# rescue Errno::ENOENT => e
rescue Exception => e
msg = strip_credential(e.message)
# The command failed, log it and re-raise
logmsg = "SCM command failed, "
logmsg += "make sure that your SCM command (e.g. svn) is "
logmsg += "in PATH (#{ENV['PATH']})\n"
logmsg += "You can configure your scm commands in config/configuration.yml.\n"
logmsg += "#{strip_credential(cmd)}\n"
logmsg += "with: #{msg}"
logger.error(logmsg)
raise CommandFailed.new(msg)
end
end
# Hides username/password in a given command
def self.strip_credential(cmd)
q = (Redmine::Platform.mswin? ? '"' : "'")
cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
end
def strip_credential(cmd)
self.class.strip_credential(cmd)
end
def scm_iconv(to, from, str)
return nil if str.nil?
return str if to == from && str.encoding.to_s == from
str.force_encoding(from)
begin
str.encode(to)
rescue Exception => err
logger.error("failed to convert from #{from} to #{to}. #{err}")
nil
end
end
def parse_xml(xml)
if RUBY_PLATFORM == 'java'
xml = xml.sub(%r{<\?xml[^>]*\?>}, '')
end
ActiveSupport::XmlMini.parse(xml)
end
end
class Entries < Array
def sort_by_name
dup.sort! {|x,y|
if x.kind == y.kind
x.name.to_s <=> y.name.to_s
else
x.kind <=> y.kind
end
}
end
def revisions
revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
end
end
class Info
attr_accessor :root_url, :lastrev
def initialize(attributes={})
self.root_url = attributes[:root_url] if attributes[:root_url]
self.lastrev = attributes[:lastrev]
end
end
class Entry
attr_accessor :name, :path, :kind, :size, :lastrev, :changeset
def initialize(attributes={})
self.name = attributes[:name] if attributes[:name]
self.path = attributes[:path] if attributes[:path]
self.kind = attributes[:kind] if attributes[:kind]
self.size = attributes[:size].to_i if attributes[:size]
self.lastrev = attributes[:lastrev]
end
def is_file?
'file' == self.kind
end
def is_dir?
'dir' == self.kind
end
def is_text?
Redmine::MimeType.is_type?('text', name)
end
def author
if changeset
changeset.author.to_s
elsif lastrev
Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first)
end
end
end
class Revisions < Array
def latest
sort {|x,y|
unless x.time.nil? or y.time.nil?
x.time <=> y.time
else
0
end
}.last
end
end
class Revision
attr_accessor :scmid, :name, :author, :time, :message,
:paths, :revision, :branch, :identifier,
:parents
def initialize(attributes={})
self.identifier = attributes[:identifier]
self.scmid = attributes[:scmid]
self.name = attributes[:name] || self.identifier
self.author = attributes[:author]
self.time = attributes[:time]
self.message = attributes[:message] || ""
self.paths = attributes[:paths]
self.revision = attributes[:revision]
self.branch = attributes[:branch]
self.parents = attributes[:parents]
end
# Returns the readable identifier.
def format_identifier
self.identifier.to_s
end
def ==(other)
if other.nil?
false
elsif scmid.present?
scmid == other.scmid
elsif identifier.present?
identifier == other.identifier
elsif revision.present?
revision == other.revision
end
end
end
class Annotate
attr_reader :lines, :revisions
def initialize
@lines = []
@revisions = []
end
def add_line(line, revision)
@lines << line
@revisions << revision
end
def content
content = lines.join("\n")
end
def empty?
lines.empty?
end
end
class Branch < String
attr_accessor :revision, :scmid
end
module ScmData
def self.binary?(data)
unless data.empty?
data.count( "^ -~", "^\r\n" ).fdiv(data.size) > 0.3 || data.index( "\x00" )
end
end
end
end
end
end

View file

@ -0,0 +1,338 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
module Redmine
module Scm
module Adapters
class BazaarAdapter < AbstractAdapter
# Bazaar executable name
BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr"
class << self
def client_command
@@bin ||= BZR_BIN
end
def sq_bin
@@sq_bin ||= shell_quote_command
end
def client_version
@@client_version ||= (scm_command_version || [])
end
def client_available
!client_version.empty?
end
def scm_command_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT')
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i)
end
end
def scm_version_from_command_line
shellout("#{sq_bin} --version") { |io| io.read }.to_s
end
end
def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
@url = url
@root_url = url
@path_encoding = 'UTF-8'
# do not call *super* for non ASCII repository path
end
def bzr_path_encodig=(encoding)
@path_encoding = encoding
end
# Get info about the repository
def info
cmd_args = %w|revno|
cmd_args << bzr_target('')
info = nil
scm_cmd(*cmd_args) do |io|
if io.read =~ %r{^(\d+)\r?$}
info = Info.new({:root_url => url,
:lastrev => Revision.new({
:identifier => $1
})
})
end
end
info
rescue ScmCommandAborted
return nil
end
# Returns an Entries collection
# or nil if the given path doesn't exist in the repository
def entries(path=nil, identifier=nil, options={})
path ||= ''
entries = Entries.new
identifier = -1 unless identifier && identifier.to_i > 0
cmd_args = %w|ls -v --show-ids|
cmd_args << "-r#{identifier.to_i}"
cmd_args << bzr_target(path)
scm_cmd(*cmd_args) do |io|
prefix_utf8 = "#{url}/#{path}".gsub('\\', '/')
logger.debug "PREFIX: #{prefix_utf8}"
prefix = scm_iconv(@path_encoding, 'UTF-8', prefix_utf8)
prefix.force_encoding('ASCII-8BIT')
re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$}
io.each_line do |line|
next unless line =~ re
name_locale, slash, revision = $3.strip, $4, $5.strip
name = scm_iconv('UTF-8', @path_encoding, name_locale)
entries << Entry.new({:name => name,
:path => ((path.empty? ? "" : "#{path}/") + name),
:kind => (slash.blank? ? 'file' : 'dir'),
:size => nil,
:lastrev => Revision.new(:revision => revision)
})
end
end
if logger && logger.debug?
logger.debug("Found #{entries.size} entries in the repository for #{target(path)}")
end
entries.sort_by_name
rescue ScmCommandAborted
return nil
end
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
path ||= ''
identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1'
identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
revisions = Revisions.new
cmd_args = %w|log -v --show-ids|
cmd_args << "-r#{identifier_to}..#{identifier_from}"
cmd_args << bzr_target(path)
scm_cmd(*cmd_args) do |io|
revision = nil
parsing = nil
io.each_line do |line|
if line =~ /^----/
revisions << revision if revision
revision = Revision.new(:paths => [], :message => '')
parsing = nil
else
next unless revision
if line =~ /^revno: (\d+)($|\s\[merge\]$)/
revision.identifier = $1.to_i
elsif line =~ /^committer: (.+)$/
revision.author = $1.strip
elsif line =~ /^revision-id:(.+)$/
revision.scmid = $1.strip
elsif line =~ /^timestamp: (.+)$/
revision.time = Time.parse($1).localtime
elsif line =~ /^ -----/
# partial revisions
parsing = nil unless parsing == 'message'
elsif line =~ /^(message|added|modified|removed|renamed):/
parsing = $1
elsif line =~ /^ (.*)$/
if parsing == 'message'
revision.message << "#{$1}\n"
else
if $1 =~ /^(.*)\s+(\S+)$/
path_locale = $1.strip
path = scm_iconv('UTF-8', @path_encoding, path_locale)
revid = $2
case parsing
when 'added'
revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid}
when 'modified'
revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid}
when 'removed'
revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid}
when 'renamed'
new_path = path.split('=>').last
if new_path
revision.paths << {:action => 'M', :path => "/#{new_path.strip}",
:revision => revid}
end
end
end
end
else
parsing = nil
end
end
end
revisions << revision if revision
end
revisions
rescue ScmCommandAborted
return nil
end
def diff(path, identifier_from, identifier_to=nil)
path ||= ''
if identifier_to
identifier_to = identifier_to.to_i
else
identifier_to = identifier_from.to_i - 1
end
if identifier_from
identifier_from = identifier_from.to_i
end
diff = []
cmd_args = %w|diff|
cmd_args << "-r#{identifier_to}..#{identifier_from}"
cmd_args << bzr_target(path)
scm_cmd_no_raise(*cmd_args) do |io|
io.each_line do |line|
diff << line
end
end
diff
end
def cat(path, identifier=nil)
cat = nil
cmd_args = %w|cat|
cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0
cmd_args << bzr_target(path)
scm_cmd(*cmd_args) do |io|
io.binmode
cat = io.read
end
cat
rescue ScmCommandAborted
return nil
end
def annotate(path, identifier=nil)
blame = Annotate.new
cmd_args = %w|annotate -q --all|
cmd_args << "-r#{identifier.to_i}" if identifier && identifier.to_i > 0
cmd_args << bzr_target(path)
scm_cmd(*cmd_args) do |io|
author = nil
identifier = nil
io.each_line do |line|
next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$}
rev = $1
blame.add_line($3.rstrip,
Revision.new(
:identifier => rev,
:revision => rev,
:author => $2.strip
))
end
end
blame
rescue ScmCommandAborted
return nil
end
def self.branch_conf_path(path)
bcp = nil
m = path.match(%r{^(.*[/\\])\.bzr.*$})
if m
bcp = m[1]
else
bcp = path
end
bcp.gsub!(%r{[\/\\]$}, "")
if bcp
bcp = File.join(bcp, ".bzr", "branch", "branch.conf")
end
bcp
end
def append_revisions_only
return @aro if ! @aro.nil?
@aro = false
bcp = self.class.branch_conf_path(url)
if bcp && File.exist?(bcp)
begin
f = File::open(bcp, "r")
cnt = 0
f.each_line do |line|
l = line.chomp.to_s
if l =~ /^\s*append_revisions_only\s*=\s*(\w+)\s*$/
str_aro = $1
if str_aro.upcase == "TRUE"
@aro = true
cnt += 1
elsif str_aro.upcase == "FALSE"
@aro = false
cnt += 1
end
if cnt > 1
@aro = false
break
end
end
end
ensure
f.close
end
end
@aro
end
def scm_cmd(*args, &block)
full_args = []
full_args += args
full_args_locale = []
full_args.map do |e|
full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e)
end
ret = shellout(
self.class.sq_bin + ' ' +
full_args_locale.map { |e| shell_quote e.to_s }.join(' '),
&block
)
if $? && $?.exitstatus != 0
raise ScmCommandAborted, "bzr exited with non-zero status: #{$?.exitstatus}"
end
ret
end
private :scm_cmd
def scm_cmd_no_raise(*args, &block)
full_args = []
full_args += args
full_args_locale = []
full_args.map do |e|
full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e)
end
ret = shellout(
self.class.sq_bin + ' ' +
full_args_locale.map { |e| shell_quote e.to_s }.join(' '),
&block
)
ret
end
private :scm_cmd_no_raise
def bzr_target(path)
target(path, false)
end
private :bzr_target
end
end
end
end

View file

@ -0,0 +1,25 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module Redmine
module Scm
module Adapters
class CommandFailed < StandardError #:nodoc:
end
end
end
end

View file

@ -0,0 +1,459 @@
# redMine - project management software
# Copyright (C) 2006-2007 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
module Redmine
module Scm
module Adapters
class CvsAdapter < AbstractAdapter
# CVS executable name
CVS_BIN = Redmine::Configuration['scm_cvs_command'] || "cvs"
class << self
def client_command
@@bin ||= CVS_BIN
end
def sq_bin
@@sq_bin ||= shell_quote_command
end
def client_version
@@client_version ||= (scm_command_version || [])
end
def client_available
client_version_above?([1, 12])
end
def scm_command_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT')
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}m)
m[2].scan(%r{\d+}).collect(&:to_i)
end
end
def scm_version_from_command_line
shellout("#{sq_bin} --version") { |io| io.read }.to_s
end
end
# Guidelines for the input:
# url -> the project-path, relative to the cvsroot (eg. module name)
# root_url -> the good old, sometimes damned, CVSROOT
# login -> unnecessary
# password -> unnecessary too
def initialize(url, root_url=nil, login=nil, password=nil,
path_encoding=nil)
@path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
@url = url
# TODO: better Exception here (IllegalArgumentException)
raise CommandFailed if root_url.blank?
@root_url = root_url
# These are unused.
@login = login if login && !login.empty?
@password = (password || "") if @login
end
def path_encoding
@path_encoding
end
def info
logger.debug "<cvs> info"
Info.new({:root_url => @root_url, :lastrev => nil})
end
def get_previous_revision(revision)
CvsRevisionHelper.new(revision).prevRev
end
# Returns an Entries collection
# or nil if the given path doesn't exist in the repository
# this method is used by the repository-browser (aka LIST)
def entries(path=nil, identifier=nil, options={})
logger.debug "<cvs> entries '#{path}' with identifier '#{identifier}'"
path_locale = scm_iconv(@path_encoding, 'UTF-8', path)
path_locale.force_encoding("ASCII-8BIT")
entries = Entries.new
cmd_args = %w|-q rls -e|
cmd_args << "-D" << time_to_cvstime_rlog(identifier) if identifier
cmd_args << path_with_proj(path)
scm_cmd(*cmd_args) do |io|
io.each_line() do |line|
fields = line.chop.split('/',-1)
logger.debug(">>InspectLine #{fields.inspect}")
if fields[0]!="D"
time = nil
# Thu Dec 13 16:27:22 2007
time_l = fields[-3].split(' ')
if time_l.size == 5 && time_l[4].length == 4
begin
time = Time.parse(
"#{time_l[1]} #{time_l[2]} #{time_l[3]} GMT #{time_l[4]}")
rescue
end
end
entries << Entry.new(
{
:name => scm_iconv('UTF-8', @path_encoding, fields[-5]),
#:path => fields[-4].include?(path)?fields[-4]:(path + "/"+ fields[-4]),
:path => scm_iconv('UTF-8', @path_encoding, "#{path_locale}/#{fields[-5]}"),
:kind => 'file',
:size => nil,
:lastrev => Revision.new(
{
:revision => fields[-4],
:name => scm_iconv('UTF-8', @path_encoding, fields[-4]),
:time => time,
:author => ''
})
})
else
entries << Entry.new(
{
:name => scm_iconv('UTF-8', @path_encoding, fields[1]),
:path => scm_iconv('UTF-8', @path_encoding, "#{path_locale}/#{fields[1]}"),
:kind => 'dir',
:size => nil,
:lastrev => nil
})
end
end
end
entries.sort_by_name
rescue ScmCommandAborted
nil
end
STARTLOG="----------------------------"
ENDLOG ="============================================================================="
# Returns all revisions found between identifier_from and identifier_to
# in the repository. both identifier have to be dates or nil.
# these method returns nothing but yield every result in block
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}, &block)
path_with_project_utf8 = path_with_proj(path)
path_with_project_locale = scm_iconv(@path_encoding, 'UTF-8', path_with_project_utf8)
logger.debug "<cvs> revisions path:" +
"'#{path}',identifier_from #{identifier_from}, identifier_to #{identifier_to}"
cmd_args = %w|-q rlog|
cmd_args << "-d" << ">#{time_to_cvstime_rlog(identifier_from)}" if identifier_from
cmd_args << path_with_project_utf8
scm_cmd(*cmd_args) do |io|
state = "entry_start"
commit_log = String.new
revision = nil
date = nil
author = nil
entry_path = nil
entry_name = nil
file_state = nil
branch_map = nil
io.each_line() do |line|
if state != "revision" && /^#{ENDLOG}/ =~ line
commit_log = String.new
revision = nil
state = "entry_start"
end
if state == "entry_start"
branch_map = Hash.new
if /^RCS file: #{Regexp.escape(root_url_path)}\/#{Regexp.escape(path_with_project_locale)}(.+),v$/ =~ line
entry_path = normalize_cvs_path($1)
entry_name = normalize_path(File.basename($1))
logger.debug("Path #{entry_path} <=> Name #{entry_name}")
elsif /^head: (.+)$/ =~ line
entry_headRev = $1 #unless entry.nil?
elsif /^symbolic names:/ =~ line
state = "symbolic" #unless entry.nil?
elsif /^#{STARTLOG}/ =~ line
commit_log = String.new
state = "revision"
end
next
elsif state == "symbolic"
if /^(.*):\s(.*)/ =~ (line.strip)
branch_map[$1] = $2
else
state = "tags"
next
end
elsif state == "tags"
if /^#{STARTLOG}/ =~ line
commit_log = ""
state = "revision"
elsif /^#{ENDLOG}/ =~ line
state = "head"
end
next
elsif state == "revision"
if /^#{ENDLOG}/ =~ line || /^#{STARTLOG}/ =~ line
if revision
revHelper = CvsRevisionHelper.new(revision)
revBranch = "HEAD"
branch_map.each() do |branch_name, branch_point|
if revHelper.is_in_branch_with_symbol(branch_point)
revBranch = branch_name
end
end
logger.debug("********** YIELD Revision #{revision}::#{revBranch}")
yield Revision.new({
:time => date,
:author => author,
:message => commit_log.chomp,
:paths => [{
:revision => revision.dup,
:branch => revBranch.dup,
:path => scm_iconv('UTF-8', @path_encoding, entry_path),
:name => scm_iconv('UTF-8', @path_encoding, entry_name),
:kind => 'file',
:action => file_state
}]
})
end
commit_log = String.new
revision = nil
if /^#{ENDLOG}/ =~ line
state = "entry_start"
end
next
end
if /^branches: (.+)$/ =~ line
# TODO: version.branch = $1
elsif /^revision (\d+(?:\.\d+)+).*$/ =~ line
revision = $1
elsif /^date:\s+(\d+.\d+.\d+\s+\d+:\d+:\d+)/ =~ line
date = Time.parse($1)
line_utf8 = scm_iconv('UTF-8', options[:log_encoding], line)
author_utf8 = /author: ([^;]+)/.match(line_utf8)[1]
author = scm_iconv(options[:log_encoding], 'UTF-8', author_utf8)
file_state = /state: ([^;]+)/.match(line)[1]
# TODO:
# linechanges only available in CVS....
# maybe a feature our SVN implementation.
# I'm sure, they are useful for stats or something else
# linechanges =/lines: \+(\d+) -(\d+)/.match(line)
# unless linechanges.nil?
# version.line_plus = linechanges[1]
# version.line_minus = linechanges[2]
# else
# version.line_plus = 0
# version.line_minus = 0
# end
else
commit_log << line unless line =~ /^\*\*\* empty log message \*\*\*/
end
end
end
end
rescue ScmCommandAborted
Revisions.new
end
def diff(path, identifier_from, identifier_to=nil)
logger.debug "<cvs> diff path:'#{path}'" +
",identifier_from #{identifier_from}, identifier_to #{identifier_to}"
cmd_args = %w|rdiff -u|
cmd_args << "-r#{identifier_to}"
cmd_args << "-r#{identifier_from}"
cmd_args << path_with_proj(path)
diff = []
scm_cmd(*cmd_args) do |io|
io.each_line do |line|
diff << line
end
end
diff
rescue ScmCommandAborted
nil
end
def cat(path, identifier=nil)
identifier = (identifier) ? identifier : "HEAD"
logger.debug "<cvs> cat path:'#{path}',identifier #{identifier}"
cmd_args = %w|-q co|
cmd_args << "-D" << time_to_cvstime(identifier) if identifier
cmd_args << "-p" << path_with_proj(path)
cat = nil
scm_cmd(*cmd_args) do |io|
io.binmode
cat = io.read
end
cat
rescue ScmCommandAborted
nil
end
def annotate(path, identifier=nil)
identifier = (identifier) ? identifier : "HEAD"
logger.debug "<cvs> annotate path:'#{path}',identifier #{identifier}"
cmd_args = %w|rannotate|
cmd_args << "-D" << time_to_cvstime(identifier) if identifier
cmd_args << path_with_proj(path)
blame = Annotate.new
scm_cmd(*cmd_args) do |io|
io.each_line do |line|
next unless line =~ %r{^([\d\.]+)\s+\(([^\)]+)\s+[^\)]+\):\s(.*)$}
blame.add_line(
$3.rstrip,
Revision.new(
:revision => $1,
:identifier => nil,
:author => $2.strip
))
end
end
blame
rescue ScmCommandAborted
Annotate.new
end
private
# Returns the root url without the connexion string
# :pserver:anonymous@foo.bar:/path => /path
# :ext:cvsservername:/path => /path
def root_url_path
root_url.to_s.gsub(%r{^:.+?(?=/)}, '')
end
# convert a date/time into the CVS-format
def time_to_cvstime(time)
return nil if time.nil?
time = Time.now if (time.kind_of?(String) && time == 'HEAD')
unless time.kind_of? Time
time = Time.parse(time)
end
return time_to_cvstime_rlog(time)
end
def time_to_cvstime_rlog(time)
return nil if time.nil?
t1 = time.clone.localtime
return t1.strftime("%Y-%m-%d %H:%M:%S")
end
def normalize_cvs_path(path)
normalize_path(path.gsub(/Attic\//,''))
end
def normalize_path(path)
path.sub(/^(\/)*(.*)/,'\2').sub(/(.*)(,v)+/,'\1')
end
def path_with_proj(path)
"#{url}#{with_leading_slash(path)}"
end
private :path_with_proj
class Revision < Redmine::Scm::Adapters::Revision
# Returns the readable identifier
def format_identifier
revision.to_s
end
end
def scm_cmd(*args, &block)
full_args = ['-d', root_url]
full_args += args
full_args_locale = []
full_args.map do |e|
full_args_locale << scm_iconv(@path_encoding, 'UTF-8', e)
end
ret = shellout(
self.class.sq_bin + ' ' + full_args_locale.map { |e| shell_quote e.to_s }.join(' '),
&block
)
if $? && $?.exitstatus != 0
raise ScmCommandAborted, "cvs exited with non-zero status: #{$?.exitstatus}"
end
ret
end
private :scm_cmd
end
class CvsRevisionHelper
attr_accessor :complete_rev, :revision, :base, :branchid
def initialize(complete_rev)
@complete_rev = complete_rev
parseRevision()
end
def branchPoint
return @base
end
def branchVersion
if isBranchRevision
return @base+"."+@branchid
end
return @base
end
def isBranchRevision
!@branchid.nil?
end
def prevRev
unless @revision == 0
return buildRevision( @revision - 1 )
end
return buildRevision( @revision )
end
def is_in_branch_with_symbol(branch_symbol)
bpieces = branch_symbol.split(".")
branch_start = "#{bpieces[0..-3].join(".")}.#{bpieces[-1]}"
return ( branchVersion == branch_start )
end
private
def buildRevision(rev)
if rev == 0
if @branchid.nil?
@base + ".0"
else
@base
end
elsif @branchid.nil?
@base + "." + rev.to_s
else
@base + "." + @branchid + "." + rev.to_s
end
end
# Interpretiert die cvs revisionsnummern wie z.b. 1.14 oder 1.3.0.15
def parseRevision()
pieces = @complete_rev.split(".")
@revision = pieces.last.to_i
baseSize = 1
baseSize += (pieces.size / 2)
@base = pieces[0..-baseSize].join(".")
if baseSize > 2
@branchid = pieces[-2]
end
end
end
end
end
end

View file

@ -0,0 +1,239 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
require 'rexml/document'
module Redmine
module Scm
module Adapters
class DarcsAdapter < AbstractAdapter
# Darcs executable name
DARCS_BIN = Redmine::Configuration['scm_darcs_command'] || "darcs"
class << self
def client_command
@@bin ||= DARCS_BIN
end
def sq_bin
@@sq_bin ||= shell_quote_command
end
def client_version
@@client_version ||= (darcs_binary_version || [])
end
def client_available
!client_version.empty?
end
def darcs_binary_version
darcsversion = darcs_binary_version_from_command_line.dup.force_encoding('ASCII-8BIT')
if m = darcsversion.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i)
end
end
def darcs_binary_version_from_command_line
shellout("#{sq_bin} --version") { |io| io.read }.to_s
end
end
def initialize(url, root_url=nil, login=nil, password=nil,
path_encoding=nil)
@url = url
@root_url = url
end
def supports_cat?
# cat supported in darcs 2.0.0 and higher
self.class.client_version_above?([2, 0, 0])
end
# Get info about the darcs repository
def info
rev = revisions(nil,nil,nil,{:limit => 1})
rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil
end
# Returns an Entries collection
# or nil if the given path doesn't exist in the repository
def entries(path=nil, identifier=nil, options={})
path_prefix = (path.blank? ? '' : "#{path}/")
if path.blank?
path = ( self.class.client_version_above?([2, 2, 0]) ? @url : '.' )
end
entries = Entries.new
cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --xml-output"
cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
cmd << " #{shell_quote path}"
shellout(cmd) do |io|
begin
doc = REXML::Document.new(io)
if doc.root.name == 'directory'
doc.elements.each('directory/*') do |element|
next unless ['file', 'directory'].include? element.name
entries << entry_from_xml(element, path_prefix)
end
elsif doc.root.name == 'file'
entries << entry_from_xml(doc.root, path_prefix)
end
rescue
end
end
return nil if $? && $?.exitstatus != 0
entries.compact!
entries.sort_by_name
end
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
path = '.' if path.blank?
revisions = Revisions.new
cmd = "#{self.class.sq_bin} changes --repodir #{shell_quote @url} --xml-output"
cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from
cmd << " --last #{options[:limit].to_i}" if options[:limit]
shellout(cmd) do |io|
begin
doc = REXML::Document.new(io)
doc.elements.each("changelog/patch") do |patch|
message = patch.elements['name'].text
message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment']
revisions << Revision.new({:identifier => nil,
:author => patch.attributes['author'],
:scmid => patch.attributes['hash'],
:time => Time.parse(patch.attributes['local_date']),
:message => message,
:paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil)
})
end
rescue
end
end
return nil if $? && $?.exitstatus != 0
revisions
end
def diff(path, identifier_from, identifier_to=nil)
path = '*' if path.blank?
cmd = "#{self.class.sq_bin} diff --repodir #{shell_quote @url}"
if identifier_to.nil?
cmd << " --match #{shell_quote("hash #{identifier_from}")}"
else
cmd << " --to-match #{shell_quote("hash #{identifier_from}")}"
cmd << " --from-match #{shell_quote("hash #{identifier_to}")}"
end
cmd << " -u #{shell_quote path}"
diff = []
shellout(cmd) do |io|
io.each_line do |line|
diff << line
end
end
return nil if $? && $?.exitstatus != 0
diff
end
def cat(path, identifier=nil)
cmd = "#{self.class.sq_bin} show content --repodir #{shell_quote @url}"
cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
cmd << " #{shell_quote path}"
cat = nil
shellout(cmd) do |io|
io.binmode
cat = io.read
end
return nil if $? && $?.exitstatus != 0
cat
end
private
# Returns an Entry from the given XML element
# or nil if the entry was deleted
def entry_from_xml(element, path_prefix)
modified_element = element.elements['modified']
if modified_element.elements['modified_how'].text.match(/removed/)
return nil
end
Entry.new({:name => element.attributes['name'],
:path => path_prefix + element.attributes['name'],
:kind => element.name == 'file' ? 'file' : 'dir',
:size => nil,
:lastrev => Revision.new({
:identifier => nil,
:scmid => modified_element.elements['patch'].attributes['hash']
})
})
end
def get_paths_for_patch(hash)
paths = get_paths_for_patch_raw(hash)
if self.class.client_version_above?([2, 4])
orig_paths = paths
paths = []
add_paths = []
add_paths_name = []
mod_paths = []
other_paths = []
orig_paths.each do |path|
if path[:action] == 'A'
add_paths << path
add_paths_name << path[:path]
elsif path[:action] == 'M'
mod_paths << path
else
other_paths << path
end
end
add_paths_name.each do |add_path|
mod_paths.delete_if { |m| m[:path] == add_path }
end
paths.concat add_paths
paths.concat mod_paths
paths.concat other_paths
end
paths
end
# Retrieve changed paths for a single patch
def get_paths_for_patch_raw(hash)
cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --summary --xml-output"
cmd << " --match #{shell_quote("hash #{hash}")} "
paths = []
shellout(cmd) do |io|
begin
# Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7)
# A root element is added so that REXML doesn't raise an error
doc = REXML::Document.new("<fake_root>" + io.read + "</fake_root>")
doc.elements.each('fake_root/summary/*') do |modif|
paths << {:action => modif.name[0,1].upcase,
:path => "/" + modif.text.chomp.gsub(/^\s*/, '')
}
end
rescue
end
end
paths
rescue CommandFailed
paths
end
end
end
end
end

View file

@ -0,0 +1,118 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# FileSystem adapter
# File written by Paul Rivier, at Demotera.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
require 'find'
module Redmine
module Scm
module Adapters
class FilesystemAdapter < AbstractAdapter
class << self
def client_available
true
end
end
def initialize(url, root_url=nil, login=nil, password=nil,
path_encoding=nil)
@url = with_trailling_slash(url)
@path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
end
def path_encoding
@path_encoding
end
def format_path_ends(path, leading=true, trailling=true)
path = leading ? with_leading_slash(path) :
without_leading_slash(path)
trailling ? with_trailling_slash(path) :
without_trailling_slash(path)
end
def info
info = Info.new({:root_url => target(),
:lastrev => nil
})
info
rescue CommandFailed
return nil
end
def entries(path="", identifier=nil, options={})
entries = Entries.new
trgt_utf8 = target(path)
trgt = scm_iconv(@path_encoding, 'UTF-8', trgt_utf8)
Dir.new(trgt).each do |e1|
e_utf8 = scm_iconv('UTF-8', @path_encoding, e1)
next if e_utf8.blank?
relative_path_utf8 = format_path_ends(
(format_path_ends(path,false,true) + e_utf8),false,false)
t1_utf8 = target(relative_path_utf8)
t1 = scm_iconv(@path_encoding, 'UTF-8', t1_utf8)
relative_path = scm_iconv(@path_encoding, 'UTF-8', relative_path_utf8)
e1 = scm_iconv(@path_encoding, 'UTF-8', e_utf8)
if File.exist?(t1) and # paranoid test
%w{file directory}.include?(File.ftype(t1)) and # avoid special types
not File.basename(e1).match(/^\.+$/) # avoid . and ..
p1 = File.readable?(t1) ? relative_path : ""
utf_8_path = scm_iconv('UTF-8', @path_encoding, p1)
entries <<
Entry.new({ :name => scm_iconv('UTF-8', @path_encoding, File.basename(e1)),
# below : list unreadable files, but dont link them.
:path => utf_8_path,
:kind => (File.directory?(t1) ? 'dir' : 'file'),
:size => (File.directory?(t1) ? nil : [File.size(t1)].pack('l').unpack('L').first),
:lastrev =>
Revision.new({:time => (File.mtime(t1)) })
})
end
end
entries.sort_by_name
rescue => err
logger.error "scm: filesystem: error: #{err.message}"
raise CommandFailed.new(err.message)
end
def cat(path, identifier=nil)
p = scm_iconv(@path_encoding, 'UTF-8', target(path))
File.new(p, "rb").read
rescue => err
logger.error "scm: filesystem: error: #{err.message}"
raise CommandFailed.new(err.message)
end
private
# AbstractAdapter::target is implicitly made to quote paths.
# Here we do not shell-out, so we do not want quotes.
def target(path=nil)
# Prevent the use of ..
if path and !path.match(/(^|\/)\.\.(\/|$)/)
return "#{self.url}#{without_leading_slash(path)}"
end
return self.url
end
end
end
end
end

View file

@ -0,0 +1,411 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
module Redmine
module Scm
module Adapters
class GitAdapter < AbstractAdapter
# Git executable name
GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
class GitBranch < Branch
attr_accessor :is_default
end
class << self
def client_command
@@bin ||= GIT_BIN
end
def sq_bin
@@sq_bin ||= shell_quote_command
end
def client_version
@@client_version ||= (scm_command_version || [])
end
def client_available
!client_version.empty?
end
def scm_command_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT')
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i)
end
end
def scm_version_from_command_line
shellout("#{sq_bin} --version") { |io| io.read }.to_s
end
end
def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
super
@path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
end
def path_encoding
@path_encoding
end
def info
begin
Info.new(:root_url => url, :lastrev => lastrev('',nil))
rescue
nil
end
end
def branches
return @branches if @branches
@branches = []
cmd_args = %w|branch --no-color --verbose --no-abbrev|
git_cmd(cmd_args) do |io|
io.each_line do |line|
branch_rev = line.match('\s*(\*?)\s*(.*?)\s*([0-9a-f]{40}).*$')
bran = GitBranch.new(branch_rev[2])
bran.revision = branch_rev[3]
bran.scmid = branch_rev[3]
bran.is_default = ( branch_rev[1] == '*' )
@branches << bran
end
end
@branches.sort!
rescue ScmCommandAborted
nil
end
def tags
return @tags if @tags
@tags = []
cmd_args = %w|tag|
git_cmd(cmd_args) do |io|
@tags = io.readlines.sort!.map{|t| t.strip}
end
@tags
rescue ScmCommandAborted
nil
end
def default_branch
bras = self.branches
return nil if bras.nil?
default_bras = bras.select{|x| x.is_default == true}
return default_bras.first.to_s if ! default_bras.empty?
master_bras = bras.select{|x| x.to_s == 'master'}
master_bras.empty? ? bras.first.to_s : 'master'
end
def entry(path=nil, identifier=nil)
parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
search_path = parts[0..-2].join('/')
search_name = parts[-1]
if search_path.blank? && search_name.blank?
# Root entry
Entry.new(:path => '', :kind => 'dir')
else
# Search for the entry in the parent directory
es = entries(search_path, identifier,
options = {:report_last_commit => false})
es ? es.detect {|e| e.name == search_name} : nil
end
end
def entries(path=nil, identifier=nil, options={})
path ||= ''
p = scm_iconv(@path_encoding, 'UTF-8', path)
entries = Entries.new
cmd_args = %w|ls-tree -l|
cmd_args << "HEAD:#{p}" if identifier.nil?
cmd_args << "#{identifier}:#{p}" if identifier
git_cmd(cmd_args) do |io|
io.each_line do |line|
e = line.chomp.to_s
if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
type = $1
sha = $2
size = $3
name = $4.force_encoding(@path_encoding)
full_path = p.empty? ? name : "#{p}/#{name}"
n = scm_iconv('UTF-8', @path_encoding, name)
full_p = scm_iconv('UTF-8', @path_encoding, full_path)
entries << Entry.new({:name => n,
:path => full_p,
:kind => (type == "tree") ? 'dir' : 'file',
:size => (type == "tree") ? nil : size,
:lastrev => options[:report_last_commit] ?
lastrev(full_path, identifier) : Revision.new
}) unless entries.detect{|entry| entry.name == name}
end
end
end
entries.sort_by_name
rescue ScmCommandAborted
nil
end
def lastrev(path, rev)
return nil if path.nil?
cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
cmd_args << '--no-renames' if self.class.client_version_above?([2, 9])
cmd_args << rev if rev
cmd_args << "--" << path unless path.empty?
lines = []
git_cmd(cmd_args) { |io| lines = io.readlines }
begin
id = lines[0].split[1]
author = lines[1].match('Author:\s+(.*)$')[1]
time = Time.parse(lines[4].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
rescue ScmCommandAborted
nil
end
def revisions(path, identifier_from, identifier_to, options={})
revs = Revisions.new
cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents --stdin|
cmd_args << '--no-renames' if self.class.client_version_above?([2, 9])
cmd_args << "--reverse" if options[:reverse]
cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
revisions = []
if identifier_from || identifier_to
revisions << ""
revisions[0] << "#{identifier_from}.." if identifier_from
revisions[0] << "#{identifier_to}" if identifier_to
else
unless options[:includes].blank?
revisions += options[:includes]
end
unless options[:excludes].blank?
revisions += options[:excludes].map{|r| "^#{r}"}
end
end
git_cmd(cmd_args, {:write_stdin => true}) do |io|
io.binmode
io.puts(revisions.join("\n"))
io.close_write
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})(( [0-9a-f]{40})*)$/
key = "commit"
value = $1
parents_str = $2
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,
:parents => changeset[:parents]
})
if block_given?
yield revision
else
revs << revision
end
changeset = {}
files = []
end
changeset[:commit] = $1
unless parents_str.nil? or parents_str == ""
changeset[:parents] = parents_str.strip.split(' ')
end
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)\t(.+)$/
parsing_descr = 2
fileaction = $1
filepath = $2
p = scm_iconv('UTF-8', @path_encoding, filepath)
files << {:action => fileaction, :path => p}
elsif (parsing_descr == 1 || parsing_descr == 2) \
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
parsing_descr = 2
fileaction = $1
filepath = $3
p = scm_iconv('UTF-8', @path_encoding, filepath)
files << {:action => fileaction, :path => p}
elsif (parsing_descr == 1) && line.chomp.to_s == ""
parsing_descr = 2
elsif (parsing_descr == 1)
changeset[:description] << line[4..-1]
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,
:parents => changeset[:parents]
})
if block_given?
yield revision
else
revs << revision
end
end
end
revs
rescue ScmCommandAborted => e
err_msg = "git log error: #{e.message}"
logger.error(err_msg)
if block_given?
raise CommandFailed, err_msg
else
revs
end
end
def diff(path, identifier_from, identifier_to=nil)
path ||= ''
cmd_args = []
if identifier_to
cmd_args << "diff" << "--no-color" << identifier_to << identifier_from
else
cmd_args << "show" << "--no-color" << identifier_from
end
cmd_args << '--no-renames' if self.class.client_version_above?([2, 9])
cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty?
diff = []
git_cmd(cmd_args) do |io|
io.each_line do |line|
diff << line
end
end
diff
rescue ScmCommandAborted
nil
end
def annotate(path, identifier=nil)
identifier = 'HEAD' if identifier.blank?
cmd_args = %w|blame --encoding=UTF-8|
cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path)
blame = Annotate.new
content = nil
git_cmd(cmd_args) { |io| io.binmode; content = io.read }
# git annotates binary files
return nil if ScmData.binary?(content)
identifier = ''
# git shows commit author on the first occurrence only
authors_by_commit = {}
content.split("\n").each do |line|
if line =~ /^([0-9a-f]{39,40})\s.*/
identifier = $1
elsif line =~ /^author (.+)/
authors_by_commit[identifier] = $1.strip
elsif line =~ /^\t(.*)/
blame.add_line($1, Revision.new(
:identifier => identifier,
:revision => identifier,
:scmid => identifier,
:author => authors_by_commit[identifier]
))
identifier = ''
author = ''
end
end
blame
rescue ScmCommandAborted
nil
end
def cat(path, identifier=nil)
if identifier.nil?
identifier = 'HEAD'
end
cmd_args = %w|show --no-color|
cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
cat = nil
git_cmd(cmd_args) do |io|
io.binmode
cat = io.read
end
cat
rescue ScmCommandAborted
nil
end
class Revision < Redmine::Scm::Adapters::Revision
# Returns the readable identifier
def format_identifier
identifier[0,8]
end
end
def git_cmd(args, options = {}, &block)
repo_path = root_url || url
full_args = ['--git-dir', repo_path]
if self.class.client_version_above?([1, 7, 2])
full_args << '-c' << 'core.quotepath=false'
full_args << '-c' << 'log.decorate=no'
end
full_args += args
ret = shellout(
self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
options,
&block
)
if $? && $?.exitstatus != 0
raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"
end
ret
end
private :git_cmd
end
end
end
end

View file

@ -0,0 +1,12 @@
changeset = 'This template must be used with --debug option\n'
changeset_quiet = 'This template must be used with --debug option\n'
changeset_verbose = 'This template must be used with --debug option\n'
changeset_debug = '<logentry revision="{rev}" node="{node}">\n<author>{author|escape}</author>\n<date>{date|isodatesec}</date>\n<paths>\n{file_mods}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n<parents>\n{parents}</parents>\n</logentry>\n\n'
file_mod = '<path action="M">{file_mod|urlescape}</path>\n'
file_add = '<path action="A">{file_add|urlescape}</path>\n'
file_del = '<path action="D">{file_del|urlescape}</path>\n'
file_copy = '<path-copied copyfrom-path="{source|urlescape}">{name|urlescape}</path-copied>\n'
parent = '<parent>{node}</parent>\n'
header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
# footer="</log>"

View file

@ -0,0 +1,226 @@
# redminehelper: Redmine helper extension for Mercurial
#
# Copyright 2010 Alessio Franceschelli (alefranz.net)
# Copyright 2010-2011 Yuya Nishihara <yuya@tcha.org>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
"""helper commands for Redmine to reduce the number of hg calls
To test this extension, please try::
$ hg --config extensions.redminehelper=redminehelper.py rhsummary
I/O encoding:
:file path: urlencoded, raw string
:tag name: utf-8
:branch name: utf-8
:node: hex string
Output example of rhsummary::
<?xml version="1.0"?>
<rhsummary>
<repository root="/foo/bar">
<tip revision="1234" node="abcdef0123..."/>
<tag revision="123" node="34567abc..." name="1.1.1"/>
<branch .../>
...
</repository>
</rhsummary>
Output example of rhmanifest::
<?xml version="1.0"?>
<rhmanifest>
<repository root="/foo/bar">
<manifest revision="1234" path="lib">
<file name="diff.rb" revision="123" node="34567abc..." time="12345"
size="100"/>
...
<dir name="redmine"/>
...
</manifest>
</repository>
</rhmanifest>
"""
import re, time, cgi, urllib
from mercurial import cmdutil, commands, node, error, hg
cmdtable = {}
command = cmdutil.command(cmdtable)
_x = cgi.escape
_u = lambda s: cgi.escape(urllib.quote(s))
def _tip(ui, repo):
# see mercurial/commands.py:tip
def tiprev():
try:
return len(repo) - 1
except TypeError: # Mercurial < 1.1
return repo.changelog.count() - 1
tipctx = repo.changectx(tiprev())
ui.write('<tip revision="%d" node="%s"/>\n'
% (tipctx.rev(), _x(node.hex(tipctx.node()))))
_SPECIAL_TAGS = ('tip',)
def _tags(ui, repo):
# see mercurial/commands.py:tags
for t, n in reversed(repo.tagslist()):
if t in _SPECIAL_TAGS:
continue
try:
r = repo.changelog.rev(n)
except error.LookupError:
continue
ui.write('<tag revision="%d" node="%s" name="%s"/>\n'
% (r, _x(node.hex(n)), _x(t)))
def _branches(ui, repo):
# see mercurial/commands.py:branches
def iterbranches():
if getattr(repo, 'branchtags', None) is not None:
# Mercurial < 2.9
for t, n in repo.branchtags().iteritems():
yield t, n, repo.changelog.rev(n)
else:
for tag, heads, tip, isclosed in repo.branchmap().iterbranches():
yield tag, tip, repo.changelog.rev(tip)
def branchheads(branch):
try:
return repo.branchheads(branch, closed=False)
except TypeError: # Mercurial < 1.2
return repo.branchheads(branch)
for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True):
if repo.lookup(r) in branchheads(t):
ui.write('<branch revision="%d" node="%s" name="%s"/>\n'
% (r, _x(node.hex(n)), _x(t)))
def _manifest(ui, repo, path, rev):
ctx = repo.changectx(rev)
ui.write('<manifest revision="%d" path="%s">\n'
% (ctx.rev(), _u(path)))
known = set()
pathprefix = (path.rstrip('/') + '/').lstrip('/')
for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]):
if not f.startswith(pathprefix):
continue
name = re.sub(r'/.*', '/', f[len(pathprefix):])
if name in known:
continue
known.add(name)
if name.endswith('/'):
ui.write('<dir name="%s"/>\n'
% _x(urllib.quote(name[:-1])))
else:
fctx = repo.filectx(f, fileid=n)
tm, tzoffset = fctx.date()
ui.write('<file name="%s" revision="%d" node="%s" '
'time="%d" size="%d"/>\n'
% (_u(name), fctx.rev(), _x(node.hex(fctx.node())),
tm, fctx.size(), ))
ui.write('</manifest>\n')
@command('rhannotate',
[('r', 'rev', '', 'revision'),
('u', 'user', None, 'list the author (long with -v)'),
('n', 'number', None, 'list the revision number (default)'),
('c', 'changeset', None, 'list the changeset'),
],
'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...')
def rhannotate(ui, repo, *pats, **opts):
rev = urllib.unquote_plus(opts.pop('rev', None))
opts['rev'] = rev
return commands.annotate(ui, repo, *map(urllib.unquote_plus, pats), **opts)
@command('rhcat',
[('r', 'rev', '', 'revision')],
'hg rhcat ([-r REV] ...) FILE...')
def rhcat(ui, repo, file1, *pats, **opts):
rev = urllib.unquote_plus(opts.pop('rev', None))
opts['rev'] = rev
return commands.cat(ui, repo, urllib.unquote_plus(file1), *map(urllib.unquote_plus, pats), **opts)
@command('rhdiff',
[('r', 'rev', [], 'revision'),
('c', 'change', '', 'change made by revision')],
'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...')
def rhdiff(ui, repo, *pats, **opts):
"""diff repository (or selected files)"""
change = opts.pop('change', None)
if change: # add -c option for Mercurial<1.1
base = repo.changectx(change).parents()[0].rev()
opts['rev'] = [str(base), change]
opts['nodates'] = True
return commands.diff(ui, repo, *map(urllib.unquote_plus, pats), **opts)
@command('rhlog',
[
('r', 'rev', [], 'show the specified revision'),
('b', 'branch', [],
'show changesets within the given named branch'),
('l', 'limit', '',
'limit number of changes displayed'),
('d', 'date', '',
'show revisions matching date spec'),
('u', 'user', [],
'revisions committed by user'),
('', 'from', '',
''),
('', 'to', '',
''),
('', 'rhbranch', '',
''),
('', 'template', '',
'display with template')],
'hg rhlog [OPTION]... [FILE]')
def rhlog(ui, repo, *pats, **opts):
rev = opts.pop('rev')
bra0 = opts.pop('branch')
from_rev = urllib.unquote_plus(opts.pop('from', None))
to_rev = urllib.unquote_plus(opts.pop('to' , None))
bra = urllib.unquote_plus(opts.pop('rhbranch', None))
from_rev = from_rev.replace('"', '\\"')
to_rev = to_rev.replace('"', '\\"')
if hg.util.version() >= '1.6':
opts['rev'] = ['"%s":"%s"' % (from_rev, to_rev)]
else:
opts['rev'] = ['%s:%s' % (from_rev, to_rev)]
opts['branch'] = [bra]
return commands.log(ui, repo, *map(urllib.unquote_plus, pats), **opts)
@command('rhmanifest',
[('r', 'rev', '', 'show the specified revision')],
'hg rhmanifest [-r REV] [PATH]')
def rhmanifest(ui, repo, path='', **opts):
"""output the sub-manifest of the specified directory"""
ui.write('<?xml version="1.0"?>\n')
ui.write('<rhmanifest>\n')
ui.write('<repository root="%s">\n' % _u(repo.root))
try:
_manifest(ui, repo, urllib.unquote_plus(path), urllib.unquote_plus(opts.get('rev')))
finally:
ui.write('</repository>\n')
ui.write('</rhmanifest>\n')
@command('rhsummary',[], 'hg rhsummary')
def rhsummary(ui, repo, **opts):
"""output the summary of the repository"""
ui.write('<?xml version="1.0"?>\n')
ui.write('<rhsummary>\n')
ui.write('<repository root="%s">\n' % _u(repo.root))
try:
_tip(ui, repo)
_tags(ui, repo)
_branches(ui, repo)
# TODO: bookmarks in core (Mercurial>=1.8)
finally:
ui.write('</repository>\n')
ui.write('</rhsummary>\n')

View file

@ -0,0 +1,345 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
require 'cgi'
module Redmine
module Scm
module Adapters
class MercurialAdapter < AbstractAdapter
# Mercurial executable name
HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
HELPERS_DIR = File.dirname(__FILE__) + "/mercurial"
HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py"
TEMPLATE_NAME = "hg-template"
TEMPLATE_EXTENSION = "tmpl"
# raised if hg command exited with error, e.g. unknown revision.
class HgCommandAborted < CommandFailed; end
# raised if bad command argument detected before executing hg.
class HgCommandArgumentError < CommandFailed; end
class << self
def client_command
@@bin ||= HG_BIN
end
def sq_bin
@@sq_bin ||= shell_quote_command
end
def client_version
@@client_version ||= (hgversion || [])
end
def client_available
client_version_above?([1, 2])
end
def hgversion
# The hg version is expressed either as a
# release number (eg 0.9.5 or 1.0) or as a revision
# id composed of 12 hexa characters.
theversion = hgversion_from_command_line.dup.force_encoding('ASCII-8BIT')
if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i)
end
end
def hgversion_from_command_line
shellout("#{sq_bin} --version") { |io| io.read }.to_s
end
def template_path
@@template_path ||= template_path_for(client_version)
end
def template_path_for(version)
"#{HELPERS_DIR}/#{TEMPLATE_NAME}-1.0.#{TEMPLATE_EXTENSION}"
end
end
def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
super
@path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
end
def path_encoding
@path_encoding
end
def info
tip = summary['repository']['tip']
Info.new(:root_url => CGI.unescape(summary['repository']['root']),
:lastrev => Revision.new(:revision => tip['revision'],
:scmid => tip['node']))
# rescue HgCommandAborted
rescue Exception => e
logger.error "hg: error during getting info: #{e.message}"
nil
end
def tags
as_ary(summary['repository']['tag']).map { |e| e['name'] }
end
# Returns map of {'tag' => 'nodeid', ...}
def tagmap
alist = as_ary(summary['repository']['tag']).map do |e|
e.values_at('name', 'node')
end
Hash[*alist.flatten]
end
def branches
brs = []
as_ary(summary['repository']['branch']).each do |e|
br = Branch.new(e['name'])
br.revision = e['revision']
br.scmid = e['node']
brs << br
end
brs
end
# Returns map of {'branch' => 'nodeid', ...}
def branchmap
alist = as_ary(summary['repository']['branch']).map do |e|
e.values_at('name', 'node')
end
Hash[*alist.flatten]
end
def summary
return @summary if @summary
hg 'rhsummary' do |io|
output = io.read.force_encoding('UTF-8')
begin
@summary = parse_xml(output)['rhsummary']
rescue
end
end
end
private :summary
def entries(path=nil, identifier=nil, options={})
p1 = scm_iconv(@path_encoding, 'UTF-8', path)
manifest = hg('rhmanifest', "-r#{CGI.escape(hgrev(identifier))}",
'--', CGI.escape(without_leading_slash(p1.to_s))) do |io|
output = io.read.force_encoding('UTF-8')
begin
parse_xml(output)['rhmanifest']['repository']['manifest']
rescue
end
end
path_prefix = path.blank? ? '' : with_trailling_slash(path)
entries = Entries.new
as_ary(manifest['dir']).each do |e|
n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
p = "#{path_prefix}#{n}"
entries << Entry.new(:name => n, :path => p, :kind => 'dir')
end
as_ary(manifest['file']).each do |e|
n = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['name']))
p = "#{path_prefix}#{n}"
lr = Revision.new(:revision => e['revision'], :scmid => e['node'],
:identifier => e['node'],
:time => Time.at(e['time'].to_i))
entries << Entry.new(:name => n, :path => p, :kind => 'file',
:size => e['size'].to_i, :lastrev => lr)
end
entries
rescue HgCommandAborted
nil # means not found
end
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
revs = Revisions.new
each_revision(path, identifier_from, identifier_to, options) { |e| revs << e }
revs
end
# Iterates the revisions by using a template file that
# makes Mercurial produce a xml output.
def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={})
hg_args = ['log', '--debug', '-C', "--style=#{self.class.template_path}"]
hg_args << "-r#{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
hg_args << "--limit=#{options[:limit]}" if options[:limit]
hg_args << '--' << hgtarget(path) unless path.blank?
log = hg(*hg_args) do |io|
output = io.read.force_encoding('UTF-8')
begin
# Mercurial < 1.5 does not support footer template for '</log>'
parse_xml("#{output}</log>")['log']
rescue
end
end
as_ary(log['logentry']).each do |le|
cpalist = as_ary(le['paths']['path-copied']).map do |e|
[e['__content__'], e['copyfrom-path']].map do |s|
scm_iconv('UTF-8', @path_encoding, CGI.unescape(s))
end
end
cpmap = Hash[*cpalist.flatten]
paths = as_ary(le['paths']['path']).map do |e|
p = scm_iconv('UTF-8', @path_encoding, CGI.unescape(e['__content__']) )
{:action => e['action'],
:path => with_leading_slash(p),
:from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil),
:from_revision => (cpmap.member?(p) ? le['node'] : nil)}
end.sort { |a, b| a[:path] <=> b[:path] }
parents_ary = []
as_ary(le['parents']['parent']).map do |par|
parents_ary << par['__content__'] if par['__content__'] != "0000000000000000000000000000000000000000"
end
yield Revision.new(:revision => le['revision'],
:scmid => le['node'],
:author => (le['author']['__content__'] rescue ''),
:time => Time.parse(le['date']['__content__']),
:message => le['msg']['__content__'],
:paths => paths,
:parents => parents_ary)
end
self
end
# Returns list of nodes in the specified branch
def nodes_in_branch(branch, options={})
hg_args = ['rhlog', '--template={node}\n', "--rhbranch=#{CGI.escape(branch)}"]
hg_args << "--from=#{CGI.escape(branch)}"
hg_args << '--to=0'
hg_args << "--limit=#{options[:limit]}" if options[:limit]
hg(*hg_args) { |io| io.readlines.map { |e| e.chomp } }
end
def diff(path, identifier_from, identifier_to=nil)
hg_args = %w|rhdiff|
if identifier_to
hg_args << "-r#{hgrev(identifier_to)}" << "-r#{hgrev(identifier_from)}"
else
hg_args << "-c#{hgrev(identifier_from)}"
end
unless path.blank?
p = scm_iconv(@path_encoding, 'UTF-8', path)
hg_args << '--' << CGI.escape(hgtarget(p))
end
diff = []
hg *hg_args do |io|
io.each_line do |line|
diff << line
end
end
diff
rescue HgCommandAborted
nil # means not found
end
def cat(path, identifier=nil)
p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
hg 'rhcat', "-r#{CGI.escape(hgrev(identifier))}", '--', hgtarget(p) do |io|
io.binmode
io.read
end
rescue HgCommandAborted
nil # means not found
end
def annotate(path, identifier=nil)
p = CGI.escape(scm_iconv(@path_encoding, 'UTF-8', path))
blame = Annotate.new
hg 'rhannotate', '-ncu', "-r#{CGI.escape(hgrev(identifier))}", '--', hgtarget(p) do |io|
io.each_line do |line|
line.force_encoding('ASCII-8BIT')
next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
:identifier => $3)
blame.add_line($4.rstrip, r)
end
end
blame
rescue HgCommandAborted
# means not found or cannot be annotated
Annotate.new
end
class Revision < Redmine::Scm::Adapters::Revision
# Returns the readable identifier
def format_identifier
"#{revision}:#{scmid}"
end
end
# command options which may be processed earlier, by faulty parser in hg
HG_EARLY_BOOL_ARG = /^--(debugger|profile|traceback)$/
HG_EARLY_LIST_ARG = /^(--(config|cwd|repo(sitory)?)\b|-R)/
private_constant :HG_EARLY_BOOL_ARG, :HG_EARLY_LIST_ARG
# Runs 'hg' command with the given args
def hg(*args, &block)
# as of hg 4.4.1, early parsing of bool options is not terminated at '--'
if args.any? { |s| s =~ HG_EARLY_BOOL_ARG }
raise HgCommandArgumentError, "malicious command argument detected"
end
if args.take_while { |s| s != '--' }.any? { |s| s =~ HG_EARLY_LIST_ARG }
raise HgCommandArgumentError, "malicious command argument detected"
end
repo_path = root_url || url
full_args = ["-R#{repo_path}", '--encoding=utf-8']
# don't use "--config=<value>" form for compatibility with ancient Mercurial
full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}"
full_args << '--config' << 'diff.git=false'
full_args += args
ret = shellout(
self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
&block
)
if $? && $?.exitstatus != 0
raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}"
end
ret
end
private :hg
# Returns correct revision identifier
def hgrev(identifier, sq=false)
rev = identifier.blank? ? 'tip' : identifier.to_s
rev = shell_quote(rev) if sq
rev
end
private :hgrev
def hgtarget(path)
path ||= ''
root_url + '/' + without_leading_slash(path)
end
private :hgtarget
def as_ary(o)
return [] unless o
o.is_a?(Array) ? o : Array[o]
end
private :as_ary
end
end
end
end

View file

@ -0,0 +1,276 @@
# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/scm/adapters/abstract_adapter'
require 'uri'
module Redmine
module Scm
module Adapters
class SubversionAdapter < AbstractAdapter
# SVN executable name
SVN_BIN = Redmine::Configuration['scm_subversion_command'] || "svn"
class << self
def client_command
@@bin ||= SVN_BIN
end
def sq_bin
@@sq_bin ||= shell_quote_command
end
def client_version
@@client_version ||= (svn_binary_version || [])
end
def client_available
# --xml options are introduced in 1.3.
# http://subversion.apache.org/docs/release-notes/1.3.html
client_version_above?([1, 3])
end
def svn_binary_version
scm_version = scm_version_from_command_line.dup.force_encoding('ASCII-8BIT')
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
m[2].scan(%r{\d+}).collect(&:to_i)
end
end
def scm_version_from_command_line
shellout("#{sq_bin} --version") { |io| io.read }.to_s
end
end
# Get info about the svn repository
def info
cmd = "#{self.class.sq_bin} info --xml #{target}"
cmd << credentials_string
info = nil
shellout(cmd) do |io|
output = io.read.force_encoding('UTF-8')
begin
doc = parse_xml(output)
# root_url = doc.elements["info/entry/repository/root"].text
info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'],
:lastrev => Revision.new({
:identifier => doc['info']['entry']['commit']['revision'],
:time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime,
:author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "")
})
})
rescue
end
end
return nil if $? && $?.exitstatus != 0
info
rescue CommandFailed
return nil
end
# Returns an Entries collection
# or nil if the given path doesn't exist in the repository
def entries(path=nil, identifier=nil, options={})
path ||= ''
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
entries = Entries.new
cmd = "#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}"
cmd << credentials_string
shellout(cmd) do |io|
output = io.read.force_encoding('UTF-8')
begin
doc = parse_xml(output)
each_xml_element(doc['lists']['list'], 'entry') do |entry|
commit = entry['commit']
commit_date = commit['date']
# Skip directory if there is no commit date (usually that
# means that we don't have read access to it)
next if entry['kind'] == 'dir' && commit_date.nil?
name = entry['name']['__content__']
entries << Entry.new({:name => URI.unescape(name),
:path => ((path.empty? ? "" : "#{path}/") + name),
:kind => entry['kind'],
:size => ((s = entry['size']) ? s['__content__'].to_i : nil),
:lastrev => Revision.new({
:identifier => commit['revision'],
:time => Time.parse(commit_date['__content__'].to_s).localtime,
:author => ((a = commit['author']) ? a['__content__'] : nil)
})
})
end
rescue Exception => e
logger.error("Error parsing svn output: #{e.message}")
logger.error("Output was:\n #{output}")
end
end
return nil if $? && $?.exitstatus != 0
logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
entries.sort_by_name
end
def properties(path, identifier=nil)
# proplist xml output supported in svn 1.5.0 and higher
return nil unless self.class.client_version_above?([1, 5, 0])
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
cmd = "#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}"
cmd << credentials_string
properties = {}
shellout(cmd) do |io|
output = io.read.force_encoding('UTF-8')
begin
doc = parse_xml(output)
each_xml_element(doc['properties']['target'], 'property') do |property|
properties[ property['name'] ] = property['__content__'].to_s
end
rescue
end
end
return nil if $? && $?.exitstatus != 0
properties
end
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
path ||= ''
identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1
revisions = Revisions.new
cmd = "#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}"
cmd << credentials_string
cmd << " --verbose " if options[:with_paths]
cmd << " --limit #{options[:limit].to_i}" if options[:limit]
cmd << ' ' + target(path)
shellout(cmd) do |io|
output = io.read.force_encoding('UTF-8')
begin
doc = parse_xml(output)
each_xml_element(doc['log'], 'logentry') do |logentry|
paths = []
each_xml_element(logentry['paths'], 'path') do |path|
paths << {:action => path['action'],
:path => path['__content__'],
:from_path => path['copyfrom-path'],
:from_revision => path['copyfrom-rev']
}
end if logentry['paths'] && logentry['paths']['path']
paths.sort! { |x,y| x[:path] <=> y[:path] }
revisions << Revision.new({:identifier => logentry['revision'],
:author => (logentry['author'] ? logentry['author']['__content__'] : ""),
:time => Time.parse(logentry['date']['__content__'].to_s).localtime,
:message => logentry['msg']['__content__'],
:paths => paths
})
end
rescue
end
end
return nil if $? && $?.exitstatus != 0
revisions
end
def diff(path, identifier_from, identifier_to=nil)
path ||= ''
identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
cmd = "#{self.class.sq_bin} diff -r "
cmd << "#{identifier_to}:"
cmd << "#{identifier_from}"
cmd << " #{target(path)}@#{identifier_from}"
cmd << credentials_string
diff = []
shellout(cmd) do |io|
io.each_line do |line|
diff << line
end
end
return nil if $? && $?.exitstatus != 0
diff
end
def cat(path, identifier=nil)
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
cmd = "#{self.class.sq_bin} cat #{target(path)}@#{identifier}"
cmd << credentials_string
cat = nil
shellout(cmd) do |io|
io.binmode
cat = io.read
end
return nil if $? && $?.exitstatus != 0
cat
end
def annotate(path, identifier=nil)
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
cmd = "#{self.class.sq_bin} blame #{target(path)}@#{identifier}"
cmd << credentials_string
blame = Annotate.new
shellout(cmd) do |io|
io.each_line do |line|
next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
rev = $1
blame.add_line($3.rstrip,
Revision.new(
:identifier => rev,
:revision => rev,
:author => $2.strip
))
end
end
return nil if $? && $?.exitstatus != 0
blame
end
private
def credentials_string
str = ''
str << " --username #{shell_quote(@login)}" unless @login.blank?
str << " --password #{shell_quote(@password)}" unless @login.blank? || @password.blank?
str << " --no-auth-cache --non-interactive"
str
end
# Helper that iterates over the child elements of a xml node
# MiniXml returns a hash when a single child is found
# or an array of hashes for multiple children
def each_xml_element(node, name)
if node && node[name]
if node[name].is_a?(Hash)
yield node[name]
else
node[name].each do |element|
yield element
end
end
end
end
def target(path = '')
base = path.match(/^\//) ? root_url : url
uri = "#{base}/#{path}"
uri = URI.escape(URI.escape(uri), '[]')
shell_quote(uri.gsub(/[?<>\*]/, ''))
end
end
end
end
end