Class: SvnFixture::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/svn-fixture/repository.rb

Overview

Repository sets up the repository and is reponsible for checkouts and the actual commit(s). No actual work is done until commit is called.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, repos_path = nil, wc_path = nil, &block) ⇒ Repository

Arguments (last two are optional)

  • name: The name of the repository, used by Repository.get and used in repos_path and wc_path if not given.

  • repos_path: The path where the repository is stored (defaults to “#:base_path/repo_#name”

  • wc_path: The path where the working copy is checked out (defaults to “#:base_path/wc_#name”

Note: the paths should be normal file system paths, not file:/// paths.

new also accepts a block which is evaluated within the Repository instance:

SvnFixture::Repository.new('name') do
  revision(1, 'log msg') ...
end

Otherwise, you could, for example:

r = SvnFixture::Repository.new('name')
r.revision(1, 'log msg') do
  ...
end
r.commit


67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/svn-fixture/repository.rb', line 67

def initialize(name, repos_path = nil, wc_path = nil, &block)
  @name = name
  if self.class.repositories[name]
    raise RuntimeError, "A Repository with this name (#{@name}) already exists."
  end
  
  @repos_path = repos_path || ::File.join(SvnFixture::config[:base_path], "repo_#{name}")
  @wc_path    = wc_path    || ::File.join(SvnFixture::config[:base_path], "wc_#{name}")
  check_paths_available
  @revisions = []
  @dirs_created = [] # Keep track of any directories created for use by #destroy
  self.class.repositories[name] = self
  self.instance_eval(&block) if block_given?
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



5
6
7
# File 'lib/svn-fixture/repository.rb', line 5

def ctx
  @ctx
end

#reposObject (readonly)

Returns the value of attribute repos.



5
6
7
# File 'lib/svn-fixture/repository.rb', line 5

def repos
  @repos
end

#revisionsObject (readonly)

Returns the value of attribute revisions.



5
6
7
# File 'lib/svn-fixture/repository.rb', line 5

def revisions
  @revisions
end

Class Method Details

.destroy_allObject

Remove all Repositories from .repositories and delete repos and working copy directories. Useful to call upon completion of tests.



39
40
41
# File 'lib/svn-fixture/repository.rb', line 39

def destroy_all
  repositories.each {|name, repo| repo.destroy}
end

.get(name, repos_path = nil, wc_path = nil, &block) ⇒ Object

Get an SvnFixture::Repository by name. If not found, it creates a new one. It accepts a block which is evaluated within the Repository instance. get is useful for re-accessing a Repository after initially created. For example:

SvnFixture::Repository.get('test') do
  revision(1, 'log msg') ...
end

SvnFixture::Repository.get('test') do
  revision(2, 'log msg') ...
  revision(3, 'log msg') ...
end

SvnFixture::Repository.get('test').commit


23
24
25
26
27
28
29
30
# File 'lib/svn-fixture/repository.rb', line 23

def get(name, repos_path = nil, wc_path = nil, &block)
  if repositories[name]
    repositories[name].instance_eval(&block) if block_given?
    repositories[name]
  else
    Repository.new(name, repos_path, wc_path, &block)
  end
end

.repositoriesObject

Hash of => Repository of currently defined Repositories



33
34
35
# File 'lib/svn-fixture/repository.rb', line 33

def repositories
  @repositories ||= {}
end

Instance Method Details

#checkoutObject

Checkout a working copy, and setup context. This is call by #commit unless something already exists at @wc_path. It can also be called directly. This allows the flexibility of doing some work between checking out the Repository and commit, or checking out some other way. Also, calls #create if needed.



113
114
115
116
117
118
119
120
121
# File 'lib/svn-fixture/repository.rb', line 113

def checkout
  create unless ::File.exist?(@repos_path)
  @repos = ::Svn::Repos.open(@repos_path)
  FileUtils.mkdir_p(@wc_path)
  @dirs_created << @wc_path
  @ctx = SvnFixture::simple_context
  @ctx.checkout(self.uri, @wc_path)
  self
end

#commit(*to_commit) ⇒ Object

Commit actually commits the changes of the revisions. It optionally accepts Revisions or Revision names. If none are given, it commits all revisions. If any of the arguments are Revisions (not revision names), they do not need to be explicitly part of this Repository (that is, they do not need to have been created through self#revision)

repos.commit # Commits all Revisions added through self#revision
repos.commit(1,2,4) # Commits Revisions named 1, 2, and 4, added through self#revision
repos.commit(rev1, rev3) # Assuming rev1 and rev3 are instances of
                         # SvnFixture::Revision, commits them
                         # whether or not they were added through self#revision

A Revision can be added to the revisions Array directly:

repos.revisions << Revision.new(1, 'msg')


138
139
140
141
142
143
144
145
146
147
# File 'lib/svn-fixture/repository.rb', line 138

def commit(*to_commit)
  checkout unless ::File.exist?(@wc_path)
  to_commit = @revisions if to_commit.empty?
  to_commit = [to_commit] if (!to_commit.respond_to?(:each) || to_commit.kind_of?(String))
  
  to_commit.each do | rev |
    rev = @revisions.find{ |r| r.name == rev } unless rev.kind_of?(Revision)
    rev.commit(self)
  end
end

#createObject

Create the Subversion repository. This is called by #checkout unless something already exists at @repos_path. It can also be called directly. This allows the flexibility of doing some work between creating the Repository and running checkout or commit (although I’ve yet to think of what that work would be), or creating the repository some other way.



101
102
103
104
105
106
# File 'lib/svn-fixture/repository.rb', line 101

def create
  FileUtils.mkdir_p(@repos_path)
  @dirs_created << @repos_path
  ::Svn::Repos.create(@repos_path)
  self
end

#destroyObject

Remove Repository from .repositories and delete repos and working copy directories.



151
152
153
154
# File 'lib/svn-fixture/repository.rb', line 151

def destroy
  @dirs_created.each { |d| FileUtils.rm_rf(d) }
  self.class.repositories.delete(@name)
end

#repos_pathObject

Absolute path to repository



162
163
164
# File 'lib/svn-fixture/repository.rb', line 162

def repos_path
  ::File.expand_path(@repos_path)
end

#revision(name, msg, options = {}, &block) ⇒ Object

Add a Revision to this Repository. name and msg are required.

  • name: A name (or number of Revision). This is used in informational messages only.

  • msg: Log message for the revision.

  • options: :author and :date Revision properties.

  • Accepts a block that is processed by Revision#commit within a Directory instance (the root directory at this revision). See Directory for more information.



90
91
92
93
94
# File 'lib/svn-fixture/repository.rb', line 90

def revision(name, msg, options = {}, &block)
  r = Revision.new(name, msg, options, &block)
  @revisions << r
  r
end

#uriObject

URI (file://…) for accessing the Repository



167
168
169
# File 'lib/svn-fixture/repository.rb', line 167

def uri
  "file://" + self.repos_path
end

#wc_pathObject

Absolute path to working copy



157
158
159
# File 'lib/svn-fixture/repository.rb', line 157

def wc_path
  ::File.expand_path(@wc_path)
end