annotate .svn/pristine/54/54bd7e60efd3a104b0657fe44ac9bc64d28fe9fb.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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