annotate lib/redmine/scm/adapters/git_adapter.rb @ 1480:75fd8eace091 issue_556

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