Module: Git::Repository::ObjectOperations Private
- Included in:
- Git::Repository
- Defined in:
- lib/git/repository/object_operations.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Facade methods for raw git object store queries
Included by Git::Repository.
Instance Method Summary collapse
-
#archive(treeish, file = nil, opts = {}) ⇒ String
private
Create an archive of the repository tree and write it to a file.
-
#cat_file_commit(object) ⇒ Hash
private
Returns parsed commit data for the given git object.
-
#cat_file_contents(object, &block)
(also: #cat_file)
private
Returns the raw content of a git object, or streams it into a tempfile.
-
#cat_file_size(object) ⇒ Integer
private
Returns the size of a git object in bytes.
-
#cat_file_tag(object) ⇒ Hash
private
Returns parsed tag data for the given annotated tag object.
-
#cat_file_type(object) ⇒ String
private
Returns the type of a git object.
-
#full_tree(objectish) ⇒ Array<String>
private
Returns all recursive entries for a given tree object.
-
#gblob(objectish) ⇒ Git::Object::Blob
private
Returns a blob object for the given object reference.
-
#gcommit(objectish) ⇒ Git::Object::Commit
private
Returns a commit object for the given object reference.
-
#grep(pattern, path_limiter = nil, opts = {}) ⇒ Hash<String, Array<Array(Integer, String)>>
private
Search tracked file contents in a git tree for a pattern.
-
#gtree(objectish) ⇒ Git::Object::Tree
private
Returns a tree object for the given object reference.
-
#ls_tree(objectish, opts = {}) ⇒ Hash<String, Hash<String, Hash>>
private
List the objects in a git tree.
-
#name_rev(commit_ish) ⇒ String?
private
Find the first symbolic name for a commit-ish.
-
#object(objectish) ⇒ Git::Object::Blob, ...
private
Returns the appropriate git object for the given object reference.
-
#rev_parse(objectish) ⇒ String
private
Resolve a revision specifier to its full object ID.
-
#tag_create(name, *args)
private
Create a new tag and return its metadata.
-
#tag_delete(name) ⇒ String
private
Delete a tag.
-
#tag_list(*patterns) ⇒ Array<Git::TagInfo>
private
Returns the tags in the repository as structured objects.
-
#tag_sha(tag_name) ⇒ String
private
Returns the SHA of a named tag.
-
#tree_depth(objectish) ⇒ Integer
private
Returns the number of entries in a tree.
Instance Method Details
#archive(treeish, file = nil, opts = {}) ⇒ 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.
A failure leaves nothing behind: the staging and temporary files
are removed and an existing file is not replaced.
Create an archive of the repository tree and write it to a file
Writes the archive content to a file and returns the file path. The
default format is zip. Pass format: 'tar' for an uncompressed tar
archive, or format: 'tgz' for a gzip-compressed tar archive
(equivalent to format: 'tar' with add_gzip: true).
When no file path is given, a temporary file is created and its path
is returned.
File replacement behavior when file is given:
The archive is first written to a staging file in the same directory as
file. This means write permission is required on the parent directory
of file, not just on file itself. Once the archive is fully written,
the staging file atomically replaces file via rename.
If file already exists, only its numeric permission bits are applied to
the new archive; ownership, ACLs, and extended attributes are not
transferred. If file does not exist, the archive receives the standard
file creation mode (0666 & ~umask). On Windows, File.chmod has no
effect, so the archive always receives the default creation mode
regardless of whether file already exists.
If file is a symlink that does not point to a directory, the symlink
itself is replaced by the new archive file rather than writing through
the link to its target. A symlink that points to a directory is treated
as a directory and rejected with ArgumentError.
597 598 599 600 601 602 603 604 |
# File 'lib/git/repository/object_operations.rb', line 597 def archive(treeish, file = nil, opts = {}) SharedPrivate.assert_valid_opts!(ARCHIVE_ALLOWED_OPTS, **opts) raise ArgumentError, "#{file.inspect} is a directory" if file && File.directory?(file) Git::SystemCallGuard.call('Failed to write the archive') do Private.write_archive(@execution_context, treeish, file, opts) end end |
#cat_file_commit(object) ⇒ Hash
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 parsed commit data for the given git object
184 185 186 187 |
# File 'lib/git/repository/object_operations.rb', line 184 def cat_file_commit(object) result = Git::Commands::CatFile::Raw.new(@execution_context).call('commit', object) Git::Parsers::CatFile.parse_commit(result.stdout.split("\n"), object) end |
#cat_file_contents(object) ⇒ String #cat_file_contents(object) {|file| ... } ⇒ Object Also known as: cat_file
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 raw content of a git object, or streams it into a tempfile
Without a block, the full content is buffered in memory and returned as a
String. With a block, git output is streamed directly to disk without
memory buffering — safe for large blobs.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/git/repository/object_operations.rb', line 81 def cat_file_contents(object, &block) raise ArgumentError, "Invalid object: '#{object}'" if object&.start_with?('-') return Git::Commands::CatFile::Raw.new(@execution_context).call(object, p: true).stdout unless block_given? # Stream git output directly to a tempfile to avoid buffering large # object content in memory when a block is given. Git::SystemCallGuard.call('Failed to write the object content to a temporary file') do |guard| Tempfile.create do |file| file.binmode Git::Commands::CatFile::Raw.new(@execution_context).call(object, p: true, out: file) file.rewind guard.unguarded { block.call(file) } end end end |
#cat_file_size(object) ⇒ Integer
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 a git object in bytes
122 123 124 125 126 |
# File 'lib/git/repository/object_operations.rb', line 122 def cat_file_size(object) raise ArgumentError, "Invalid object: '#{object}'" if object&.start_with?('-') Git::Commands::CatFile::Raw.new(@execution_context).call(object, s: true).stdout.chomp.to_i end |
#cat_file_tag(object) ⇒ Hash
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 parsed tag data for the given annotated tag object
Does not work with lightweight tags. To list all annotated tags in a repository:
git for-each-ref --format='%(refname:strip=2)' refs/tags | \
while read tag; do
git cat-file tag "$tag" >/dev/null 2>&1 && echo "$tag"
done
231 232 233 234 235 236 |
# File 'lib/git/repository/object_operations.rb', line 231 def cat_file_tag(object) raise ArgumentError, "Invalid object: '#{object}'" if object&.start_with?('-') tdata = Git::Commands::CatFile::Raw.new(@execution_context).call('tag', object).stdout.split("\n") Git::Parsers::CatFile.parse_tag(tdata, object) end |
#cat_file_type(object) ⇒ 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 type of a git object
147 148 149 150 151 |
# File 'lib/git/repository/object_operations.rb', line 147 def cat_file_type(object) raise ArgumentError, "Invalid object: '#{object}'" if object&.start_with?('-') Git::Commands::CatFile::Raw.new(@execution_context).call(object, t: true).stdout.chomp end |
#full_tree(objectish) ⇒ Array<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 all recursive entries for a given tree object
Equivalent to running git ls-tree -r <objectish> and splitting the
output on newlines. Each returned line describes a single entry in the
tree in the format produced by git ls-tree: <mode> <type> <object>\t<file>.
320 321 322 |
# File 'lib/git/repository/object_operations.rb', line 320 def full_tree(objectish) Git::Commands::LsTree.new(@execution_context).call(objectish, r: true).stdout.split("\n") end |
#gblob(objectish) ⇒ Git::Object::Blob
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 blob object for the given object reference
The returned object is lazy: no git command is invoked until a property (e.g. Object::AbstractObject#sha, Object::AbstractObject#contents) is accessed on the result.
622 623 624 |
# File 'lib/git/repository/object_operations.rb', line 622 def gblob(objectish) Git::Object.new(self, objectish, 'blob') end |
#gcommit(objectish) ⇒ Git::Object::Commit
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 commit object for the given object reference
The returned object is lazy: no git command is invoked until a property (e.g. Object::AbstractObject#sha, Object::Commit#message) is accessed on the result.
646 647 648 |
# File 'lib/git/repository/object_operations.rb', line 646 def gcommit(objectish) Git::Object.new(self, objectish, 'commit') end |
#grep(pattern, 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.
On Git for Windows, git's default regex engine matches bytes rather
than characters, so a metacharacter such as . or a POSIX class such as
[[:alpha:]] never matches a whole multi-byte character. The failure is
silent: nothing raises, and the empty hash returned is indistinguishable
from a pattern that genuinely does not occur in the tree. Pass
perl_regexp: true to select PCRE, which does match characters. PCRE is a
different dialect than git's default POSIX basic/extended regular
expressions, so this is a deliberate choice by the caller rather than a
transparent substitution, and it requires a git built with PCRE support.
Search tracked file contents in a git tree for a pattern
Runs git grep against the given tree-ish and returns every match as a
filename-keyed hash of [line_number, text] pairs.
499 500 501 502 503 504 505 506 507 508 |
# File 'lib/git/repository/object_operations.rb', line 499 def grep(pattern, path_limiter = nil, opts = {}) SharedPrivate.assert_valid_opts!(GREP_ALLOWED_OPTS, **opts) opts = opts.dup object = opts.delete(:object) || 'HEAD' opts[:pathspec] = Array(path_limiter).map(&:to_s) if path_limiter result = Git::Commands::Grep.new(@execution_context).call( object, pattern:, **opts, no_color: true, line_number: true, null: true ) Private.parse_grep_result(result) end |
#gtree(objectish) ⇒ Git::Object::Tree
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 tree object for the given object reference
The returned object is lazy: no git command is invoked until a property (e.g. Object::AbstractObject#sha, Object::Tree#children) is accessed on the result.
666 667 668 |
# File 'lib/git/repository/object_operations.rb', line 666 def gtree(objectish) Git::Object.new(self, objectish, 'tree') end |
#ls_tree(objectish, opts = {}) ⇒ Hash<String, Hash<String, Hash>>
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.
List the objects in a git tree
Runs git ls-tree against the given sha and returns a Hash of tree
entries organised by object type.
411 412 413 414 415 416 417 418 419 |
# File 'lib/git/repository/object_operations.rb', line 411 def ls_tree(objectish, opts = {}) SharedPrivate.assert_valid_opts!(LS_TREE_ALLOWED_OPTS, **opts) paths = Array(opts[:path]).compact r_value = opts[:recursive] = {} [:r] = r_value unless r_value.nil? result = Git::Commands::LsTree.new(@execution_context).call(objectish, *paths, **) Git::Parsers::LsTree.parse(result.stdout) end |
#name_rev(commit_ish) ⇒ 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.
Find the first symbolic name for a commit-ish
364 365 366 367 368 |
# File 'lib/git/repository/object_operations.rb', line 364 def name_rev(commit_ish) raise ArgumentError, "Invalid commit_ish: '#{commit_ish}'" if commit_ish&.start_with?('-') Git::Commands::NameRev.new(@execution_context).call(commit_ish).stdout.split[1] end |
#object(objectish) ⇒ Git::Object::Blob, ...
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 appropriate git object for the given object reference
Runs git cat-file -t to determine the object type, then constructs
and returns the corresponding Git::Object::* subclass instance.
694 695 696 |
# File 'lib/git/repository/object_operations.rb', line 694 def object(objectish) Git::Object.new(self, objectish) end |
#rev_parse(objectish) ⇒ 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.
Resolve a revision specifier to its full object ID
Passes the given revision specifier to git rev-parse and returns the
full object ID.
263 264 265 |
# File 'lib/git/repository/object_operations.rb', line 263 def rev_parse(objectish) Git::Commands::RevParse.new(@execution_context).call(objectish, '--', revs_only: true).stdout end |
#tag_create(name, options = {}) ⇒ Git::TagInfo #tag_create(name, target, options = {}) ⇒ Git::TagInfo
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.
Create a new tag and return its metadata
850 851 852 853 854 855 856 |
# File 'lib/git/repository/object_operations.rb', line 850 def tag_create(name, *args) target, = Private.(args) SharedPrivate.assert_valid_opts!(TAG_CREATE_ALLOWED_OPTS, **) Private.create_tag(@execution_context, name, target, ) tag_list(name).first || raise(Git::UnexpectedResultError, "tag was created but not found in the tag list: #{name}") end |
#tag_delete(name) ⇒ 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.
Delete a tag
869 870 871 872 873 874 |
# File 'lib/git/repository/object_operations.rb', line 869 def tag_delete(name) result = Git::Commands::Tag::Delete.new(@execution_context).call(name) raise Git::FailedError, result if result.status.exitstatus.positive? result.stdout end |
#tag_list(*patterns) ⇒ Array<Git::TagInfo>
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 tags in the repository as structured objects
732 733 734 735 736 737 |
# File 'lib/git/repository/object_operations.rb', line 732 def tag_list(*patterns) result = Git::Commands::Tag::List.new(@execution_context).call( *patterns, format: Git::Parsers::Tag::FORMAT_STRING ) Git::Parsers::Tag.parse_list(result.stdout) end |
#tag_sha(tag_name) ⇒ 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 SHA of a named tag
Returns an empty string when the tag does not exist.
Reads the loose ref file under refs/tags directly when it exists and
falls back to git show-ref otherwise, including when the file cannot
be read.
289 290 291 292 293 |
# File 'lib/git/repository/object_operations.rb', line 289 def tag_sha(tag_name) = File.(File.join(@execution_context.git_dir, 'refs', 'tags')) head = File.(File.join(, tag_name)) Private.loose_tag_sha(, head) || Private.show_ref_tag_sha(@execution_context, tag_name) end |
#tree_depth(objectish) ⇒ Integer
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 number of entries in a tree
Runs git ls-tree -r <objectish> and counts output lines.
This matches Git::Lib#tree_depth behavior in the 4.x branch.
341 342 343 |
# File 'lib/git/repository/object_operations.rb', line 341 def tree_depth(objectish) Git::Commands::LsTree.new(@execution_context).call(objectish, r: true).stdout.each_line.count end |