annotate lib/redmine/scm/adapters/.svn/text-base/git_adapter.rb.svn-base @ 245:051f544170fe

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