annotate .svn/pristine/ce/cea51a8cc1d29add5095e952d8b4a92de6169347.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require 'redmine/scm/adapters/abstract_adapter'
Chris@1494 19 require 'rexml/document'
Chris@1494 20
Chris@1494 21 module Redmine
Chris@1494 22 module Scm
Chris@1494 23 module Adapters
Chris@1494 24 class DarcsAdapter < AbstractAdapter
Chris@1494 25 # Darcs executable name
Chris@1494 26 DARCS_BIN = Redmine::Configuration['scm_darcs_command'] || "darcs"
Chris@1494 27
Chris@1494 28 class << self
Chris@1494 29 def client_command
Chris@1494 30 @@bin ||= DARCS_BIN
Chris@1494 31 end
Chris@1494 32
Chris@1494 33 def sq_bin
Chris@1494 34 @@sq_bin ||= shell_quote_command
Chris@1494 35 end
Chris@1494 36
Chris@1494 37 def client_version
Chris@1494 38 @@client_version ||= (darcs_binary_version || [])
Chris@1494 39 end
Chris@1494 40
Chris@1494 41 def client_available
Chris@1494 42 !client_version.empty?
Chris@1494 43 end
Chris@1494 44
Chris@1494 45 def darcs_binary_version
Chris@1494 46 darcsversion = darcs_binary_version_from_command_line.dup
Chris@1494 47 if darcsversion.respond_to?(:force_encoding)
Chris@1494 48 darcsversion.force_encoding('ASCII-8BIT')
Chris@1494 49 end
Chris@1494 50 if m = darcsversion.match(%r{\A(.*?)((\d+\.)+\d+)})
Chris@1494 51 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@1494 52 end
Chris@1494 53 end
Chris@1494 54
Chris@1494 55 def darcs_binary_version_from_command_line
Chris@1494 56 shellout("#{sq_bin} --version") { |io| io.read }.to_s
Chris@1494 57 end
Chris@1494 58 end
Chris@1494 59
Chris@1494 60 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@1494 61 path_encoding=nil)
Chris@1494 62 @url = url
Chris@1494 63 @root_url = url
Chris@1494 64 end
Chris@1494 65
Chris@1494 66 def supports_cat?
Chris@1494 67 # cat supported in darcs 2.0.0 and higher
Chris@1494 68 self.class.client_version_above?([2, 0, 0])
Chris@1494 69 end
Chris@1494 70
Chris@1494 71 # Get info about the darcs repository
Chris@1494 72 def info
Chris@1494 73 rev = revisions(nil,nil,nil,{:limit => 1})
Chris@1494 74 rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil
Chris@1494 75 end
Chris@1494 76
Chris@1494 77 # Returns an Entries collection
Chris@1494 78 # or nil if the given path doesn't exist in the repository
Chris@1494 79 def entries(path=nil, identifier=nil, options={})
Chris@1494 80 path_prefix = (path.blank? ? '' : "#{path}/")
Chris@1494 81 if path.blank?
Chris@1494 82 path = ( self.class.client_version_above?([2, 2, 0]) ? @url : '.' )
Chris@1494 83 end
Chris@1494 84 entries = Entries.new
Chris@1494 85 cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --xml-output"
Chris@1494 86 cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
Chris@1494 87 cmd << " #{shell_quote path}"
Chris@1494 88 shellout(cmd) do |io|
Chris@1494 89 begin
Chris@1494 90 doc = REXML::Document.new(io)
Chris@1494 91 if doc.root.name == 'directory'
Chris@1494 92 doc.elements.each('directory/*') do |element|
Chris@1494 93 next unless ['file', 'directory'].include? element.name
Chris@1494 94 entries << entry_from_xml(element, path_prefix)
Chris@1494 95 end
Chris@1494 96 elsif doc.root.name == 'file'
Chris@1494 97 entries << entry_from_xml(doc.root, path_prefix)
Chris@1494 98 end
Chris@1494 99 rescue
Chris@1494 100 end
Chris@1494 101 end
Chris@1494 102 return nil if $? && $?.exitstatus != 0
Chris@1494 103 entries.compact!
Chris@1494 104 entries.sort_by_name
Chris@1494 105 end
Chris@1494 106
Chris@1494 107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@1494 108 path = '.' if path.blank?
Chris@1494 109 revisions = Revisions.new
Chris@1494 110 cmd = "#{self.class.sq_bin} changes --repodir #{shell_quote @url} --xml-output"
Chris@1494 111 cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from
Chris@1494 112 cmd << " --last #{options[:limit].to_i}" if options[:limit]
Chris@1494 113 shellout(cmd) do |io|
Chris@1494 114 begin
Chris@1494 115 doc = REXML::Document.new(io)
Chris@1494 116 doc.elements.each("changelog/patch") do |patch|
Chris@1494 117 message = patch.elements['name'].text
Chris@1494 118 message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment']
Chris@1494 119 revisions << Revision.new({:identifier => nil,
Chris@1494 120 :author => patch.attributes['author'],
Chris@1494 121 :scmid => patch.attributes['hash'],
Chris@1494 122 :time => Time.parse(patch.attributes['local_date']),
Chris@1494 123 :message => message,
Chris@1494 124 :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil)
Chris@1494 125 })
Chris@1494 126 end
Chris@1494 127 rescue
Chris@1494 128 end
Chris@1494 129 end
Chris@1494 130 return nil if $? && $?.exitstatus != 0
Chris@1494 131 revisions
Chris@1494 132 end
Chris@1494 133
Chris@1494 134 def diff(path, identifier_from, identifier_to=nil)
Chris@1494 135 path = '*' if path.blank?
Chris@1494 136 cmd = "#{self.class.sq_bin} diff --repodir #{shell_quote @url}"
Chris@1494 137 if identifier_to.nil?
Chris@1494 138 cmd << " --match #{shell_quote("hash #{identifier_from}")}"
Chris@1494 139 else
Chris@1494 140 cmd << " --to-match #{shell_quote("hash #{identifier_from}")}"
Chris@1494 141 cmd << " --from-match #{shell_quote("hash #{identifier_to}")}"
Chris@1494 142 end
Chris@1494 143 cmd << " -u #{shell_quote path}"
Chris@1494 144 diff = []
Chris@1494 145 shellout(cmd) do |io|
Chris@1494 146 io.each_line do |line|
Chris@1494 147 diff << line
Chris@1494 148 end
Chris@1494 149 end
Chris@1494 150 return nil if $? && $?.exitstatus != 0
Chris@1494 151 diff
Chris@1494 152 end
Chris@1494 153
Chris@1494 154 def cat(path, identifier=nil)
Chris@1494 155 cmd = "#{self.class.sq_bin} show content --repodir #{shell_quote @url}"
Chris@1494 156 cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
Chris@1494 157 cmd << " #{shell_quote path}"
Chris@1494 158 cat = nil
Chris@1494 159 shellout(cmd) do |io|
Chris@1494 160 io.binmode
Chris@1494 161 cat = io.read
Chris@1494 162 end
Chris@1494 163 return nil if $? && $?.exitstatus != 0
Chris@1494 164 cat
Chris@1494 165 end
Chris@1494 166
Chris@1494 167 private
Chris@1494 168
Chris@1494 169 # Returns an Entry from the given XML element
Chris@1494 170 # or nil if the entry was deleted
Chris@1494 171 def entry_from_xml(element, path_prefix)
Chris@1494 172 modified_element = element.elements['modified']
Chris@1494 173 if modified_element.elements['modified_how'].text.match(/removed/)
Chris@1494 174 return nil
Chris@1494 175 end
Chris@1494 176
Chris@1494 177 Entry.new({:name => element.attributes['name'],
Chris@1494 178 :path => path_prefix + element.attributes['name'],
Chris@1494 179 :kind => element.name == 'file' ? 'file' : 'dir',
Chris@1494 180 :size => nil,
Chris@1494 181 :lastrev => Revision.new({
Chris@1494 182 :identifier => nil,
Chris@1494 183 :scmid => modified_element.elements['patch'].attributes['hash']
Chris@1494 184 })
Chris@1494 185 })
Chris@1494 186 end
Chris@1494 187
Chris@1494 188 def get_paths_for_patch(hash)
Chris@1494 189 paths = get_paths_for_patch_raw(hash)
Chris@1494 190 if self.class.client_version_above?([2, 4])
Chris@1494 191 orig_paths = paths
Chris@1494 192 paths = []
Chris@1494 193 add_paths = []
Chris@1494 194 add_paths_name = []
Chris@1494 195 mod_paths = []
Chris@1494 196 other_paths = []
Chris@1494 197 orig_paths.each do |path|
Chris@1494 198 if path[:action] == 'A'
Chris@1494 199 add_paths << path
Chris@1494 200 add_paths_name << path[:path]
Chris@1494 201 elsif path[:action] == 'M'
Chris@1494 202 mod_paths << path
Chris@1494 203 else
Chris@1494 204 other_paths << path
Chris@1494 205 end
Chris@1494 206 end
Chris@1494 207 add_paths_name.each do |add_path|
Chris@1494 208 mod_paths.delete_if { |m| m[:path] == add_path }
Chris@1494 209 end
Chris@1494 210 paths.concat add_paths
Chris@1494 211 paths.concat mod_paths
Chris@1494 212 paths.concat other_paths
Chris@1494 213 end
Chris@1494 214 paths
Chris@1494 215 end
Chris@1494 216
Chris@1494 217 # Retrieve changed paths for a single patch
Chris@1494 218 def get_paths_for_patch_raw(hash)
Chris@1494 219 cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --summary --xml-output"
Chris@1494 220 cmd << " --match #{shell_quote("hash #{hash}")} "
Chris@1494 221 paths = []
Chris@1494 222 shellout(cmd) do |io|
Chris@1494 223 begin
Chris@1494 224 # Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7)
Chris@1494 225 # A root element is added so that REXML doesn't raise an error
Chris@1494 226 doc = REXML::Document.new("<fake_root>" + io.read + "</fake_root>")
Chris@1494 227 doc.elements.each('fake_root/summary/*') do |modif|
Chris@1494 228 paths << {:action => modif.name[0,1].upcase,
Chris@1494 229 :path => "/" + modif.text.chomp.gsub(/^\s*/, '')
Chris@1494 230 }
Chris@1494 231 end
Chris@1494 232 rescue
Chris@1494 233 end
Chris@1494 234 end
Chris@1494 235 paths
Chris@1494 236 rescue CommandFailed
Chris@1494 237 paths
Chris@1494 238 end
Chris@1494 239 end
Chris@1494 240 end
Chris@1494 241 end
Chris@1494 242 end