##// END OF EJS Templates
Merged r4633 from trunk....
Toshi MARUYAMA -
r4519:9f7cc355ada6
parent child
Show More
@@ -1,12 +1,12
1 changeset = 'This template must be used with --debug option\n'
1 changeset = 'This template must be used with --debug option\n'
2 changeset_quiet = 'This template must be used with --debug option\n'
2 changeset_quiet = 'This template must be used with --debug option\n'
3 changeset_verbose = 'This template must be used with --debug option\n'
3 changeset_verbose = 'This template must be used with --debug option\n'
4 changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodate}</date>\n<paths>\n{files}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n{tags}</logentry>\n\n'
4 changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodate}</date>\n<paths>\n{files}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n{tags}</logentry>\n\n'
5
5
6 file = '<path action="M">{file|escape}</path>\n'
6 file = '<path action="M">{file|urlescape}</path>\n'
7 file_add = '<path action="A">{file_add|escape}</path>\n'
7 file_add = '<path action="A">{file_add|urlescape}</path>\n'
8 file_del = '<path action="D">{file_del|escape}</path>\n'
8 file_del = '<path action="D">{file_del|urlescape}</path>\n'
9 file_copy = '<path-copied copyfrom-path="{source|escape}">{name|urlescape}</path-copied>\n'
9 file_copy = '<path-copied copyfrom-path="{source|urlescape}">{name|urlescape}</path-copied>\n'
10 tag = '<tag>{tag|escape}</tag>\n'
10 tag = '<tag>{tag|escape}</tag>\n'
11 header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
11 header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
12 # footer="</log>" No newline at end of file
12 # footer="</log>"
@@ -1,12 +1,12
1 changeset = 'This template must be used with --debug option\n'
1 changeset = 'This template must be used with --debug option\n'
2 changeset_quiet = 'This template must be used with --debug option\n'
2 changeset_quiet = 'This template must be used with --debug option\n'
3 changeset_verbose = 'This template must be used with --debug option\n'
3 changeset_verbose = 'This template must be used with --debug option\n'
4 changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodatesec}</date>\n<paths>\n{file_mods}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n{tags}</logentry>\n\n'
4 changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodatesec}</date>\n<paths>\n{file_mods}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|escape}</msg>\n{tags}</logentry>\n\n'
5
5
6 file_mod = '<path action="M">{file_mod|escape}</path>\n'
6 file_mod = '<path action="M">{file_mod|urlescape}</path>\n'
7 file_add = '<path action="A">{file_add|escape}</path>\n'
7 file_add = '<path action="A">{file_add|urlescape}</path>\n'
8 file_del = '<path action="D">{file_del|escape}</path>\n'
8 file_del = '<path action="D">{file_del|urlescape}</path>\n'
9 file_copy = '<path-copied copyfrom-path="{source|escape}">{name|urlescape}</path-copied>\n'
9 file_copy = '<path-copied copyfrom-path="{source|urlescape}">{name|urlescape}</path-copied>\n'
10 tag = '<tag>{tag|escape}</tag>\n'
10 tag = '<tag>{tag|escape}</tag>\n'
11 header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
11 header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
12 # footer="</log>"
12 # footer="</log>"
@@ -1,208 +1,209
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 require 'cgi'
19
20
20 module Redmine
21 module Redmine
21 module Scm
22 module Scm
22 module Adapters
23 module Adapters
23 class MercurialAdapter < AbstractAdapter
24 class MercurialAdapter < AbstractAdapter
24
25
25 # Mercurial executable name
26 # Mercurial executable name
26 HG_BIN = "hg"
27 HG_BIN = "hg"
27 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
28 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
28 TEMPLATE_NAME = "hg-template"
29 TEMPLATE_NAME = "hg-template"
29 TEMPLATE_EXTENSION = "tmpl"
30 TEMPLATE_EXTENSION = "tmpl"
30
31
31 class << self
32 class << self
32 def client_version
33 def client_version
33 @@client_version ||= (hgversion || [])
34 @@client_version ||= (hgversion || [])
34 end
35 end
35
36
36 def hgversion
37 def hgversion
37 # The hg version is expressed either as a
38 # The hg version is expressed either as a
38 # release number (eg 0.9.5 or 1.0) or as a revision
39 # release number (eg 0.9.5 or 1.0) or as a revision
39 # id composed of 12 hexa characters.
40 # id composed of 12 hexa characters.
40 theversion = hgversion_from_command_line
41 theversion = hgversion_from_command_line
41 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
42 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
42 m[2].scan(%r{\d+}).collect(&:to_i)
43 m[2].scan(%r{\d+}).collect(&:to_i)
43 end
44 end
44 end
45 end
45
46
46 def hgversion_from_command_line
47 def hgversion_from_command_line
47 shellout("#{HG_BIN} --version") { |io| io.read }.to_s
48 shellout("#{HG_BIN} --version") { |io| io.read }.to_s
48 end
49 end
49
50
50 def template_path
51 def template_path
51 @@template_path ||= template_path_for(client_version)
52 @@template_path ||= template_path_for(client_version)
52 end
53 end
53
54
54 def template_path_for(version)
55 def template_path_for(version)
55 if ((version <=> [0,9,5]) > 0) || version.empty?
56 if ((version <=> [0,9,5]) > 0) || version.empty?
56 ver = "1.0"
57 ver = "1.0"
57 else
58 else
58 ver = "0.9.5"
59 ver = "0.9.5"
59 end
60 end
60 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
61 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
61 end
62 end
62 end
63 end
63
64
64 def info
65 def info
65 cmd = "#{HG_BIN} -R #{target('')} root"
66 cmd = "#{HG_BIN} -R #{target('')} root"
66 root_url = nil
67 root_url = nil
67 shellout(cmd) do |io|
68 shellout(cmd) do |io|
68 root_url = io.read
69 root_url = io.read
69 end
70 end
70 return nil if $? && $?.exitstatus != 0
71 return nil if $? && $?.exitstatus != 0
71 info = Info.new({:root_url => root_url.chomp,
72 info = Info.new({:root_url => root_url.chomp,
72 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
73 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
73 })
74 })
74 info
75 info
75 rescue CommandFailed
76 rescue CommandFailed
76 return nil
77 return nil
77 end
78 end
78
79
79 def entries(path=nil, identifier=nil)
80 def entries(path=nil, identifier=nil)
80 path ||= ''
81 path ||= ''
81 entries = Entries.new
82 entries = Entries.new
82 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
83 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
83 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
84 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
84 cmd << " " + shell_quote("path:#{path}") unless path.empty?
85 cmd << " " + shell_quote("path:#{path}") unless path.empty?
85 shellout(cmd) do |io|
86 shellout(cmd) do |io|
86 io.each_line do |line|
87 io.each_line do |line|
87 # HG uses antislashs as separator on Windows
88 # HG uses antislashs as separator on Windows
88 line = line.gsub(/\\/, "/")
89 line = line.gsub(/\\/, "/")
89 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
90 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
90 e ||= line
91 e ||= line
91 e = e.chomp.split(%r{[\/\\]})
92 e = e.chomp.split(%r{[\/\\]})
92 entries << Entry.new({:name => e.first,
93 entries << Entry.new({:name => e.first,
93 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
94 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
94 :kind => (e.size > 1 ? 'dir' : 'file'),
95 :kind => (e.size > 1 ? 'dir' : 'file'),
95 :lastrev => Revision.new
96 :lastrev => Revision.new
96 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
97 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
97 end
98 end
98 end
99 end
99 end
100 end
100 return nil if $? && $?.exitstatus != 0
101 return nil if $? && $?.exitstatus != 0
101 entries.sort_by_name
102 entries.sort_by_name
102 end
103 end
103
104
104 # Fetch the revisions by using a template file that
105 # Fetch the revisions by using a template file that
105 # makes Mercurial produce a xml output.
106 # makes Mercurial produce a xml output.
106 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
107 revisions = Revisions.new
108 revisions = Revisions.new
108 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
109 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
109 if identifier_from && identifier_to
110 if identifier_from && identifier_to
110 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
111 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
111 elsif identifier_from
112 elsif identifier_from
112 cmd << " -r #{identifier_from.to_i}:"
113 cmd << " -r #{identifier_from.to_i}:"
113 end
114 end
114 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
115 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
115 cmd << " #{shell_quote path}" unless path.blank?
116 cmd << " #{shell_quote path}" unless path.blank?
116 shellout(cmd) do |io|
117 shellout(cmd) do |io|
117 begin
118 begin
118 # HG doesn't close the XML Document...
119 # HG doesn't close the XML Document...
119 doc = REXML::Document.new(io.read << "</log>")
120 doc = REXML::Document.new(io.read << "</log>")
120 doc.elements.each("log/logentry") do |logentry|
121 doc.elements.each("log/logentry") do |logentry|
121 paths = []
122 paths = []
122 copies = logentry.get_elements('paths/path-copied')
123 copies = logentry.get_elements('paths/path-copied')
123 logentry.elements.each("paths/path") do |path|
124 logentry.elements.each("paths/path") do |path|
124 # Detect if the added file is a copy
125 # Detect if the added file is a copy
125 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
126 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
126 from_path = c.attributes['copyfrom-path']
127 from_path = c.attributes['copyfrom-path']
127 from_rev = logentry.attributes['revision']
128 from_rev = logentry.attributes['revision']
128 end
129 end
129 paths << {:action => path.attributes['action'],
130 paths << {:action => path.attributes['action'],
130 :path => "/#{path.text}",
131 :path => "/#{CGI.unescape(path.text)}",
131 :from_path => from_path ? "/#{from_path}" : nil,
132 :from_path => from_path ? "/#{CGI.unescape(from_path)}" : nil,
132 :from_revision => from_rev ? from_rev : nil
133 :from_revision => from_rev ? from_rev : nil
133 }
134 }
134 end
135 end
135 paths.sort! { |x,y| x[:path] <=> y[:path] }
136 paths.sort! { |x,y| x[:path] <=> y[:path] }
136
137
137 revisions << Revision.new({:identifier => logentry.attributes['revision'],
138 revisions << Revision.new({:identifier => logentry.attributes['revision'],
138 :scmid => logentry.attributes['node'],
139 :scmid => logentry.attributes['node'],
139 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
140 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
140 :time => Time.parse(logentry.elements['date'].text).localtime,
141 :time => Time.parse(logentry.elements['date'].text).localtime,
141 :message => logentry.elements['msg'].text,
142 :message => logentry.elements['msg'].text,
142 :paths => paths
143 :paths => paths
143 })
144 })
144 end
145 end
145 rescue
146 rescue
146 logger.debug($!)
147 logger.debug($!)
147 end
148 end
148 end
149 end
149 return nil if $? && $?.exitstatus != 0
150 return nil if $? && $?.exitstatus != 0
150 revisions
151 revisions
151 end
152 end
152
153
153 def diff(path, identifier_from, identifier_to=nil)
154 def diff(path, identifier_from, identifier_to=nil)
154 path ||= ''
155 path ||= ''
155 if identifier_to
156 if identifier_to
156 identifier_to = identifier_to.to_i
157 identifier_to = identifier_to.to_i
157 else
158 else
158 identifier_to = identifier_from.to_i - 1
159 identifier_to = identifier_from.to_i - 1
159 end
160 end
160 if identifier_from
161 if identifier_from
161 identifier_from = identifier_from.to_i
162 identifier_from = identifier_from.to_i
162 end
163 end
163 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
164 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
164 cmd << " -I #{target(path)}" unless path.empty?
165 cmd << " -I #{target(path)}" unless path.empty?
165 diff = []
166 diff = []
166 shellout(cmd) do |io|
167 shellout(cmd) do |io|
167 io.each_line do |line|
168 io.each_line do |line|
168 diff << line
169 diff << line
169 end
170 end
170 end
171 end
171 return nil if $? && $?.exitstatus != 0
172 return nil if $? && $?.exitstatus != 0
172 diff
173 diff
173 end
174 end
174
175
175 def cat(path, identifier=nil)
176 def cat(path, identifier=nil)
176 cmd = "#{HG_BIN} -R #{target('')} cat"
177 cmd = "#{HG_BIN} -R #{target('')} cat"
177 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
178 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
178 cmd << " #{target(path)}"
179 cmd << " #{target(path)}"
179 cat = nil
180 cat = nil
180 shellout(cmd) do |io|
181 shellout(cmd) do |io|
181 io.binmode
182 io.binmode
182 cat = io.read
183 cat = io.read
183 end
184 end
184 return nil if $? && $?.exitstatus != 0
185 return nil if $? && $?.exitstatus != 0
185 cat
186 cat
186 end
187 end
187
188
188 def annotate(path, identifier=nil)
189 def annotate(path, identifier=nil)
189 path ||= ''
190 path ||= ''
190 cmd = "#{HG_BIN} -R #{target('')}"
191 cmd = "#{HG_BIN} -R #{target('')}"
191 cmd << " annotate -n -u"
192 cmd << " annotate -n -u"
192 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
193 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
193 cmd << " -r #{identifier.to_i}" if identifier
194 cmd << " -r #{identifier.to_i}" if identifier
194 cmd << " #{target(path)}"
195 cmd << " #{target(path)}"
195 blame = Annotate.new
196 blame = Annotate.new
196 shellout(cmd) do |io|
197 shellout(cmd) do |io|
197 io.each_line do |line|
198 io.each_line do |line|
198 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
199 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
199 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
200 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
200 end
201 end
201 end
202 end
202 return nil if $? && $?.exitstatus != 0
203 return nil if $? && $?.exitstatus != 0
203 blame
204 blame
204 end
205 end
205 end
206 end
206 end
207 end
207 end
208 end
208 end
209 end
General Comments 0
You need to be logged in to leave comments. Login now