Class: Git::Repository
- Inherits:
-
Object
- Object
- Git::Repository
- Includes:
- Branching, Committing, Configuring, Diffing, Inspecting, Logging, Merging, ObjectOperations, RemoteOperations, Staging, Stashing, StatusOperations, WorktreeOperations
- Defined in:
- lib/git/repository.rb,
lib/git/repository/diffing.rb,
lib/git/repository/logging.rb,
lib/git/repository/merging.rb,
lib/git/repository/staging.rb,
lib/git/repository/stashing.rb,
lib/git/repository/branching.rb,
lib/git/repository/committing.rb,
lib/git/repository/inspecting.rb,
lib/git/repository/configuring.rb,
lib/git/repository/path_resolver.rb,
lib/git/repository/shared_private.rb,
lib/git/repository/object_operations.rb,
lib/git/repository/remote_operations.rb,
lib/git/repository/status_operations.rb,
lib/git/repository/worktree_operations.rb
Overview
The main public interface for interacting with a Git repository
Git::Repository is the orchestration layer for all git operations. It acts
as the glue between the user-facing API and the underlying components, but
contains minimal domain logic itself. For each operation it:
- Pre-processes arguments — transforms user-provided values into forms suitable for the command layer (e.g. path expansion, option normalization, Ruby-idiomatic defaults, deprecation handling, input validation).
- Calls commands — invokes one or more
Git::Commands::*classes via the injectedGit::ExecutionContext::Repository. - Builds rich return values — passes raw command output through
Git::Parsers::*classes and result-class factory methods to assemble the meaningful Ruby objects the caller expects.
Some operations are genuinely one-line delegators when no pre/post-processing is
needed (e.g. add, reset), but many are short orchestration sequences that
coordinate argument preparation, one or more command calls, and result assembly.
Facade methods are organized into focused modules under lib/git/repository/
(e.g. Staging) and included into this class.
Defined Under Namespace
Modules: Branching, Committing, Configuring, Diffing, Inspecting, Logging, Merging, ObjectOperations, PathResolver, RemoteOperations, Staging, Stashing, StatusOperations, WorktreeOperations
Instance Attribute Summary collapse
-
#execution_context ⇒ Git::ExecutionContext::Repository
readonly
private
The execution context used to run git commands for this repository.
Class Method Summary collapse
-
.bare(git_dir, options = {}) ⇒ Git::Repository
Open an existing bare repository at
git_dir. -
.open(working_dir, options = {}) ⇒ Git::Repository
Open a working copy at an existing path.
Instance Method Summary collapse
-
#dir ⇒ Pathname?
Returns the root of the working tree, or
nilfor a bare repository. -
#index ⇒ Pathname?
Returns the git index file.
-
#initialize(execution_context:) ⇒ Repository
constructor
A new instance of Repository.
-
#repo ⇒ Pathname?
Returns the repository (
.git) directory. -
#repo_size ⇒ Integer
Returns the size of the repository directory in bytes.
Methods included from WorktreeOperations
#worktree, #worktree_add, #worktree_prune, #worktree_remove, #worktrees, #worktrees_all
Methods included from StatusOperations
#ls_files, #no_commits?, #status, #untracked_files
Methods included from Stashing
#stash_apply, #stash_clear, #stash_save, #stashes_all
Methods included from Staging
#add, #clean, #ignored_files, #reset, #rm
Methods included from RemoteOperations
#add_remote, #config_remote, #fetch, #pull, #push, #remote, #remote_set_branches, #remotes, #remove_remote, #set_remote_url
Methods included from ObjectOperations
#add_tag, #archive, #cat_file_commit, #cat_file_contents, #cat_file_size, #cat_file_tag, #cat_file_type, #delete_tag, #full_tree, #gblob, #gcommit, #grep, #gtree, #ls_tree, #name_rev, #object, #rev_parse, #tag, #tag_sha, #tags, #tree_depth
Methods included from Merging
#each_conflict, #merge, #merge_base, #revert
Methods included from Logging
Methods included from Inspecting
Methods included from Diffing
#diff, #diff_files, #diff_full, #diff_index, #diff_numstat, #diff_path_status, #diff_stats
Methods included from Configuring
Methods included from Committing
#commit, #commit_all, #commit_tree, #write_and_commit_tree, #write_tree
Methods included from Branching
#branch, #branch?, #branch_contains, #branch_delete, #branch_new, #branches, #branches_all, #checkout, #checkout_file, #checkout_index, #current_branch, #local_branch?, #remote_branch?, #update_ref
Constructor Details
#initialize(execution_context:) ⇒ Repository
Returns a new instance of Repository.
200 201 202 203 204 |
# File 'lib/git/repository.rb', line 200 def initialize(execution_context:) raise ArgumentError, 'execution_context must not be nil' if execution_context.nil? @execution_context = execution_context end |
Instance Attribute Details
#execution_context ⇒ Git::ExecutionContext::Repository (readonly)
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 execution context used to run git commands for this repository.
193 194 195 |
# File 'lib/git/repository.rb', line 193 def execution_context @execution_context end |
Class Method Details
.bare(git_dir, options = {}) ⇒ Git::Repository
Open an existing bare repository at git_dir
The new repository factories are additive scaffolding introduced by the architectural redesign. The top-level Git.bare entry point still returns a Base object; this method exists so future work can route construction through Git::Repository without changing the public entry points.
140 141 142 143 144 |
# File 'lib/git/repository.rb', line 140 def self.(git_dir, = {}) paths = PathResolver.resolve_paths(repository: git_dir, bare: true) from_paths(, paths) end |
.open(working_dir, options = {}) ⇒ Git::Repository
Open a working copy at an existing path
The new repository factories are additive scaffolding introduced by the architectural redesign. The top-level Git.open entry point still returns a Base object; this method exists so future work can route construction through Git::Repository without changing the public entry points.
Note: this method opens working copies only. To open a bare repository, use bare.
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/git/repository.rb', line 102 def self.open(working_dir, = {}) raise ArgumentError, "'#{working_dir}' is not a directory" unless Dir.exist?(working_dir) working_dir = resolve_open_working_dir(working_dir, ) unless [:repository] paths = PathResolver.resolve_paths( working_directory: working_dir, repository: [:repository], index: [:index] ) from_paths(, paths) end |
Instance Method Details
#dir ⇒ Pathname?
Returns the root of the working tree, or nil for a bare repository
213 214 215 216 |
# File 'lib/git/repository.rb', line 213 def dir working_dir = execution_context.git_work_dir working_dir && Pathname.new(working_dir) end |
#index ⇒ Pathname?
Returns the git index file
237 238 239 240 |
# File 'lib/git/repository.rb', line 237 def index index_file = execution_context.git_index_file index_file && Pathname.new(index_file) end |
#repo ⇒ Pathname?
Returns the repository (.git) directory
225 226 227 228 |
# File 'lib/git/repository.rb', line 225 def repo repository = execution_context.git_dir repository && Pathname.new(repository) end |
#repo_size ⇒ Integer
Returns the size of the repository directory in bytes
Sums the sizes of every regular file under the repository (.git)
directory in a single traversal. Symbolic links are not followed, so files
that physically live outside the repository (reached through a symlinked
directory) are never counted. Files that disappear mid-traversal are
silently skipped.
255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/git/repository.rb', line 255 def repo_size repository = repo return 0 unless repository&.directory? total = 0 Find.find(repository.to_s) do |path| stat = File.lstat(path) total += stat.size if stat.file? rescue Errno::ENOENT next end total end |