annotate .svn/pristine/54/54bd7e60efd3a104b0657fe44ac9bc64d28fe9fb.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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