##// END OF EJS Templates
Fixes Mercurial browsing under unix-like os and for directory depth > 2 and (patch #826 by Frederic Moulins, slightly edited to preserve win32 compatibility)....
Jean-Philippe Lang -
r1214:71d3bb2f7b97
parent child
Show More
@@ -1,171 +1,171
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 -X */*/*"
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 << " * */*"
48 cmd << " 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('\\')
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 -R #{target('')} log"
74 cmd << " -r #{identifier_from.to_i}:" if identifier_from
74 cmd << " -r #{identifier_from.to_i}:" if identifier_from
75 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
75 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
76 shellout(cmd) do |io|
76 shellout(cmd) do |io|
77 changeset = {}
77 changeset = {}
78 parsing_descr = false
78 parsing_descr = false
79 line_feeds = 0
79 line_feeds = 0
80
80
81 io.each_line do |line|
81 io.each_line do |line|
82 if line =~ /^(\w+):\s*(.*)$/
82 if line =~ /^(\w+):\s*(.*)$/
83 key = $1
83 key = $1
84 value = $2
84 value = $2
85 if parsing_descr && line_feeds > 1
85 if parsing_descr && line_feeds > 1
86 parsing_descr = false
86 parsing_descr = false
87 revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
87 revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
88 :scmid => changeset[:changeset].split(':').last,
88 :scmid => changeset[:changeset].split(':').last,
89 :author => changeset[:user],
89 :author => changeset[:user],
90 :time => Time.parse(changeset[:date]),
90 :time => Time.parse(changeset[:date]),
91 :message => changeset[:description],
91 :message => changeset[:description],
92 :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
92 :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
93 })
93 })
94 changeset = {}
94 changeset = {}
95 end
95 end
96 if !parsing_descr
96 if !parsing_descr
97 changeset.store key.to_sym, value
97 changeset.store key.to_sym, value
98 if $1 == "description"
98 if $1 == "description"
99 parsing_descr = true
99 parsing_descr = true
100 line_feeds = 0
100 line_feeds = 0
101 next
101 next
102 end
102 end
103 end
103 end
104 end
104 end
105 if parsing_descr
105 if parsing_descr
106 changeset[:description] << line
106 changeset[:description] << line
107 line_feeds += 1 if line.chomp.empty?
107 line_feeds += 1 if line.chomp.empty?
108 end
108 end
109 end
109 end
110 revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
110 revisions << Revision.new({:identifier => changeset[:changeset].split(':').first.to_i,
111 :scmid => changeset[:changeset].split(':').last,
111 :scmid => changeset[:changeset].split(':').last,
112 :author => changeset[:user],
112 :author => changeset[:user],
113 :time => Time.parse(changeset[:date]),
113 :time => Time.parse(changeset[:date]),
114 :message => changeset[:description],
114 :message => changeset[:description],
115 :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
115 :paths => changeset[:files].to_s.split.collect{|path| {:action => 'X', :path => "/#{path}"}}
116 })
116 })
117 end
117 end
118 return nil if $? && $?.exitstatus != 0
118 return nil if $? && $?.exitstatus != 0
119 revisions
119 revisions
120 end
120 end
121
121
122 def diff(path, identifier_from, identifier_to=nil, type="inline")
122 def diff(path, identifier_from, identifier_to=nil, type="inline")
123 path ||= ''
123 path ||= ''
124 if identifier_to
124 if identifier_to
125 identifier_to = identifier_to.to_i
125 identifier_to = identifier_to.to_i
126 else
126 else
127 identifier_to = identifier_from.to_i - 1
127 identifier_to = identifier_from.to_i - 1
128 end
128 end
129 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
129 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
130 cmd << " -I #{target(path)}" unless path.empty?
130 cmd << " -I #{target(path)}" unless path.empty?
131 diff = []
131 diff = []
132 shellout(cmd) do |io|
132 shellout(cmd) do |io|
133 io.each_line do |line|
133 io.each_line do |line|
134 diff << line
134 diff << line
135 end
135 end
136 end
136 end
137 return nil if $? && $?.exitstatus != 0
137 return nil if $? && $?.exitstatus != 0
138 DiffTableList.new diff, type
138 DiffTableList.new diff, type
139 end
139 end
140
140
141 def cat(path, identifier=nil)
141 def cat(path, identifier=nil)
142 cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}"
142 cmd = "#{HG_BIN} -R #{target('')} cat #{target(path)}"
143 cat = nil
143 cat = nil
144 shellout(cmd) do |io|
144 shellout(cmd) do |io|
145 io.binmode
145 io.binmode
146 cat = io.read
146 cat = io.read
147 end
147 end
148 return nil if $? && $?.exitstatus != 0
148 return nil if $? && $?.exitstatus != 0
149 cat
149 cat
150 end
150 end
151
151
152 def annotate(path, identifier=nil)
152 def annotate(path, identifier=nil)
153 path ||= ''
153 path ||= ''
154 cmd = "#{HG_BIN} -R #{target('')}"
154 cmd = "#{HG_BIN} -R #{target('')}"
155 cmd << " annotate -n -u"
155 cmd << " annotate -n -u"
156 cmd << " -r #{identifier.to_i}" if identifier
156 cmd << " -r #{identifier.to_i}" if identifier
157 cmd << " #{target(path)}"
157 cmd << " #{target(path)}"
158 blame = Annotate.new
158 blame = Annotate.new
159 shellout(cmd) do |io|
159 shellout(cmd) do |io|
160 io.each_line do |line|
160 io.each_line do |line|
161 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
161 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
162 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
162 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
163 end
163 end
164 end
164 end
165 return nil if $? && $?.exitstatus != 0
165 return nil if $? && $?.exitstatus != 0
166 blame
166 blame
167 end
167 end
168 end
168 end
169 end
169 end
170 end
170 end
171 end
171 end
General Comments 0
You need to be logged in to leave comments. Login now