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