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@119
|
21 # sort changesets by revision number
|
Chris@119
|
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
|
Chris@119
|
23
|
Chris@0
|
24 attr_protected :root_url
|
Chris@0
|
25 validates_presence_of :url
|
Chris@0
|
26
|
Chris@245
|
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
|
Chris@245
|
28
|
Chris@245
|
29 ATTRIBUTE_KEY_NAMES = {
|
Chris@245
|
30 "url" => "Root directory",
|
Chris@245
|
31 }
|
Chris@245
|
32 def self.human_attribute_name(attribute_key_name)
|
Chris@245
|
33 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
|
Chris@245
|
34 end
|
Chris@245
|
35
|
Chris@245
|
36 def self.scm_adapter_class
|
Chris@0
|
37 Redmine::Scm::Adapters::MercurialAdapter
|
Chris@0
|
38 end
|
Chris@119
|
39
|
Chris@0
|
40 def self.scm_name
|
Chris@0
|
41 'Mercurial'
|
Chris@0
|
42 end
|
Chris@119
|
43
|
Chris@245
|
44 def repo_log_encoding
|
Chris@245
|
45 'UTF-8'
|
Chris@245
|
46 end
|
Chris@245
|
47
|
Chris@119
|
48 # Returns the readable identifier for the given mercurial changeset
|
Chris@119
|
49 def self.format_changeset_identifier(changeset)
|
Chris@119
|
50 "#{changeset.revision}:#{changeset.scmid}"
|
Chris@119
|
51 end
|
Chris@119
|
52
|
Chris@119
|
53 # Returns the identifier for the given Mercurial changeset
|
Chris@119
|
54 def self.changeset_identifier(changeset)
|
Chris@119
|
55 changeset.scmid
|
Chris@119
|
56 end
|
Chris@119
|
57
|
Chris@245
|
58 def branches
|
Chris@245
|
59 nil
|
Chris@245
|
60 end
|
Chris@245
|
61
|
Chris@245
|
62 def tags
|
Chris@245
|
63 nil
|
Chris@245
|
64 end
|
Chris@245
|
65
|
Chris@119
|
66 def diff_format_revisions(cs, cs_to, sep=':')
|
Chris@119
|
67 super(cs, cs_to, ' ')
|
Chris@119
|
68 end
|
Chris@119
|
69
|
Chris@119
|
70 # Finds and returns a revision with a number or the beginning of a hash
|
Chris@119
|
71 def find_changeset_by_name(name)
|
Chris@119
|
72 return nil if name.nil? || name.empty?
|
Chris@119
|
73 if /[^\d]/ =~ name or name.to_s.size > 8
|
Chris@119
|
74 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
|
Chris@119
|
75 else
|
Chris@119
|
76 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
|
Chris@119
|
77 end
|
Chris@119
|
78 return e if e
|
Chris@119
|
79 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
|
Chris@119
|
80 end
|
Chris@119
|
81
|
Chris@119
|
82 # Returns the latest changesets for +path+; sorted by revision number
|
Chris@245
|
83 # Default behavior is to search in cached changesets
|
Chris@119
|
84 def latest_changesets(path, rev, limit=10)
|
Chris@119
|
85 if path.blank?
|
Chris@119
|
86 changesets.find(:all, :include => :user, :limit => limit)
|
Chris@119
|
87 else
|
Chris@245
|
88 changesets.find(:all, :select => "DISTINCT #{Changeset.table_name}.*",
|
Chris@245
|
89 :joins => :changes,
|
Chris@245
|
90 :conditions => ["#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ? ESCAPE ?",
|
Chris@245
|
91 path.with_leading_slash,
|
Chris@245
|
92 "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%", '\\'],
|
Chris@245
|
93 :include => :user, :limit => limit)
|
Chris@119
|
94 end
|
Chris@119
|
95 end
|
Chris@119
|
96
|
Chris@0
|
97 def fetch_changesets
|
Chris@245
|
98 scm_rev = scm.info.lastrev.revision.to_i
|
Chris@245
|
99 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
|
Chris@245
|
100 return unless db_rev < scm_rev # already up-to-date
|
Chris@245
|
101
|
Chris@245
|
102 logger.debug "Fetching changesets for repository #{url}" if logger
|
Chris@245
|
103 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
|
Chris@245
|
104 transaction do
|
Chris@245
|
105 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
|
Chris@245
|
106 cs = Changeset.create(:repository => self,
|
Chris@245
|
107 :revision => re.revision,
|
Chris@245
|
108 :scmid => re.scmid,
|
Chris@245
|
109 :committer => re.author,
|
Chris@245
|
110 :committed_on => re.time,
|
Chris@245
|
111 :comments => re.message)
|
Chris@245
|
112 re.paths.each { |e| cs.create_change(e) }
|
Chris@0
|
113 end
|
Chris@0
|
114 end
|
Chris@0
|
115 end
|
Chris@245
|
116 self
|
Chris@0
|
117 end
|
Chris@0
|
118 end
|