Class: SCV::Repository

Inherits:
VCSToolkit::Repository
  • Object
show all
Defined in:
lib/scv/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, init: false) ⇒ Repository

Returns a new instance of Repository.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/scv/repository.rb', line 7

def initialize(path, init: false)
  repository_path = "#{path}/.scv"

  working_directory = FileStore.new   path
  object_store      = ObjectStore.new FileStore.new(repository_path)
  @config           = Config.new      FileStore.new(repository_path), 'config.yml'

  super object_store,
        working_directory,
        commit_class: Objects::Commit,
        tree_class:   Objects::Tree,
        blob_class:   Objects::Blob,
        label_class:  Objects::Label

  if init
    set_label :master, nil
    set_label :head,   :master

    @config['version'] = SCV::VERSION
    @config.save
  end

  self.head = get_object(:head).reference_id
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/scv/repository.rb', line 5

def config
  @config
end

Class Method Details

.create_at(working_directory) ⇒ Object

Create an empty repository in the specified directory.

Initializes the directory structure and creates a head label.



114
115
116
117
118
119
120
121
122
# File 'lib/scv/repository.rb', line 114

def self.create_at(working_directory)
  directories = %w(.scv/objects .scv/refs .scv/blobs)

  directories.each do |directory|
    FileUtils.mkdir_p File.join(working_directory, directory)
  end

  new working_directory, init: true
end

Instance Method Details

#[](object_id, to_type = nil) ⇒ Object

Convenience method to resolve objects



78
79
80
81
82
# File 'lib/scv/repository.rb', line 78

def [](object_id, to_type=nil)
  return super(object_id) if to_type.nil?

  resolve object_id, to_type
end

#delete_label(name) ⇒ Object

Delete a label (only).



87
88
89
90
91
92
93
# File 'lib/scv/repository.rb', line 87

def delete_label(name)
  label = get_object name

  raise 'The object is not a label' if label.object_type != :label

  object_store.remove name
end

#fetch(remote_store, remote_branch, local_branch) ⇒ Object

Fetch history from remote repository



105
106
107
# File 'lib/scv/repository.rb', line 105

def fetch(remote_store, remote_branch, local_branch)
  VCSToolkit::Utils::Sync.sync remote_store, remote_branch, object_store, local_branch
end

#push(remote_store, local_branch, remote_branch) ⇒ Object

Push history to remote repository



98
99
100
# File 'lib/scv/repository.rb', line 98

def push(remote_store, local_branch, remote_branch)
  VCSToolkit::Utils::Sync.sync object_store, local_branch, remote_store, remote_branch
end

#resolve(object_id, to_type, parent_offset: 0) ⇒ Object

Resolve references from ‘object_id` to the first object of type `to_type`. Examples:

label -> commit
label -> label  -> commit
label -> commit -> tree

If ‘object_id` contains ’~n’ suffix, where ‘n` >= 0 and the reference path contains a commit, the `parent` pointer of the commit is followed and the `n`-th commit is picked.

Raises:

  • (KeyError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/scv/repository.rb', line 44

def resolve(object_id, to_type, parent_offset: 0)
  if object_id =~ /^(.+)~([0-9]+)$/
    object_id    = $1
    parent_offset = $2.to_i
  end

  object = self[object_id]

  raise KeyError, "Cannot find object #{object_id}" if object.nil?

  if object.object_type == :commit and parent_offset > 0
    parent_offset.times do
      raise "Commit #{object.id} has more than one parent" if object.parents.size > 1
      raise "Commit #{object_id} has no parents"           if object.parents.size.zero?

      object = self[object.parents.first]
    end
  end

  case object.object_type
  when to_type
    object
  when :label
    resolve object.reference_id, to_type, parent_offset: parent_offset
  when :commit
    resolve object.tree, to_type
  else
    raise "Cannot resolve #{object_id} to a #{to_type}"
  end
end