Class: Cir::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/cir/repository.rb

Overview

Main database with tracked files and such.

Constant Summary collapse

FILE_LIST =

Outside of the git repository we also have separate database of all files that we’re tracking with additional metadata stored in this yaml file.

'cir.file_list.yml'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rootPath) ⇒ Repository

Load repository (must exists) from given path.



43
44
45
46
47
# File 'lib/cir/repository.rb', line 43

def initialize(rootPath)
  # Database with files and their characteristics
  @git = Cir::GitRepository.new(rootPath)
  @database = YAML::Store.new(rootPath + '/' + FILE_LIST)
end

Class Method Details

.create(rootPath) ⇒ Object

Create new repository backend (initialize git repo and the metadata database)



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cir/repository.rb', line 26

def self.create(rootPath)
  git = Cir::GitRepository.create(rootPath)

  # Create database
  database = YAML::Store.new(rootPath + '/' + FILE_LIST)
  database.transaction do
    database[:version] = 1
    database[:files] = {} 
  end

  # Add it to git and finish
  git.add_file FILE_LIST
  git.commit
end

Instance Method Details

#deregister(files, options = {}) ⇒ Object

Deregister file

Options

:message -> optional git message that should be used


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/cir/repository.rb', line 77

def deregister(files, options = {})
  @database.transaction do
    expand(files) do |file|
      stored = stored_file(file)

      # Remove the file from git, our database and finally from git working directory
      FileUtils.rm(stored.repository_location)
      @git.remove_file(file[1..-1]) # Removing leading "/" to make the absolute path relative to the repository's root
      @database[:files].delete(file)

      puts "Deregistering file: #{file}"
    end
  end

  # And finally commit the transaction
  @git.commit(options[:message])
end

#register(files, options = {}) ⇒ Object

Register new file. Given path must be absolute.

Options

:message -> optional git message that should be used


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cir/repository.rb', line 54

def register(files, options = {})
  expand(files) do |file|
    # Register is one time operation, one can't re-register existing file
    raise Cir::Exception::AlreadyRegistered, file if registered?(file)

    # Import file to repository
    import_file file

    # Create new metadata for the tracked file
    @database.transaction { @database[:files][file] = {} }

    puts "Registering file: #{file}"
  end

  # And finally commit the transaction
  @git.commit(options[:message])
end

#registered?(file) ⇒ Boolean

Returns true if given file is registered, otherwise false.

Returns:

  • (Boolean)


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

def registered?(file)
  @database.transaction { return @database[:files][file] != nil }
end

#restore(requested_files = nil, force = false) ⇒ Object

Restore persistent variant of the files



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/cir/repository.rb', line 128

def restore(requested_files = nil, force = false)
  generate_file_list(requested_files).each do |file|
    # If the destination file doesn't exist, we will simply copy it over
    if not File.exists?(file.file_path)
      FileUtils.cp(file.repository_location, file.file_path)
      puts "Restoring #{file.file_path}"
      next
    end

    # Skipping files that did not changed
    next unless file.diff.changed?

    # If we're run with force or in case of specific files, remove existing file and replace it
    if force or not requested_files.nil?
      FileUtils.remove_entry(file.file_path)
      FileUtils.cp(file.repository_location, file.file_path)
      puts "Restoring #{file.file_path}"
    else
      puts "Skipped mass change to #{key}."
    end
  end
end

#status(requested_files = nil) ⇒ Object

Return status for all registered files



104
105
106
# File 'lib/cir/repository.rb', line 104

def status(requested_files = nil)
  generate_file_list(requested_files)
end

#update(requested_files = nil, options = {}) ⇒ Object

Will update stored variant of existing files with their newer copy

Options

:message -> optional git message that should be used


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

def update(requested_files = nil, options = {})
  generate_file_list(requested_files).each do |file|
    if file.diff.changed?
      import_file(file.file_path)
      puts "Updating #{file.file_path}"
    end
  end

  # Finally commit the transaction
  @git.commit(options[:message])
end