2011-05-12 11:06:57 +04:00
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2011 Jean-Philippe Lang
|
2008-03-12 23:28:49 +03:00
|
|
|
# Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
|
2011-05-12 11:06:57 +04:00
|
|
|
#
|
2008-03-12 23:28:49 +03:00
|
|
|
# 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.
|
2011-05-12 11:06:57 +04:00
|
|
|
#
|
2008-03-12 23:28:49 +03:00
|
|
|
# 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.
|
2011-05-12 11:06:57 +04:00
|
|
|
#
|
2008-03-12 23:28:49 +03:00
|
|
|
# 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/git_adapter'
|
|
|
|
|
|
|
|
class Repository::Git < Repository
|
|
|
|
attr_protected :root_url
|
|
|
|
validates_presence_of :url
|
|
|
|
|
2011-03-01 08:14:03 +03:00
|
|
|
def self.human_attribute_name(attribute_key_name)
|
2011-04-23 11:37:03 +04:00
|
|
|
attr_name = attribute_key_name
|
|
|
|
if attr_name == "url"
|
|
|
|
attr_name = "path_to_repository"
|
|
|
|
end
|
|
|
|
super(attr_name)
|
2011-03-01 08:14:03 +03:00
|
|
|
end
|
|
|
|
|
2011-02-14 11:45:34 +03:00
|
|
|
def self.scm_adapter_class
|
2008-03-12 23:28:49 +03:00
|
|
|
Redmine::Scm::Adapters::GitAdapter
|
|
|
|
end
|
2011-02-14 11:45:34 +03:00
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def self.scm_name
|
|
|
|
'Git'
|
|
|
|
end
|
|
|
|
|
2011-05-13 07:55:21 +04:00
|
|
|
def report_last_commit
|
|
|
|
extra_report_last_commit
|
|
|
|
end
|
|
|
|
|
2011-05-06 07:43:11 +04:00
|
|
|
def extra_report_last_commit
|
2011-05-13 07:51:14 +04:00
|
|
|
return false if extra_info.nil?
|
|
|
|
v = extra_info["extra_report_last_commit"]
|
|
|
|
return false if v.nil?
|
|
|
|
v.to_s != '0'
|
2011-05-05 15:43:09 +04:00
|
|
|
end
|
|
|
|
|
2011-03-16 03:50:11 +03:00
|
|
|
def supports_directory_revisions?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2011-02-28 17:12:47 +03:00
|
|
|
def repo_log_encoding
|
|
|
|
'UTF-8'
|
|
|
|
end
|
|
|
|
|
2011-01-02 12:45:05 +03:00
|
|
|
# Returns the identifier for the given git changeset
|
|
|
|
def self.changeset_identifier(changeset)
|
|
|
|
changeset.scmid
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the readable identifier for the given git changeset
|
|
|
|
def self.format_changeset_identifier(changeset)
|
|
|
|
changeset.revision[0, 8]
|
|
|
|
end
|
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
def branches
|
|
|
|
scm.branches
|
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
|
|
|
scm.tags
|
|
|
|
end
|
|
|
|
|
2011-02-16 14:16:53 +03:00
|
|
|
def find_changeset_by_name(name)
|
|
|
|
return nil if name.nil? || name.empty?
|
|
|
|
e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
|
|
|
|
return e if e
|
|
|
|
changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"])
|
|
|
|
end
|
|
|
|
|
2011-05-05 15:42:46 +04:00
|
|
|
def entries(path=nil, identifier=nil)
|
2011-05-05 15:43:29 +04:00
|
|
|
scm.entries(path,
|
|
|
|
identifier,
|
2011-05-06 07:43:11 +04:00
|
|
|
options = {:report_last_commit => extra_report_last_commit})
|
2011-05-05 15:42:46 +04:00
|
|
|
end
|
|
|
|
|
2011-05-05 03:38:11 +04:00
|
|
|
# In Git and Mercurial, revisions are not in date order.
|
|
|
|
# Mercurial fixed issues.
|
|
|
|
# * Redmine Takes Too Long On Large Mercurial Repository
|
|
|
|
# http://www.redmine.org/issues/3449
|
2011-05-12 11:06:57 +04:00
|
|
|
# * Sorting for changesets might go wrong on Mercurial repos
|
2011-05-05 03:38:11 +04:00
|
|
|
# http://www.redmine.org/issues/3567
|
|
|
|
# Database revision column is text, so Redmine can not sort by revision.
|
|
|
|
# Mercurial has revision number, and revision number guarantees revision order.
|
|
|
|
# Mercurial adapter uses "hg log -r 0:tip --limit 10"
|
|
|
|
# to get limited revisions from old to new.
|
|
|
|
# And Mercurial model stored revisions ordered by database id in database.
|
|
|
|
# So, Mercurial can use correct order revisions.
|
|
|
|
#
|
|
|
|
# But, Git 1.7.3.4 does not support --reverse with -n or --skip.
|
|
|
|
#
|
2009-08-16 02:41:40 +04:00
|
|
|
# With SCM's that have a sequential commit numbering, redmine is able to be
|
|
|
|
# clever and only fetch changesets going forward from the most recent one
|
2011-05-05 03:36:57 +04:00
|
|
|
# it knows about.
|
|
|
|
# However, with git, you never know if people have merged
|
2010-02-07 18:17:21 +03:00
|
|
|
# commits into the middle of the repository history, so we should parse
|
2011-05-05 03:36:57 +04:00
|
|
|
# the entire log.
|
|
|
|
#
|
|
|
|
# Since it's way too slow for large repositories,
|
|
|
|
# we only parse 1 week before the last known commit.
|
|
|
|
#
|
2010-02-07 18:17:21 +03:00
|
|
|
# The repository can still be fully reloaded by calling #clear_changesets
|
|
|
|
# before fetching changesets (eg. for offline resync)
|
2008-03-12 23:28:49 +03:00
|
|
|
def fetch_changesets
|
2010-02-07 18:17:21 +03:00
|
|
|
c = changesets.find(:first, :order => 'committed_on DESC')
|
|
|
|
since = (c ? c.committed_on - 7.days : nil)
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2011-03-10 07:30:47 +03:00
|
|
|
revisions = scm.revisions('', nil, nil, {:all => true, :since => since, :reverse => true})
|
2009-08-16 02:41:40 +04:00
|
|
|
return if revisions.nil? || revisions.empty?
|
|
|
|
|
2010-02-07 18:17:21 +03:00
|
|
|
recent_changesets = changesets.find(:all, :conditions => ['committed_on >= ?', since])
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
# Clean out revisions that are no longer in git
|
2010-02-07 18:17:21 +03:00
|
|
|
recent_changesets.each {|c| c.destroy unless revisions.detect {|r| r.scmid.to_s == c.scmid.to_s }}
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
# Subtract revisions that redmine already knows about
|
2010-02-07 18:17:21 +03:00
|
|
|
recent_revisions = recent_changesets.map{|c| c.scmid}
|
|
|
|
revisions.reject!{|r| recent_revisions.include?(r.scmid)}
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
# Save the remaining ones to the database
|
2011-02-28 07:46:42 +03:00
|
|
|
unless revisions.nil?
|
|
|
|
revisions.each do |rev|
|
|
|
|
transaction do
|
2011-05-05 10:13:23 +04:00
|
|
|
save_revision(rev)
|
2011-05-04 07:44:10 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_revision(rev)
|
|
|
|
changeset = Changeset.new(
|
|
|
|
:repository => self,
|
|
|
|
:revision => rev.identifier,
|
|
|
|
:scmid => rev.scmid,
|
2011-05-12 11:06:57 +04:00
|
|
|
:committer => rev.author,
|
2011-02-28 07:46:42 +03:00
|
|
|
:committed_on => rev.time,
|
2011-05-04 07:44:10 +04:00
|
|
|
:comments => rev.message
|
|
|
|
)
|
|
|
|
if changeset.save
|
|
|
|
rev.paths.each do |file|
|
|
|
|
Change.create(
|
2011-02-28 07:46:42 +03:00
|
|
|
:changeset => changeset,
|
|
|
|
:action => file[:action],
|
|
|
|
:path => file[:path])
|
|
|
|
end
|
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
2011-05-04 07:44:10 +04:00
|
|
|
private :save_revision
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
def latest_changesets(path,rev,limit=10)
|
|
|
|
revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
|
|
|
|
return [] if revisions.nil? || revisions.empty?
|
2008-03-12 23:28:49 +03:00
|
|
|
|
2009-08-16 02:41:40 +04:00
|
|
|
changesets.find(
|
2011-05-12 11:06:57 +04:00
|
|
|
:all,
|
2009-08-16 02:41:40 +04:00
|
|
|
:conditions => [
|
2011-05-12 11:06:57 +04:00
|
|
|
"scmid IN (?)",
|
2009-08-16 02:41:40 +04:00
|
|
|
revisions.map!{|c| c.scmid}
|
|
|
|
],
|
|
|
|
:order => 'committed_on DESC'
|
|
|
|
)
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
end
|