Module: Git::Repository::PathResolver Private

Defined in:
lib/git/repository/path_resolver.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.

Resolves and normalizes the filesystem paths that locate a Git repository

PathResolver is the single home for the path-resolution logic used by the Git::Repository factory class methods (open and bare). It computes the absolute working-directory, repository (.git), and index paths from the caller-supplied values, following the same rules Git itself uses (including gitdir-pointer files for submodules and linked worktrees).

Class Method Summary collapse

Class Method Details

.resolve_paths(working_directory: nil, repository: nil, index: nil, bare: false) ⇒ Hash{Symbol => (String, nil)}

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 and normalize the paths that locate a Git repository

Returns a new hash containing the resolved absolute paths for:

  • :working_directory — the working tree root (nil for bare repos)
  • :repository — the .git directory
  • :index — the index file

This method does not mutate any inputs.

Examples:

Resolve paths for a working tree

Git::Repository::PathResolver.resolve_paths(working_directory: '/repo')
#=> { working_directory: '/repo', repository: '/repo/.git', index: '/repo/.git/index' }

Parameters:

  • working_directory (String, nil) (defaults to: nil)

    the working directory path

  • repository (String, nil) (defaults to: nil)

    the repository (.git) directory path

  • index (String, nil) (defaults to: nil)

    the index file path

  • bare (Boolean) (defaults to: false)

    whether this is a bare repository

Returns:

  • (Hash{Symbol => (String, nil)})

    a hash with :working_directory, :repository, and :index keys



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/git/repository/path_resolver.rb', line 47

def resolve_paths(working_directory: nil, repository: nil, index: nil, bare: false)
  working_dir = resolve_working_directory(working_directory, bare: bare)
  # For bare repos, use working_directory as the default repository location
  repo_path = resolve_repository(repository, working_dir, bare: bare, bare_default: working_directory)
  index_path = resolve_index(index, repo_path)

  {
    working_directory: working_dir,
    repository: repo_path,
    index: index_path
  }
end

.root_of_worktree(working_dir, binary_path: :use_global_config, git_ssh: :use_global_config) ⇒ 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.

Find the root of the working tree that contains working_dir

Runs git rev-parse --show-toplevel from working_dir to locate the top-level directory of the working tree.

Examples:

Find the worktree root from a subdirectory

Git::Repository::PathResolver.root_of_worktree('/repo/subdir') #=> '/repo'

Parameters:

  • working_dir (String)

    a path inside the working tree

  • binary_path (String, :use_global_config) (defaults to: :use_global_config)

    path to the git binary

    Controls which git binary is invoked during root detection. Defaults to :use_global_config, which resolves to Git::Base.config.binary_path.

  • git_ssh (String, nil, :use_global_config) (defaults to: :use_global_config)

    the SSH wrapper path

    Forwarded as GIT_SSH. Defaults to :use_global_config.

Returns:

  • (String)

    the absolute path to the root of the working tree

Raises:

  • (ArgumentError)

    if working_dir does not exist, is not a directory, or is not inside a git working tree

    Also raised if the git binary cannot be found.



86
87
88
89
90
# File 'lib/git/repository/path_resolver.rb', line 86

def root_of_worktree(working_dir, binary_path: :use_global_config, git_ssh: :use_global_config)
  raise ArgumentError, "'#{working_dir}' does not exist or is not a directory" unless Dir.exist?(working_dir)

  execute_rev_parse_toplevel(working_dir, binary_path: binary_path, git_ssh: git_ssh)
end