2008-03-12 23:28:49 +03:00
|
|
|
# redMine - project management software
|
|
|
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
require 'redmine/scm/adapters/abstract_adapter'
|
|
|
|
|
|
|
|
module Redmine
|
|
|
|
module Scm
|
|
|
|
module Adapters
|
|
|
|
class GitAdapter < AbstractAdapter
|
|
|
|
# Git executable name
|
2008-11-08 20:15:18 +03:00
|
|
|
GIT_BIN = "git"
|
2008-03-12 23:28:49 +03:00
|
|
|
|
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 = []
|
2009-08-16 02:41:40 +04:00
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} branch"
|
|
|
|
shellout(cmd) 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!
|
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
|
2009-08-16 02:41:40 +04:00
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} tag"
|
|
|
|
shellout(cmd) 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
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
|
|
|
branches.include?('master') ? 'master' : branches.first
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def entries(path=nil, identifier=nil)
|
|
|
|
path ||= ''
|
|
|
|
entries = Entries.new
|
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l "
|
|
|
|
cmd << shell_quote("HEAD:" + path) if identifier.nil?
|
|
|
|
cmd << shell_quote(identifier + ":" + path) if identifier
|
|
|
|
shellout(cmd) do |io|
|
|
|
|
io.each_line do |line|
|
|
|
|
e = line.chomp.to_s
|
|
|
|
if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/
|
|
|
|
type = $1
|
|
|
|
sha = $2
|
|
|
|
size = $3
|
|
|
|
name = $4
|
2009-08-16 02:41:40 +04:00
|
|
|
full_path = path.empty? ? name : "#{path}/#{name}"
|
2008-03-12 23:28:49 +03:00
|
|
|
entries << Entry.new({:name => name,
|
2009-08-16 02:41:40 +04:00
|
|
|
:path => full_path,
|
|
|
|
:kind => (type == "tree") ? 'dir' : 'file',
|
|
|
|
:size => (type == "tree") ? nil : size,
|
|
|
|
:lastrev => lastrev(full_path,identifier)
|
|
|
|
}) unless entries.detect{|entry| entry.name == name}
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil if $? && $?.exitstatus != 0
|
|
|
|
entries.sort_by_name
|
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
def lastrev(path,rev)
|
|
|
|
return nil if path.nil?
|
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} log --pretty=fuller --no-merges -n 1 "
|
|
|
|
cmd << " #{shell_quote rev} " if rev
|
|
|
|
cmd << "-- #{path} " unless path.empty?
|
|
|
|
shellout(cmd) do |io|
|
|
|
|
begin
|
|
|
|
id = io.gets.split[1]
|
|
|
|
author = io.gets.match('Author:\s+(.*)$')[1]
|
|
|
|
2.times { io.gets }
|
|
|
|
time = io.gets.match('CommitDate:\s+(.*)$')[1]
|
|
|
|
|
|
|
|
Revision.new({
|
|
|
|
:identifier => id,
|
|
|
|
:scmid => id,
|
|
|
|
:author => author,
|
|
|
|
:time => time,
|
|
|
|
:message => nil,
|
|
|
|
:paths => nil
|
|
|
|
})
|
|
|
|
rescue NoMethodError => e
|
|
|
|
logger.error("The revision '#{path}' has a wrong format")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def revisions(path, identifier_from, identifier_to, options={})
|
|
|
|
revisions = Revisions.new
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2010-08-08 11:07:30 +04:00
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw --date=iso --pretty=fuller "
|
|
|
|
cmd << " --reverse " if options[:reverse]
|
|
|
|
cmd << " --all " if options[:all]
|
2009-08-16 02:41:40 +04:00
|
|
|
cmd << " -n #{options[:limit]} " if options[:limit]
|
2010-08-08 11:07:30 +04:00
|
|
|
cmd << "#{shell_quote(identifier_from + '..')}" if identifier_from
|
|
|
|
cmd << "#{shell_quote identifier_to}" if identifier_to
|
2010-02-07 18:17:21 +03:00
|
|
|
cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since]
|
2009-08-16 02:41:40 +04:00
|
|
|
cmd << " -- #{path}" if path && !path.empty?
|
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
shellout(cmd) do |io|
|
|
|
|
files=[]
|
|
|
|
changeset = {}
|
|
|
|
parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
|
|
|
|
revno = 1
|
|
|
|
|
|
|
|
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 = []
|
|
|
|
revno = revno + 1
|
|
|
|
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) \
|
|
|
|
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
|
2008-03-12 23:28:49 +03:00
|
|
|
parsing_descr = 2
|
|
|
|
fileaction = $1
|
|
|
|
filepath = $2
|
|
|
|
files << {:action => fileaction, :path => filepath}
|
2009-08-16 02:41:40 +04:00
|
|
|
elsif (parsing_descr == 1 || parsing_descr == 2) \
|
|
|
|
&& line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\s+(.+)$/
|
|
|
|
parsing_descr = 2
|
|
|
|
fileaction = $1
|
|
|
|
filepath = $3
|
|
|
|
files << {:action => fileaction, :path => filepath}
|
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
|
2009-08-16 02:41:40 +04: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
|
|
|
|
|
|
|
|
return nil if $? && $?.exitstatus != 0
|
|
|
|
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 ||= ''
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
if identifier_to
|
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}"
|
|
|
|
else
|
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}"
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
cmd << " -- #{shell_quote path}" unless path.empty?
|
|
|
|
diff = []
|
|
|
|
shellout(cmd) do |io|
|
|
|
|
io.each_line do |line|
|
|
|
|
diff << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil if $? && $?.exitstatus != 0
|
2008-06-08 20:28:42 +04:00
|
|
|
diff
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def annotate(path, identifier=nil)
|
|
|
|
identifier = 'HEAD' if identifier.blank?
|
2010-02-28 15:09:09 +03:00
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}"
|
2008-03-12 23:28:49 +03:00
|
|
|
blame = Annotate.new
|
2008-03-19 20:51:13 +03:00
|
|
|
content = nil
|
|
|
|
shellout(cmd) { |io| io.binmode; content = io.read }
|
2008-03-12 23:28:49 +03:00
|
|
|
return nil if $? && $?.exitstatus != 0
|
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(.*)/
|
2010-03-04 00:15:46 +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
|
|
|
|
end
|
|
|
|
|
|
|
|
def cat(path, identifier=nil)
|
|
|
|
if identifier.nil?
|
|
|
|
identifier = 'HEAD'
|
|
|
|
end
|
|
|
|
cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
|
|
|
|
cat = nil
|
|
|
|
shellout(cmd) do |io|
|
|
|
|
io.binmode
|
|
|
|
cat = io.read
|
|
|
|
end
|
|
|
|
return nil if $? && $?.exitstatus != 0
|
|
|
|
cat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|