Class: Redmine::Scm::Adapters::DarcsAdapter

Inherits:
AbstractAdapter show all
Defined in:
lib/redmine/scm/adapters/darcs_adapter.rb

Constant Summary

DARCS_BIN =

Darcs executable name

Redmine::Configuration['scm_darcs_command'] || "darcs"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAdapter

#adapter_name, #branches, client_version_above?, client_version_string, #default_branch, #entry, #path_encoding, #properties, #root_url, shell_quote, #shell_quote, shell_quote_command, #supports_annotate?, #tags, #url, #with_leading_slash, #with_trailling_slash, #without_leading_slash, #without_trailling_slash

Constructor Details

#initialize(url, root_url = nil, login = nil, password = nil, path_encoding = nil) ⇒ DarcsAdapter

Returns a new instance of DarcsAdapter



57
58
59
60
61
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 57

def initialize(url, root_url=nil, =nil, password=nil,
               path_encoding=nil)
  @url = url
  @root_url = url
end

Class Method Details

.client_availableObject



41
42
43
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 41

def client_available
  !client_version.empty?
end

.client_commandObject



29
30
31
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 29

def client_command
  @@bin    ||= DARCS_BIN
end

.client_versionObject



37
38
39
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 37

def client_version
  @@client_version ||= (darcs_binary_version || [])
end

.darcs_binary_versionObject



45
46
47
48
49
50
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 45

def darcs_binary_version
  darcsversion = darcs_binary_version_from_command_line.dup.force_encoding('ASCII-8BIT')
  if m = darcsversion.match(%r{\A(.*?)((\d+\.)+\d+)})
    m[2].scan(%r{\d+}).collect(&:to_i)
  end
end

.darcs_binary_version_from_command_lineObject



52
53
54
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 52

def darcs_binary_version_from_command_line
  shellout("#{sq_bin} --version") { |io| io.read }.to_s
end

.sq_binObject



33
34
35
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 33

def sq_bin
  @@sq_bin ||= shell_quote_command
end

Instance Method Details

#cat(path, identifier = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 151

def cat(path, identifier=nil)
  cmd = "#{self.class.sq_bin} show content --repodir #{shell_quote @url}"
  cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
  cmd << " #{shell_quote path}"
  cat = nil
  shellout(cmd) do |io|
    io.binmode
    cat = io.read
  end
  return nil if $? && $?.exitstatus != 0
  cat
end

#diff(path, identifier_from, identifier_to = nil) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 131

def diff(path, identifier_from, identifier_to=nil)
  path = '*' if path.blank?
  cmd = "#{self.class.sq_bin} diff --repodir #{shell_quote @url}"
  if identifier_to.nil?
    cmd << " --match #{shell_quote("hash #{identifier_from}")}"
  else
    cmd << " --to-match #{shell_quote("hash #{identifier_from}")}"
    cmd << " --from-match #{shell_quote("hash #{identifier_to}")}"
  end
  cmd << " -u #{shell_quote path}"
  diff = []
  shellout(cmd) do |io|
    io.each_line do |line|
      diff << line
    end
  end
  return nil if $? && $?.exitstatus != 0
  diff
end

#entries(path = nil, identifier = nil, options = {}) ⇒ Object

Returns an Entries collection or nil if the given path doesn't exist in the repository



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 76

def entries(path=nil, identifier=nil, options={})
  path_prefix = (path.blank? ? '' : "#{path}/")
  if path.blank?
    path = ( self.class.client_version_above?([2, 2, 0]) ? @url : '.' )
  end
  entries = Entries.new
  cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --xml-output"
  cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
  cmd << " #{shell_quote path}"
  shellout(cmd) do |io|
    begin
      doc = REXML::Document.new(io)
      if doc.root.name == 'directory'
        doc.elements.each('directory/*') do |element|
          next unless ['file', 'directory'].include? element.name
          entries << entry_from_xml(element, path_prefix)
        end
      elsif doc.root.name == 'file'
        entries << entry_from_xml(doc.root, path_prefix)
      end
    rescue
    end
  end
  return nil if $? && $?.exitstatus != 0
  entries.compact!
  entries.sort_by_name
end

#infoObject

Get info about the darcs repository



69
70
71
72
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 69

def info
  rev = revisions(nil,nil,nil,{:limit => 1})
  rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil
end

#revisions(path = nil, identifier_from = nil, identifier_to = nil, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 104

def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  path = '.' if path.blank?
  revisions = Revisions.new
  cmd = "#{self.class.sq_bin} changes --repodir #{shell_quote @url} --xml-output"
  cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from
  cmd << " --last #{options[:limit].to_i}" if options[:limit]
  shellout(cmd) do |io|
    begin
      doc = REXML::Document.new(io)
      doc.elements.each("changelog/patch") do |patch|
        message = patch.elements['name'].text
        message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment']
        revisions << Revision.new({:identifier => nil,
                      :author => patch.attributes['author'],
                      :scmid => patch.attributes['hash'],
                      :time => Time.parse(patch.attributes['local_date']),
                      :message => message,
                      :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil)
                    })
      end
    rescue
    end
  end
  return nil if $? && $?.exitstatus != 0
  revisions
end

#supports_cat?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/redmine/scm/adapters/darcs_adapter.rb', line 63

def supports_cat?
  # cat supported in darcs 2.0.0 and higher
  self.class.client_version_above?([2, 0, 0])
end