Class: Git::Repository

Inherits:
Object
  • Object
show all
Includes:
Configuring, Branching, Committing, ContextHelpers, Diffing, Inspecting, Logging, Maintenance, 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/maintenance.rb,
lib/git/repository/shared_private.rb,
lib/git/repository/context_helpers.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:

  1. 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).
  2. Calls commands — invokes one or more Git::Commands::* classes via the injected Git::ExecutionContext::Repository.
  3. 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, ContextHelpers, Diffing, Inspecting, Logging, Maintenance, Merging, ObjectOperations, RemoteOperations, Staging, Stashing, StatusOperations, WorktreeOperations

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WorktreeOperations

#worktree_add, #worktree_list, #worktree_lock, #worktree_move, #worktree_prune, #worktree_remove, #worktree_repair, #worktree_unlock

Methods included from StatusOperations

#ls_files, #no_commits?, #status_info, #untracked_files

Methods included from Stashing

#stash_apply, #stash_branch, #stash_clear, #stash_create, #stash_drop, #stash_list, #stash_pop, #stash_push, #stash_show, #stash_store

Methods included from Staging

#add, #apply, #apply_mail, #clean, #ignored_files, #mv, #read_tree, #reset, #rm

Methods included from RemoteOperations

#fetch, #ls_remote, #pull, #push, #remote_add, #remote_list, #remote_names, #remote_remove, #remote_set_branches, #remote_set_url

Methods included from ObjectOperations

#archive, #cat_file_commit, #cat_file_contents, #cat_file_size, #cat_file_tag, #cat_file_type, #full_tree, #gblob, #gcommit, #grep, #gtree, #ls_tree, #name_rev, #object, #rev_parse, #tag_create, #tag_delete, #tag_list, #tag_sha, #tree_depth

Methods included from Merging

#each_conflict, #merge, #merge_base, #merge_into, #revert, #unmerged

Methods included from Maintenance

#gc, #repack

Methods included from Logging

#full_log_commits, #log

Methods included from Inspecting

#describe, #fsck, #show

Methods included from Diffing

#diff, #diff_files, #diff_full, #diff_index, #diff_numstat, #diff_path_status, #diff_stats

Methods included from Committing

#commit, #commit_all, #commit_tree, #write_and_commit_tree, #write_tree

Methods included from ContextHelpers

#chdir, #set_index, #set_working, #with_index, #with_temp_index, #with_temp_working, #with_working

Methods included from Branching

#branch?, #branch_contains, #branch_delete, #branch_list, #branch_new, #change_head_branch, #checkout, #checkout_file, #checkout_index, #current_branch, #current_branch_state, #in_branch, #local_branch?, #remote_branch?, #update_ref

Methods included from Configuring

#config_add, #config_get, #config_get_all, #config_get_colorbool, #config_get_regexp, #config_get_urlmatch, #config_list, #config_remove_section, #config_rename_section, #config_replace_all, #config_set, #config_unset, #config_unset_all

Constructor Details

#initialize(execution_context:) ⇒ Repository

Returns a new instance of Repository.

Parameters:

Raises:

  • (ArgumentError) —

    if execution_context is nil



77
78
79
80
81
# File 'lib/git/repository.rb', line 77

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.

Returns:



70
71
72
# File 'lib/git/repository.rb', line 70

def execution_context
  @execution_context
end

Instance Method Details

#binary_path ⇒ String, :use_global_config

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 path to the git binary.

Returns:

  • (String, :use_global_config) —

    the path to the git binary



152
# File 'lib/git/repository.rb', line 152

def binary_path = execution_context.binary_path

#dir ⇒ Pathname?

Returns the root of the working tree, or nil for a bare repository

Examples:

Get the working directory path

repository.dir #=> #<Pathname:/path/to/repo>

Returns:

  • (Pathname, nil) —

    the working directory path, or nil when bare



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

def dir
  working_dir = execution_context.git_work_dir
  working_dir && Pathname.new(working_dir)
end

#git_dir ⇒ 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 git directory path.

Returns:

  • (String, nil) —

    the git directory path



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

def git_dir = execution_context.git_dir

#git_index_file ⇒ 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 index file path.

Returns:

  • (String, nil) —

    the index file path



132
# File 'lib/git/repository.rb', line 132

def git_index_file = execution_context.git_index_file

#git_ssh ⇒ 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 SSH wrapper path.

Returns:

  • (String, nil) —

    the SSH wrapper path



147
# File 'lib/git/repository.rb', line 147

def git_ssh = execution_context.git_ssh

#git_version(timeout: nil) ⇒ Git::Version

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 installed git version

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    seconds to wait for git version; nil uses the default timeout for this execution context

Returns:



142
# File 'lib/git/repository.rb', line 142

def git_version(timeout: nil) = execution_context.git_version(timeout: timeout)

#git_work_dir ⇒ 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 working directory path.

Returns:

  • (String, nil) —

    the working directory path



127
# File 'lib/git/repository.rb', line 127

def git_work_dir = execution_context.git_work_dir

#index ⇒ Pathname?

Returns the git index file

Examples:

Get the index file path

repository.index #=> #<Pathname:/path/to/repo/.git/index>

Returns:

  • (Pathname, nil) —

    the index file path



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

def index
  index_file = execution_context.git_index_file
  index_file && Pathname.new(index_file)
end

#repo ⇒ Pathname?

Returns the repository (.git) directory

Examples:

Get the repository directory path

repository.repo #=> #<Pathname:/path/to/repo/.git>

Returns:

  • (Pathname, nil) —

    the repository directory path



102
103
104
105
# File 'lib/git/repository.rb', line 102

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.

Examples:

Get the repository size in bytes

repository.repo_size #=> 12345

Returns:

  • (Integer) —

    the total size in bytes of the repository directory

Raises:

  • (Git::Error) —

    if the repository directory cannot be traversed, for example when a subdirectory cannot be searched



170
171
172
173
174
175
176
177
# File 'lib/git/repository.rb', line 170

def repo_size
  repository = repo
  return 0 unless repository&.directory?

  Git::SystemCallGuard.call('Failed to compute the repository size') do
    repo_size_of_directory(repository.to_s)
  end
end