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