Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require 'redmine/scm/adapters/git_adapter' Chris@0: Chris@0: class Repository::Git < Repository Chris@0: attr_protected :root_url Chris@0: validates_presence_of :url Chris@0: Chris@0: def scm_adapter Chris@0: Redmine::Scm::Adapters::GitAdapter Chris@0: end Chris@0: Chris@0: def self.scm_name Chris@0: 'Git' Chris@0: end Chris@0: Chris@117: # Returns the identifier for the given git changeset Chris@117: def self.changeset_identifier(changeset) Chris@117: changeset.scmid Chris@117: end Chris@117: Chris@117: # Returns the readable identifier for the given git changeset Chris@117: def self.format_changeset_identifier(changeset) Chris@117: changeset.revision[0, 8] Chris@117: end Chris@117: Chris@0: def branches Chris@0: scm.branches Chris@0: end Chris@0: Chris@0: def tags Chris@0: scm.tags Chris@0: end Chris@0: Chris@0: # With SCM's that have a sequential commit numbering, redmine is able to be Chris@0: # clever and only fetch changesets going forward from the most recent one Chris@0: # it knows about. However, with git, you never know if people have merged Chris@0: # commits into the middle of the repository history, so we should parse Chris@0: # the entire log. Since it's way too slow for large repositories, we only Chris@0: # parse 1 week before the last known commit. Chris@0: # The repository can still be fully reloaded by calling #clear_changesets Chris@0: # before fetching changesets (eg. for offline resync) Chris@0: def fetch_changesets Chris@0: c = changesets.find(:first, :order => 'committed_on DESC') Chris@0: since = (c ? c.committed_on - 7.days : nil) Chris@0: Chris@0: revisions = scm.revisions('', nil, nil, :all => true, :since => since) Chris@0: return if revisions.nil? || revisions.empty? Chris@0: Chris@0: recent_changesets = changesets.find(:all, :conditions => ['committed_on >= ?', since]) Chris@0: Chris@0: # Clean out revisions that are no longer in git Chris@0: recent_changesets.each {|c| c.destroy unless revisions.detect {|r| r.scmid.to_s == c.scmid.to_s }} Chris@0: Chris@0: # Subtract revisions that redmine already knows about Chris@0: recent_revisions = recent_changesets.map{|c| c.scmid} Chris@0: revisions.reject!{|r| recent_revisions.include?(r.scmid)} Chris@0: Chris@0: # Save the remaining ones to the database Chris@0: revisions.each{|r| r.save(self)} unless revisions.nil? Chris@0: end Chris@0: Chris@0: def latest_changesets(path,rev,limit=10) Chris@0: revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false) Chris@0: return [] if revisions.nil? || revisions.empty? Chris@0: Chris@0: changesets.find( Chris@0: :all, Chris@0: :conditions => [ Chris@0: "scmid IN (?)", Chris@0: revisions.map!{|c| c.scmid} Chris@0: ], Chris@0: :order => 'committed_on DESC' Chris@0: ) Chris@0: end Chris@0: end