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