Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 #
|
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/mercurial_adapter'
|
Chris@0
|
19
|
Chris@0
|
20 class Repository::Mercurial < Repository
|
Chris@117
|
21 # sort changesets by revision number
|
Chris@117
|
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
|
Chris@117
|
23
|
Chris@0
|
24 attr_protected :root_url
|
Chris@0
|
25 validates_presence_of :url
|
Chris@0
|
26
|
Chris@0
|
27 def scm_adapter
|
Chris@0
|
28 Redmine::Scm::Adapters::MercurialAdapter
|
Chris@0
|
29 end
|
Chris@0
|
30
|
Chris@0
|
31 def self.scm_name
|
Chris@0
|
32 'Mercurial'
|
Chris@0
|
33 end
|
Chris@0
|
34
|
Chris@0
|
35 def entries(path=nil, identifier=nil)
|
Chris@0
|
36 entries=scm.entries(path, identifier)
|
Chris@0
|
37 if entries
|
Chris@0
|
38 entries.each do |entry|
|
Chris@0
|
39 next unless entry.is_file?
|
Chris@0
|
40 # Set the filesize unless browsing a specific revision
|
Chris@0
|
41 if identifier.nil?
|
Chris@0
|
42 full_path = File.join(root_url, entry.path)
|
Chris@0
|
43 entry.size = File.stat(full_path).size if File.file?(full_path)
|
Chris@0
|
44 end
|
Chris@0
|
45 # Search the DB for the entry's last change
|
Chris@0
|
46 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
|
Chris@0
|
47 if change
|
Chris@0
|
48 entry.lastrev.identifier = change.changeset.revision
|
Chris@0
|
49 entry.lastrev.name = change.changeset.revision
|
Chris@0
|
50 entry.lastrev.author = change.changeset.committer
|
Chris@0
|
51 entry.lastrev.revision = change.revision
|
Chris@0
|
52 end
|
Chris@0
|
53 end
|
Chris@0
|
54 end
|
Chris@0
|
55 entries
|
Chris@0
|
56 end
|
Chris@0
|
57
|
Chris@117
|
58 # Returns the latest changesets for +path+; sorted by revision number
|
Chris@117
|
59 def latest_changesets(path, rev, limit=10)
|
Chris@117
|
60 if path.blank?
|
Chris@117
|
61 changesets.find(:all, :include => :user, :limit => limit)
|
Chris@117
|
62 else
|
Chris@117
|
63 changes.find(:all, :include => {:changeset => :user},
|
Chris@117
|
64 :conditions => ["path = ?", path.with_leading_slash],
|
Chris@117
|
65 :order => "#{Changeset.table_name}.id DESC",
|
Chris@117
|
66 :limit => limit).collect(&:changeset)
|
Chris@117
|
67 end
|
Chris@117
|
68 end
|
Chris@117
|
69
|
Chris@0
|
70 def fetch_changesets
|
Chris@0
|
71 scm_info = scm.info
|
Chris@0
|
72 if scm_info
|
Chris@0
|
73 # latest revision found in database
|
Chris@0
|
74 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
|
Chris@0
|
75 # latest revision in the repository
|
Chris@0
|
76 latest_revision = scm_info.lastrev
|
Chris@0
|
77 return if latest_revision.nil?
|
Chris@0
|
78 scm_revision = latest_revision.identifier.to_i
|
Chris@0
|
79 if db_revision < scm_revision
|
Chris@0
|
80 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
|
Chris@0
|
81 identifier_from = db_revision + 1
|
Chris@0
|
82 while (identifier_from <= scm_revision)
|
Chris@0
|
83 # loads changesets by batches of 100
|
Chris@0
|
84 identifier_to = [identifier_from + 99, scm_revision].min
|
Chris@0
|
85 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
|
Chris@0
|
86 transaction do
|
Chris@0
|
87 revisions.each do |revision|
|
Chris@0
|
88 changeset = Changeset.create(:repository => self,
|
Chris@0
|
89 :revision => revision.identifier,
|
Chris@0
|
90 :scmid => revision.scmid,
|
Chris@0
|
91 :committer => revision.author,
|
Chris@0
|
92 :committed_on => revision.time,
|
Chris@0
|
93 :comments => revision.message)
|
Chris@0
|
94
|
Chris@0
|
95 revision.paths.each do |change|
|
Chris@0
|
96 changeset.create_change(change)
|
Chris@0
|
97 end
|
Chris@0
|
98 end
|
Chris@0
|
99 end unless revisions.nil?
|
Chris@0
|
100 identifier_from = identifier_to + 1
|
Chris@0
|
101 end
|
Chris@0
|
102 end
|
Chris@0
|
103 end
|
Chris@0
|
104 end
|
Chris@0
|
105 end
|