@@ -1,142 +1,145 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | 22 | has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id' |
|
23 | 23 | |
|
24 | 24 | attr_protected :root_url |
|
25 | 25 | validates_presence_of :url |
|
26 | 26 | |
|
27 | 27 | FETCH_AT_ONCE = 100 # number of changesets to fetch at once |
|
28 | 28 | |
|
29 | 29 | ATTRIBUTE_KEY_NAMES = { |
|
30 | 30 | "url" => "Root directory", |
|
31 | 31 | } |
|
32 | 32 | def self.human_attribute_name(attribute_key_name) |
|
33 | 33 | ATTRIBUTE_KEY_NAMES[attribute_key_name] || super |
|
34 | 34 | end |
|
35 | 35 | |
|
36 | 36 | def self.scm_adapter_class |
|
37 | 37 | Redmine::Scm::Adapters::MercurialAdapter |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def self.scm_name |
|
41 | 41 | 'Mercurial' |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def supports_directory_revisions? |
|
45 | 45 | true |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | def repo_log_encoding |
|
49 | 49 | 'UTF-8' |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | # Returns the readable identifier for the given mercurial changeset |
|
53 | 53 | def self.format_changeset_identifier(changeset) |
|
54 | 54 | "#{changeset.revision}:#{changeset.scmid}" |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | # Returns the identifier for the given Mercurial changeset |
|
58 | 58 | def self.changeset_identifier(changeset) |
|
59 | 59 | changeset.scmid |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def diff_format_revisions(cs, cs_to, sep=':') |
|
63 | 63 | super(cs, cs_to, ' ') |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | # Finds and returns a revision with a number or the beginning of a hash |
|
67 | 67 | def find_changeset_by_name(name) |
|
68 | 68 | return nil if name.nil? || name.empty? |
|
69 | 69 | if /[^\d]/ =~ name or name.to_s.size > 8 |
|
70 | 70 | e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s]) |
|
71 | 71 | else |
|
72 | 72 | e = changesets.find(:first, :conditions => ['revision = ?', name.to_s]) |
|
73 | 73 | end |
|
74 | 74 | return e if e |
|
75 | 75 | changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | # Returns the latest changesets for +path+; sorted by revision number |
|
79 | 79 | # |
|
80 | 80 | # Because :order => 'id DESC' is defined at 'has_many', |
|
81 | 81 | # there is no need to set 'order'. |
|
82 | 82 | # But, MySQL test fails. |
|
83 | 83 | # Sqlite3 and PostgreSQL pass. |
|
84 | 84 | # Is this MySQL bug? |
|
85 | 85 | def latest_changesets(path, rev, limit=10) |
|
86 | 86 | changesets.find(:all, :include => :user, |
|
87 | 87 | :conditions => latest_changesets_cond(path, rev, limit), |
|
88 | 88 | :limit => limit, :order => "#{Changeset.table_name}.id DESC") |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def latest_changesets_cond(path, rev, limit) |
|
92 | 92 | cond, args = [], [] |
|
93 | 93 | if scm.branchmap.member? rev |
|
94 | 94 | # Mercurial named branch is *stable* in each revision. |
|
95 | 95 | # So, named branch can be stored in database. |
|
96 | 96 | # Mercurial provides *bookmark* which is equivalent with git branch. |
|
97 | 97 | # But, bookmark is not implemented. |
|
98 | 98 | cond << "#{Changeset.table_name}.scmid IN (?)" |
|
99 | 99 | # Revisions in root directory and sub directory are not equal. |
|
100 | 100 | # So, in order to get correct limit, we need to get all revisions. |
|
101 | 101 | # But, it is very heavy. |
|
102 | args << scm.nodes_in_branch(rev, :limit => limit) | |
|
102 | # Mercurial does not treat direcotry. | |
|
103 | # So, "hg log DIR" is very heavy. | |
|
104 | branch_limit = path.blank? ? limit : ( limit * 5 ) | |
|
105 | args << scm.nodes_in_branch(rev, :limit => branch_limit) | |
|
103 | 106 | elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil |
|
104 | 107 | cond << "#{Changeset.table_name}.id <= ?" |
|
105 | 108 | args << last.id |
|
106 | 109 | end |
|
107 | 110 | |
|
108 | 111 | unless path.blank? |
|
109 | 112 | cond << "EXISTS (SELECT * FROM #{Change.table_name} |
|
110 | 113 | WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id |
|
111 | 114 | AND (#{Change.table_name}.path = ? |
|
112 | 115 | OR #{Change.table_name}.path LIKE ? ESCAPE ?))" |
|
113 | 116 | args << path.with_leading_slash |
|
114 | 117 | args << "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%" << '\\' |
|
115 | 118 | end |
|
116 | 119 | |
|
117 | 120 | [cond.join(' AND '), *args] unless cond.empty? |
|
118 | 121 | end |
|
119 | 122 | private :latest_changesets_cond |
|
120 | 123 | |
|
121 | 124 | def fetch_changesets |
|
122 | 125 | scm_rev = scm.info.lastrev.revision.to_i |
|
123 | 126 | db_rev = latest_changeset ? latest_changeset.revision.to_i : -1 |
|
124 | 127 | return unless db_rev < scm_rev # already up-to-date |
|
125 | 128 | |
|
126 | 129 | logger.debug "Fetching changesets for repository #{url}" if logger |
|
127 | 130 | (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i| |
|
128 | 131 | transaction do |
|
129 | 132 | scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re| |
|
130 | 133 | cs = Changeset.create(:repository => self, |
|
131 | 134 | :revision => re.revision, |
|
132 | 135 | :scmid => re.scmid, |
|
133 | 136 | :committer => re.author, |
|
134 | 137 | :committed_on => re.time, |
|
135 | 138 | :comments => re.message) |
|
136 | 139 | re.paths.each { |e| cs.create_change(e) } |
|
137 | 140 | end |
|
138 | 141 | end |
|
139 | 142 | end |
|
140 | 143 | self |
|
141 | 144 | end |
|
142 | 145 | end |
General Comments 0
You need to be logged in to leave comments.
Login now