annotate .svn/pristine/20/201ba8bb33fe7d932e25d4d032482548144aca3a.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require 'redmine/scm/adapters/abstract_adapter'
Chris@1494 19
Chris@1494 20 module Redmine
Chris@1494 21 module Scm
Chris@1494 22 module Adapters
Chris@1494 23 class GitAdapter < AbstractAdapter
Chris@1494 24
Chris@1494 25 # Git executable name
Chris@1494 26 GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
Chris@1494 27
Chris@1494 28 class GitBranch < Branch
Chris@1494 29 attr_accessor :is_default
Chris@1494 30 end
Chris@1494 31
Chris@1494 32 class << self
Chris@1494 33 def client_command
Chris@1494 34 @@bin ||= GIT_BIN
Chris@1494 35 end
Chris@1494 36
Chris@1494 37 def sq_bin
Chris@1494 38 @@sq_bin ||= shell_quote_command
Chris@1494 39 end
Chris@1494 40
Chris@1494 41 def client_version
Chris@1494 42 @@client_version ||= (scm_command_version || [])
Chris@1494 43 end
Chris@1494 44
Chris@1494 45 def client_available
Chris@1494 46 !client_version.empty?
Chris@1494 47 end
Chris@1494 48
Chris@1494 49 def scm_command_version
Chris@1494 50 scm_version = scm_version_from_command_line.dup
Chris@1494 51 if scm_version.respond_to?(:force_encoding)
Chris@1494 52 scm_version.force_encoding('ASCII-8BIT')
Chris@1494 53 end
Chris@1494 54 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
Chris@1494 55 m[2].scan(%r{\d+}).collect(&:to_i)
Chris@1494 56 end
Chris@1494 57 end
Chris@1494 58
Chris@1494 59 def scm_version_from_command_line
Chris@1494 60 shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s
Chris@1494 61 end
Chris@1494 62 end
Chris@1494 63
Chris@1494 64 def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
Chris@1494 65 super
Chris@1494 66 @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding
Chris@1494 67 end
Chris@1494 68
Chris@1494 69 def path_encoding
Chris@1494 70 @path_encoding
Chris@1494 71 end
Chris@1494 72
Chris@1494 73 def info
Chris@1494 74 begin
Chris@1494 75 Info.new(:root_url => url, :lastrev => lastrev('',nil))
Chris@1494 76 rescue
Chris@1494 77 nil
Chris@1494 78 end
Chris@1494 79 end
Chris@1494 80
Chris@1494 81 def branches
Chris@1494 82 return @branches if @branches
Chris@1494 83 @branches = []
Chris@1494 84 cmd_args = %w|branch --no-color --verbose --no-abbrev|
Chris@1494 85 git_cmd(cmd_args) do |io|
Chris@1494 86 io.each_line do |line|
Chris@1494 87 branch_rev = line.match('\s*(\*?)\s*(.*?)\s*([0-9a-f]{40}).*$')
Chris@1494 88 bran = GitBranch.new(branch_rev[2])
Chris@1494 89 bran.revision = branch_rev[3]
Chris@1494 90 bran.scmid = branch_rev[3]
Chris@1494 91 bran.is_default = ( branch_rev[1] == '*' )
Chris@1494 92 @branches << bran
Chris@1494 93 end
Chris@1494 94 end
Chris@1494 95 @branches.sort!
Chris@1494 96 rescue ScmCommandAborted
Chris@1494 97 nil
Chris@1494 98 end
Chris@1494 99
Chris@1494 100 def tags
Chris@1494 101 return @tags if @tags
Chris@1494 102 cmd_args = %w|tag|
Chris@1494 103 git_cmd(cmd_args) do |io|
Chris@1494 104 @tags = io.readlines.sort!.map{|t| t.strip}
Chris@1494 105 end
Chris@1494 106 rescue ScmCommandAborted
Chris@1494 107 nil
Chris@1494 108 end
Chris@1494 109
Chris@1494 110 def default_branch
Chris@1494 111 bras = self.branches
Chris@1494 112 return nil if bras.nil?
Chris@1494 113 default_bras = bras.select{|x| x.is_default == true}
Chris@1494 114 return default_bras.first.to_s if ! default_bras.empty?
Chris@1494 115 master_bras = bras.select{|x| x.to_s == 'master'}
Chris@1494 116 master_bras.empty? ? bras.first.to_s : 'master'
Chris@1494 117 end
Chris@1494 118
Chris@1494 119 def entry(path=nil, identifier=nil)
Chris@1494 120 parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?}
Chris@1494 121 search_path = parts[0..-2].join('/')
Chris@1494 122 search_name = parts[-1]
Chris@1494 123 if search_path.blank? && search_name.blank?
Chris@1494 124 # Root entry
Chris@1494 125 Entry.new(:path => '', :kind => 'dir')
Chris@1494 126 else
Chris@1494 127 # Search for the entry in the parent directory
Chris@1494 128 es = entries(search_path, identifier,
Chris@1494 129 options = {:report_last_commit => false})
Chris@1494 130 es ? es.detect {|e| e.name == search_name} : nil
Chris@1494 131 end
Chris@1494 132 end
Chris@1494 133
Chris@1494 134 def entries(path=nil, identifier=nil, options={})
Chris@1494 135 path ||= ''
Chris@1494 136 p = scm_iconv(@path_encoding, 'UTF-8', path)
Chris@1494 137 entries = Entries.new
Chris@1494 138 cmd_args = %w|ls-tree -l|
Chris@1494 139 cmd_args << "HEAD:#{p}" if identifier.nil?
Chris@1494 140 cmd_args << "#{identifier}:#{p}" if identifier
Chris@1494 141 git_cmd(cmd_args) do |io|
Chris@1494 142 io.each_line do |line|
Chris@1494 143 e = line.chomp.to_s
Chris@1494 144 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
Chris@1494 145 type = $1
Chris@1494 146 sha = $2
Chris@1494 147 size = $3
Chris@1494 148 name = $4
Chris@1494 149 if name.respond_to?(:force_encoding)
Chris@1494 150 name.force_encoding(@path_encoding)
Chris@1494 151 end
Chris@1494 152 full_path = p.empty? ? name : "#{p}/#{name}"
Chris@1494 153 n = scm_iconv('UTF-8', @path_encoding, name)
Chris@1494 154 full_p = scm_iconv('UTF-8', @path_encoding, full_path)
Chris@1494 155 entries << Entry.new({:name => n,
Chris@1494 156 :path => full_p,
Chris@1494 157 :kind => (type == "tree") ? 'dir' : 'file',
Chris@1494 158 :size => (type == "tree") ? nil : size,
Chris@1494 159 :lastrev => options[:report_last_commit] ?
Chris@1494 160 lastrev(full_path, identifier) : Revision.new
Chris@1494 161 }) unless entries.detect{|entry| entry.name == name}
Chris@1494 162 end
Chris@1494 163 end
Chris@1494 164 end
Chris@1494 165 entries.sort_by_name
Chris@1494 166 rescue ScmCommandAborted
Chris@1494 167 nil
Chris@1494 168 end
Chris@1494 169
Chris@1494 170 def lastrev(path, rev)
Chris@1494 171 return nil if path.nil?
Chris@1494 172 cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
Chris@1494 173 cmd_args << rev if rev
Chris@1494 174 cmd_args << "--" << path unless path.empty?
Chris@1494 175 lines = []
Chris@1494 176 git_cmd(cmd_args) { |io| lines = io.readlines }
Chris@1494 177 begin
Chris@1494 178 id = lines[0].split[1]
Chris@1494 179 author = lines[1].match('Author:\s+(.*)$')[1]
Chris@1494 180 time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1])
Chris@1494 181
Chris@1494 182 Revision.new({
Chris@1494 183 :identifier => id,
Chris@1494 184 :scmid => id,
Chris@1494 185 :author => author,
Chris@1494 186 :time => time,
Chris@1494 187 :message => nil,
Chris@1494 188 :paths => nil
Chris@1494 189 })
Chris@1494 190 rescue NoMethodError => e
Chris@1494 191 logger.error("The revision '#{path}' has a wrong format")
Chris@1494 192 return nil
Chris@1494 193 end
Chris@1494 194 rescue ScmCommandAborted
Chris@1494 195 nil
Chris@1494 196 end
Chris@1494 197
Chris@1494 198 def revisions(path, identifier_from, identifier_to, options={})
Chris@1494 199 revs = Revisions.new
Chris@1494 200 cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents --stdin|
Chris@1494 201 cmd_args << "--reverse" if options[:reverse]
Chris@1494 202 cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
Chris@1494 203 cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
Chris@1494 204 revisions = []
Chris@1494 205 if identifier_from || identifier_to
Chris@1494 206 revisions << ""
Chris@1494 207 revisions[0] << "#{identifier_from}.." if identifier_from
Chris@1494 208 revisions[0] << "#{identifier_to}" if identifier_to
Chris@1494 209 else
Chris@1494 210 unless options[:includes].blank?
Chris@1494 211 revisions += options[:includes]
Chris@1494 212 end
Chris@1494 213 unless options[:excludes].blank?
Chris@1494 214 revisions += options[:excludes].map{|r| "^#{r}"}
Chris@1494 215 end
Chris@1494 216 end
Chris@1494 217
Chris@1494 218 git_cmd(cmd_args, {:write_stdin => true}) do |io|
Chris@1494 219 io.binmode
Chris@1494 220 io.puts(revisions.join("\n"))
Chris@1494 221 io.close_write
Chris@1494 222 files=[]
Chris@1494 223 changeset = {}
Chris@1494 224 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
Chris@1494 225
Chris@1494 226 io.each_line do |line|
Chris@1494 227 if line =~ /^commit ([0-9a-f]{40})(( [0-9a-f]{40})*)$/
Chris@1494 228 key = "commit"
Chris@1494 229 value = $1
Chris@1494 230 parents_str = $2
Chris@1494 231 if (parsing_descr == 1 || parsing_descr == 2)
Chris@1494 232 parsing_descr = 0
Chris@1494 233 revision = Revision.new({
Chris@1494 234 :identifier => changeset[:commit],
Chris@1494 235 :scmid => changeset[:commit],
Chris@1494 236 :author => changeset[:author],
Chris@1494 237 :time => Time.parse(changeset[:date]),
Chris@1494 238 :message => changeset[:description],
Chris@1494 239 :paths => files,
Chris@1494 240 :parents => changeset[:parents]
Chris@1494 241 })
Chris@1494 242 if block_given?
Chris@1494 243 yield revision
Chris@1494 244 else
Chris@1494 245 revs << revision
Chris@1494 246 end
Chris@1494 247 changeset = {}
Chris@1494 248 files = []
Chris@1494 249 end
Chris@1494 250 changeset[:commit] = $1
Chris@1494 251 unless parents_str.nil? or parents_str == ""
Chris@1494 252 changeset[:parents] = parents_str.strip.split(' ')
Chris@1494 253 end
Chris@1494 254 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
Chris@1494 255 key = $1
Chris@1494 256 value = $2
Chris@1494 257 if key == "Author"
Chris@1494 258 changeset[:author] = value
Chris@1494 259 elsif key == "CommitDate"
Chris@1494 260 changeset[:date] = value
Chris@1494 261 end
Chris@1494 262 elsif (parsing_descr == 0) && line.chomp.to_s == ""
Chris@1494 263 parsing_descr = 1
Chris@1494 264 changeset[:description] = ""
Chris@1494 265 elsif (parsing_descr == 1 || parsing_descr == 2) \
Chris@1494 266 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
Chris@1494 267 parsing_descr = 2
Chris@1494 268 fileaction = $1
Chris@1494 269 filepath = $2
Chris@1494 270 p = scm_iconv('UTF-8', @path_encoding, filepath)
Chris@1494 271 files << {:action => fileaction, :path => p}
Chris@1494 272 elsif (parsing_descr == 1 || parsing_descr == 2) \
Chris@1494 273 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
Chris@1494 274 parsing_descr = 2
Chris@1494 275 fileaction = $1
Chris@1494 276 filepath = $3
Chris@1494 277 p = scm_iconv('UTF-8', @path_encoding, filepath)
Chris@1494 278 files << {:action => fileaction, :path => p}
Chris@1494 279 elsif (parsing_descr == 1) && line.chomp.to_s == ""
Chris@1494 280 parsing_descr = 2
Chris@1494 281 elsif (parsing_descr == 1)
Chris@1494 282 changeset[:description] << line[4..-1]
Chris@1494 283 end
Chris@1494 284 end
Chris@1494 285
Chris@1494 286 if changeset[:commit]
Chris@1494 287 revision = Revision.new({
Chris@1494 288 :identifier => changeset[:commit],
Chris@1494 289 :scmid => changeset[:commit],
Chris@1494 290 :author => changeset[:author],
Chris@1494 291 :time => Time.parse(changeset[:date]),
Chris@1494 292 :message => changeset[:description],
Chris@1494 293 :paths => files,
Chris@1494 294 :parents => changeset[:parents]
Chris@1494 295 })
Chris@1494 296 if block_given?
Chris@1494 297 yield revision
Chris@1494 298 else
Chris@1494 299 revs << revision
Chris@1494 300 end
Chris@1494 301 end
Chris@1494 302 end
Chris@1494 303 revs
Chris@1494 304 rescue ScmCommandAborted => e
Chris@1494 305 err_msg = "git log error: #{e.message}"
Chris@1494 306 logger.error(err_msg)
Chris@1494 307 if block_given?
Chris@1494 308 raise CommandFailed, err_msg
Chris@1494 309 else
Chris@1494 310 revs
Chris@1494 311 end
Chris@1494 312 end
Chris@1494 313
Chris@1494 314 def diff(path, identifier_from, identifier_to=nil)
Chris@1494 315 path ||= ''
Chris@1494 316 cmd_args = []
Chris@1494 317 if identifier_to
Chris@1494 318 cmd_args << "diff" << "--no-color" << identifier_to << identifier_from
Chris@1494 319 else
Chris@1494 320 cmd_args << "show" << "--no-color" << identifier_from
Chris@1494 321 end
Chris@1494 322 cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty?
Chris@1494 323 diff = []
Chris@1494 324 git_cmd(cmd_args) do |io|
Chris@1494 325 io.each_line do |line|
Chris@1494 326 diff << line
Chris@1494 327 end
Chris@1494 328 end
Chris@1494 329 diff
Chris@1494 330 rescue ScmCommandAborted
Chris@1494 331 nil
Chris@1494 332 end
Chris@1494 333
Chris@1494 334 def annotate(path, identifier=nil)
Chris@1494 335 identifier = 'HEAD' if identifier.blank?
Chris@1494 336 cmd_args = %w|blame --encoding=UTF-8|
Chris@1494 337 cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path)
Chris@1494 338 blame = Annotate.new
Chris@1494 339 content = nil
Chris@1494 340 git_cmd(cmd_args) { |io| io.binmode; content = io.read }
Chris@1494 341 # git annotates binary files
Chris@1494 342 return nil if content.is_binary_data?
Chris@1494 343 identifier = ''
Chris@1494 344 # git shows commit author on the first occurrence only
Chris@1494 345 authors_by_commit = {}
Chris@1494 346 content.split("\n").each do |line|
Chris@1494 347 if line =~ /^([0-9a-f]{39,40})\s.*/
Chris@1494 348 identifier = $1
Chris@1494 349 elsif line =~ /^author (.+)/
Chris@1494 350 authors_by_commit[identifier] = $1.strip
Chris@1494 351 elsif line =~ /^\t(.*)/
Chris@1494 352 blame.add_line($1, Revision.new(
Chris@1494 353 :identifier => identifier,
Chris@1494 354 :revision => identifier,
Chris@1494 355 :scmid => identifier,
Chris@1494 356 :author => authors_by_commit[identifier]
Chris@1494 357 ))
Chris@1494 358 identifier = ''
Chris@1494 359 author = ''
Chris@1494 360 end
Chris@1494 361 end
Chris@1494 362 blame
Chris@1494 363 rescue ScmCommandAborted
Chris@1494 364 nil
Chris@1494 365 end
Chris@1494 366
Chris@1494 367 def cat(path, identifier=nil)
Chris@1494 368 if identifier.nil?
Chris@1494 369 identifier = 'HEAD'
Chris@1494 370 end
Chris@1494 371 cmd_args = %w|show --no-color|
Chris@1494 372 cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
Chris@1494 373 cat = nil
Chris@1494 374 git_cmd(cmd_args) do |io|
Chris@1494 375 io.binmode
Chris@1494 376 cat = io.read
Chris@1494 377 end
Chris@1494 378 cat
Chris@1494 379 rescue ScmCommandAborted
Chris@1494 380 nil
Chris@1494 381 end
Chris@1494 382
Chris@1494 383 class Revision < Redmine::Scm::Adapters::Revision
Chris@1494 384 # Returns the readable identifier
Chris@1494 385 def format_identifier
Chris@1494 386 identifier[0,8]
Chris@1494 387 end
Chris@1494 388 end
Chris@1494 389
Chris@1494 390 def git_cmd(args, options = {}, &block)
Chris@1494 391 repo_path = root_url || url
Chris@1494 392 full_args = ['--git-dir', repo_path]
Chris@1494 393 if self.class.client_version_above?([1, 7, 2])
Chris@1494 394 full_args << '-c' << 'core.quotepath=false'
Chris@1494 395 full_args << '-c' << 'log.decorate=no'
Chris@1494 396 end
Chris@1494 397 full_args += args
Chris@1494 398 ret = shellout(
Chris@1494 399 self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
Chris@1494 400 options,
Chris@1494 401 &block
Chris@1494 402 )
Chris@1494 403 if $? && $?.exitstatus != 0
Chris@1494 404 raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"
Chris@1494 405 end
Chris@1494 406 ret
Chris@1494 407 end
Chris@1494 408 private :git_cmd
Chris@1494 409 end
Chris@1494 410 end
Chris@1494 411 end
Chris@1494 412 end