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