Module: Git::Repository::ObjectOperations
- Included in:
- Git::Repository
- Defined in:
- lib/git/repository/object_operations.rb
Overview
Facade methods for raw git object store queries
Included by Git::Repository.
Instance Method Summary collapse
-
#add_tag(name, *options)
deprecated
Deprecated.
Use #tag_add instead.
-
#archive(treeish, file = nil, opts = {}) ⇒ String
Create an archive of the repository tree and write it to a file.
-
#cat_file_commit(object) ⇒ Hash
(also: #commit_data)
Returns parsed commit data for the given git object.
-
#cat_file_contents(object)
(also: #cat_file, #object_contents)
Returns the raw content of a git object, or streams it into a tempfile.
-
#cat_file_size(object) ⇒ Integer
(also: #object_size)
Returns the size of a git object in bytes.
-
#cat_file_tag(object) ⇒ Hash
(also: #tag_data)
Returns parsed tag data for the given annotated tag object.
-
#cat_file_type(object) ⇒ String
(also: #object_type)
Returns the type of a git object.
-
#delete_tag(name) ⇒ String
deprecated
Deprecated.
Use #tag_delete instead.
-
#full_tree(objectish) ⇒ Array<String>
Returns all recursive entries for a given tree object.
-
#gblob(objectish) ⇒ Git::Object::Blob
Returns a blob object for the given object reference.
-
#gcommit(objectish) ⇒ Git::Object::Commit
Returns a commit object for the given object reference.
-
#grep(pattern, path_limiter = nil, opts = {}) ⇒ Hash<String, Array<Array(Integer, String)>>
Search tracked file contents in a git tree for a pattern.
-
#gtree(objectish) ⇒ Git::Object::Tree
Returns a tree object for the given object reference.
-
#ls_tree(objectish, opts = {}) ⇒ Hash<String, Hash<String, Hash>>
List the objects in a git tree.
-
#name_rev(commit_ish) ⇒ String?
(also: #namerev)
Find the first symbolic name for a commit-ish.
-
#object(objectish) ⇒ Git::Object::Blob, ...
Returns the appropriate git object for the given object reference.
-
#rev_parse(objectish) ⇒ String
(also: #revparse)
Resolve a revision specifier to its full object ID.
-
#tag(tag_name) ⇒ Git::Object::Tag
Returns a tag object for the given tag name.
-
#tag_add(name, *args)
Create a new tag.
-
#tag_delete(name) ⇒ String
Delete a tag.
-
#tag_sha(tag_name) ⇒ String
Returns the SHA of a named tag.
-
#tags ⇒ Array<Git::Object::Tag>
Returns all tags in the repository as tag objects.
-
#tree_depth(objectish) ⇒ Integer
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
Use #tag_add instead.
915 916 917 918 919 920 921 |
# File 'lib/git/repository/object_operations.rb', line 915 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
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.
605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/git/repository/object_operations.rb', line 605 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
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
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
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
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
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
Use #tag_delete instead.
Returns git's stdout from the delete.
949 950 951 952 953 954 955 |
# File 'lib/git/repository/object_operations.rb', line 949 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>
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
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.
635 636 637 |
# File 'lib/git/repository/object_operations.rb', line 635 def gblob(objectish) Git::Object.new(self, objectish, 'blob') end |
#gcommit(objectish) ⇒ Git::Object::Commit
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.
659 660 661 |
# File 'lib/git/repository/object_operations.rb', line 659 def gcommit(objectish) Git::Object.new(self, objectish, 'commit') end |
#grep(pattern, path_limiter = nil, opts = {}) ⇒ Hash<String, Array<Array(Integer, String)>>
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.
513 514 515 516 517 518 519 520 521 522 |
# File 'lib/git/repository/object_operations.rb', line 513 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
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.
679 680 681 |
# File 'lib/git/repository/object_operations.rb', line 679 def gtree(objectish) Git::Object.new(self, objectish, 'tree') end |
#ls_tree(objectish, opts = {}) ⇒ Hash<String, Hash<String, Hash>>
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
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, ...
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.
731 732 733 |
# File 'lib/git/repository/object_operations.rb', line 731 def object(objectish) Git::Object.new(self, objectish) end |
#rev_parse(objectish) ⇒ String Also known as: revparse
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
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.
703 704 705 |
# File 'lib/git/repository/object_operations.rb', line 703 def tag(tag_name) 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
Create a new tag
875 876 877 878 879 880 881 882 883 884 885 886 |
# File 'lib/git/repository/object_operations.rb', line 875 def tag_add(name, *args) = args.last.is_a?(Hash) ? args.pop : {} target = args.first return Private.tag_add_delete_deprecated(self, name, target, ) if [:d] || [:delete] = .except(:d, :delete) SharedPrivate.assert_valid_opts!(TAG_ADD_ALLOWED_OPTS, **) Private.() Git::Commands::Tag::Create.new(@execution_context).call(name, target, **) tag(name) end |
#tag_delete(name) ⇒ String
Delete a tag
934 935 936 937 938 939 |
# File 'lib/git/repository/object_operations.rb', line 934 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_sha(tag_name) ⇒ String
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>
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.
751 752 753 754 |
# File 'lib/git/repository/object_operations.rb', line 751 def result = Git::Commands::Tag::List.new(@execution_context).call(format: Git::Parsers::Tag::FORMAT_STRING) Git::Parsers::Tag.parse_list(result.stdout).map { |info| tag(info.name) } end |
#tree_depth(objectish) ⇒ Integer
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 |