annotate lib/redmine/scm/adapters/git_adapter.rb @ 1477:f2ad2199b49a bibplugin_integration

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