annotate .svn/pristine/33/3357225f79b9b7908f02d51c4dcc557f0e92041e.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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