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