Mercurial > hg > soundsoftware-site
comparison lib/redmine/scm/adapters/.svn/text-base/darcs_adapter.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | af80e5618e9b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # redMine - project management software | |
2 # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 # | |
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 # | |
9 # 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 # | |
14 # 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 require 'redmine/scm/adapters/abstract_adapter' | |
19 require 'rexml/document' | |
20 | |
21 module Redmine | |
22 module Scm | |
23 module Adapters | |
24 class DarcsAdapter < AbstractAdapter | |
25 # Darcs executable name | |
26 DARCS_BIN = "darcs" | |
27 | |
28 class << self | |
29 def client_version | |
30 @@client_version ||= (darcs_binary_version || []) | |
31 end | |
32 | |
33 def darcs_binary_version | |
34 cmd = "#{DARCS_BIN} --version" | |
35 version = nil | |
36 shellout(cmd) do |io| | |
37 # Read darcs version in first returned line | |
38 if m = io.gets.match(%r{((\d+\.)+\d+)}) | |
39 version = m[0].scan(%r{\d+}).collect(&:to_i) | |
40 end | |
41 end | |
42 return nil if $? && $?.exitstatus != 0 | |
43 version | |
44 end | |
45 end | |
46 | |
47 def initialize(url, root_url=nil, login=nil, password=nil) | |
48 @url = url | |
49 @root_url = url | |
50 end | |
51 | |
52 def supports_cat? | |
53 # cat supported in darcs 2.0.0 and higher | |
54 self.class.client_version_above?([2, 0, 0]) | |
55 end | |
56 | |
57 # Get info about the darcs repository | |
58 def info | |
59 rev = revisions(nil,nil,nil,{:limit => 1}) | |
60 rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil | |
61 end | |
62 | |
63 # Returns an Entries collection | |
64 # or nil if the given path doesn't exist in the repository | |
65 def entries(path=nil, identifier=nil) | |
66 path_prefix = (path.blank? ? '' : "#{path}/") | |
67 path = '.' if path.blank? | |
68 entries = Entries.new | |
69 cmd = "#{DARCS_BIN} annotate --repodir #{@url} --xml-output" | |
70 cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier | |
71 cmd << " #{shell_quote path}" | |
72 shellout(cmd) do |io| | |
73 begin | |
74 doc = REXML::Document.new(io) | |
75 if doc.root.name == 'directory' | |
76 doc.elements.each('directory/*') do |element| | |
77 next unless ['file', 'directory'].include? element.name | |
78 entries << entry_from_xml(element, path_prefix) | |
79 end | |
80 elsif doc.root.name == 'file' | |
81 entries << entry_from_xml(doc.root, path_prefix) | |
82 end | |
83 rescue | |
84 end | |
85 end | |
86 return nil if $? && $?.exitstatus != 0 | |
87 entries.compact.sort_by_name | |
88 end | |
89 | |
90 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) | |
91 path = '.' if path.blank? | |
92 revisions = Revisions.new | |
93 cmd = "#{DARCS_BIN} changes --repodir #{@url} --xml-output" | |
94 cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from | |
95 cmd << " --last #{options[:limit].to_i}" if options[:limit] | |
96 shellout(cmd) do |io| | |
97 begin | |
98 doc = REXML::Document.new(io) | |
99 doc.elements.each("changelog/patch") do |patch| | |
100 message = patch.elements['name'].text | |
101 message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment'] | |
102 revisions << Revision.new({:identifier => nil, | |
103 :author => patch.attributes['author'], | |
104 :scmid => patch.attributes['hash'], | |
105 :time => Time.parse(patch.attributes['local_date']), | |
106 :message => message, | |
107 :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil) | |
108 }) | |
109 end | |
110 rescue | |
111 end | |
112 end | |
113 return nil if $? && $?.exitstatus != 0 | |
114 revisions | |
115 end | |
116 | |
117 def diff(path, identifier_from, identifier_to=nil) | |
118 path = '*' if path.blank? | |
119 cmd = "#{DARCS_BIN} diff --repodir #{@url}" | |
120 if identifier_to.nil? | |
121 cmd << " --match #{shell_quote("hash #{identifier_from}")}" | |
122 else | |
123 cmd << " --to-match #{shell_quote("hash #{identifier_from}")}" | |
124 cmd << " --from-match #{shell_quote("hash #{identifier_to}")}" | |
125 end | |
126 cmd << " -u #{shell_quote path}" | |
127 diff = [] | |
128 shellout(cmd) do |io| | |
129 io.each_line do |line| | |
130 diff << line | |
131 end | |
132 end | |
133 return nil if $? && $?.exitstatus != 0 | |
134 diff | |
135 end | |
136 | |
137 def cat(path, identifier=nil) | |
138 cmd = "#{DARCS_BIN} show content --repodir #{@url}" | |
139 cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier | |
140 cmd << " #{shell_quote path}" | |
141 cat = nil | |
142 shellout(cmd) do |io| | |
143 io.binmode | |
144 cat = io.read | |
145 end | |
146 return nil if $? && $?.exitstatus != 0 | |
147 cat | |
148 end | |
149 | |
150 private | |
151 | |
152 # Returns an Entry from the given XML element | |
153 # or nil if the entry was deleted | |
154 def entry_from_xml(element, path_prefix) | |
155 modified_element = element.elements['modified'] | |
156 if modified_element.elements['modified_how'].text.match(/removed/) | |
157 return nil | |
158 end | |
159 | |
160 Entry.new({:name => element.attributes['name'], | |
161 :path => path_prefix + element.attributes['name'], | |
162 :kind => element.name == 'file' ? 'file' : 'dir', | |
163 :size => nil, | |
164 :lastrev => Revision.new({ | |
165 :identifier => nil, | |
166 :scmid => modified_element.elements['patch'].attributes['hash'] | |
167 }) | |
168 }) | |
169 end | |
170 | |
171 # Retrieve changed paths for a single patch | |
172 def get_paths_for_patch(hash) | |
173 cmd = "#{DARCS_BIN} annotate --repodir #{@url} --summary --xml-output" | |
174 cmd << " --match #{shell_quote("hash #{hash}")} " | |
175 paths = [] | |
176 shellout(cmd) do |io| | |
177 begin | |
178 # Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7) | |
179 # A root element is added so that REXML doesn't raise an error | |
180 doc = REXML::Document.new("<fake_root>" + io.read + "</fake_root>") | |
181 doc.elements.each('fake_root/summary/*') do |modif| | |
182 paths << {:action => modif.name[0,1].upcase, | |
183 :path => "/" + modif.text.chomp.gsub(/^\s*/, '') | |
184 } | |
185 end | |
186 rescue | |
187 end | |
188 end | |
189 paths | |
190 rescue CommandFailed | |
191 paths | |
192 end | |
193 end | |
194 end | |
195 end | |
196 end |