annotate app/models/repository/git.rb @ 866:2cd212a468b6 bug_152

Close obsolete branch bug_152
author Chris Cannam
date Wed, 11 May 2011 10:08:34 +0100
parents 7c48bad7d85d
children b859cc0c4fa1
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
Chris@0 3 # Copyright (C) 2007 Patrick Aljord patcito@ŋmail.com
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'redmine/scm/adapters/git_adapter'
Chris@0 19
Chris@0 20 class Repository::Git < Repository
Chris@0 21 attr_protected :root_url
Chris@0 22 validates_presence_of :url
Chris@0 23
Chris@0 24 def scm_adapter
Chris@0 25 Redmine::Scm::Adapters::GitAdapter
Chris@0 26 end
Chris@0 27
Chris@0 28 def self.scm_name
Chris@0 29 'Git'
Chris@0 30 end
Chris@0 31
Chris@0 32 def branches
Chris@0 33 scm.branches
Chris@0 34 end
Chris@0 35
Chris@0 36 def tags
Chris@0 37 scm.tags
Chris@0 38 end
Chris@0 39
Chris@0 40 # With SCM's that have a sequential commit numbering, redmine is able to be
Chris@0 41 # clever and only fetch changesets going forward from the most recent one
Chris@0 42 # it knows about. However, with git, you never know if people have merged
Chris@0 43 # commits into the middle of the repository history, so we should parse
Chris@0 44 # the entire log. Since it's way too slow for large repositories, we only
Chris@0 45 # parse 1 week before the last known commit.
Chris@0 46 # The repository can still be fully reloaded by calling #clear_changesets
Chris@0 47 # before fetching changesets (eg. for offline resync)
Chris@0 48 def fetch_changesets
Chris@0 49 c = changesets.find(:first, :order => 'committed_on DESC')
Chris@0 50 since = (c ? c.committed_on - 7.days : nil)
Chris@0 51
Chris@3 52 revisions = scm.revisions('', nil, nil, :all => true, :since => since, :reverse => true)
Chris@0 53 return if revisions.nil? || revisions.empty?
Chris@0 54
Chris@0 55 recent_changesets = changesets.find(:all, :conditions => ['committed_on >= ?', since])
Chris@0 56
Chris@0 57 # Clean out revisions that are no longer in git
Chris@0 58 recent_changesets.each {|c| c.destroy unless revisions.detect {|r| r.scmid.to_s == c.scmid.to_s }}
Chris@0 59
Chris@0 60 # Subtract revisions that redmine already knows about
Chris@0 61 recent_revisions = recent_changesets.map{|c| c.scmid}
Chris@0 62 revisions.reject!{|r| recent_revisions.include?(r.scmid)}
Chris@0 63
Chris@0 64 # Save the remaining ones to the database
Chris@0 65 revisions.each{|r| r.save(self)} unless revisions.nil?
Chris@0 66 end
Chris@0 67
Chris@0 68 def latest_changesets(path,rev,limit=10)
Chris@0 69 revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
Chris@0 70 return [] if revisions.nil? || revisions.empty?
Chris@0 71
Chris@0 72 changesets.find(
Chris@0 73 :all,
Chris@0 74 :conditions => [
Chris@0 75 "scmid IN (?)",
Chris@0 76 revisions.map!{|c| c.scmid}
Chris@0 77 ],
Chris@3 78 :order => 'id DESC'
Chris@0 79 )
Chris@0 80 end
Chris@0 81 end