##// END OF EJS Templates
scm: bazaar: add adapter method to get .bzr/branch/branch.conf path from specified path (#2799, #4741, #8030)....
Toshi MARUYAMA -
r5770:05b292030ca6
parent child
Show More
@@ -1,226 +1,240
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 19
20 20 module Redmine
21 21 module Scm
22 22 module Adapters
23 23 class BazaarAdapter < AbstractAdapter
24 24
25 25 # Bazaar executable name
26 26 BZR_BIN = Redmine::Configuration['scm_bazaar_command'] || "bzr"
27 27
28 28 class << self
29 29 def client_command
30 30 @@bin ||= BZR_BIN
31 31 end
32 32
33 33 def sq_bin
34 34 @@sq_bin ||= shell_quote(BZR_BIN)
35 35 end
36 36
37 37 def client_version
38 38 @@client_version ||= (scm_command_version || [])
39 39 end
40 40
41 41 def client_available
42 42 !client_version.empty?
43 43 end
44 44
45 45 def scm_command_version
46 46 scm_version = scm_version_from_command_line.dup
47 47 if scm_version.respond_to?(:force_encoding)
48 48 scm_version.force_encoding('ASCII-8BIT')
49 49 end
50 50 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
51 51 m[2].scan(%r{\d+}).collect(&:to_i)
52 52 end
53 53 end
54 54
55 55 def scm_version_from_command_line
56 56 shellout("#{sq_bin} --version") { |io| io.read }.to_s
57 57 end
58 58 end
59 59
60 60 # Get info about the repository
61 61 def info
62 62 cmd = "#{self.class.sq_bin} revno #{target('')}"
63 63 info = nil
64 64 shellout(cmd) do |io|
65 65 if io.read =~ %r{^(\d+)\r?$}
66 66 info = Info.new({:root_url => url,
67 67 :lastrev => Revision.new({
68 68 :identifier => $1
69 69 })
70 70 })
71 71 end
72 72 end
73 73 return nil if $? && $?.exitstatus != 0
74 74 info
75 75 rescue CommandFailed
76 76 return nil
77 77 end
78 78
79 79 # Returns an Entries collection
80 80 # or nil if the given path doesn't exist in the repository
81 81 def entries(path=nil, identifier=nil, options={})
82 82 path ||= ''
83 83 entries = Entries.new
84 84 cmd = "#{self.class.sq_bin} ls -v --show-ids"
85 85 identifier = -1 unless identifier && identifier.to_i > 0
86 86 cmd << " -r#{identifier.to_i}"
87 87 cmd << " #{target(path)}"
88 88 shellout(cmd) do |io|
89 89 prefix = "#{url}/#{path}".gsub('\\', '/')
90 90 logger.debug "PREFIX: #{prefix}"
91 91 re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$}
92 92 io.each_line do |line|
93 93 next unless line =~ re
94 94 entries << Entry.new({:name => $3.strip,
95 95 :path => ((path.empty? ? "" : "#{path}/") + $3.strip),
96 96 :kind => ($4.blank? ? 'file' : 'dir'),
97 97 :size => nil,
98 98 :lastrev => Revision.new(:revision => $5.strip)
99 99 })
100 100 end
101 101 end
102 102 return nil if $? && $?.exitstatus != 0
103 103 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
104 104 entries.sort_by_name
105 105 end
106 106
107 107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
108 108 path ||= ''
109 109 identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : 'last:1'
110 110 identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : 1
111 111 revisions = Revisions.new
112 112 cmd = "#{self.class.sq_bin} log -v --show-ids -r#{identifier_to}..#{identifier_from} #{target(path)}"
113 113 shellout(cmd) do |io|
114 114 revision = nil
115 115 parsing = nil
116 116 io.each_line do |line|
117 117 if line =~ /^----/
118 118 revisions << revision if revision
119 119 revision = Revision.new(:paths => [], :message => '')
120 120 parsing = nil
121 121 else
122 122 next unless revision
123 123 if line =~ /^revno: (\d+)($|\s\[merge\]$)/
124 124 revision.identifier = $1.to_i
125 125 elsif line =~ /^committer: (.+)$/
126 126 revision.author = $1.strip
127 127 elsif line =~ /^revision-id:(.+)$/
128 128 revision.scmid = $1.strip
129 129 elsif line =~ /^timestamp: (.+)$/
130 130 revision.time = Time.parse($1).localtime
131 131 elsif line =~ /^ -----/
132 132 # partial revisions
133 133 parsing = nil unless parsing == 'message'
134 134 elsif line =~ /^(message|added|modified|removed|renamed):/
135 135 parsing = $1
136 136 elsif line =~ /^ (.*)$/
137 137 if parsing == 'message'
138 138 revision.message << "#{$1}\n"
139 139 else
140 140 if $1 =~ /^(.*)\s+(\S+)$/
141 141 path = $1.strip
142 142 revid = $2
143 143 case parsing
144 144 when 'added'
145 145 revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid}
146 146 when 'modified'
147 147 revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid}
148 148 when 'removed'
149 149 revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid}
150 150 when 'renamed'
151 151 new_path = path.split('=>').last
152 152 revision.paths << {:action => 'M', :path => "/#{new_path.strip}", :revision => revid} if new_path
153 153 end
154 154 end
155 155 end
156 156 else
157 157 parsing = nil
158 158 end
159 159 end
160 160 end
161 161 revisions << revision if revision
162 162 end
163 163 return nil if $? && $?.exitstatus != 0
164 164 revisions
165 165 end
166 166
167 167 def diff(path, identifier_from, identifier_to=nil)
168 168 path ||= ''
169 169 if identifier_to
170 170 identifier_to = identifier_to.to_i
171 171 else
172 172 identifier_to = identifier_from.to_i - 1
173 173 end
174 174 if identifier_from
175 175 identifier_from = identifier_from.to_i
176 176 end
177 177 cmd = "#{self.class.sq_bin} diff -r#{identifier_to}..#{identifier_from} #{target(path)}"
178 178 diff = []
179 179 shellout(cmd) do |io|
180 180 io.each_line do |line|
181 181 diff << line
182 182 end
183 183 end
184 184 #return nil if $? && $?.exitstatus != 0
185 185 diff
186 186 end
187 187
188 188 def cat(path, identifier=nil)
189 189 cmd = "#{self.class.sq_bin} cat"
190 190 cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0
191 191 cmd << " #{target(path)}"
192 192 cat = nil
193 193 shellout(cmd) do |io|
194 194 io.binmode
195 195 cat = io.read
196 196 end
197 197 return nil if $? && $?.exitstatus != 0
198 198 cat
199 199 end
200 200
201 201 def annotate(path, identifier=nil)
202 202 cmd = "#{self.class.sq_bin} annotate --all"
203 203 cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0
204 204 cmd << " #{target(path)}"
205 205 blame = Annotate.new
206 206 shellout(cmd) do |io|
207 207 author = nil
208 208 identifier = nil
209 209 io.each_line do |line|
210 210 next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$}
211 211 rev = $1
212 212 blame.add_line($3.rstrip,
213 213 Revision.new(
214 214 :identifier => rev,
215 215 :revision => rev,
216 216 :author => $2.strip
217 217 ))
218 218 end
219 219 end
220 220 return nil if $? && $?.exitstatus != 0
221 221 blame
222 222 end
223
224 def self.branch_conf_path(path)
225 bcp = nil
226 m = path.match(%r{^(.*[/\\])\.bzr.*$})
227 if m
228 bcp = m[1]
229 else
230 bcp = path
231 end
232 bcp.gsub!(/[\/\\]$/, "")
233 if bcp
234 bcp = File.join(bcp, ".bzr", "branch", "branch.conf")
235 end
236 end
223 237 end
224 238 end
225 239 end
226 240 end
General Comments 0
You need to be logged in to leave comments. Login now