* subversion/bindings/swig/python/libsvn_swig_py/swigutil_py.c
[svn.git] / subversion / bindings / swig / ruby / svn / fs.rb
blob88941f8c614591f5a1fcb137b31d88aaa862055e
1 require "English"
2 require "tempfile"
3 require "svn/error"
4 require "svn/util"
5 require "svn/delta"
6 require "svn/ext/fs"
8 module Svn
9   module Fs
10     Util.set_constants(Ext::Fs, self)
11     Util.set_methods(Ext::Fs, self)
13     @@fs_pool = Svn::Core::Pool.new
14     initialize(@@fs_pool)
16     class << self
17       def modules
18         print_modules("")
19       end
20     end
22     @@alias_targets = %w(create delete open hotcopy recover)
23     class << self
24       @@alias_targets.each do |target|
25         alias_method "_#{target}", target
26       end
27     end
28     @@alias_targets.each do |target|
29       alias_method "_#{target}", target
30     end
31     @@alias_targets = nil
33     module_function
34     def create(path, config={}, &block)
35       _create(path, config, &block)
36     end
38     def delete(path)
39       Fs.delete_fs(path)
40     end
42     def open(path, config={}, &block)
43       _open(path, config, &block)
44     end
46     def hotcopy(src, dest, clean=false)
47       _hotcopy(src, dest, clean)
48     end
50     def recover(path, &cancel_func)
51       _recover(path, cancel_func)
52     end
54     FileSystem = SWIG::TYPE_p_svn_fs_t
55     class FileSystem
56       class << self
57         # For backward compatibility
58         def create(*args, &block)
59           Fs.create(*args, &block)
60         end
62         def delete(*args, &block)
63           Fs.delete(*args, &block)
64         end
66         def open(*args, &block)
67           Fs.open(*args, &block)
68         end
69         alias new open
71         def hotcopy(*args, &block)
72           Fs.hotcopy(*args, &block)
73         end
75         def recover(*args, &block)
76           Fs.recover(*args, &block)
77         end
78       end
80       def set_warning_func(&func)
81         Fs.set_warning_func_wrapper(self, func)
82       end
84       def path
85         Fs.path(self)
86       end
88       def open_txn(name)
89         Fs.open_txn(self, name)
90       end
92       def purge_txn(id)
93         Fs.purge_txn(self, id)
94       end
96       def transaction(rev=nil, flags=0)
97         txn = Fs.begin_txn2(self, rev || youngest_rev, flags)
99         if block_given?
100           yield(txn)
101           txn.commit if transactions.include?(txn.name)
102         else
103           txn
104         end
105       end
107       def youngest_revision
108         Fs.youngest_rev(self)
109       end
110       alias_method :youngest_rev, :youngest_revision
112       def deleted_revision(path, start_rev, end_rev)
113         Repos.deleted_rev(self, path, start_rev, end_rev)
114       end
115       alias_method :deleted_rev, :deleted_revision
117       def prop(name, rev=nil)
118         value = Fs.revision_prop(self, rev || youngest_rev, name)
119         if value and name == Svn::Core::PROP_REVISION_DATE
120           Time.parse_svn_format(value)
121         else
122           value
123         end
124       end
126       def set_prop(name, value, rev=nil)
127         Fs.change_rev_prop(self, rev || youngest_rev, name, value)
128       end
130       def proplist(rev=nil)
131         list = Fs.revision_proplist(self, rev || youngest_rev)
132         date_str = list[Svn::Core::PROP_REVISION_DATE]
133         if date_str
134           list[Svn::Core::PROP_REVISION_DATE] = Time.parse_svn_format(date_str)
135         end
136         list
137       end
139       def transactions
140         Fs.list_transactions(self)
141       end
143       def root(rev=nil)
144         Fs.revision_root(self, rev || youngest_rev)
145       end
147       def access
148         Fs.get_access(self)
149       end
151       def access=(new_access)
152         Fs.set_access(self, new_access)
153       end
155       def deltify_revision(rev=nil)
156         Fs.deltify_revision(self, rev || youngest_rev)
157       end
159       def uuid
160         Fs.get_uuid(self)
161       end
163       def uuid=(new_uuid)
164         Fs.set_uuid(self, new_uuid)
165       end
167       def lock(path, token=nil, comment=nil, dav_comment=true,
168                expiration_date=nil, current_rev=nil, steal_lock=false)
169         if expiration_date
170           expiration_date = expiration_date.to_apr_time
171         else
172           expiration_date = 0
173         end
174         current_rev ||= youngest_rev
175         Fs.lock(self, path, token, comment, dav_comment,
176                 expiration_date, current_rev, steal_lock)
177       end
179       def unlock(path, token, break_lock=false)
180         Fs.unlock(self, path, token, break_lock)
181       end
183       def generate_lock_token
184         Fs.generate_lock_token(self)
185       end
187       def get_lock(path)
188         Fs.get_lock(self, path)
189       end
191       def get_locks(path)
192         locks = []
193         receiver = Proc.new do |lock|
194           locks << lock
195           yield(lock) if block_given?
196         end
197         Fs.get_locks(self, path, receiver)
198         locks
199       end
201       def history(path, start_rev, end_rev,
202                   cross_copies=true, authz_read_func=nil)
203         hist = []
204         history_func = Proc.new do |path, revision|
205           yield(path, revision) if block_given?
206           hist << [path, revision]
207         end
208         Repos.history2(self, path, history_func,
209                        authz_read_func, start_rev, end_rev,
210                        cross_copies)
211         hist
212       end
214       def trace_node_locations(fs_path, location_revisions,
215                                peg_rev=nil, &authz_read_func)
216         peg_rev ||= youngest_rev
217         Repos.trace_node_locations(self, fs_path, peg_rev,
218                                    location_revisions,
219                                    authz_read_func)
220       end
221     end
223     Access = SWIG::TYPE_p_svn_fs_access_t
224     class Access
225       class << self
226         def new(username)
227           Fs.create_access(username)
228         end
229       end
231       def username
232         Fs.access_get_username(self)
233       end
235       def add_lock_token(token)
236         Fs.access_add_lock_token(self, token)
237       end
238     end
240     Transaction = SWIG::TYPE_p_svn_fs_txn_t
241     class Transaction
243       def name
244         Fs.txn_name(self)
245       end
247       def prop(name)
248         value = Fs.txn_prop(self, name)
249         if name == Svn::Core::PROP_REVISION_DATE and value
250           value = Time.parse_svn_format(value)
251         end
252         value
253       end
255       def set_prop(name, value, validate=true)
256         if validate
257           Repos.fs_change_txn_prop(self, name, value)
258         else
259           Fs.change_txn_prop(self, name, value)
260         end
261       end
263       def base_revision
264         Fs.txn_base_revision(self)
265       end
267       def root
268         Fs.txn_root(self)
269       end
271       def proplist
272         list = Fs.txn_proplist(self)
273         date_str = list[Svn::Core::PROP_REVISION_DATE]
274         if list[Svn::Core::PROP_REVISION_DATE]
275           list[Svn::Core::PROP_REVISION_DATE] = Time.parse_svn_format(date_str)
276         end
277         list
278       end
280       def abort
281         Fs.abort_txn(self)
282       end
284       def commit
285         result = Fs.commit_txn(self)
286         if result.is_a?(Array)
287           result
288         else
289           [nil, result]
290         end
291       end
292     end
295     Root = SWIG::TYPE_p_svn_fs_root_t
296     class Root
297       attr_reader :editor
299       def dir?(path)
300         Fs.is_dir(self, path)
301       end
303       def file?(path)
304         Fs.is_file(self, path)
305       end
307       def base_revision
308         Fs.txn_root_base_revision(self)
309       end
311       def revision
312         Fs.revision_root_revision(self)
313       end
315       def name
316         Fs.txn_root_name(self)
317       end
319       def fs
320         Fs.root_fs_wrapper(self)
321       end
323       def node_id(path)
324         Fs.node_id(self, path)
325       end
327       def node_created_rev(path)
328         Fs.node_created_rev(self, path)
329       end
331       def node_created_path(path)
332         Fs.node_created_path(self, path)
333       end
335       def node_prop(path, key)
336         Fs.node_prop(self, path, key)
337       end
339       def set_node_prop(path, key, value, validate=true)
340         if validate
341           Repos.fs_change_node_prop(self, path, key, value)
342         else
343           Fs.change_node_prop(self, path, key, value)
344         end
345       end
347       def node_proplist(path)
348         Fs.node_proplist(self, path)
349       end
350       alias node_prop_list node_proplist
352       def check_path(path)
353         Fs.check_path(self, path)
354       end
356       def file_length(path)
357         Fs.file_length(self, path)
358       end
360       def file_md5_checksum(path)
361         Fs.file_md5_checksum(self, path)
362       end
364       def file_contents(path)
365         stream = Fs.file_contents(self, path)
366         if block_given?
367           begin
368             yield stream
369           ensure
370             stream.close
371           end
372         else
373           stream
374         end
375       end
377       def close
378         Fs.close_root(self)
379       end
381       def dir_entries(path)
382         entries = {}
383         Fs.dir_entries(self, path).each do |name, entry|
384           entries[name] = entry
385         end
386         entries
387       end
389       def dir_delta(src_path, src_entry, tgt_root, tgt_path,
390                     editor, text_deltas=false, recurse=true,
391                     entry_props=false, ignore_ancestry=false,
392                     &authz_read_func)
393         Repos.dir_delta(self, src_path, src_entry, tgt_root,
394                         tgt_path, editor, authz_read_func,
395                         text_deltas, recurse, entry_props,
396                         ignore_ancestry)
397       end
399       def replay(editor, base_dir=nil, low_water_mark=nil, send_deltas=false,
400                  &callback)
401         base_dir ||= ""
402         low_water_mark ||= Core::INVALID_REVNUM
403         Repos.replay2(self, base_dir, low_water_mark, send_deltas,
404                       editor, callback)
405       end
407       def copied_from(path)
408         Fs.copied_from(self, path)
409       end
411       def txn_root?
412         Fs.is_txn_root(self)
413       end
415       def revision_root?
416         Fs.is_revision_root(self)
417       end
419       def paths_changed
420         Fs.paths_changed(self)
421       end
423       def node_history(path)
424         Fs.node_history(self, path)
425       end
427       def props_changed?(path1, root2, path2)
428         Fs.props_changed(self, path1, root2, path2)
429       end
431       def merge(target_path, source_root, source_path,
432                 ancestor_root, ancestor_path)
433         Fs.merge(source_root, source_path, self, target_path,
434                  ancestor_root, ancestor_path)
435       end
437       def make_dir(path)
438         Fs.make_dir(self, path)
439       end
441       def delete(path)
442         Fs._delete(self, path)
443       end
445       def copy(to_path, from_root, from_path)
446         Fs.copy(from_root, from_path, self, to_path)
447       end
449       def revision_link(from_root, path)
450         Fs.revision_link(from_root, self, path)
451       end
453       def make_file(path)
454         Fs.make_file(self, path)
455       end
457       def apply_textdelta(path, base_checksum=nil, result_checksum=nil)
458         handler, handler_baton = Fs.apply_textdelta(self, path,
459                                                     base_checksum,
460                                                     result_checksum)
461         handler.baton = handler_baton
462         handler
463       end
465       def apply_text(path, result_checksum=nil)
466         Fs.apply_text(self, path, result_checksum)
467       end
469       def contents_changed?(path1, root2, path2)
470         Fs.contents_changed(self, path1, root2, path2)
471       end
473       def file_delta_stream(arg1, arg2, target_path)
474         if arg1.is_a?(self.class)
475           source_root = arg1
476           source_path = arg2
477           target_root = self
478         else
479           source_root = self
480           source_path = arg1
481           target_root = arg2
482         end
483         Fs.get_file_delta_stream(source_root, source_path,
484                                  target_root, target_path)
485       end
487       def stat(path)
488         Repos.stat(self, path)
489       end
491       def committed_info(path)
492         Repos.get_committed_info(self, path)
493       end
495       def closest_copy(path)
496         Fs.closest_copy(self, path)
497       end
499       def mergeinfo(paths, inherit=nil, include_descendants=false)
500         paths = [paths] unless paths.is_a?(Array)
501         Fs.get_mergeinfo(self, paths, inherit, include_descendants)
502       end
504       def change_mergeinfo(path, info)
505         Fs.change_mergeinfo(self, path, info)
506       end
507     end
509     History = SWIG::TYPE_p_svn_fs_history_t
511     class History
512       def location
513         Fs.history_location(self)
514       end
516       def prev(cross_copies=true)
517         Fs.history_prev(self, cross_copies)
518       end
519     end
522     DirectoryEntry = Dirent
524     Id = SWIG::TYPE_p_svn_fs_id_t
525     class Id
526       def to_s
527         unparse
528       end
530       def unparse
531         Fs.unparse_id(self)
532       end
534       def compare(other)
535         Fs.compare_ids(self, other)
536       end
538       def related?(other)
539         Fs.check_related(self, other)
540       end
541     end
543     class PathChange
544       def modify?
545         change_kind == Svn::Fs::PATH_CHANGE_MODIFY
546       end
548       def add?
549         change_kind == Svn::Fs::PATH_CHANGE_ADD
550       end
552       def text_mod?
553         text_mod
554       end
555     end
557     class FileDiff
559       def initialize(root1, path1, root2, path2)
560         @tempfile1 = nil
561         @tempfile2 = nil
563         @binary = nil
565         @root1 = root1
566         @path1 = path1
567         @root2 = root2
568         @path2 = path2
569       end
571       def binary?
572         if @binary.nil?
573           @binary = _binary?(@root1, @path1)
574           @binary ||= _binary?(@root2, @path2)
575         end
576         @binary
577       end
579       def files
580         if @tempfile1
581           [@tempfile1, @tempfile2]
582         else
583           @tempfile1 = Tempfile.new("svn_fs")
584           @tempfile2 = Tempfile.new("svn_fs")
586           dump_contents(@tempfile1, @root1, @path1)
587           dump_contents(@tempfile2, @root2, @path2)
589           [@tempfile1, @tempfile2]
590         end
591       end
593       def diff
594         files
595         @diff ||= Core::Diff.file_diff(@tempfile1.path, @tempfile2.path)
596       end
598       def unified(label1, label2)
599         if diff and diff.diff?
600           diff.unified(label1, label2)
601         else
602           ""
603         end
604       end
606       private
607       def dump_contents(tempfile, root, path)
608         if root and path
609           begin
610             tempfile.open
611             root.file_contents(path) do |stream|
612               tempfile.print(stream.read)
613             end
614           ensure
615             tempfile.close
616           end
617         end
618       end
620       def _binary?(root, path)
621         if root and path
622           prop = root.node_prop(path, Core::PROP_MIME_TYPE)
623           prop and Core.binary_mime_type?(prop)
624         end
625       end
626     end
627   end