annotate .svn/pristine/a5/a5a2bdba2e1db7153d66f6307d6658c7a79bbbc2.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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