Class: Git::Object::AbstractObject Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a lazy wrapper for a git object

Parameters:

  • base (Git::Repository)

    the repository used to query object data

  • objectish (String, #to_s)

    the object name, SHA, ref, or treeish path



43
44
45
46
47
48
49
50
# File 'lib/git/object.rb', line 43

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

Instance Attribute Details

#modeString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the file mode from tree listings.

Returns:

  • (String, nil)

    the file mode from tree listings



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

def mode
  @mode
end

#objectishString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the object name, SHA, ref, or treeish path.

Returns:

  • (String)

    the object name, SHA, ref, or treeish path



18
19
20
# File 'lib/git/object.rb', line 18

def objectish
  @objectish
end

#sizeInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the size of this object in bytes

Returns:

  • (Integer)

    the object size in bytes



64
65
66
# File 'lib/git/object.rb', line 64

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

#typeString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the git object type.

Returns:

  • (String, nil)

    the git object type



22
23
24
# File 'lib/git/object.rb', line 22

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')

Archive a tree to a temporary tar file

git.object('v2.6').archive(nil, format: 'tar')

Archive a tree to a tgz file with a path prefix

git.object('v2.6').archive('/tmp/release.tgz', format: 'tgz', prefix: 'test/')

Archive one directory with a path prefix

git.object('v2.6').archive(
  '/tmp/ex-dir.tar',
  format: 'tar',
  prefix: 'test/',
  path: 'ex_dir/'
)

Parameters:

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

    destination file path; a temp file is created if nil

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

    archive options (see Repository#archive)

Options Hash (opts):

  • :format (String) — default: 'zip'

    archive format: 'tar', 'zip', or 'tgz'

  • :prefix (String) — default: nil

    prefix prepended to every filename in the archive

  • :path (String) — default: nil

    path within the tree to include

  • :remote (String) — default: nil

    retrieve the archive from a remote repository

  • :add_gzip (Boolean, nil) — default: nil

    apply gzip compression after writing the archive

Returns:

  • (String)

    the path to the written archive file

Raises:

  • (ArgumentError)

    when archive options or destination path are invalid

  • (Git::FailedError)

    if git archive fails



228
229
230
# File 'lib/git/object.rb', line 228

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

#blob?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether this object is a blob

Returns:

  • (Boolean)

    true when this object is a blob



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

def blob? = false

#commit?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether this object is a commit

Returns:

  • (Boolean)

    true when this object is a commit



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

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:



105
106
107
108
109
110
111
# File 'lib/git/object.rb', line 105

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

#contents_arrayArray<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the object contents split into lines

Returns:

  • (Array<String>)

    the raw contents split on newline boundaries



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

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

#diff(objectish) ⇒ Git::Diff

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a diff from this object to another object

Parameters:

  • objectish (String)

    the object name, SHA, ref, or treeish path to diff against

Returns:

  • (Git::Diff)

    the diff between the two objects



169
170
171
# File 'lib/git/object.rb', line 169

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

#grep(string, path_limiter = nil, opts = {}) ⇒ Hash<String, Array<Array(Integer, String)>>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Searches this object for matching tracked file contents

Always searches this object's resolved SHA. A caller-provided :object option is ignored.

Parameters:

  • string (String)

    the pattern to search for

  • path_limiter (String, Pathname, Array<String, Pathname>, nil) (defaults to: nil)

    path or paths to limit the search to

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

    additional grep options

Options Hash (opts):

  • :ignore_case (Boolean, nil) — default: nil

    ignore case distinctions in the pattern and file contents

    Alias: :i

  • :invert_match (Boolean, nil) — default: nil

    select non-matching lines

    Alias: :v

  • :extended_regexp (Boolean, nil) — default: nil

    use POSIX extended regular expressions for the pattern

    Alias: :E

Returns:

  • (Hash<String, Array<Array(Integer, String)>>)

    matching lines by path



158
159
160
# File 'lib/git/object.rb', line 158

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

#log(count = 30) ⇒ Git::Log

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a log scoped to this object

Parameters:

  • count (Integer) (defaults to: 30)

    maximum number of commits to include

Returns:



179
180
181
# File 'lib/git/object.rb', line 179

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

#shaString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the resolved SHA for this object

Returns:

  • (String)

    the resolved object SHA



56
57
58
# File 'lib/git/object.rb', line 56

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

#tag?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether this object is a tag

Returns:

  • (Boolean)

    true when this object is a tag



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

def tag? = false

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the original object expression

Returns:

  • (String)

    the object name, SHA, ref, or treeish path



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

def to_s
  @objectish
end

#tree?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether this object is a tree

Returns:

  • (Boolean)

    true when this object is a tree



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

def tree? = false