Chris@441: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: chris@1136: require_dependency 'redmine/scm/adapters/abstract_adapter' Chris@0: require 'rexml/document' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@245: module Adapters Chris@245: class DarcsAdapter < AbstractAdapter Chris@0: # Darcs executable name Chris@210: DARCS_BIN = Redmine::Configuration['scm_darcs_command'] || "darcs" Chris@245: Chris@0: class << self Chris@245: def client_command Chris@245: @@bin ||= DARCS_BIN Chris@245: end Chris@245: Chris@245: def sq_bin Chris@909: @@sq_bin ||= shell_quote_command Chris@245: end Chris@245: Chris@0: def client_version Chris@0: @@client_version ||= (darcs_binary_version || []) Chris@0: end Chris@245: Chris@245: def client_available Chris@245: !client_version.empty? Chris@245: end Chris@245: Chris@0: def darcs_binary_version Chris@245: darcsversion = darcs_binary_version_from_command_line.dup Chris@245: if darcsversion.respond_to?(:force_encoding) Chris@245: darcsversion.force_encoding('ASCII-8BIT') Chris@245: end Chris@210: if m = darcsversion.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@210: m[2].scan(%r{\d+}).collect(&:to_i) Chris@0: end Chris@210: end Chris@210: Chris@210: def darcs_binary_version_from_command_line Chris@245: shellout("#{sq_bin} --version") { |io| io.read }.to_s Chris@0: end Chris@0: end Chris@0: Chris@245: def initialize(url, root_url=nil, login=nil, password=nil, Chris@245: path_encoding=nil) Chris@0: @url = url Chris@0: @root_url = url Chris@0: end Chris@0: Chris@0: def supports_cat? Chris@0: # cat supported in darcs 2.0.0 and higher Chris@0: self.class.client_version_above?([2, 0, 0]) Chris@0: end Chris@0: Chris@0: # Get info about the darcs repository Chris@0: def info Chris@0: rev = revisions(nil,nil,nil,{:limit => 1}) Chris@0: rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil Chris@0: end Chris@245: Chris@0: # Returns an Entries collection Chris@0: # or nil if the given path doesn't exist in the repository Chris@441: def entries(path=nil, identifier=nil, options={}) Chris@0: path_prefix = (path.blank? ? '' : "#{path}/") Chris@210: if path.blank? Chris@210: path = ( self.class.client_version_above?([2, 2, 0]) ? @url : '.' ) Chris@210: end Chris@245: entries = Entries.new Chris@245: cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --xml-output" Chris@0: cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier Chris@0: cmd << " #{shell_quote path}" Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: doc = REXML::Document.new(io) Chris@0: if doc.root.name == 'directory' Chris@0: doc.elements.each('directory/*') do |element| Chris@0: next unless ['file', 'directory'].include? element.name Chris@0: entries << entry_from_xml(element, path_prefix) Chris@0: end Chris@0: elsif doc.root.name == 'file' Chris@0: entries << entry_from_xml(doc.root, path_prefix) Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@1115: entries.compact! Chris@1115: entries.sort_by_name Chris@0: end Chris@245: Chris@0: def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) Chris@0: path = '.' if path.blank? Chris@0: revisions = Revisions.new Chris@245: cmd = "#{self.class.sq_bin} changes --repodir #{shell_quote @url} --xml-output" Chris@0: cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from Chris@0: cmd << " --last #{options[:limit].to_i}" if options[:limit] Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: doc = REXML::Document.new(io) Chris@0: doc.elements.each("changelog/patch") do |patch| Chris@0: message = patch.elements['name'].text Chris@0: message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment'] Chris@0: revisions << Revision.new({:identifier => nil, Chris@0: :author => patch.attributes['author'], Chris@0: :scmid => patch.attributes['hash'], Chris@0: :time => Time.parse(patch.attributes['local_date']), Chris@0: :message => message, Chris@0: :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil) Chris@0: }) Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: revisions Chris@0: end Chris@245: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path = '*' if path.blank? Chris@245: cmd = "#{self.class.sq_bin} diff --repodir #{shell_quote @url}" Chris@0: if identifier_to.nil? Chris@0: cmd << " --match #{shell_quote("hash #{identifier_from}")}" Chris@0: else Chris@0: cmd << " --to-match #{shell_quote("hash #{identifier_from}")}" Chris@0: cmd << " --from-match #{shell_quote("hash #{identifier_to}")}" Chris@0: end Chris@0: cmd << " -u #{shell_quote path}" Chris@0: diff = [] Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: diff Chris@0: end Chris@245: Chris@0: def cat(path, identifier=nil) Chris@245: cmd = "#{self.class.sq_bin} show content --repodir #{shell_quote @url}" Chris@0: cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier Chris@0: cmd << " #{shell_quote path}" Chris@0: cat = nil Chris@0: shellout(cmd) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: cat Chris@0: end Chris@0: Chris@0: private Chris@245: Chris@0: # Returns an Entry from the given XML element Chris@0: # or nil if the entry was deleted Chris@0: def entry_from_xml(element, path_prefix) Chris@0: modified_element = element.elements['modified'] Chris@0: if modified_element.elements['modified_how'].text.match(/removed/) Chris@0: return nil Chris@0: end Chris@245: Chris@0: Entry.new({:name => element.attributes['name'], Chris@0: :path => path_prefix + element.attributes['name'], Chris@0: :kind => element.name == 'file' ? 'file' : 'dir', Chris@0: :size => nil, Chris@0: :lastrev => Revision.new({ Chris@0: :identifier => nil, Chris@0: :scmid => modified_element.elements['patch'].attributes['hash'] Chris@0: }) Chris@245: }) Chris@0: end Chris@210: Chris@210: def get_paths_for_patch(hash) Chris@210: paths = get_paths_for_patch_raw(hash) Chris@210: if self.class.client_version_above?([2, 4]) Chris@210: orig_paths = paths Chris@210: paths = [] Chris@210: add_paths = [] Chris@210: add_paths_name = [] Chris@210: mod_paths = [] Chris@210: other_paths = [] Chris@210: orig_paths.each do |path| Chris@210: if path[:action] == 'A' Chris@210: add_paths << path Chris@210: add_paths_name << path[:path] Chris@210: elsif path[:action] == 'M' Chris@210: mod_paths << path Chris@210: else Chris@210: other_paths << path Chris@210: end Chris@210: end Chris@210: add_paths_name.each do |add_path| Chris@210: mod_paths.delete_if { |m| m[:path] == add_path } Chris@210: end Chris@210: paths.concat add_paths Chris@210: paths.concat mod_paths Chris@210: paths.concat other_paths Chris@210: end Chris@210: paths Chris@210: end Chris@245: Chris@0: # Retrieve changed paths for a single patch Chris@210: def get_paths_for_patch_raw(hash) Chris@245: cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --summary --xml-output" Chris@0: cmd << " --match #{shell_quote("hash #{hash}")} " Chris@0: paths = [] Chris@0: shellout(cmd) do |io| Chris@0: begin Chris@0: # Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7) Chris@0: # A root element is added so that REXML doesn't raise an error Chris@0: doc = REXML::Document.new("" + io.read + "") Chris@0: doc.elements.each('fake_root/summary/*') do |modif| Chris@0: paths << {:action => modif.name[0,1].upcase, Chris@0: :path => "/" + modif.text.chomp.gsub(/^\s*/, '') Chris@0: } Chris@0: end Chris@0: rescue Chris@0: end Chris@0: end Chris@0: paths Chris@0: rescue CommandFailed Chris@0: paths Chris@0: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end