Class: ProxyTester::GitRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/proxy_tester/git_repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_path, file_creator = ProxyTester::GitFile, null_file = ProxyTester::GitNullFile.new) ⇒ GitRepository

Returns a new instance of GitRepository.



12
13
14
15
16
17
18
19
20
# File 'lib/proxy_tester/git_repository.rb', line 12

def initialize(storage_path, file_creator = ProxyTester::GitFile, null_file = ProxyTester::GitNullFile.new)
  @storage_path = ::File.expand_path(storage_path)
  @repository   = Rugged::Repository.new(storage_path)
  @file_creator = file_creator
  @null_file    = null_file

rescue Rugged::RepositoryError
  raise Exceptions::RepositoryDoesNotExist, JSON.dump(repository: storage_path)
end

Instance Attribute Details

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



10
11
12
# File 'lib/proxy_tester/git_repository.rb', line 10

def storage_path
  @storage_path
end

Class Method Details

.clone(source, destination, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/proxy_tester/git_repository.rb', line 29

def self.clone(source, destination, options =  {})
  bare = options.fetch(:bare, false)

  destination = ::File.expand_path(destination)

  if bare
    Rugged::Repository.clone_at(source, destination, bare: true)
  else
    Rugged::Repository.clone_at(source, destination)
  end

  new(destination)
rescue Rugged::NetworkError
  raise Exceptions::RepositoryDoesNotExist, JSON.dump(repository: source)
rescue Rugged::OSError
  raise Exceptions::RepositoryDoesNotExist, JSON.dump(repository: source)
rescue Rugged::RepositoryError
  raise Exceptions::RepositoryInvalid, JSON.dump(repository: source)
end

.create(storage_path) ⇒ Object



22
23
24
25
26
27
# File 'lib/proxy_tester/git_repository.rb', line 22

def self.create(storage_path) 
  storage_path = ::File.expand_path(storage_path)
  Rugged::Repository.init_at(storage_path, :bare)

  new(storage_path)
end

Instance Method Details

#add_content(path, content = '', mode = 0100644, commit_info = default_commit_info) ⇒ Object



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/proxy_tester/git_repository.rb', line 49

def add_content(path, content = '', mode = 0100644, commit_info = default_commit_info)
  index = Rugged::Index.new

  unless repository.empty? 
    root_commit  = repository.lookup(repository.head.target)

    root_commit.tree.walk_blobs(:postorder) do |r, e| 
      e.merge!( { path: "#{r}#{e[:name]}" } )
      e.delete(:name)

      index.add(e)
    end
  end

  oid = repository.write(content, :blob)
  index.add(:path => path, :oid => oid, :mode => mode)

  options              = {}
  options[:tree]       = index.write_tree(repository)
  options[:parents]    = repository.empty? ? [] : [ repository.head.target ].compact
  options[:update_ref] = 'HEAD'
  options.merge! commit_info

  Rugged::Commit.create(repository, options)
end

#all_filesObject



79
80
81
# File 'lib/proxy_tester/git_repository.rb', line 79

def all_files
  files.collect { |_,f| f }
end

#each_file(&block) ⇒ Object



83
84
85
# File 'lib/proxy_tester/git_repository.rb', line 83

def each_file(&block)
  all_files.each(&block)
end

#find_file(name) ⇒ Object



75
76
77
# File 'lib/proxy_tester/git_repository.rb', line 75

def find_file(name)
  files.fetch(name, null_file)
end