Module: Git::PathResolver Private

Defined in:
lib/git/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 factory methods (open, bare, init, and clone). 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).

A relative path given by the caller is expanded against the process working directory, as git does for a path on its command line. Only the defaults are derived from the layout: an omitted repository is <working_directory>/.git and an omitted index is <repository>/index.

Constant Summary collapse

WORKING_DIRECTORY_MESSAGE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The Error message when a working directory path cannot be expanded

'Failed to resolve the working directory'
REPOSITORY_MESSAGE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The Error message when a repository directory path cannot be expanded

'Failed to resolve the repository directory'
INDEX_MESSAGE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The Error message when an index file path cannot be expanded

'Failed to resolve the index file'
CHDIR_MESSAGE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The Error message when the :chdir option of Git.clone cannot be expanded

'Failed to resolve the chdir directory'

Class Method Summary collapse

Class Method Details

.expand_path(path, message, base: nil) ⇒ 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.

Expand a path the way a shell prompt would before git runs

Follows File.expand_path: ~ is the home directory, a relative path is joined onto base (or onto the process working directory when base is nil), . and .. are normalized, and an absolute path is unchanged.

Examples:

Expand a relative path against the process working directory

Git::PathResolver.expand_path('repo', 'Failed to resolve the working directory')
#=> '/current/dir/repo'

Expand a relative path against another directory

Git::PathResolver.expand_path('repo', 'Failed to resolve the working directory', base: '/output')
#=> '/output/repo'

Parameters:

  • path (String, Pathname, nil) —

    the path to expand, or nil for base itself

  • message (String) —

    the message of the Error raised when the expansion fails

  • base (String, Pathname, nil) (defaults to: nil) —

    the directory a relative path is expanded against, or nil for the process working directory

Returns:

  • (String) —

    the absolute path

Raises:

  • (ArgumentError) —

    if ~ cannot be expanded because no home directory can be determined, or if the path begins with ~user for a user that does not exist

  • (Git::Error) —

    if the path cannot be expanded, which happens when the process working directory has been removed



270
271
272
# File 'lib/git/path_resolver.rb', line 270

def expand_path(path, message, base: nil)
  Git::SystemCallGuard.call(message) { File.expand_path(path || '.', base) }
end

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

Raises:

  • (Git::Error) —

    if the repository path is a gitdir pointer file that cannot be read



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/git/path_resolver.rb', line 67

def resolve_paths(working_directory: nil, repository: nil, index: nil, bare: false)
  working_dir = resolve_working_directory(working_directory, bare: bare)
  repo_path = resolve_repository(repository, working_dir)
  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::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.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

  • (Git::Error) —

    if the git binary cannot be found or fails to launch, or if working_dir cannot be expanded to an absolute path



106
107
108
109
110
111
# File 'lib/git/path_resolver.rb', line 106

def root_of_worktree(working_dir, binary_path: :use_global_config, git_ssh: :use_global_config)
  expanded_dir = expand_path(working_dir, 'Failed to expand the working directory path')
  raise ArgumentError, "'#{working_dir}' does not exist or is not a directory" unless Dir.exist?(expanded_dir)

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