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
-
#add_tag(name)
deprecated
private
Deprecated.
Use #tag_create instead
The warning names #tag_add, the replacement this method shipped with, and #tag_add is deprecated as well, so a creation call emits two warnings: one for this method and one for #tag_add. The delete form
add_tag(name, d: true)emits a third, for the deprecated:dand:deleteoptions on #tag_add; use #tag_delete for that. Go straight to #tag_create for creation. -
#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
(also: #commit_data)
private
Returns parsed commit data for the given git object.
-
#cat_file_contents(object)
(also: #cat_file, #object_contents)
private
Returns the raw content of a git object, or streams it into a tempfile.
-
#cat_file_size(object) ⇒ Integer
(also: #object_size)
private
Returns the size of a git object in bytes.
-
#cat_file_tag(object) ⇒ Hash
(also: #tag_data)
private
Returns parsed tag data for the given annotated tag object.
-
#cat_file_type(object) ⇒ String
(also: #object_type)
private
Returns the type of a git object.
-
#delete_tag(name) ⇒ String
deprecated
private
Deprecated.
Use #tag_delete instead
-
#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?
(also: #namerev)
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
(also: #revparse)
private
Resolve a revision specifier to its full object ID.
-
#tag(tag_name) ⇒ Git::Object::Tag
deprecated
private
Deprecated.
Use
tag_list(name).firstinstead#tag_list returns immutable TagInfo value objects rather than Object::Tag.
tag_list(name).firstisnilwhen the tag does not exist, where this method raises UnexpectedResultError. Call the corresponding Git::Repository method (e.g. #archive, Logging#log, Diffing#diff, #cat_file_contents) withinfo.oid || info.target_oidfor operations on a tag; that is the object this method's return value pins at construction, so a later move of the tag does not redirect it, whereas the tag name would. The Object::Tag constructor is deprecated too; this method silences it so one call emits one warning. -
#tag_add(name, *args)
deprecated
private
Deprecated.
Use #tag_create instead
#tag_create accepts the same
name,target, and options and returns a TagInfo (an immutable value object) rather than a Object::Tag. It does not accept the:d/:deleteform; use #tag_delete for that. The Object::Tag constructor is deprecated too; this method silences it so one call emits one warning, except that the:d/:deleteform emits a second warning of its own. -
#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.
-
#tags ⇒ Array<Git::Object::Tag>
deprecated
private
Deprecated.
Use #tag_list instead
#tag_list returns
Array<Git::TagInfo>(immutable value objects) rather thanArray<Git::Object::Tag>. Look a tag up by name withtag_list(name).first, and call the corresponding Git::Repository method (e.g. #archive, Logging#log, Diffing#diff, #cat_file_contents) withinfo.oid || info.target_oidfor operations on a tag; that is the object each returned Object::Tag pins at construction, so a later move of the tag does not redirect it, whereas the tag name would. The Object::Tag constructor is deprecated too; this method silences it so one call emits one warning. -
#tree_depth(objectish) ⇒ Integer
private
Returns the number of entries in a tree.
Instance Method Details
#add_tag(name, options = {}) ⇒ Git::Object::Tag #add_tag(name, target, options = {}) ⇒ Git::Object::Tag
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.
Use #tag_create instead
The warning names #tag_add, the replacement this method shipped
with, and #tag_add is deprecated as well, so a creation call emits
two warnings: one for this method and one for #tag_add. The delete
form add_tag(name, d: true) emits a third, for the deprecated :d
and :delete options on #tag_add; use #tag_delete for that. Go
straight to #tag_create for creation.
1097 1098 1099 1100 1101 1102 1103 |
# File 'lib/git/repository/object_operations.rb', line 1097 def add_tag(name, *) Git::Deprecation.warn( 'Git::Repository#add_tag is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#tag_add instead.' ) tag_add(name, *) end |
#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.
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.
626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/git/repository/object_operations.rb', line 626 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) tmp = Private.write_archive_tmp(@execution_context, treeish, opts, dest_dir: Private.staging_dir_for(file)) return tmp unless file Private.atomic_replace(tmp, file) file rescue StandardError FileUtils.rm_f(tmp) if tmp raise end |
#cat_file_commit(object) ⇒ Hash Also known as: commit_data
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
198 199 200 201 |
# File 'lib/git/repository/object_operations.rb', line 198 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, object_contents
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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/git/repository/object_operations.rb', line 77 def cat_file_contents(object) 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. Tempfile.create do |file| file.binmode Git::Commands::CatFile::Raw.new(@execution_context).call(object, p: true, out: file) file.rewind yield file end end |
#cat_file_size(object) ⇒ Integer Also known as: object_size
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 Also known as: tag_data
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
252 253 254 255 256 257 |
# File 'lib/git/repository/object_operations.rb', line 252 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 Also known as: object_type
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
154 155 156 157 158 |
# File 'lib/git/repository/object_operations.rb', line 154 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 |
#delete_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.
Use #tag_delete instead
Returns git's stdout from the delete.
1131 1132 1133 1134 1135 1136 1137 |
# File 'lib/git/repository/object_operations.rb', line 1131 def delete_tag(name) Git::Deprecation.warn( 'Git::Repository#delete_tag is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#tag_delete instead.' ) tag_delete(name) 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>.
348 349 350 |
# File 'lib/git/repository/object_operations.rb', line 348 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.
656 657 658 |
# File 'lib/git/repository/object_operations.rb', line 656 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.
680 681 682 |
# File 'lib/git/repository/object_operations.rb', line 680 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.
534 535 536 537 538 539 540 541 542 543 |
# File 'lib/git/repository/object_operations.rb', line 534 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.
700 701 702 |
# File 'lib/git/repository/object_operations.rb', line 700 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.
446 447 448 449 450 451 452 453 454 |
# File 'lib/git/repository/object_operations.rb', line 446 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? Also known as: namerev
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
392 393 394 395 396 |
# File 'lib/git/repository/object_operations.rb', line 392 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.
772 773 774 |
# File 'lib/git/repository/object_operations.rb', line 772 def object(objectish) Git::Object.new(self, objectish) end |
#rev_parse(objectish) ⇒ String Also known as: revparse
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.
291 292 293 |
# File 'lib/git/repository/object_operations.rb', line 291 def rev_parse(objectish) Git::Commands::RevParse.new(@execution_context).call(objectish, '--', revs_only: true).stdout end |
#tag(tag_name) ⇒ Git::Object::Tag
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.
Use tag_list(name).first instead
#tag_list returns immutable TagInfo value objects rather
than Object::Tag. tag_list(name).first is nil when the tag
does not exist, where this method raises
UnexpectedResultError. Call the corresponding
Git::Repository method (e.g. #archive, Logging#log, Diffing#diff,
#cat_file_contents) with info.oid || info.target_oid for
operations on a tag; that is the object this method's return value
pins at construction, so a later move of the tag does not redirect
it, whereas the tag name would. The
Object::Tag constructor is deprecated too; this method
silences it so one call emits one warning.
Returns a tag object for the given tag name
Returns a Object::Tag for tag_name. The returned object is
either an annotated or a lightweight tag depending on the underlying
ref type.
740 741 742 743 744 745 746 |
# File 'lib/git/repository/object_operations.rb', line 740 def tag(tag_name) Git::Deprecation.warn( 'Git::Repository#tag is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#tag_list(name).first instead.' ) Git::Deprecation.silence { Git::Object::Tag.new(self, tag_name) } end |
#tag_add(name, options = {}) ⇒ Git::Object::Tag #tag_add(name, target, options = {}) ⇒ Git::Object::Tag #tag_add(name, delete_options) ⇒ 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.
Use #tag_create instead
#tag_create accepts the same name, target, and options and
returns a TagInfo (an immutable value object) rather than a
Object::Tag. It does not accept the :d/:delete form; use
#tag_delete for that. The Object::Tag constructor is
deprecated too; this method silences it so one call emits one
warning, except that the :d/:delete form emits a second warning
of its own.
Create a new tag
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 |
# File 'lib/git/repository/object_operations.rb', line 1048 def tag_add(name, *args) Git::Deprecation.warn( 'Git::Repository#tag_add is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#tag_create instead.' ) target, = Private.(args) return Private.tag_add_delete_deprecated(self, name, target, ) if [:d] || [:delete] = .except(:d, :delete) SharedPrivate.assert_valid_opts!(TAG_CREATE_ALLOWED_OPTS, **) Private.create_tag(@execution_context, name, target, ) Git::Deprecation.silence { Git::Object::Tag.new(self, name) } 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
965 966 967 968 969 970 |
# File 'lib/git/repository/object_operations.rb', line 965 def tag_create(name, *args) target, = Private.(args, strict: true) SharedPrivate.assert_valid_opts!(TAG_CREATE_ALLOWED_OPTS, **) Private.create_tag(@execution_context, name, target, ) tag_list(name).first 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
1116 1117 1118 1119 1120 1121 |
# File 'lib/git/repository/object_operations.rb', line 1116 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
810 811 812 813 814 815 |
# File 'lib/git/repository/object_operations.rb', line 810 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.
315 316 317 318 319 320 321 |
# File 'lib/git/repository/object_operations.rb', line 315 def tag_sha(tag_name) = File.(File.join(@execution_context.git_dir, 'refs', 'tags')) head = File.(File.join(, tag_name)) return File.read(head).chomp if head.start_with?("#{}#{File::SEPARATOR}") && File.file?(head) Private.show_ref_tag_sha(@execution_context, tag_name) end |
#tags ⇒ Array<Git::Object::Tag>
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.
Use #tag_list instead
#tag_list returns Array<Git::TagInfo> (immutable value objects)
rather than Array<Git::Object::Tag>. Look a tag up by name with
tag_list(name).first, and call the corresponding Git::Repository
method (e.g. #archive, Logging#log, Diffing#diff, #cat_file_contents) with
info.oid || info.target_oid for operations on a tag; that is the
object each returned Object::Tag pins at construction, so a
later move of the tag does not redirect it, whereas the tag name
would. The Object::Tag
constructor is deprecated too; this method silences it so one call
emits one warning.
Returns all tags in the repository as tag objects
Runs git tag --list with a machine-readable format, parses the output,
and returns a Object::Tag for each tag name.
848 849 850 851 852 853 854 |
# File 'lib/git/repository/object_operations.rb', line 848 def Git::Deprecation.warn( 'Git::Repository#tags is deprecated and will be removed in v6.0.0. ' \ 'Use Git::Repository#tag_list instead.' ) Git::Deprecation.silence { tag_list.map { |info| Git::Object::Tag.new(self, info.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.
369 370 371 |
# File 'lib/git/repository/object_operations.rb', line 369 def tree_depth(objectish) Git::Commands::LsTree.new(@execution_context).call(objectish, r: true).stdout.each_line.count end |