##// END OF EJS Templates
scm: mercurial: code clean up model....
Toshi MARUYAMA -
r5811:a267babfe806
parent child
Show More
@@ -1,146 +1,149
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/mercurial_adapter'
19 19
20 20 class Repository::Mercurial < Repository
21 21 # sort changesets by revision number
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
22 has_many :changesets,
23 :order => "#{Changeset.table_name}.id DESC",
24 :foreign_key => 'repository_id'
23 25
24 attr_protected :root_url
26 attr_protected :root_url
25 27 validates_presence_of :url
26 28
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
29 # number of changesets to fetch at once
30 FETCH_AT_ONCE = 100
28 31
29 32 def self.human_attribute_name(attribute_key_name)
30 33 attr_name = attribute_key_name
31 34 if attr_name == "url"
32 35 attr_name = "path_to_repository"
33 36 end
34 37 super(attr_name)
35 38 end
36 39
37 40 def self.scm_adapter_class
38 41 Redmine::Scm::Adapters::MercurialAdapter
39 42 end
40 43
41 44 def self.scm_name
42 45 'Mercurial'
43 46 end
44 47
45 48 def supports_directory_revisions?
46 49 true
47 50 end
48 51
49 52 def repo_log_encoding
50 53 'UTF-8'
51 54 end
52 55
53 56 # Returns the readable identifier for the given mercurial changeset
54 57 def self.format_changeset_identifier(changeset)
55 58 "#{changeset.revision}:#{changeset.scmid}"
56 59 end
57 60
58 61 # Returns the identifier for the given Mercurial changeset
59 62 def self.changeset_identifier(changeset)
60 63 changeset.scmid
61 64 end
62 65
63 66 def diff_format_revisions(cs, cs_to, sep=':')
64 67 super(cs, cs_to, ' ')
65 68 end
66 69
67 70 # Finds and returns a revision with a number or the beginning of a hash
68 71 def find_changeset_by_name(name)
69 72 return nil if name.nil? || name.empty?
70 73 if /[^\d]/ =~ name or name.to_s.size > 8
71 74 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
72 75 else
73 76 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
74 77 end
75 78 return e if e
76 79 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
77 80 end
78 81
79 82 # Returns the latest changesets for +path+; sorted by revision number
80 83 #
81 84 # Because :order => 'id DESC' is defined at 'has_many',
82 85 # there is no need to set 'order'.
83 86 # But, MySQL test fails.
84 87 # Sqlite3 and PostgreSQL pass.
85 88 # Is this MySQL bug?
86 89 def latest_changesets(path, rev, limit=10)
87 changesets.find(:all, :include => :user,
90 changesets.find(:all,
91 :include => :user,
88 92 :conditions => latest_changesets_cond(path, rev, limit),
89 :limit => limit, :order => "#{Changeset.table_name}.id DESC")
93 :limit => limit,
94 :order => "#{Changeset.table_name}.id DESC")
90 95 end
91 96
92 97 def latest_changesets_cond(path, rev, limit)
93 98 cond, args = [], []
94 99 if scm.branchmap.member? rev
95 100 # Mercurial named branch is *stable* in each revision.
96 101 # So, named branch can be stored in database.
97 102 # Mercurial provides *bookmark* which is equivalent with git branch.
98 103 # But, bookmark is not implemented.
99 104 cond << "#{Changeset.table_name}.scmid IN (?)"
100 105 # Revisions in root directory and sub directory are not equal.
101 106 # So, in order to get correct limit, we need to get all revisions.
102 107 # But, it is very heavy.
103 108 # Mercurial does not treat direcotry.
104 109 # So, "hg log DIR" is very heavy.
105 110 branch_limit = path.blank? ? limit : ( limit * 5 )
106 111 args << scm.nodes_in_branch(rev, :limit => branch_limit)
107 112 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
108 113 cond << "#{Changeset.table_name}.id <= ?"
109 114 args << last.id
110 115 end
111
112 116 unless path.blank?
113 117 cond << "EXISTS (SELECT * FROM #{Change.table_name}
114 118 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
115 119 AND (#{Change.table_name}.path = ?
116 120 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
117 121 args << path.with_leading_slash
118 122 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
119 123 end
120
121 124 [cond.join(' AND '), *args] unless cond.empty?
122 125 end
123 126 private :latest_changesets_cond
124 127
125 128 def fetch_changesets
126 129 scm_rev = scm.info.lastrev.revision.to_i
127 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
130 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
128 131 return unless db_rev < scm_rev # already up-to-date
129 132
130 133 logger.debug "Fetching changesets for repository #{url}" if logger
131 134 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
132 135 transaction do
133 136 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
134 cs = Changeset.create(:repository => self,
135 :revision => re.revision,
136 :scmid => re.scmid,
137 :committer => re.author,
137 cs = Changeset.create(:repository => self,
138 :revision => re.revision,
139 :scmid => re.scmid,
140 :committer => re.author,
138 141 :committed_on => re.time,
139 :comments => re.message)
142 :comments => re.message)
140 143 re.paths.each { |e| cs.create_change(e) }
141 144 end
142 145 end
143 146 end
144 147 self
145 148 end
146 149 end
General Comments 0
You need to be logged in to leave comments. Login now