OSDN Git Service

add Redmine trunk rev 3089
[redminele/redminele.git] / redmine / app / models / repository / git.rb
1 # redMine - project management software
2 # Copyright (C) 2006-2007  Jean-Philippe Lang
3 # Copyright (C) 2007  Patrick Aljord patcito@Ĺ‹mail.com
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 require 'redmine/scm/adapters/git_adapter'
19
20 class Repository::Git < Repository
21   attr_protected :root_url
22   validates_presence_of :url
23
24   def scm_adapter
25     Redmine::Scm::Adapters::GitAdapter
26   end
27   
28   def self.scm_name
29     'Git'
30   end
31
32   def branches
33     scm.branches
34   end
35
36   def tags
37     scm.tags
38   end
39
40   # With SCM's that have a sequential commit numbering, redmine is able to be
41   # clever and only fetch changesets going forward from the most recent one
42   # it knows about.  However, with git, you never know if people have merged
43   # commits into the middle of the repository history, so we always have to
44   # parse the entire log.
45   def fetch_changesets
46     # Save ourselves an expensive operation if we're already up to date
47     return if scm.num_revisions == changesets.count
48
49     revisions = scm.revisions('', nil, nil, :all => true)
50     return if revisions.nil? || revisions.empty?
51
52     # Find revisions that redmine knows about already
53     existing_revisions = changesets.find(:all).map!{|c| c.scmid}
54
55     # Clean out revisions that are no longer in git
56     Changeset.delete_all(["scmid NOT IN (?) AND repository_id = (?)", revisions.map{|r| r.scmid}, self.id])
57
58     # Subtract revisions that redmine already knows about
59     revisions.reject!{|r| existing_revisions.include?(r.scmid)}
60
61     # Save the remaining ones to the database
62     revisions.each{|r| r.save(self)} unless revisions.nil?
63   end
64
65   def latest_changesets(path,rev,limit=10)
66     revisions = scm.revisions(path, nil, rev, :limit => limit, :all => false)
67     return [] if revisions.nil? || revisions.empty?
68
69     changesets.find(
70       :all, 
71       :conditions => [
72         "scmid IN (?)", 
73         revisions.map!{|c| c.scmid}
74       ],
75       :order => 'committed_on DESC'
76     )
77   end
78 end