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