Class: Gitback::Repository

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_path, &block) ⇒ Repository

Returns a new instance of Repository.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gitback/repository.rb', line 6

def initialize(repository_path, &block)
  @repository_path = clean_path(repository_path)

  if File.exists?(@repository_path)
    prepare_git_repository

    if @repo
      yield self
      commit_git_changes
    end
  else
    puts "ERROR: it doesn't look like '#{@repository_path}' exists."
  end
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



4
5
6
# File 'lib/gitback/repository.rb', line 4

def branch
  @branch
end

#remoteObject

Returns the value of attribute remote.



3
4
5
# File 'lib/gitback/repository.rb', line 3

def remote
  @remote
end

Instance Method Details

#backup(file_path) ⇒ Object

Sets up the paths and copies the files into the git repo



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitback/repository.rb', line 29

def backup(file_path)
  file_path = clean_path(file_path)

  begin
    dest_path = file_path
    dest_path = '/' + @namespace + file_path if @namespace
    dest_path = @repository_path + dest_path

    dirname = File.dirname(dest_path)

    # Make sure the path exists in the repo
    if !File.exists?(dirname)
      FileUtils.mkdir_p(dirname)
    end

    # Copy the file(s) to the repo
    if File.exists?(file_path)
      # Make sure we only copy the directory contents,
      # not the directory itself.
      if File.directory?(file_path)
        file_path += '/.'
      end

      # We pass remove_destination to avoid issues with symlinks
      FileUtils.cp_r file_path, dest_path, :remove_destination => true
    else
      puts "ERROR: '#{file_path}' doesn't seem to exist."
    end
  rescue Errno::EACCES
    puts "ERROR: '#{file_path}' doesn't seem to be readable and/or writable by this user."
  end
end

#namespace(name) {|_self| ... } ⇒ Object

Creates a namespace for files to be stored under Allows a single repository to be used for many different backups

Yields:

  • (_self)

Yield Parameters:



23
24
25
26
# File 'lib/gitback/repository.rb', line 23

def namespace(name, &block)
  @namespace = clean_path(name)
  yield self
end