Class: Amp::Core::Repositories::Git::LooseObject

Inherits:
RawObject
  • Object
show all
Defined in:
lib/amp-git/repo_format/loose_object.rb

Overview

LooseObject

A single loose object (tree, tag, commit, etc.) in the Git system. Its type and content will be determined after we read the file.

It is uniquely identified by a SHA1 hash.

Constant Summary

Constants inherited from RawObject

RawObject::AUTHOR_MATCH

Instance Attribute Summary collapse

Attributes inherited from RawObject

#content, #hash_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RawObject

construct, for_hash

Constructor Details

#initialize(hsh, opener, content = nil) ⇒ LooseObject

Initializes the RawObject. Needs a hash to identify it and an opener. The opener should point to the .git directory.

Parameters:

  • hsh (String)

    the hash to use to find the object

  • opener (Support::RootedOpener)

    the opener to use to open the object file



70
71
72
# File 'lib/amp-git/repo_format/loose_object.rb', line 70

def initialize(hsh, opener, content = nil)
  @hash_id, @opener, @content = hsh, opener, content
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



61
62
63
# File 'lib/amp-git/repo_format/loose_object.rb', line 61

def type
  @type
end

Class Method Details

.lookup(hsh, opener) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/amp-git/repo_format/loose_object.rb', line 36

def lookup(hsh, opener)
  require 'scanf'
  path = File.join("objects", hsh[0..1], hsh[2..40])
  mode = "r"
  type, content = nil, nil
  begin
    opener.open(path, mode) do |fp|
      type, content_size = fp.scanf("%s %d")
      fp.seek(type.size + 1 + content_size.to_s.size + 1, IO::SEEK_SET)
      content = fp.read(content_size)
    end
  rescue SystemCallError
    if create
      FileUtils.mkdir_p(opener.join("objects", hsh[0..1]))
      mode = "w+"
      retry
    else
      raise
    end
  end
  
  RawObject.construct(hsh, opener, type, content)
end