@@ -1,175 +1,175 | |||||
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 |
|
27 | |||
28 | def info |
|
28 | def info | |
29 | cmd = "#{HG_BIN} -R #{target('')} root" |
|
29 | cmd = "#{HG_BIN} -R #{target('')} root" | |
30 | root_url = nil |
|
30 | root_url = nil | |
31 | shellout(cmd) do |io| |
|
31 | shellout(cmd) do |io| | |
32 | root_url = io.gets |
|
32 | root_url = io.gets | |
33 | end |
|
33 | end | |
34 | return nil if $? && $?.exitstatus != 0 |
|
34 | return nil if $? && $?.exitstatus != 0 | |
35 | info = Info.new({:root_url => root_url.chomp, |
|
35 | info = Info.new({:root_url => root_url.chomp, | |
36 | :lastrev => revisions(nil,nil,nil,{:limit => 1}).last |
|
36 | :lastrev => revisions(nil,nil,nil,{:limit => 1}).last | |
37 | }) |
|
37 | }) | |
38 | info |
|
38 | info | |
39 | rescue CommandFailed |
|
39 | rescue CommandFailed | |
40 | return nil |
|
40 | return nil | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | def entries(path=nil, identifier=nil) |
|
43 | def entries(path=nil, identifier=nil) | |
44 | path ||= '' |
|
44 | path ||= '' | |
45 | entries = Entries.new |
|
45 | entries = Entries.new | |
46 | cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate" |
|
46 | cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate" | |
47 | cmd << " -r #{identifier.to_i}" if identifier |
|
47 | cmd << " -r #{identifier.to_i}" if identifier | |
48 | cmd << " " + shell_quote('glob:**') |
|
48 | cmd << " " + shell_quote('glob:**') | |
49 | shellout(cmd) do |io| |
|
49 | shellout(cmd) do |io| | |
50 | io.each_line do |line| |
|
50 | io.each_line do |line| | |
51 | e = line.chomp.split(%r{[\/\\]}) |
|
51 | e = line.chomp.split(%r{[\/\\]}) | |
52 | entries << Entry.new({:name => e.first, |
|
52 | entries << Entry.new({:name => e.first, | |
53 | :path => (path.empty? ? e.first : "#{path}/#{e.first}"), |
|
53 | :path => (path.empty? ? e.first : "#{path}/#{e.first}"), | |
54 | :kind => (e.size > 1 ? 'dir' : 'file'), |
|
54 | :kind => (e.size > 1 ? 'dir' : 'file'), | |
55 | :lastrev => Revision.new |
|
55 | :lastrev => Revision.new | |
56 | }) unless entries.detect{|entry| entry.name == e.first} |
|
56 | }) unless entries.detect{|entry| entry.name == e.first} | |
57 | end |
|
57 | end | |
58 | end |
|
58 | end | |
59 | return nil if $? && $?.exitstatus != 0 |
|
59 | return nil if $? && $?.exitstatus != 0 | |
60 | entries.sort_by_name |
|
60 | entries.sort_by_name | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def entry(path=nil, identifier=nil) |
|
63 | def entry(path=nil, identifier=nil) | |
64 | path ||= '' |
|
64 | path ||= '' | |
65 | search_path = path.split('/')[0..-2].join('/') |
|
65 | search_path = path.split('/')[0..-2].join('/') | |
66 | entry_name = path.split('/').last |
|
66 | entry_name = path.split('/').last | |
67 | e = entries(search_path, identifier) |
|
67 | e = entries(search_path, identifier) | |
68 | e ? e.detect{|entry| entry.name == entry_name} : nil |
|
68 | e ? e.detect{|entry| entry.name == entry_name} : nil | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) |
|
71 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) | |
72 | revisions = Revisions.new |
|
72 | revisions = Revisions.new | |
73 | cmd = "#{HG_BIN} -v -R #{target('')} log" |
|
73 | cmd = "#{HG_BIN} -v --encoding utf8 -R #{target('')} log" | |
74 | if identifier_from && identifier_to |
|
74 | if identifier_from && identifier_to | |
75 | cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}" |
|
75 | cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}" | |
76 | elsif identifier_from |
|
76 | elsif identifier_from | |
77 | cmd << " -r #{identifier_from.to_i}:" |
|
77 | cmd << " -r #{identifier_from.to_i}:" | |
78 | end |
|
78 | end | |
79 | cmd << " --limit #{options[:limit].to_i}" if options[:limit] |
|
79 | cmd << " --limit #{options[:limit].to_i}" if options[:limit] | |
80 | shellout(cmd) do |io| |
|
80 | shellout(cmd) do |io| | |
81 | changeset = {} |
|
81 | changeset = {} | |
82 | parsing_descr = false |
|
82 | parsing_descr = false | |
83 | line_feeds = 0 |
|
83 | line_feeds = 0 | |
84 |
|
84 | |||
85 | io.each_line do |line| |
|
85 | io.each_line do |line| | |
86 | if line =~ /^(\w+):\s*(.*)$/ |
|
86 | if line =~ /^(\w+):\s*(.*)$/ | |
87 | key = $1 |
|
87 | key = $1 | |
88 | value = $2 |
|
88 | value = $2 | |
89 | if parsing_descr && line_feeds > 1 |
|
89 | if parsing_descr && line_feeds > 1 | |
90 | parsing_descr = false |
|
90 | parsing_descr = false | |
91 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, |
|
91 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, | |
92 | :scmid => changeset[:changeset].split(':').last, |
|
92 | :scmid => changeset[:changeset].split(':').last, | |
93 | :author => changeset[:user], |
|
93 | :author => changeset[:user], | |
94 | :time => Time.parse(changeset[:date]), |
|
94 | :time => Time.parse(changeset[:date]), | |
95 | :message => changeset[:description], |
|
95 | :message => changeset[:description], | |
96 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} |
|
96 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} | |
97 | }) |
|
97 | }) | |
98 | changeset = {} |
|
98 | changeset = {} | |
99 | end |
|
99 | end | |
100 | if !parsing_descr |
|
100 | if !parsing_descr | |
101 | changeset.store key.to_sym, value |
|
101 | changeset.store key.to_sym, value | |
102 | if $1 == "description" |
|
102 | if $1 == "description" | |
103 | parsing_descr = true |
|
103 | parsing_descr = true | |
104 | line_feeds = 0 |
|
104 | line_feeds = 0 | |
105 | next |
|
105 | next | |
106 | end |
|
106 | end | |
107 | end |
|
107 | end | |
108 | end |
|
108 | end | |
109 | if parsing_descr |
|
109 | if parsing_descr | |
110 | changeset[:description] << line |
|
110 | changeset[:description] << line | |
111 | line_feeds += 1 if line.chomp.empty? |
|
111 | line_feeds += 1 if line.chomp.empty? | |
112 | end |
|
112 | end | |
113 | end |
|
113 | end | |
114 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, |
|
114 | revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i, | |
115 | :scmid => changeset[:changeset].split(':').last, |
|
115 | :scmid => changeset[:changeset].split(':').last, | |
116 | :author => changeset[:user], |
|
116 | :author => changeset[:user], | |
117 | :time => Time.parse(changeset[:date]), |
|
117 | :time => Time.parse(changeset[:date]), | |
118 | :message => changeset[:description], |
|
118 | :message => changeset[:description], | |
119 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} |
|
119 | :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}} | |
120 | }) |
|
120 | }) | |
121 | end |
|
121 | end | |
122 | return nil if $? && $?.exitstatus != 0 |
|
122 | return nil if $? && $?.exitstatus != 0 | |
123 | revisions |
|
123 | revisions | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def diff(path, identifier_from, identifier_to=nil, type="inline") |
|
126 | def diff(path, identifier_from, identifier_to=nil, type="inline") | |
127 | path ||= '' |
|
127 | path ||= '' | |
128 | if identifier_to |
|
128 | if identifier_to | |
129 | identifier_to = identifier_to.to_i |
|
129 | identifier_to = identifier_to.to_i | |
130 | else |
|
130 | else | |
131 | identifier_to = identifier_from.to_i - 1 |
|
131 | identifier_to = identifier_from.to_i - 1 | |
132 | end |
|
132 | end | |
133 | cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates" |
|
133 | cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates" | |
134 | cmd << " -I #{target(path)}" unless path.empty? |
|
134 | cmd << " -I #{target(path)}" unless path.empty? | |
135 | diff = [] |
|
135 | diff = [] | |
136 | shellout(cmd) do |io| |
|
136 | shellout(cmd) do |io| | |
137 | io.each_line do |line| |
|
137 | io.each_line do |line| | |
138 | diff << line |
|
138 | diff << line | |
139 | end |
|
139 | end | |
140 | end |
|
140 | end | |
141 | return nil if $? && $?.exitstatus != 0 |
|
141 | return nil if $? && $?.exitstatus != 0 | |
142 | DiffTableList.new diff, type |
|
142 | DiffTableList.new diff, type | |
143 | end |
|
143 | end | |
144 |
|
144 | |||
145 | def cat(path, identifier=nil) |
|
145 | def cat(path, identifier=nil) | |
146 | cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}" |
|
146 | cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}" | |
147 | cat = nil |
|
147 | cat = nil | |
148 | shellout(cmd) do |io| |
|
148 | shellout(cmd) do |io| | |
149 | io.binmode |
|
149 | io.binmode | |
150 | cat = io.read |
|
150 | cat = io.read | |
151 | end |
|
151 | end | |
152 | return nil if $? && $?.exitstatus != 0 |
|
152 | return nil if $? && $?.exitstatus != 0 | |
153 | cat |
|
153 | cat | |
154 | end |
|
154 | end | |
155 |
|
155 | |||
156 | def annotate(path, identifier=nil) |
|
156 | def annotate(path, identifier=nil) | |
157 | path ||= '' |
|
157 | path ||= '' | |
158 | cmd = "#{HG_BIN} -R #{target('')}" |
|
158 | cmd = "#{HG_BIN} -R #{target('')}" | |
159 | cmd << " annotate -n -u" |
|
159 | cmd << " annotate -n -u" | |
160 | cmd << " -r #{identifier.to_i}" if identifier |
|
160 | cmd << " -r #{identifier.to_i}" if identifier | |
161 | cmd << " #{target(path)}" |
|
161 | cmd << " #{target(path)}" | |
162 | blame = Annotate.new |
|
162 | blame = Annotate.new | |
163 | shellout(cmd) do |io| |
|
163 | shellout(cmd) do |io| | |
164 | io.each_line do |line| |
|
164 | io.each_line do |line| | |
165 | next unless line =~ %r{^([^:]+)\s(\d+):(.*)$} |
|
165 | next unless line =~ %r{^([^:]+)\s(\d+):(.*)$} | |
166 | blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip)) |
|
166 | blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip)) | |
167 | end |
|
167 | end | |
168 | end |
|
168 | end | |
169 | return nil if $? && $?.exitstatus != 0 |
|
169 | return nil if $? && $?.exitstatus != 0 | |
170 | blame |
|
170 | blame | |
171 | end |
|
171 | end | |
172 | end |
|
172 | end | |
173 | end |
|
173 | end | |
174 | end |
|
174 | end | |
175 | end |
|
175 | end |
General Comments 0
You need to be logged in to leave comments.
Login now