Class: LocalPac::GitRepository

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_path, file_creator = LocalPac::File, null_file = LocalPac::NullFile.new) ⇒ GitRepository



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

def initialize(storage_path, file_creator = LocalPac::File, null_file = LocalPac::NullFile.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, "Sorry, but #{storage_path} is not a git repository."
end

Instance Attribute Details

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



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

def storage_path
  @storage_path
end

Class Method Details

.create(storage_path) ⇒ Object



22
23
24
25
26
27
# File 'lib/local_pac/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



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
# File 'lib/local_pac/git_repository.rb', line 29

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

#added_files(old_commit_id, new_commit_id) ⇒ Object



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

def added_files(old_commit_id, new_commit_id)
  begin
    commit_old = repository.lookup(old_commit_id)
  rescue Rugged::OdbError
    LocalPac.ui_logger.warn "Invalid commit id \"#{old_commit_id}\". Object cannot be found in repository \"#{storage_path}\"."
    raise Exceptions::CommitDoesNotExist
  end

  begin
    commit_new = repository.lookup(new_commit_id)
  rescue Rugged::OdbError
    LocalPac.ui_logger.warn "Invalid commit id \"#{old_commit_id}\". Object cannot be found in repository \"#{storage_path}\"."
    raise Exceptions::CommitDoesNotExist
  end

  commit_old.diff(commit_new).deltas.keep_if { |d| [:added, :modified].include?(d.status) }.collect { |d| File.new(d.new_file[:path], repository.lookup(d.new_file[:oid]).content) }
end

#all_filesObject



77
78
79
# File 'lib/local_pac/git_repository.rb', line 77

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

#each_file(&block) ⇒ Object



81
82
83
# File 'lib/local_pac/git_repository.rb', line 81

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

#find_file(name) ⇒ Object



73
74
75
# File 'lib/local_pac/git_repository.rb', line 73

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