Class: Redmine::Scm::Adapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Includes:
Utils::Shell
Defined in:
lib/redmine/scm/adapters/abstract_adapter.rb

Defined Under Namespace

Classes: ScmCommandAborted

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AbstractAdapter.


72
73
74
75
76
77
78
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 72

def initialize(url, root_url=nil, =nil, password=nil,
               path_encoding=nil)
  @url = url
  @login =  if  && !.empty?
  @password = (password || "") if @login
  @root_url = root_url.blank? ? retrieve_root_url : root_url
end

Class Method Details

.client_availableObject


67
68
69
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 67

def client_available
  true
end

.client_commandObject


34
35
36
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 34

def client_command
  ""
end

.client_versionObject

Returns the version of the scm client Eg: [1, 5, 0] or [] if unknown


48
49
50
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 48

def client_version
  []
end

.client_version_above?(v, options = {}) ⇒ Boolean

Returns true if the current client version is above or equals the given one If option is :unknown is set to true, it will return true if the client version is unknown

Returns:

  • (Boolean)

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

def client_version_above?(v, options={})
  ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
end

.client_version_stringObject

Returns the version string of the scm client Eg: '1.5.0' or 'Unknown version' if unknown


54
55
56
57
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 54

def client_version_string
  v = client_version || 'Unknown version'
  v.is_a?(Array) ? v.join('.') : v.to_s
end

.loggerObject


241
242
243
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 241

def logger
  Rails.logger
end

.shell_quote(str) ⇒ Object


38
39
40
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 38

def shell_quote(str)
  Redmine::Utils::Shell.shell_quote str
end

.shell_quote_commandObject


42
43
44
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 42

def shell_quote_command
  Redmine::Utils::Shell.shell_quote_command client_command
end

.shellout(cmd, options = {}, &block) ⇒ Object


245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 245

def shellout(cmd, options = {}, &block)
  if logger && logger.debug?
    logger.debug "Shelling out: #{strip_credential(cmd)}"
    # Capture stderr in a log file
    if stderr_log_file
      cmd = "#{cmd} 2>>#{shell_quote(stderr_log_file)}"
    end
  end
  begin
    mode = "r+"
    IO.popen(cmd, mode) do |io|
      io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
      io.close_write unless options[:write_stdin]
      yield(io) if block_given?
    end
  rescue => e
    msg = strip_credential(e.message)
    # The command failed, log it and re-raise
    logmsg = "SCM command failed, "
    logmsg += "make sure that your SCM command (e.g. svn) is "
    logmsg += "in PATH (#{ENV['PATH']})\n"
    logmsg += "You can configure your scm commands in config/configuration.yml.\n"
    logmsg += "#{strip_credential(cmd)}\n"
    logmsg += "with: #{msg}"
    logger.error(logmsg)
    raise CommandFailed.new(msg)
  end
end

Instance Method Details

#adapter_nameObject


80
81
82
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 80

def adapter_name
  'Abstract'
end

#branchesObject


131
132
133
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 131

def branches
  return nil
end

#cat(path, identifier = nil) ⇒ Object


155
156
157
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 155

def cat(path, identifier=nil)
  return nil
end

#default_branchObject


139
140
141
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 139

def default_branch
  return nil
end

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


151
152
153
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 151

def diff(path, identifier_from, identifier_to=nil)
  return nil
end

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

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


127
128
129
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 127

def entries(path=nil, identifier=nil, options={})
  return nil
end

#entry(path = nil, identifier = nil) ⇒ Object

Returns the entry identified by path and revision identifier or nil if entry doesn't exist in the repository


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 111

def entry(path=nil, identifier=nil)
  parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
  search_path = parts[0..-2].join('/')
  search_name = parts[-1]
  if search_path.blank? && search_name.blank?
    # Root entry
    Entry.new(:path => '', :kind => 'dir')
  else
    # Search for the entry in the parent directory
    es = entries(search_path, identifier)
    es ? es.detect {|e| e.name == search_name} : nil
  end
end

#infoObject

get info about the svn repository


105
106
107
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 105

def info
  return nil
end

#path_encodingObject


100
101
102
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 100

def path_encoding
  nil
end

#properties(path, identifier = nil) ⇒ Object


143
144
145
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 143

def properties(path, identifier=nil)
  return nil
end

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


147
148
149
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 147

def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
  return nil
end

#root_urlObject


92
93
94
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 92

def root_url
  @root_url
end

#supports_annotate?Boolean

Returns:

  • (Boolean)

88
89
90
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 88

def supports_annotate?
  respond_to?(:annotate)
end

#supports_cat?Boolean

Returns:

  • (Boolean)

84
85
86
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 84

def supports_cat?
  true
end

#tagsObject


135
136
137
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 135

def tags
  return nil
end

#urlObject


96
97
98
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 96

def url
  @url
end

#valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)

179
180
181
182
183
184
185
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 179

def valid_name?(name)
  return true if name.nil?
  return true if name.is_a?(Integer) && name > 0
  return true if name.is_a?(String) && name =~ /\A[0-9]*\z/

  false
end

#with_leading_slash(path) ⇒ Object


159
160
161
162
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 159

def with_leading_slash(path)
  path ||= ''
  (path[0, 1]!="/") ? "/#{path}" : path
end

#with_trailling_slash(path) ⇒ Object


164
165
166
167
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 164

def with_trailling_slash(path)
  path ||= ''
  (path[-1, 1] == "/") ? path : "#{path}/"
end

#without_leading_slash(path) ⇒ Object


169
170
171
172
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 169

def without_leading_slash(path)
  path ||= ''
  path.gsub(%r{^/+}, '')
end

#without_trailling_slash(path) ⇒ Object


174
175
176
177
# File 'lib/redmine/scm/adapters/abstract_adapter.rb', line 174

def without_trailling_slash(path)
  path ||= ''
  (path[-1, 1] == "/") ? path[0..-2] : path
end