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