Class: Git::Repository

Inherits:
Object
  • Object
show all
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:

  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, Configuring, Diffing, Inspecting, Logging, Merging, ObjectOperations, PathResolver, RemoteOperations, Staging, Stashing, StatusOperations, WorktreeOperations

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#full_log_commits, #log

Methods included from Inspecting

#fsck, #show

Methods included from Diffing

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

Methods included from Configuring

#config

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.

Parameters:

Raises:

  • (ArgumentError)

    if execution_context is nil



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_contextGit::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:



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.

Examples:

Open a bare repository

repository = Git::Repository.bare('/path/to/repo.git')

Parameters:

  • git_dir (String)

    the path to the bare repository directory

  • options (Hash) (defaults to: {})

    options forwarded to the constructed repository

Options Hash (options):

  • :log (Logger)

    a logger forwarded to the command layer

  • :git_ssh (String, nil, :use_global_config)

    path to a custom SSH executable; pass :use_global_config (the default) to use Git::Base.config.git_ssh

  • :binary_path (String, :use_global_config)

    path to the git binary; pass :use_global_config (the default) to use Git::Base.config.binary_path

Returns:

  • (Git::Repository)

    a repository bound to the bare repository directory



140
141
142
143
144
# File 'lib/git/repository.rb', line 140

def self.bare(git_dir, options = {})
  paths = PathResolver.resolve_paths(repository: git_dir, bare: true)

  from_paths(options, 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.

Examples:

Open the working copy in the current directory

repository = Git::Repository.open('.')

Parameters:

  • working_dir (String)

    the path to the root of the working copy

    May be any path inside the working tree when :repository is not given.

  • options (Hash) (defaults to: {})

    options that control how the repository is located

Options Hash (options):

  • :repository (String)

    a non-standard path to the .git directory

    When given, working_dir is used as-is (the working tree root is not auto-detected).

  • :index (String)

    a non-standard path to the index file

  • :log (Logger)

    a logger forwarded to the command layer

  • :git_ssh (String, nil, :use_global_config)

    path to a custom SSH executable; pass :use_global_config (the default) to use Git::Base.config.git_ssh

  • :binary_path (String, :use_global_config)

    path to the git binary; pass :use_global_config (the default) to use Git::Base.config.binary_path

Returns:

Raises:

  • (ArgumentError)

    if working_dir is not a directory or is not inside a git working tree



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, options = {})
  raise ArgumentError, "'#{working_dir}' is not a directory" unless Dir.exist?(working_dir)

  working_dir = resolve_open_working_dir(working_dir, options) unless options[:repository]

  paths = PathResolver.resolve_paths(
    working_directory: working_dir,
    repository: options[:repository],
    index: options[:index]
  )

  from_paths(options, paths)
end

Instance Method Details

#dirPathname?

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



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

#indexPathname?

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



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

#repoPathname?

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



225
226
227
228
# File 'lib/git/repository.rb', line 225

def repo
  repository = execution_context.git_dir
  repository && Pathname.new(repository)
end

#repo_sizeInteger

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



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