annotate .svn/pristine/dc/dce2b25d87da51cebea469c60089fcd76b121f3d.svn-base @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@1517 1 # Redmine - project management software
Chris@1517 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1517 3 #
Chris@1517 4 # This program is free software; you can redistribute it and/or
Chris@1517 5 # modify it under the terms of the GNU General Public License
Chris@1517 6 # as published by the Free Software Foundation; either version 2
Chris@1517 7 # of the License, or (at your option) any later version.
Chris@1517 8 #
Chris@1517 9 # This program is distributed in the hope that it will be useful,
Chris@1517 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1517 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1517 12 # GNU General Public License for more details.
Chris@1517 13 #
Chris@1517 14 # You should have received a copy of the GNU General Public License
Chris@1517 15 # along with this program; if not, write to the Free Software
Chris@1517 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1517 17
Chris@1517 18 require 'cgi'
Chris@1517 19 require 'redmine/scm/adapters'
Chris@1517 20
Chris@1517 21 if RUBY_VERSION < '1.9'
Chris@1517 22 require 'iconv'
Chris@1517 23 end
Chris@1517 24
Chris@1517 25 module Redmine
Chris@1517 26 module Scm
Chris@1517 27 module Adapters
Chris@1517 28 class AbstractAdapter #:nodoc:
Chris@1517 29
Chris@1517 30 # raised if scm command exited with error, e.g. unknown revision.
Chris@1517 31 class ScmCommandAborted < CommandFailed; end
Chris@1517 32
Chris@1517 33 class << self
Chris@1517 34 def client_command
Chris@1517 35 ""
Chris@1517 36 end
Chris@1517 37
Chris@1517 38 def shell_quote_command
Chris@1517 39 if Redmine::Platform.mswin? && RUBY_PLATFORM == 'java'
Chris@1517 40 client_command
Chris@1517 41 else
Chris@1517 42 shell_quote(client_command)
Chris@1517 43 end
Chris@1517 44 end
Chris@1517 45
Chris@1517 46 # Returns the version of the scm client
Chris@1517 47 # Eg: [1, 5, 0] or [] if unknown
Chris@1517 48 def client_version
Chris@1517 49 []
Chris@1517 50 end
Chris@1517 51
Chris@1517 52 # Returns the version string of the scm client
Chris@1517 53 # Eg: '1.5.0' or 'Unknown version' if unknown
Chris@1517 54 def client_version_string
Chris@1517 55 v = client_version || 'Unknown version'
Chris@1517 56 v.is_a?(Array) ? v.join('.') : v.to_s
Chris@1517 57 end
Chris@1517 58
Chris@1517 59 # Returns true if the current client version is above
Chris@1517 60 # or equals the given one
Chris@1517 61 # If option is :unknown is set to true, it will return
Chris@1517 62 # true if the client version is unknown
Chris@1517 63 def client_version_above?(v, options={})
Chris@1517 64 ((client_version <=> v) >= 0) || (client_version.empty? && options[:unknown])
Chris@1517 65 end
Chris@1517 66
Chris@1517 67 def client_available
Chris@1517 68 true
Chris@1517 69 end
Chris@1517 70
Chris@1517 71 def shell_quote(str)
Chris@1517 72 if Redmine::Platform.mswin?
Chris@1517 73 '"' + str.gsub(/"/, '\\"') + '"'
Chris@1517 74 else
Chris@1517 75 "'" + str.gsub(/'/, "'\"'\"'") + "'"
Chris@1517 76 end
Chris@1517 77 end
Chris@1517 78 end
Chris@1517 79
Chris@1517 80 def initialize(url, root_url=nil, login=nil, password=nil,
Chris@1517 81 path_encoding=nil)
Chris@1517 82 @url = url
Chris@1517 83 @login = login if login && !login.empty?
Chris@1517 84 @password = (password || "") if @login
Chris@1517 85 @root_url = root_url.blank? ? retrieve_root_url : root_url
Chris@1517 86 end
Chris@1517 87
Chris@1517 88 def adapter_name
Chris@1517 89 'Abstract'
Chris@1517 90 end
Chris@1517 91
Chris@1517 92 def supports_cat?
Chris@1517 93 true
Chris@1517 94 end
Chris@1517 95
Chris@1517 96 def supports_annotate?
Chris@1517 97 respond_to?('annotate')
Chris@1517 98 end
Chris@1517 99
Chris@1517 100 def root_url
Chris@1517 101 @root_url
Chris@1517 102 end
Chris@1517 103
Chris@1517 104 def url
Chris@1517 105 @url
Chris@1517 106 end
Chris@1517 107
Chris@1517 108 def path_encoding
Chris@1517 109 nil
Chris@1517 110 end
Chris@1517 111
Chris@1517 112 # get info about the svn repository
Chris@1517 113 def info
Chris@1517 114 return nil
Chris@1517 115 end
Chris@1517 116
Chris@1517 117 # Returns the entry identified by path and revision identifier
Chris@1517 118 # or nil if entry doesn't exist in the repository
Chris@1517 119 def entry(path=nil, identifier=nil)
Chris@1517 120 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
Chris@1517 121 search_path = parts[0..-2].join('/')
Chris@1517 122 search_name = parts[-1]
Chris@1517 123 if search_path.blank? && search_name.blank?
Chris@1517 124 # Root entry
Chris@1517 125 Entry.new(:path => '', :kind => 'dir')
Chris@1517 126 else
Chris@1517 127 # Search for the entry in the parent directory
Chris@1517 128 es = entries(search_path, identifier)
Chris@1517 129 es ? es.detect {|e| e.name == search_name} : nil
Chris@1517 130 end
Chris@1517 131 end
Chris@1517 132
Chris@1517 133 # Returns an Entries collection
Chris@1517 134 # or nil if the given path doesn't exist in the repository
Chris@1517 135 def entries(path=nil, identifier=nil, options={})
Chris@1517 136 return nil
Chris@1517 137 end
Chris@1517 138
Chris@1517 139 def branches
Chris@1517 140 return nil
Chris@1517 141 end
Chris@1517 142
Chris@1517 143 def tags
Chris@1517 144 return nil
Chris@1517 145 end
Chris@1517 146
Chris@1517 147 def default_branch
Chris@1517 148 return nil
Chris@1517 149 end
Chris@1517 150
Chris@1517 151 def properties(path, identifier=nil)
Chris@1517 152 return nil
Chris@1517 153 end
Chris@1517 154
Chris@1517 155 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
Chris@1517 156 return nil
Chris@1517 157 end
Chris@1517 158
Chris@1517 159 def diff(path, identifier_from, identifier_to=nil)
Chris@1517 160 return nil
Chris@1517 161 end
Chris@1517 162
Chris@1517 163 def cat(path, identifier=nil)
Chris@1517 164 return nil
Chris@1517 165 end
Chris@1517 166
Chris@1517 167 def with_leading_slash(path)
Chris@1517 168 path ||= ''
Chris@1517 169 (path[0,1]!="/") ? "/#{path}" : path
Chris@1517 170 end
Chris@1517 171
Chris@1517 172 def with_trailling_slash(path)
Chris@1517 173 path ||= ''
Chris@1517 174 (path[-1,1] == "/") ? path : "#{path}/"
Chris@1517 175 end
Chris@1517 176
Chris@1517 177 def without_leading_slash(path)
Chris@1517 178 path ||= ''
Chris@1517 179 path.gsub(%r{^/+}, '')
Chris@1517 180 end
Chris@1517 181
Chris@1517 182 def without_trailling_slash(path)
Chris@1517 183 path ||= ''
Chris@1517 184 (path[-1,1] == "/") ? path[0..-2] : path
Chris@1517 185 end
Chris@1517 186
Chris@1517 187 def shell_quote(str)
Chris@1517 188 self.class.shell_quote(str)
Chris@1517 189 end
Chris@1517 190
Chris@1517 191 private
Chris@1517 192 def retrieve_root_url
Chris@1517 193 info = self.info
Chris@1517 194 info ? info.root_url : nil
Chris@1517 195 end
Chris@1517 196
Chris@1517 197 def target(path, sq=true)
Chris@1517 198 path ||= ''
Chris@1517 199 base = path.match(/^\//) ? root_url : url
Chris@1517 200 str = "#{base}/#{path}".gsub(/[?<>\*]/, '')
Chris@1517 201 if sq
Chris@1517 202 str = shell_quote(str)
Chris@1517 203 end
Chris@1517 204 str
Chris@1517 205 end
Chris@1517 206
Chris@1517 207 def logger
Chris@1517 208 self.class.logger
Chris@1517 209 end
Chris@1517 210
Chris@1517 211 def shellout(cmd, options = {}, &block)
Chris@1517 212 self.class.shellout(cmd, options, &block)
Chris@1517 213 end
Chris@1517 214
Chris@1517 215 def self.logger
Chris@1517 216 Rails.logger
Chris@1517 217 end
Chris@1517 218
Chris@1517 219 # Path to the file where scm stderr output is logged
Chris@1517 220 # Returns nil if the log file is not writable
Chris@1517 221 def self.stderr_log_file
Chris@1517 222 if @stderr_log_file.nil?
Chris@1517 223 writable = false
Chris@1517 224 path = Redmine::Configuration['scm_stderr_log_file'].presence
Chris@1517 225 path ||= Rails.root.join("log/#{Rails.env}.scm.stderr.log").to_s
Chris@1517 226 if File.exists?(path)
Chris@1517 227 if File.file?(path) && File.writable?(path)
Chris@1517 228 writable = true
Chris@1517 229 else
Chris@1517 230 logger.warn("SCM log file (#{path}) is not writable")
Chris@1517 231 end
Chris@1517 232 else
Chris@1517 233 begin
Chris@1517 234 File.open(path, "w") {}
Chris@1517 235 writable = true
Chris@1517 236 rescue => e
Chris@1517 237 logger.warn("SCM log file (#{path}) cannot be created: #{e.message}")
Chris@1517 238 end
Chris@1517 239 end
Chris@1517 240 @stderr_log_file = writable ? path : false
Chris@1517 241 end
Chris@1517 242 @stderr_log_file || nil
Chris@1517 243 end
Chris@1517 244
Chris@1517 245 def self.shellout(cmd, options = {}, &block)
Chris@1517 246 if logger && logger.debug?
Chris@1517 247 logger.debug "Shelling out: #{strip_credential(cmd)}"
Chris@1517 248 # Capture stderr in a log file
Chris@1517 249 if stderr_log_file
Chris@1517 250 cmd = "#{cmd} 2>>#{shell_quote(stderr_log_file)}"
Chris@1517 251 end
Chris@1517 252 end
Chris@1517 253 begin
Chris@1517 254 mode = "r+"
Chris@1517 255 IO.popen(cmd, mode) do |io|
Chris@1517 256 io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
Chris@1517 257 io.close_write unless options[:write_stdin]
Chris@1517 258 block.call(io) if block_given?
Chris@1517 259 end
Chris@1517 260 ## If scm command does not exist,
Chris@1517 261 ## Linux JRuby 1.6.2 (ruby-1.8.7-p330) raises java.io.IOException
Chris@1517 262 ## in production environment.
Chris@1517 263 # rescue Errno::ENOENT => e
Chris@1517 264 rescue Exception => e
Chris@1517 265 msg = strip_credential(e.message)
Chris@1517 266 # The command failed, log it and re-raise
Chris@1517 267 logmsg = "SCM command failed, "
Chris@1517 268 logmsg += "make sure that your SCM command (e.g. svn) is "
Chris@1517 269 logmsg += "in PATH (#{ENV['PATH']})\n"
Chris@1517 270 logmsg += "You can configure your scm commands in config/configuration.yml.\n"
Chris@1517 271 logmsg += "#{strip_credential(cmd)}\n"
Chris@1517 272 logmsg += "with: #{msg}"
Chris@1517 273 logger.error(logmsg)
Chris@1517 274 raise CommandFailed.new(msg)
Chris@1517 275 end
Chris@1517 276 end
Chris@1517 277
Chris@1517 278 # Hides username/password in a given command
Chris@1517 279 def self.strip_credential(cmd)
Chris@1517 280 q = (Redmine::Platform.mswin? ? '"' : "'")
Chris@1517 281 cmd.to_s.gsub(/(\-\-(password|username))\s+(#{q}[^#{q}]+#{q}|[^#{q}]\S+)/, '\\1 xxxx')
Chris@1517 282 end
Chris@1517 283
Chris@1517 284 def strip_credential(cmd)
Chris@1517 285 self.class.strip_credential(cmd)
Chris@1517 286 end
Chris@1517 287
Chris@1517 288 def scm_iconv(to, from, str)
Chris@1517 289 return nil if str.nil?
Chris@1517 290 return str if to == from
Chris@1517 291 if str.respond_to?(:force_encoding)
Chris@1517 292 str.force_encoding(from)
Chris@1517 293 begin
Chris@1517 294 str.encode(to)
Chris@1517 295 rescue Exception => err
Chris@1517 296 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@1517 297 nil
Chris@1517 298 end
Chris@1517 299 else
Chris@1517 300 begin
Chris@1517 301 Iconv.conv(to, from, str)
Chris@1517 302 rescue Iconv::Failure => err
Chris@1517 303 logger.error("failed to convert from #{from} to #{to}. #{err}")
Chris@1517 304 nil
Chris@1517 305 end
Chris@1517 306 end
Chris@1517 307 end
Chris@1517 308
Chris@1517 309 def parse_xml(xml)
Chris@1517 310 if RUBY_PLATFORM == 'java'
Chris@1517 311 xml = xml.sub(%r{<\?xml[^>]*\?>}, '')
Chris@1517 312 end
Chris@1517 313 ActiveSupport::XmlMini.parse(xml)
Chris@1517 314 end
Chris@1517 315 end
Chris@1517 316
Chris@1517 317 class Entries < Array
Chris@1517 318 def sort_by_name
Chris@1517 319 dup.sort! {|x,y|
Chris@1517 320 if x.kind == y.kind
Chris@1517 321 x.name.to_s <=> y.name.to_s
Chris@1517 322 else
Chris@1517 323 x.kind <=> y.kind
Chris@1517 324 end
Chris@1517 325 }
Chris@1517 326 end
Chris@1517 327
Chris@1517 328 def revisions
Chris@1517 329 revisions ||= Revisions.new(collect{|entry| entry.lastrev}.compact)
Chris@1517 330 end
Chris@1517 331 end
Chris@1517 332
Chris@1517 333 class Info
Chris@1517 334 attr_accessor :root_url, :lastrev
Chris@1517 335 def initialize(attributes={})
Chris@1517 336 self.root_url = attributes[:root_url] if attributes[:root_url]
Chris@1517 337 self.lastrev = attributes[:lastrev]
Chris@1517 338 end
Chris@1517 339 end
Chris@1517 340
Chris@1517 341 class Entry
Chris@1517 342 attr_accessor :name, :path, :kind, :size, :lastrev, :changeset
Chris@1517 343
Chris@1517 344 def initialize(attributes={})
Chris@1517 345 self.name = attributes[:name] if attributes[:name]
Chris@1517 346 self.path = attributes[:path] if attributes[:path]
Chris@1517 347 self.kind = attributes[:kind] if attributes[:kind]
Chris@1517 348 self.size = attributes[:size].to_i if attributes[:size]
Chris@1517 349 self.lastrev = attributes[:lastrev]
Chris@1517 350 end
Chris@1517 351
Chris@1517 352 def is_file?
Chris@1517 353 'file' == self.kind
Chris@1517 354 end
Chris@1517 355
Chris@1517 356 def is_dir?
Chris@1517 357 'dir' == self.kind
Chris@1517 358 end
Chris@1517 359
Chris@1517 360 def is_text?
Chris@1517 361 Redmine::MimeType.is_type?('text', name)
Chris@1517 362 end
Chris@1517 363
Chris@1517 364 def author
Chris@1517 365 if changeset
Chris@1517 366 changeset.author.to_s
Chris@1517 367 elsif lastrev
Chris@1517 368 Redmine::CodesetUtil.replace_invalid_utf8(lastrev.author.to_s.split('<').first)
Chris@1517 369 end
Chris@1517 370 end
Chris@1517 371 end
Chris@1517 372
Chris@1517 373 class Revisions < Array
Chris@1517 374 def latest
Chris@1517 375 sort {|x,y|
Chris@1517 376 unless x.time.nil? or y.time.nil?
Chris@1517 377 x.time <=> y.time
Chris@1517 378 else
Chris@1517 379 0
Chris@1517 380 end
Chris@1517 381 }.last
Chris@1517 382 end
Chris@1517 383 end
Chris@1517 384
Chris@1517 385 class Revision
Chris@1517 386 attr_accessor :scmid, :name, :author, :time, :message,
Chris@1517 387 :paths, :revision, :branch, :identifier,
Chris@1517 388 :parents
Chris@1517 389
Chris@1517 390 def initialize(attributes={})
Chris@1517 391 self.identifier = attributes[:identifier]
Chris@1517 392 self.scmid = attributes[:scmid]
Chris@1517 393 self.name = attributes[:name] || self.identifier
Chris@1517 394 self.author = attributes[:author]
Chris@1517 395 self.time = attributes[:time]
Chris@1517 396 self.message = attributes[:message] || ""
Chris@1517 397 self.paths = attributes[:paths]
Chris@1517 398 self.revision = attributes[:revision]
Chris@1517 399 self.branch = attributes[:branch]
Chris@1517 400 self.parents = attributes[:parents]
Chris@1517 401 end
Chris@1517 402
Chris@1517 403 # Returns the readable identifier.
Chris@1517 404 def format_identifier
Chris@1517 405 self.identifier.to_s
Chris@1517 406 end
Chris@1517 407
Chris@1517 408 def ==(other)
Chris@1517 409 if other.nil?
Chris@1517 410 false
Chris@1517 411 elsif scmid.present?
Chris@1517 412 scmid == other.scmid
Chris@1517 413 elsif identifier.present?
Chris@1517 414 identifier == other.identifier
Chris@1517 415 elsif revision.present?
Chris@1517 416 revision == other.revision
Chris@1517 417 end
Chris@1517 418 end
Chris@1517 419 end
Chris@1517 420
Chris@1517 421 class Annotate
Chris@1517 422 attr_reader :lines, :revisions
Chris@1517 423
Chris@1517 424 def initialize
Chris@1517 425 @lines = []
Chris@1517 426 @revisions = []
Chris@1517 427 end
Chris@1517 428
Chris@1517 429 def add_line(line, revision)
Chris@1517 430 @lines << line
Chris@1517 431 @revisions << revision
Chris@1517 432 end
Chris@1517 433
Chris@1517 434 def content
Chris@1517 435 content = lines.join("\n")
Chris@1517 436 end
Chris@1517 437
Chris@1517 438 def empty?
Chris@1517 439 lines.empty?
Chris@1517 440 end
Chris@1517 441 end
Chris@1517 442
Chris@1517 443 class Branch < String
Chris@1517 444 attr_accessor :revision, :scmid
Chris@1517 445 end
Chris@1517 446 end
Chris@1517 447 end
Chris@1517 448 end