2008-03-12 23:28:49 +03:00
|
|
|
# redMine - project management software
|
|
|
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
|
|
|
# Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
|
|
|
|
# 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/git_adapter'
|
|
|
|
|
|
|
|
class Repository::Git < Repository
|
|
|
|
attr_protected :root_url
|
|
|
|
validates_presence_of :url
|
|
|
|
|
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-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
|
|
|
|
|
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
|
|
|
|
# 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
|
|
|
|
# the entire log. Since it's way too slow for large repositories, we only
|
|
|
|
# parse 1 week before the last known commit.
|
|
|
|
# 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
|
|
|
|
2010-02-07 18:17:21 +03:00
|
|
|
revisions = scm.revisions('', nil, nil, :all => true, :since => since)
|
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
|
|
|
|
revisions.each{|r| r.save(self)} unless revisions.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
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(
|
|
|
|
:all,
|
|
|
|
:conditions => [
|
|
|
|
"scmid IN (?)",
|
|
|
|
revisions.map!{|c| c.scmid}
|
|
|
|
],
|
|
|
|
:order => 'committed_on DESC'
|
|
|
|
)
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
end
|