bzrwrapper 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +25 -0
  2. data/lib/bzrwrapper.rb +241 -0
  3. metadata +47 -0
data/README ADDED
@@ -0,0 +1,25 @@
1
+ # == Synopsis
2
+ # easy wrapper to get some information out of bazaar-branches
3
+ # wraps: bzr version-info, bzr log --forward
4
+ # processes the 'log' so you can deal with single 'Commits' that make up the log...
5
+ #
6
+ # == Author
7
+ # Rene Paulokat
8
+ #
9
+ # == Copyright
10
+ # Copyright (c) 2007 Rene Paulokat
11
+ #
12
+ # This program is free software; you can redistribute it and/or modify
13
+ # it under the terms of the GNU General Public License as published by
14
+ # the Free Software Foundation; either version 2 of the License, or
15
+ # (at your option) any later version.
16
+ #
17
+ # This program is distributed in the hope that it will be useful,
18
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ # GNU General Public License for more details.
21
+ #
22
+ # You should have received a copy of the GNU General Public License
23
+ # along with this program; if not, write to the Free Software
24
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
+ #
data/lib/bzrwrapper.rb ADDED
@@ -0,0 +1,241 @@
1
+ # BzrWrapper is a simple tool to extract information from
2
+ # bazaar-branches (http://bazaar-vcs.org)
3
+ # it gives u the opportunity to extract log/commits/version-info
4
+ #--
5
+ # Author:: rene paulolat (mailto: rene@so36.net)
6
+ # Copyright:: Copyright (c) 2007 rene paulokat
7
+ # License::
8
+ #
9
+ # This program is free software: you can redistribute it and/or modify
10
+ # it under the terms of the GNU General Public License as published by
11
+ # the Free Software Foundation, either version 3 of the License, or
12
+ # (at your option) any later version.
13
+ #
14
+ # This program is distributed in the hope that it will be useful,
15
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ # GNU General Public License for more details.
18
+ #
19
+ # You should have received a copy of the GNU General Public License
20
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
+ #
22
+
23
+ # :include: ./README
24
+
25
+ # == Extract information of bzr-branches
26
+ #
27
+ # pp branch = BzrWrapper::Branch.new('/home/rp/devel/rubzr')
28
+ # ->
29
+ ## <BzrWrapper::Branch:0x2b7b13335ce8
30
+ # @path="/home/rp/devel/rubzr",
31
+ # @version_info=
32
+ # #<BzrWrapper::Info:0x2b7b133352c0
33
+ # @branch_nick=" rubzr",
34
+ # @date=Wed Aug 22 11:22:03 +0200 2007,
35
+ # @revision_id=" rp@dwarf-20070822092203-xzink973ch9e5d07",
36
+ # @revno=" 6">>
37
+ #
38
+ # * iterate over the commits
39
+ # branch.log.each_entry { |e| puts e.time.to_s }
40
+ # -> Tue Aug 21 16:58:29 +0200 2007
41
+ # -> Tue Aug 21 16:57:22 +0200 2007
42
+ # * get the latest 2 commits
43
+ # BzrWrapper::Branch.new('/home/rp/devel/rubzr').last_commits(2) { |c| puts c.message + " / " + c.time.to_s }
44
+ # -> added Info / Tue Aug 21 16:57:22 +0200 2007
45
+ # -> log-parsing... / Tue Aug 21 21:05:42 +0200 2007
46
+ #
47
+ # == Classes
48
+ # * BzrWrapper::Branch
49
+ # * BzrWrapper::Log
50
+ # * BzrWrapper::Info
51
+ # * BzrWrapper::Commit
52
+ #
53
+ module BzrWrapper
54
+ require 'time'
55
+
56
+ # represents bazaar-branch
57
+ #
58
+ class Branch
59
+
60
+ # filepath to the bazaar-branch
61
+ attr_reader :path
62
+ # returns BzrWrapper::Info
63
+ attr_reader :version_info
64
+
65
+ # initiate instance with path to existent
66
+ # bazaar-branch
67
+ # :call-seq:
68
+ # BzrWrapper::Branch.new('/your/path/to/branch') -> BzrWrapper::Branch
69
+ #
70
+ def initialize(path)
71
+ unless File.exist?(path) && File.readable?(path)
72
+ raise ArgumentError, "no such file / not readable: #{path}"
73
+ end
74
+ begin
75
+ Branch.is_branch?(path)
76
+ rescue BzrException => e
77
+ puts "no branch: #{e}"
78
+ exit 0
79
+ end
80
+ @path = path
81
+ @version_info = create_info
82
+ end
83
+
84
+ # returns BzrWrapper::Log
85
+ #
86
+ def log
87
+ @log || create_log
88
+ end
89
+
90
+ # checks if given 'path' is a bazaar-branch
91
+ #
92
+ def Branch.is_branch?(path)
93
+ Dir.chdir(path) do |branch|
94
+ IO.popen('bzr version-info') do |io|
95
+ if io.gets.nil?
96
+ raise BzrException, "Not ab branch: #{path}"
97
+ else
98
+ return true
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ # returns last _num_ Commit's or
105
+ # yields commit if block given
106
+ #
107
+ def last_commits(num,&block)
108
+ x = -num
109
+ block_given? ? log.commits[x..-1].each { |commit| yield commit } : log.commits[x..-1]
110
+ end
111
+
112
+ private
113
+
114
+ # wraps system-call 'bzr version-info'
115
+ # and creates BzrWrapper::Info
116
+ #
117
+ def create_info
118
+ Dir.chdir(@path) do |path|
119
+ arr = []
120
+ hash = {}
121
+ IO.popen('bzr version-info') do |io|
122
+ unless io.eof?
123
+ io.readlines.each do |line|
124
+ k , v = line.split(':',2)
125
+ hash[k] = v
126
+ end
127
+ end
128
+ end
129
+ ['date','revno','branch-nick','revision-id'].each { |x| raise BzrException.new("no such info-key:: #{x}") unless hash.has_key?(x) }
130
+ return Info.new(hash['date'], hash['revno'], hash['branch-nick'], hash['revision-id'])
131
+ end
132
+ end
133
+
134
+ # wraps system-call 'bzr log --forward'
135
+ # and returns BzrWrapper::Log
136
+ #
137
+ def create_log(forward=true,start=1, stop=-1)
138
+ @entries = []
139
+ cmd = "bzr log "
140
+ cmd << "--forward " if forward
141
+ cmd << "-r" << start.to_s << ".." << stop.to_s
142
+ Dir.chdir(@path) do
143
+ offsets = []
144
+ arr = IO.popen(cmd).readlines
145
+ arr.each_index { |x| offsets.push(x) if arr[x].strip.chomp == Log::SEPARATOR }
146
+ while not offsets.empty?
147
+ hash = {}
148
+ start = @_tmp || offsets.shift
149
+ stop = if offsets.empty?
150
+ -1
151
+ else
152
+ @_tmp = offsets.shift
153
+ @_tmp
154
+ end
155
+ _arr = arr[start..stop]
156
+ _arr.each_index { |x| _arr.delete_at(x) if _arr[x].strip.chomp == Log::SEPARATOR }
157
+ _arr.each_with_index do |line,i|
158
+ if line.match('message:')
159
+ hash['message'] = _arr[i+1].chomp.strip unless _arr[i+1].nil?
160
+ else
161
+ line.gsub!('branch nick:', 'branch_nick:') if line.match('branch nick:')
162
+ ['revno:','committer:','branch_nick:','timestamp:','merged:'].each do |thing|
163
+ if line.match(thing)
164
+ hash[line.split(':',2)[0].chomp.strip] = line.split(':',2)[1].chomp.strip
165
+ end
166
+ end
167
+ end
168
+ end
169
+ @entries.push(Commit.new(hash['revno'],hash['committer'], hash['branch_nick'], hash['timestamp'], hash['message'],hash['merged']))
170
+ end
171
+ end
172
+ @log = Log.new(@entries)
173
+ end
174
+ end
175
+
176
+ # class representing status-information of given bazaar-branch
177
+ #
178
+ class Info
179
+ # date of last commit
180
+ attr_reader :date
181
+ # latest revision-number
182
+ attr_reader :revno
183
+ # name/nick of the branch
184
+ attr_reader :branch_nick
185
+ # latest revision-id
186
+ attr_reader :revision_id
187
+
188
+ def initialize(date_string, revision, nick, revision_id)
189
+ @date,@revno,@branch_nick, @revision_id = Time.parse(date_string.chomp.strip), revision.chomp, nick.chomp, revision_id.chomp
190
+ end
191
+
192
+ def to_s
193
+ "revision: #{@revno} | date: #{@date.to_s} | branch-nick: #{@branch_nick} | revision-id: #{@revision_id}"
194
+ end
195
+ end
196
+
197
+ # represents the branch-log
198
+ # with <n> 'Commit's
199
+ #
200
+ class Log
201
+ SEPARATOR = '------------------------------------------------------------'
202
+ # num of log-entries
203
+ attr_reader :count
204
+ # array of BzrWrapper::Commit's of this BzrWrapper::Log
205
+ attr_reader :commits
206
+
207
+ def initialize(le_array, &block)
208
+ @count = le_array.length
209
+ @commits = le_array
210
+ block_given? ? @commits.each { |x| yield x } : self
211
+ end
212
+
213
+ # Iterates over supplied Commits.
214
+ def each_entry
215
+ block_given? ? @commits.each { |c| yield c } : @commits
216
+ end
217
+
218
+ end
219
+
220
+ # represents single commit / checkin
221
+ #
222
+ class Commit
223
+ attr_reader :revno, :committer, :branch_nick, :timestamp, :message, :merged
224
+
225
+ def initialize(revno,committer,branch_nick,timestamp,message,merged=nil)
226
+ @revno,@committer,@branch_nick,@timestamp,@message,@merged = revno,committer,branch_nick,timestamp,message,merged
227
+ end
228
+
229
+ # returns time-object of this commit
230
+ #
231
+ def time
232
+ Time.parse(@timestamp)
233
+ end
234
+
235
+ end
236
+
237
+ class BzrException < Exception
238
+ end
239
+
240
+ end
241
+ #include BzrWrapper
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: bzrwrapper
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.1"
7
+ date: 2007-08-24 00:00:00 +02:00
8
+ summary: "Wrapper for bazaar-vcs to extract information of bazaar-branches / see: http://bazaar-vcs.org"
9
+ require_paths:
10
+ - lib
11
+ email: rene@so36.net
12
+ homepage: http://oopen.de
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: bzrwrapper
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Rene Paulokat
31
+ files:
32
+ - lib/bzrwrapper.rb
33
+ - README
34
+ test_files: []
35
+
36
+ rdoc_options: []
37
+
38
+ extra_rdoc_files:
39
+ - README
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies: []
47
+