Mercurial > hg > soundsoftware-site
comparison lib/redmine/scm/adapters/bazaar_adapter.rb @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | af80e5618e9b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # redMine - project management software | |
2 # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require 'redmine/scm/adapters/abstract_adapter' | |
19 | |
20 module Redmine | |
21 module Scm | |
22 module Adapters | |
23 class BazaarAdapter < AbstractAdapter | |
24 | |
25 # Bazaar executable name | |
26 BZR_BIN = "bzr" | |
27 | |
28 # Get info about the repository | |
29 def info | |
30 cmd = "#{BZR_BIN} revno #{target('')}" | |
31 info = nil | |
32 shellout(cmd) do |io| | |
33 if io.read =~ %r{^(\d+)\r?$} | |
34 info = Info.new({:root_url => url, | |
35 :lastrev => Revision.new({ | |
36 :identifier => $1 | |
37 }) | |
38 }) | |
39 end | |
40 end | |
41 return nil if $? && $?.exitstatus != 0 | |
42 info | |
43 rescue CommandFailed | |
44 return nil | |
45 end | |
46 | |
47 # Returns an Entries collection | |
48 # or nil if the given path doesn't exist in the repository | |
49 def entries(path=nil, identifier=nil) | |
50 path ||= '' | |
51 entries = Entries.new | |
52 cmd = "#{BZR_BIN} ls -v --show-ids" | |
53 identifier = -1 unless identifier && identifier.to_i > 0 | |
54 cmd << " -r#{identifier.to_i}" | |
55 cmd << " #{target(path)}" | |
56 shellout(cmd) do |io| | |
57 prefix = "#{url}/#{path}".gsub('\\', '/') | |
58 logger.debug "PREFIX: #{prefix}" | |
59 re = %r{^V\s+(#{Regexp.escape(prefix)})?(\/?)([^\/]+)(\/?)\s+(\S+)\r?$} | |
60 io.each_line do |line| | |
61 next unless line =~ re | |
62 entries << Entry.new({:name => $3.strip, | |
63 :path => ((path.empty? ? "" : "#{path}/") + $3.strip), | |
64 :kind => ($4.blank? ? 'file' : 'dir'), | |
65 :size => nil, | |
66 :lastrev => Revision.new(:revision => $5.strip) | |
67 }) | |
68 end | |
69 end | |
70 return nil if $? && $?.exitstatus != 0 | |
71 logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug? | |
72 entries.sort_by_name | |
73 end | |
74 | |
75 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) | |
76 path ||= '' | |
77 identifier_from = 'last:1' unless identifier_from and identifier_from.to_i > 0 | |
78 identifier_to = 1 unless identifier_to and identifier_to.to_i > 0 | |
79 revisions = Revisions.new | |
80 cmd = "#{BZR_BIN} log -v --show-ids -r#{identifier_to.to_i}..#{identifier_from} #{target(path)}" | |
81 shellout(cmd) do |io| | |
82 revision = nil | |
83 parsing = nil | |
84 io.each_line do |line| | |
85 if line =~ /^----/ | |
86 revisions << revision if revision | |
87 revision = Revision.new(:paths => [], :message => '') | |
88 parsing = nil | |
89 else | |
90 next unless revision | |
91 | |
92 if line =~ /^revno: (\d+)($|\s\[merge\]$)/ | |
93 revision.identifier = $1.to_i | |
94 elsif line =~ /^committer: (.+)$/ | |
95 revision.author = $1.strip | |
96 elsif line =~ /^revision-id:(.+)$/ | |
97 revision.scmid = $1.strip | |
98 elsif line =~ /^timestamp: (.+)$/ | |
99 revision.time = Time.parse($1).localtime | |
100 elsif line =~ /^ -----/ | |
101 # partial revisions | |
102 parsing = nil unless parsing == 'message' | |
103 elsif line =~ /^(message|added|modified|removed|renamed):/ | |
104 parsing = $1 | |
105 elsif line =~ /^ (.*)$/ | |
106 if parsing == 'message' | |
107 revision.message << "#{$1}\n" | |
108 else | |
109 if $1 =~ /^(.*)\s+(\S+)$/ | |
110 path = $1.strip | |
111 revid = $2 | |
112 case parsing | |
113 when 'added' | |
114 revision.paths << {:action => 'A', :path => "/#{path}", :revision => revid} | |
115 when 'modified' | |
116 revision.paths << {:action => 'M', :path => "/#{path}", :revision => revid} | |
117 when 'removed' | |
118 revision.paths << {:action => 'D', :path => "/#{path}", :revision => revid} | |
119 when 'renamed' | |
120 new_path = path.split('=>').last | |
121 revision.paths << {:action => 'M', :path => "/#{new_path.strip}", :revision => revid} if new_path | |
122 end | |
123 end | |
124 end | |
125 else | |
126 parsing = nil | |
127 end | |
128 end | |
129 end | |
130 revisions << revision if revision | |
131 end | |
132 return nil if $? && $?.exitstatus != 0 | |
133 revisions | |
134 end | |
135 | |
136 def diff(path, identifier_from, identifier_to=nil) | |
137 path ||= '' | |
138 if identifier_to | |
139 identifier_to = identifier_to.to_i | |
140 else | |
141 identifier_to = identifier_from.to_i - 1 | |
142 end | |
143 cmd = "#{BZR_BIN} diff -r#{identifier_to}..#{identifier_from} #{target(path)}" | |
144 diff = [] | |
145 shellout(cmd) do |io| | |
146 io.each_line do |line| | |
147 diff << line | |
148 end | |
149 end | |
150 #return nil if $? && $?.exitstatus != 0 | |
151 diff | |
152 end | |
153 | |
154 def cat(path, identifier=nil) | |
155 cmd = "#{BZR_BIN} cat" | |
156 cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0 | |
157 cmd << " #{target(path)}" | |
158 cat = nil | |
159 shellout(cmd) do |io| | |
160 io.binmode | |
161 cat = io.read | |
162 end | |
163 return nil if $? && $?.exitstatus != 0 | |
164 cat | |
165 end | |
166 | |
167 def annotate(path, identifier=nil) | |
168 cmd = "#{BZR_BIN} annotate --all" | |
169 cmd << " -r#{identifier.to_i}" if identifier && identifier.to_i > 0 | |
170 cmd << " #{target(path)}" | |
171 blame = Annotate.new | |
172 shellout(cmd) do |io| | |
173 author = nil | |
174 identifier = nil | |
175 io.each_line do |line| | |
176 next unless line =~ %r{^(\d+) ([^|]+)\| (.*)$} | |
177 blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip)) | |
178 end | |
179 end | |
180 return nil if $? && $?.exitstatus != 0 | |
181 blame | |
182 end | |
183 end | |
184 end | |
185 end | |
186 end |