annotate lib/redmine/scm/adapters/.svn/text-base/git_adapter.rb.svn-base @ 1176:7d9db6065048 bug_352

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