##// END OF EJS Templates
implement local cache for mercurial remote repositories...
Nicolas Chuche -
r1906:7e46d9b35b73
parent child
Show More
@@ -1,94 +1,105
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 attr_protected :root_url
21 attr_protected :root_url
22 validates_presence_of :url
22 validates_presence_of :url
23
23
24 def init_cache
25 return unless dir = repositories_cache_directory
26 # we need to use a cache only if repository isn't local and dir exists
27 if url[/^(|https?|ssh):\/\//]
28 update_attribute(:cache_path, dir + project.identifier)
29 update_attribute(:cache, true)
30 end
31 end
32
24 def scm_adapter
33 def scm_adapter
25 Redmine::Scm::Adapters::MercurialAdapter
34 Redmine::Scm::Adapters::MercurialAdapter
26 end
35 end
27
36
28 def self.scm_name
37 def self.scm_name
29 'Mercurial'
38 'Mercurial'
30 end
39 end
31
40
32 def entries(path=nil, identifier=nil)
41 def entries(path=nil, identifier=nil)
33 entries=scm.entries(path, identifier)
42 entries=scm.entries(path, identifier)
34 if entries
43 if entries
35 entries.each do |entry|
44 entries.each do |entry|
36 next unless entry.is_file?
45 next unless entry.is_file?
37 # Set the filesize unless browsing a specific revision
46 # Set the filesize unless browsing a specific revision
38 if identifier.nil?
47 if identifier.nil?
39 full_path = File.join(root_url, entry.path)
48 full_path = File.join(root_url, entry.path)
40 entry.size = File.stat(full_path).size if File.file?(full_path)
49 entry.size = File.stat(full_path).size if File.file?(full_path)
41 end
50 end
42 # Search the DB for the entry's last change
51 # Search the DB for the entry's last change
43 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
52 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
44 if change
53 if change
45 entry.lastrev.identifier = change.changeset.revision
54 entry.lastrev.identifier = change.changeset.revision
46 entry.lastrev.name = change.changeset.revision
55 entry.lastrev.name = change.changeset.revision
47 entry.lastrev.author = change.changeset.committer
56 entry.lastrev.author = change.changeset.committer
48 entry.lastrev.revision = change.revision
57 entry.lastrev.revision = change.revision
49 end
58 end
50 end
59 end
51 end
60 end
52 entries
61 entries
53 end
62 end
54
63
55 def fetch_changesets
64 def fetch_changesets
65 create_or_sync_cache if cache
66
56 scm_info = scm.info
67 scm_info = scm.info
57 if scm_info
68 if scm_info
58 # latest revision found in database
69 # latest revision found in database
59 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
70 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
60 # latest revision in the repository
71 # latest revision in the repository
61 latest_revision = scm_info.lastrev
72 latest_revision = scm_info.lastrev
62 return if latest_revision.nil?
73 return if latest_revision.nil?
63 scm_revision = latest_revision.identifier.to_i
74 scm_revision = latest_revision.identifier.to_i
64 if db_revision < scm_revision
75 if db_revision < scm_revision
65 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
76 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
66 identifier_from = db_revision + 1
77 identifier_from = db_revision + 1
67 while (identifier_from <= scm_revision)
78 while (identifier_from <= scm_revision)
68 # loads changesets by batches of 100
79 # loads changesets by batches of 100
69 identifier_to = [identifier_from + 99, scm_revision].min
80 identifier_to = [identifier_from + 99, scm_revision].min
70 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
81 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
71 transaction do
82 transaction do
72 revisions.each do |revision|
83 revisions.each do |revision|
73 changeset = Changeset.create(:repository => self,
84 changeset = Changeset.create(:repository => self,
74 :revision => revision.identifier,
85 :revision => revision.identifier,
75 :scmid => revision.scmid,
86 :scmid => revision.scmid,
76 :committer => revision.author,
87 :committer => revision.author,
77 :committed_on => revision.time,
88 :committed_on => revision.time,
78 :comments => revision.message)
89 :comments => revision.message)
79
90
80 revision.paths.each do |change|
91 revision.paths.each do |change|
81 Change.create(:changeset => changeset,
92 Change.create(:changeset => changeset,
82 :action => change[:action],
93 :action => change[:action],
83 :path => change[:path],
94 :path => change[:path],
84 :from_path => change[:from_path],
95 :from_path => change[:from_path],
85 :from_revision => change[:from_revision])
96 :from_revision => change[:from_revision])
86 end
97 end
87 end
98 end
88 end unless revisions.nil?
99 end unless revisions.nil?
89 identifier_from = identifier_to + 1
100 identifier_from = identifier_to + 1
90 end
101 end
91 end
102 end
92 end
103 end
93 end
104 end
94 end
105 end
@@ -1,205 +1,217
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/abstract_adapter'
18 require 'redmine/scm/adapters/abstract_adapter'
19
19
20 module Redmine
20 module Redmine
21 module Scm
21 module Scm
22 module Adapters
22 module Adapters
23 class MercurialAdapter < AbstractAdapter
23 class MercurialAdapter < AbstractAdapter
24
24
25 # Mercurial executable name
25 # Mercurial executable name
26 HG_BIN = "hg"
26 HG_BIN = "hg"
27 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
27 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
28 TEMPLATE_NAME = "hg-template"
28 TEMPLATE_NAME = "hg-template"
29 TEMPLATE_EXTENSION = "tmpl"
29 TEMPLATE_EXTENSION = "tmpl"
30
30
31 class << self
31 class << self
32 def client_version
32 def client_version
33 @@client_version ||= (hgversion || [])
33 @@client_version ||= (hgversion || [])
34 end
34 end
35
35
36 def hgversion
36 def hgversion
37 # The hg version is expressed either as a
37 # The hg version is expressed either as a
38 # release number (eg 0.9.5 or 1.0) or as a revision
38 # release number (eg 0.9.5 or 1.0) or as a revision
39 # id composed of 12 hexa characters.
39 # id composed of 12 hexa characters.
40 theversion = hgversion_from_command_line
40 theversion = hgversion_from_command_line
41 if theversion.match(/^\d+(\.\d+)+/)
41 if theversion.match(/^\d+(\.\d+)+/)
42 theversion.split(".").collect(&:to_i)
42 theversion.split(".").collect(&:to_i)
43 end
43 end
44 end
44 end
45
45
46 def hgversion_from_command_line
46 def hgversion_from_command_line
47 %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
47 %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
48 end
48 end
49
49
50 def template_path
50 def template_path
51 @@template_path ||= template_path_for(client_version)
51 @@template_path ||= template_path_for(client_version)
52 end
52 end
53
53
54 def template_path_for(version)
54 def template_path_for(version)
55 if ((version <=> [0,9,5]) > 0) || version.empty?
55 if ((version <=> [0,9,5]) > 0) || version.empty?
56 ver = "1.0"
56 ver = "1.0"
57 else
57 else
58 ver = "0.9.5"
58 ver = "0.9.5"
59 end
59 end
60 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
60 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
61 end
61 end
62 end
62 end
63
63
64 def info
64 def info
65 cmd = "#{HG_BIN} -R #{target('')} root"
65 cmd = "#{HG_BIN} -R #{target('')} root"
66 root_url = nil
66 root_url = nil
67 shellout(cmd) do |io|
67 shellout(cmd) do |io|
68 root_url = io.gets
68 root_url = io.gets
69 end
69 end
70 return nil if $? && $?.exitstatus != 0
70 return nil if $? && $?.exitstatus != 0
71 info = Info.new({:root_url => root_url.chomp,
71 info = Info.new({:root_url => root_url.chomp,
72 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
72 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
73 })
73 })
74 info
74 info
75 rescue CommandFailed
75 rescue CommandFailed
76 return nil
76 return nil
77 end
77 end
78
78
79 def entries(path=nil, identifier=nil)
79 def entries(path=nil, identifier=nil)
80 path ||= ''
80 path ||= ''
81 entries = Entries.new
81 entries = Entries.new
82 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
82 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
83 cmd << " -r " + (identifier ? identifier.to_s : "tip")
83 cmd << " -r " + (identifier ? identifier.to_s : "tip")
84 cmd << " " + shell_quote("path:#{path}") unless path.empty?
84 cmd << " " + shell_quote("path:#{path}") unless path.empty?
85 shellout(cmd) do |io|
85 shellout(cmd) do |io|
86 io.each_line do |line|
86 io.each_line do |line|
87 # HG uses antislashs as separator on Windows
87 # HG uses antislashs as separator on Windows
88 line = line.gsub(/\\/, "/")
88 line = line.gsub(/\\/, "/")
89 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
89 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
90 e ||= line
90 e ||= line
91 e = e.chomp.split(%r{[\/\\]})
91 e = e.chomp.split(%r{[\/\\]})
92 entries << Entry.new({:name => e.first,
92 entries << Entry.new({:name => e.first,
93 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
93 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
94 :kind => (e.size > 1 ? 'dir' : 'file'),
94 :kind => (e.size > 1 ? 'dir' : 'file'),
95 :lastrev => Revision.new
95 :lastrev => Revision.new
96 }) unless entries.detect{|entry| entry.name == e.first}
96 }) unless entries.detect{|entry| entry.name == e.first}
97 end
97 end
98 end
98 end
99 end
99 end
100 return nil if $? && $?.exitstatus != 0
100 return nil if $? && $?.exitstatus != 0
101 entries.sort_by_name
101 entries.sort_by_name
102 end
102 end
103
103
104 # Fetch the revisions by using a template file that
104 # Fetch the revisions by using a template file that
105 # makes Mercurial produce a xml output.
105 # makes Mercurial produce a xml output.
106 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
106 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
107 revisions = Revisions.new
107 revisions = Revisions.new
108 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}"
108 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}"
109 if identifier_from && identifier_to
109 if identifier_from && identifier_to
110 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
110 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
111 elsif identifier_from
111 elsif identifier_from
112 cmd << " -r #{identifier_from.to_i}:"
112 cmd << " -r #{identifier_from.to_i}:"
113 end
113 end
114 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
114 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
115 cmd << " #{path}" if path
115 cmd << " #{path}" if path
116 shellout(cmd) do |io|
116 shellout(cmd) do |io|
117 begin
117 begin
118 # HG doesn't close the XML Document...
118 # HG doesn't close the XML Document...
119 doc = REXML::Document.new(io.read << "</log>")
119 doc = REXML::Document.new(io.read << "</log>")
120 doc.elements.each("log/logentry") do |logentry|
120 doc.elements.each("log/logentry") do |logentry|
121 paths = []
121 paths = []
122 copies = logentry.get_elements('paths/path-copied')
122 copies = logentry.get_elements('paths/path-copied')
123 logentry.elements.each("paths/path") do |path|
123 logentry.elements.each("paths/path") do |path|
124 # Detect if the added file is a copy
124 # Detect if the added file is a copy
125 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
125 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
126 from_path = c.attributes['copyfrom-path']
126 from_path = c.attributes['copyfrom-path']
127 from_rev = logentry.attributes['revision']
127 from_rev = logentry.attributes['revision']
128 end
128 end
129 paths << {:action => path.attributes['action'],
129 paths << {:action => path.attributes['action'],
130 :path => "/#{path.text}",
130 :path => "/#{path.text}",
131 :from_path => from_path ? "/#{from_path}" : nil,
131 :from_path => from_path ? "/#{from_path}" : nil,
132 :from_revision => from_rev ? from_rev : nil
132 :from_revision => from_rev ? from_rev : nil
133 }
133 }
134 end
134 end
135 paths.sort! { |x,y| x[:path] <=> y[:path] }
135 paths.sort! { |x,y| x[:path] <=> y[:path] }
136
136
137 revisions << Revision.new({:identifier => logentry.attributes['revision'],
137 revisions << Revision.new({:identifier => logentry.attributes['revision'],
138 :scmid => logentry.attributes['node'],
138 :scmid => logentry.attributes['node'],
139 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
139 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
140 :time => Time.parse(logentry.elements['date'].text).localtime,
140 :time => Time.parse(logentry.elements['date'].text).localtime,
141 :message => logentry.elements['msg'].text,
141 :message => logentry.elements['msg'].text,
142 :paths => paths
142 :paths => paths
143 })
143 })
144 end
144 end
145 rescue
145 rescue
146 logger.debug($!)
146 logger.debug($!)
147 end
147 end
148 end
148 end
149 return nil if $? && $?.exitstatus != 0
149 return nil if $? && $?.exitstatus != 0
150 revisions
150 revisions
151 end
151 end
152
152
153 def diff(path, identifier_from, identifier_to=nil)
153 def diff(path, identifier_from, identifier_to=nil)
154 path ||= ''
154 path ||= ''
155 if identifier_to
155 if identifier_to
156 identifier_to = identifier_to.to_i
156 identifier_to = identifier_to.to_i
157 else
157 else
158 identifier_to = identifier_from.to_i - 1
158 identifier_to = identifier_from.to_i - 1
159 end
159 end
160 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
160 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
161 cmd << " -I #{target(path)}" unless path.empty?
161 cmd << " -I #{target(path)}" unless path.empty?
162 diff = []
162 diff = []
163 shellout(cmd) do |io|
163 shellout(cmd) do |io|
164 io.each_line do |line|
164 io.each_line do |line|
165 diff << line
165 diff << line
166 end
166 end
167 end
167 end
168 return nil if $? && $?.exitstatus != 0
168 return nil if $? && $?.exitstatus != 0
169 diff
169 diff
170 end
170 end
171
171
172 def cat(path, identifier=nil)
172 def cat(path, identifier=nil)
173 cmd = "#{HG_BIN} -R #{target('')} cat"
173 cmd = "#{HG_BIN} -R #{target('')} cat"
174 cmd << " -r " + (identifier ? identifier.to_s : "tip")
174 cmd << " -r " + (identifier ? identifier.to_s : "tip")
175 cmd << " #{target(path)}"
175 cmd << " #{target(path)}"
176 cat = nil
176 cat = nil
177 shellout(cmd) do |io|
177 shellout(cmd) do |io|
178 io.binmode
178 io.binmode
179 cat = io.read
179 cat = io.read
180 end
180 end
181 return nil if $? && $?.exitstatus != 0
181 return nil if $? && $?.exitstatus != 0
182 cat
182 cat
183 end
183 end
184
184
185 def annotate(path, identifier=nil)
185 def annotate(path, identifier=nil)
186 path ||= ''
186 path ||= ''
187 cmd = "#{HG_BIN} -R #{target('')}"
187 cmd = "#{HG_BIN} -R #{target('')}"
188 cmd << " annotate -n -u"
188 cmd << " annotate -n -u"
189 cmd << " -r " + (identifier ? identifier.to_s : "tip")
189 cmd << " -r " + (identifier ? identifier.to_s : "tip")
190 cmd << " -r #{identifier.to_i}" if identifier
190 cmd << " -r #{identifier.to_i}" if identifier
191 cmd << " #{target(path)}"
191 cmd << " #{target(path)}"
192 blame = Annotate.new
192 blame = Annotate.new
193 shellout(cmd) do |io|
193 shellout(cmd) do |io|
194 io.each_line do |line|
194 io.each_line do |line|
195 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
195 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
196 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
196 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
197 end
197 end
198 end
198 end
199 return nil if $? && $?.exitstatus != 0
199 return nil if $? && $?.exitstatus != 0
200 blame
200 blame
201 end
201 end
202
203 def create_cache
204 cmd = "#{HG_BIN} clone #{@orig_url} #{@root_url}"
205 shellout(cmd) { |io| io.read }
206 end
207
208 def synchronize
209 return unless File.directory?(@url)
210 cmd = "#{HG_BIN} -R #{@url} pull"
211 shellout(cmd)
212 end
213
202 end
214 end
203 end
215 end
204 end
216 end
205 end
217 end
General Comments 0
You need to be logged in to leave comments. Login now