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.



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

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.



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

def mode
  @mode
end

#objectish

Returns the value of attribute objectish.



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

def objectish
  @objectish
end

#size



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

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

#type

Returns the value of attribute type.



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

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 Repository#archive)

Returns:

  • (String)

    the path to the written archive file

Raises:



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

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

#blob?Boolean

Returns:

  • (Boolean)


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

def blob? = false

#commit?Boolean

Returns:

  • (Boolean)


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

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:



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

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

#contents_array



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

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

#diff(objectish)



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

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

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



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

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

#log(count = 30)



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

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

#sha



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

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

#tag?Boolean

Returns:

  • (Boolean)


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

def tag? = false

#to_s



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

def to_s
  @objectish
end

#tree?Boolean

Returns:

  • (Boolean)


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

def tree? = false