2008-03-12 23:28:49 +03:00
|
|
|
require 'redmine/scm/adapters/abstract_adapter'
|
|
|
|
|
|
|
|
module Redmine
|
|
|
|
module Scm
|
2011-02-14 09:14:34 +03:00
|
|
|
module Adapters
|
2008-03-12 23:28:49 +03:00
|
|
|
class GitAdapter < AbstractAdapter
|
2011-02-24 16:01:13 +03:00
|
|
|
|
|
|
|
SCM_GIT_REPORT_LAST_COMMIT = true
|
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
# Git executable name
|
2011-02-04 13:24:10 +03:00
|
|
|
GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
|
2008-03-12 23:28:49 +03:00
|
|
|
|
2011-02-20 04:33:30 +03:00
|
|
|
# raised if scm command exited with error, e.g. unknown revision.
|
|
|
|
class ScmCommandAborted < CommandFailed; end
|
|
|
|
|
2011-02-14 09:14:34 +03:00
|
|
|
class << self
|
|
|
|
def client_command
|
|
|
|
@@bin ||= GIT_BIN
|
|
|
|
end
|
|
|
|
|
|
|
|
def sq_bin
|
|
|
|
@@sq_bin ||= shell_quote(GIT_BIN)
|
|
|
|
end
|
|
|
|
|
2011-02-15 08:58:31 +03:00
|
|
|
def client_version
|
|
|
|
@@client_version ||= (scm_command_version || [])
|
|
|
|
end
|
|
|
|
|
2011-02-14 09:14:34 +03:00
|
|
|
def client_available
|
|
|
|
!client_version.empty?
|
2011-02-15 08:58:31 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def scm_command_version
|
2011-03-03 08:32:33 +03:00
|
|
|
scm_version = scm_version_from_command_line.dup
|
2011-02-22 11:05:51 +03:00
|
|
|
if scm_version.respond_to?(:force_encoding)
|
|
|
|
scm_version.force_encoding('ASCII-8BIT')
|
|
|
|
end
|
2011-02-15 08:58:31 +03:00
|
|
|
if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
|
|
|
|
m[2].scan(%r{\d+}).collect(&:to_i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def scm_version_from_command_line
|
2011-02-20 04:33:09 +03:00
|
|
|
shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s
|
2011-02-14 09:14:34 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-24 16:01:13 +03:00
|
|
|
def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
|
|
|
|
super
|
2011-03-08 06:29:36 +03:00
|
|
|
@path_encoding = path_encoding || 'UTF-8'
|
2011-02-24 16:01:13 +03:00
|
|
|
@flag_report_last_commit = SCM_GIT_REPORT_LAST_COMMIT
|
|
|
|
end
|
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
def info
|
|
|
|
begin
|
|
|
|
Info.new(:root_url => url, :lastrev => lastrev('',nil))
|
|
|
|
rescue
|
|
|
|
nil
|
2008-05-29 22:48:00 +04:00
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
def branches
|
2010-02-20 16:35:22 +03:00
|
|
|
return @branches if @branches
|
|
|
|
@branches = []
|
2011-03-08 08:18:04 +03:00
|
|
|
cmd_args = %w|branch --no-color|
|
|
|
|
scm_cmd(*cmd_args) do |io|
|
2008-03-12 23:28:49 +03:00
|
|
|
io.each_line do |line|
|
2010-02-20 16:35:22 +03:00
|
|
|
@branches << line.match('\s*\*?\s*(.*)$')[1]
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2010-02-20 16:35:22 +03:00
|
|
|
@branches.sort!
|
2011-03-08 08:18:04 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
def tags
|
2010-02-20 16:35:22 +03:00
|
|
|
return @tags if @tags
|
2011-03-08 09:10:53 +03:00
|
|
|
cmd_args = %w|tag|
|
|
|
|
scm_cmd(*cmd_args) do |io|
|
2010-02-20 16:35:22 +03:00
|
|
|
@tags = io.readlines.sort!.map{|t| t.strip}
|
2008-03-13 02:09:11 +03:00
|
|
|
end
|
2011-03-08 09:10:53 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
2011-03-15 10:14:19 +03:00
|
|
|
bras = self.branches
|
2011-03-19 18:50:08 +03:00
|
|
|
return nil if bras.nil?
|
2011-03-15 10:14:19 +03:00
|
|
|
bras.include?('master') ? 'master' : bras.first
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-02-20 04:33:30 +03:00
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def entries(path=nil, identifier=nil)
|
|
|
|
path ||= ''
|
2011-03-08 07:50:48 +03:00
|
|
|
p = scm_iconv(@path_encoding, 'UTF-8', path)
|
2008-03-12 23:28:49 +03:00
|
|
|
entries = Entries.new
|
2011-03-08 07:50:48 +03:00
|
|
|
cmd_args = %w|ls-tree -l|
|
|
|
|
cmd_args << "HEAD:#{p}" if identifier.nil?
|
|
|
|
cmd_args << "#{identifier}:#{p}" if identifier
|
|
|
|
scm_cmd(*cmd_args) do |io|
|
2008-03-12 23:28:49 +03:00
|
|
|
io.each_line do |line|
|
|
|
|
e = line.chomp.to_s
|
2010-09-27 01:54:52 +04:00
|
|
|
if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
|
2008-03-12 23:28:49 +03:00
|
|
|
type = $1
|
2011-03-08 07:50:48 +03:00
|
|
|
sha = $2
|
2008-03-12 23:28:49 +03:00
|
|
|
size = $3
|
|
|
|
name = $4
|
2011-03-08 16:22:53 +03:00
|
|
|
if name.respond_to?(:force_encoding)
|
|
|
|
name.force_encoding(@path_encoding)
|
|
|
|
end
|
2011-03-08 14:13:18 +03:00
|
|
|
full_path = p.empty? ? name : "#{p}/#{name}"
|
2011-03-08 07:50:48 +03:00
|
|
|
n = scm_iconv('UTF-8', @path_encoding, name)
|
|
|
|
full_p = scm_iconv('UTF-8', @path_encoding, full_path)
|
|
|
|
entries << Entry.new({:name => n,
|
|
|
|
:path => full_p,
|
2009-08-16 02:41:40 +04:00
|
|
|
:kind => (type == "tree") ? 'dir' : 'file',
|
|
|
|
:size => (type == "tree") ? nil : size,
|
2011-03-08 07:50:48 +03:00
|
|
|
:lastrev => @flag_report_last_commit ? lastrev(full_path, identifier) : Revision.new
|
2009-08-16 02:41:40 +04:00
|
|
|
}) unless entries.detect{|entry| entry.name == name}
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
entries.sort_by_name
|
2011-03-08 07:50:48 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2011-02-28 17:12:24 +03:00
|
|
|
def lastrev(path, rev)
|
2009-08-16 02:41:40 +04:00
|
|
|
return nil if path.nil?
|
2011-02-28 17:12:47 +03:00
|
|
|
cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
|
2011-02-28 17:12:24 +03:00
|
|
|
cmd_args << rev if rev
|
|
|
|
cmd_args << "--" << path unless path.empty?
|
2011-01-03 13:45:00 +03:00
|
|
|
lines = []
|
2011-02-28 17:12:24 +03:00
|
|
|
scm_cmd(*cmd_args) { |io| lines = io.readlines }
|
2011-01-03 13:45:00 +03:00
|
|
|
begin
|
|
|
|
id = lines[0].split[1]
|
|
|
|
author = lines[1].match('Author:\s+(.*)$')[1]
|
2011-02-14 11:45:54 +03:00
|
|
|
time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1])
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
Revision.new({
|
|
|
|
:identifier => id,
|
|
|
|
:scmid => id,
|
|
|
|
:author => author,
|
|
|
|
:time => time,
|
|
|
|
:message => nil,
|
|
|
|
:paths => nil
|
2011-02-28 17:12:24 +03:00
|
|
|
})
|
2011-01-03 13:45:00 +03:00
|
|
|
rescue NoMethodError => e
|
2009-08-16 02:41:40 +04:00
|
|
|
logger.error("The revision '#{path}' has a wrong format")
|
|
|
|
return nil
|
|
|
|
end
|
2011-02-28 17:12:24 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def revisions(path, identifier_from, identifier_to, options={})
|
|
|
|
revisions = Revisions.new
|
2011-02-28 17:12:47 +03:00
|
|
|
cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller|
|
2011-02-20 04:33:50 +03:00
|
|
|
cmd_args << "--reverse" if options[:reverse]
|
|
|
|
cmd_args << "--all" if options[:all]
|
|
|
|
cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
|
|
|
|
from_to = ""
|
|
|
|
from_to << "#{identifier_from}.." if identifier_from
|
|
|
|
from_to << "#{identifier_to}" if identifier_to
|
|
|
|
cmd_args << from_to if !from_to.empty?
|
|
|
|
cmd_args << "--since=#{options[:since].strftime("%Y-%m-%d %H:%M:%S")}" if options[:since]
|
2011-03-08 07:50:06 +03:00
|
|
|
cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2011-02-20 04:33:50 +03:00
|
|
|
scm_cmd *cmd_args do |io|
|
2008-03-12 23:28:49 +03:00
|
|
|
files=[]
|
|
|
|
changeset = {}
|
|
|
|
parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
|
|
|
|
|
|
|
|
io.each_line do |line|
|
|
|
|
if line =~ /^commit ([0-9a-f]{40})$/
|
|
|
|
key = "commit"
|
|
|
|
value = $1
|
|
|
|
if (parsing_descr == 1 || parsing_descr == 2)
|
|
|
|
parsing_descr = 0
|
2009-08-16 02:41:40 +04:00
|
|
|
revision = Revision.new({
|
|
|
|
:identifier => changeset[:commit],
|
|
|
|
:scmid => changeset[:commit],
|
|
|
|
:author => changeset[:author],
|
|
|
|
:time => Time.parse(changeset[:date]),
|
|
|
|
:message => changeset[:description],
|
|
|
|
:paths => files
|
|
|
|
})
|
2008-06-29 13:41:42 +04:00
|
|
|
if block_given?
|
|
|
|
yield revision
|
|
|
|
else
|
|
|
|
revisions << revision
|
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
changeset = {}
|
|
|
|
files = []
|
|
|
|
end
|
|
|
|
changeset[:commit] = $1
|
|
|
|
elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
|
|
|
|
key = $1
|
|
|
|
value = $2
|
|
|
|
if key == "Author"
|
|
|
|
changeset[:author] = value
|
2008-11-09 15:07:35 +03:00
|
|
|
elsif key == "CommitDate"
|
2008-03-12 23:28:49 +03:00
|
|
|
changeset[:date] = value
|
|
|
|
end
|
|
|
|
elsif (parsing_descr == 0) && line.chomp.to_s == ""
|
|
|
|
parsing_descr = 1
|
|
|
|
changeset[:description] = ""
|
2009-08-16 02:41:40 +04:00
|
|
|
elsif (parsing_descr == 1 || parsing_descr == 2) \
|
2011-03-08 06:29:11 +03:00
|
|
|
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
|
2008-03-12 23:28:49 +03:00
|
|
|
parsing_descr = 2
|
2011-03-08 06:29:11 +03:00
|
|
|
fileaction = $1
|
|
|
|
filepath = $2
|
2011-03-08 06:29:58 +03:00
|
|
|
p = scm_iconv('UTF-8', @path_encoding, filepath)
|
|
|
|
files << {:action => fileaction, :path => p}
|
2009-08-16 02:41:40 +04:00
|
|
|
elsif (parsing_descr == 1 || parsing_descr == 2) \
|
2011-03-08 06:29:11 +03:00
|
|
|
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
|
2009-08-16 02:41:40 +04:00
|
|
|
parsing_descr = 2
|
2011-03-08 06:29:11 +03:00
|
|
|
fileaction = $1
|
|
|
|
filepath = $3
|
2011-03-08 06:29:58 +03:00
|
|
|
p = scm_iconv('UTF-8', @path_encoding, filepath)
|
|
|
|
files << {:action => fileaction, :path => p}
|
2008-03-12 23:28:49 +03:00
|
|
|
elsif (parsing_descr == 1) && line.chomp.to_s == ""
|
|
|
|
parsing_descr = 2
|
|
|
|
elsif (parsing_descr == 1)
|
|
|
|
changeset[:description] << line[4..-1]
|
|
|
|
end
|
2011-03-08 07:50:28 +03:00
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
|
2008-06-29 13:41:42 +04:00
|
|
|
if changeset[:commit]
|
2009-08-16 02:41:40 +04:00
|
|
|
revision = Revision.new({
|
|
|
|
:identifier => changeset[:commit],
|
|
|
|
:scmid => changeset[:commit],
|
|
|
|
:author => changeset[:author],
|
|
|
|
:time => Time.parse(changeset[:date]),
|
|
|
|
:message => changeset[:description],
|
|
|
|
:paths => files
|
|
|
|
})
|
|
|
|
|
2008-06-29 13:41:42 +04:00
|
|
|
if block_given?
|
|
|
|
yield revision
|
|
|
|
else
|
|
|
|
revisions << revision
|
|
|
|
end
|
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-02-20 04:33:50 +03:00
|
|
|
revisions
|
|
|
|
rescue ScmCommandAborted
|
2008-03-12 23:28:49 +03:00
|
|
|
revisions
|
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2008-06-08 20:28:42 +04:00
|
|
|
def diff(path, identifier_from, identifier_to=nil)
|
2008-03-12 23:28:49 +03:00
|
|
|
path ||= ''
|
2011-03-08 07:50:28 +03:00
|
|
|
cmd_args = []
|
2009-08-16 02:41:40 +04:00
|
|
|
if identifier_to
|
2011-03-08 07:50:28 +03:00
|
|
|
cmd_args << "diff" << "--no-color" << identifier_to << identifier_from
|
2009-08-16 02:41:40 +04:00
|
|
|
else
|
2011-03-08 07:50:28 +03:00
|
|
|
cmd_args << "show" << "--no-color" << identifier_from
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-03-08 07:50:28 +03:00
|
|
|
cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty?
|
2008-03-12 23:28:49 +03:00
|
|
|
diff = []
|
2011-03-08 07:50:28 +03:00
|
|
|
scm_cmd *cmd_args do |io|
|
2008-03-12 23:28:49 +03:00
|
|
|
io.each_line do |line|
|
|
|
|
diff << line
|
|
|
|
end
|
|
|
|
end
|
2008-06-08 20:28:42 +04:00
|
|
|
diff
|
2011-03-08 07:50:28 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-03-08 08:17:22 +03:00
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def annotate(path, identifier=nil)
|
|
|
|
identifier = 'HEAD' if identifier.blank?
|
2011-03-08 08:17:22 +03:00
|
|
|
cmd_args = %w|blame|
|
|
|
|
cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path)
|
2008-03-12 23:28:49 +03:00
|
|
|
blame = Annotate.new
|
2008-03-19 20:51:13 +03:00
|
|
|
content = nil
|
2011-03-08 08:17:22 +03:00
|
|
|
scm_cmd(*cmd_args) { |io| io.binmode; content = io.read }
|
2008-03-19 20:51:13 +03:00
|
|
|
# git annotates binary files
|
|
|
|
return nil if content.is_binary_data?
|
2010-02-28 15:09:09 +03:00
|
|
|
identifier = ''
|
2010-03-04 00:15:46 +03:00
|
|
|
# git shows commit author on the first occurrence only
|
|
|
|
authors_by_commit = {}
|
2008-03-19 20:51:13 +03:00
|
|
|
content.split("\n").each do |line|
|
2010-02-28 15:09:09 +03:00
|
|
|
if line =~ /^([0-9a-f]{39,40})\s.*/
|
|
|
|
identifier = $1
|
|
|
|
elsif line =~ /^author (.+)/
|
2010-03-04 00:15:46 +03:00
|
|
|
authors_by_commit[identifier] = $1.strip
|
2010-02-28 15:09:09 +03:00
|
|
|
elsif line =~ /^\t(.*)/
|
2011-03-08 08:17:22 +03:00
|
|
|
blame.add_line($1, Revision.new(
|
|
|
|
:identifier => identifier,
|
|
|
|
:author => authors_by_commit[identifier]))
|
2010-02-28 15:09:09 +03:00
|
|
|
identifier = ''
|
|
|
|
author = ''
|
|
|
|
end
|
2008-03-19 20:51:13 +03:00
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
blame
|
2011-03-08 08:17:22 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-02-20 04:33:30 +03:00
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def cat(path, identifier=nil)
|
|
|
|
if identifier.nil?
|
|
|
|
identifier = 'HEAD'
|
|
|
|
end
|
2011-03-08 08:17:43 +03:00
|
|
|
cmd_args = %w|show --no-color|
|
|
|
|
cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
|
2008-03-12 23:28:49 +03:00
|
|
|
cat = nil
|
2011-03-08 08:17:43 +03:00
|
|
|
scm_cmd(*cmd_args) do |io|
|
2008-03-12 23:28:49 +03:00
|
|
|
io.binmode
|
|
|
|
cat = io.read
|
|
|
|
end
|
|
|
|
cat
|
2011-03-08 08:17:43 +03:00
|
|
|
rescue ScmCommandAborted
|
|
|
|
nil
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-01-02 12:45:05 +03:00
|
|
|
|
|
|
|
class Revision < Redmine::Scm::Adapters::Revision
|
|
|
|
# Returns the readable identifier
|
|
|
|
def format_identifier
|
|
|
|
identifier[0,8]
|
|
|
|
end
|
|
|
|
end
|
2011-02-20 04:33:30 +03:00
|
|
|
|
|
|
|
def scm_cmd(*args, &block)
|
|
|
|
repo_path = root_url || url
|
2011-03-09 02:30:29 +03:00
|
|
|
full_args = [GIT_BIN, '--git-dir', repo_path]
|
|
|
|
if self.class.client_version_above?([1, 7, 2])
|
|
|
|
full_args << '-c' << 'core.quotepath=false'
|
|
|
|
end
|
2011-02-20 04:33:30 +03:00
|
|
|
full_args += args
|
|
|
|
ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
|
|
|
|
if $? && $?.exitstatus != 0
|
|
|
|
raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"
|
|
|
|
end
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
private :scm_cmd
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|