Class: Git::Object::AbstractObject

Inherits:
Object
  • Object
show all
Defined in:
lib/git/object.rb

Overview

A base class for all Git objects

Direct Known Subclasses

Blob, Commit, Tag, Tree

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, objectish) ⇒ AbstractObject

Returns a new instance of AbstractObject.



18
19
20
21
22
23
24
25
# File 'lib/git/object.rb', line 18

def initialize(base, objectish)
  @base = base
  @objectish = objectish.to_s
  @contents = nil
  @trees = nil
  @size = nil
  @sha = nil
end

Instance Attribute Details

#mode

Returns the value of attribute mode.



14
15
16
# File 'lib/git/object.rb', line 14

def mode
  @mode
end

#objectish

Returns the value of attribute objectish.



14
15
16
# File 'lib/git/object.rb', line 14

def objectish
  @objectish
end

#size



31
32
33
# File 'lib/git/object.rb', line 31

def size
  @size ||= object_repository.cat_file_size(@objectish)
end

#type

Returns the value of attribute type.



14
15
16
# File 'lib/git/object.rb', line 14

def type
  @type
end

Instance Method Details

#archive(file = nil, opts = {}) ⇒ String

Creates an archive of this object and writes it to a file

Examples:

Archive a tree to a zip file

git.object('v1.0').archive('/tmp/release.zip', format: 'zip')

Parameters:

  • file (String, nil) (defaults to: nil)

    destination file path; a temp file is created if nil

  • opts (Hash) (defaults to: {})

    archive options (see Lib#archive)

Returns:

  • (String)

    the path to the written archive file

Raises:



115
116
117
# File 'lib/git/object.rb', line 115

def archive(file = nil, opts = {})
  object_repository.archive(@objectish, file, opts)
end

#blob?Boolean

Returns:

  • (Boolean)


121
# File 'lib/git/object.rb', line 121

def blob? = false

#commit?Boolean

Returns:

  • (Boolean)


123
# File 'lib/git/object.rb', line 123

def commit? = false

#contentsString #contents {|file| ... } ⇒ Object

Returns the raw content of this git object or streams it into a temporary file

Without a block, the full content is buffered in memory and cached, then returned as a String. With a block, git output is streamed directly to a temporary file on disk — suitable for large objects.

Overloads:

  • #contentsString

    Returns the cached content as a string.

    Examples:

    Get the contents of a blob

    git.object('HEAD:README.md').contents # => "This is a README file\n"

    Returns:

    • (String)

      the raw content of the object, cached after first call

    Raises:

  • #contents {|file| ... } ⇒ Object

    Streams the content to a temporary file and yields it.

    Git output is written directly to a file without buffering in memory. Use this form for large blobs to avoid memory pressure.

    Examples:

    Read a large blob without loading it into memory

    git.object('HEAD:large_file.bin').contents { |f| upload(f) }

    Yields:

    • (file)

      the temporary file, positioned at the start of the content

    Yield Parameters:

    • file (File)

      readable IO object positioned at the beginning

    Yield Returns:

    • (Object)

      the value to return from this method

    Returns:

    • (Object)

      the value returned by the block

    Raises:



72
73
74
75
76
77
78
# File 'lib/git/object.rb', line 72

def contents(&)
  if block_given?
    object_repository.cat_file_contents(@objectish, &)
  else
    @contents ||= object_repository.cat_file_contents(@objectish)
  end
end

#contents_array



80
81
82
# File 'lib/git/object.rb', line 80

def contents_array
  contents.split("\n")
end

#diff(objectish)



92
93
94
# File 'lib/git/object.rb', line 92

def diff(objectish)
  Git::Diff.new(@base, @objectish, objectish)
end

#grep(string, path_limiter = nil, opts = {})



88
89
90
# File 'lib/git/object.rb', line 88

def grep(string, path_limiter = nil, opts = {})
  object_repository.grep(string, path_limiter, opts.merge(object: sha))
end

#log(count = 30)



96
97
98
# File 'lib/git/object.rb', line 96

def log(count = 30)
  Git::Log.new(@base, count).object(@objectish)
end

#sha



27
28
29
# File 'lib/git/object.rb', line 27

def sha
  @sha ||= object_repository.rev_parse(@objectish)
end

#tag?Boolean

Returns:

  • (Boolean)


125
# File 'lib/git/object.rb', line 125

def tag? = false

#to_s



84
85
86
# File 'lib/git/object.rb', line 84

def to_s
  @objectish
end

#tree?Boolean

Returns:

  • (Boolean)


119
# File 'lib/git/object.rb', line 119

def tree? = false