Module: Git::Factories Private

Included in:
Git
Defined in:
lib/git/factories.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.

Implementation of the Git module factory entry points

Provides .open, .init, .clone, and .bare — the four methods that construct a Repository. Extended onto the Git module so that Git.open(...), Git.init(...), etc. resolve here.

Instance Method Summary collapse

Instance Method Details

#bare(git_dir, options = {}) ⇒ Git::Repository

Open an existing bare repository at git_dir

Examples:

Open a bare repository

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

Bind a bare repository to a scratch index

repository = Git.bare('/srv/repo.git', index: '/tmp/scratch.index')
repository.read_tree('HEAD')
tree_sha = repository.write_tree

Parameters:

  • git_dir (String) —

    the path to the bare repository directory

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

    options used to configure the repository instance

Options Hash (options):

  • :index (String, nil) —

    a non-standard path to the index file

    A relative path is expanded against the process working directory.

  • :log (Logger, nil) —

    logger used for git operations

  • :git_ssh (String, nil, :use_global_config) —

    path to a custom SSH executable

    Pass :use_global_config (the default) to use Git.config.git_ssh.

  • :binary_path (String, :use_global_config) —

    path to the git binary

    Pass :use_global_config (the default) to use Git.config.binary_path.

Returns:

  • (Git::Repository) —

    a repository bound to the bare repository directory

Raises:

  • (ArgumentError) —

    if options contains an unknown key

  • (Git::Error) —

    if git_dir is a gitdir pointer file that cannot be read



432
433
434
435
436
437
# File 'lib/git/factories.rb', line 432

def bare(git_dir, options = {})
  reject_unknown_option_keys!(options.keys, BARE_ALLOWED_OPTS)
  paths = resolve_repository_paths(git_dir, bare: true, index: options[:index])

  from_paths(options, paths)
end

#clone(repository_url, directory = nil, options = {}) ⇒ Git::Repository

Note:

If git fails before the transfer completes, git removes directory and the separate git directory it created, or empties directory when it existed before the call. A :repository path that already exists is refused before the transfer starts. If the transfer completes but the checkout fails, or the :timeout or the global timeout kills git, what git wrote is left in place. If UnexpectedResultError is raised, the completed clone is left in place.

Clone a repository into a new directory

Examples:

Clone into the default directory

repository = Git.clone('https://github.com/ruby-git/ruby-git.git')

Clone into a specific directory

repo_url = 'https://github.com/ruby-git/ruby-git.git'
repository = Git.clone(repo_url, 'local')

Clone a bare repository

repo_url = 'https://github.com/ruby-git/ruby-git.git'
repository = Git.clone(repo_url, nil, bare: true)

Parameters:

  • repository_url (String, URI, Pathname) —

    the URL or path of the repository to clone

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

    the local directory name to clone into; git derives the name from the URL when nil

    Expanded before git runs, so ~ is the home directory and a relative path is relative to :chdir when given and to the process working directory otherwise. git creates the expanded directory, and the returned repository is bound to it.

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

    options that control cloning

    Some options configure the returned Repository instance after the clone completes. Supported git clone options are forwarded.

Options Hash (options):

  • :template (String, nil) —

    template directory to use

  • :local (Boolean, nil) —

    use the local clone optimization

  • :no_local (Boolean, nil) —

    disable the local clone optimization

  • :shared (Boolean, nil) —

    set up a shared clone

  • :no_hardlinks (Boolean, nil) —

    copy files instead of hardlinks

  • :quiet (Boolean, nil) —

    suppress progress output

  • :verbose (Boolean, nil) —

    run verbosely

  • :progress (Boolean, nil) —

    force progress output

  • :no_checkout (Boolean, nil) —

    skip checking out HEAD

  • :bare (Boolean, nil) —

    clone as a bare repository

  • :mirror (Boolean, nil) —

    set up a mirror of the source (implies :bare)

  • :origin (String, nil) —

    remote name to use instead of origin

  • :branch (String, nil) —

    the branch or tag to check out after cloning

  • :revision (String, nil) —

    revision to check out after cloning

  • :upload_pack (String, nil) —

    remote git-upload-pack path

  • :reference (String, Array<String>, nil) —

    reference repository

  • :reference_if_able (String, Array<String>, nil) —

    optional reference repository

  • :dissociate (Boolean, nil) —

    stop borrowing from references

  • :repository (String, nil) —

    alternate git directory path

    Writes a gitfile in the working tree. Alias: :separate_git_dir. Expanded before git runs, like directory: ~ is the home directory and a relative path is relative to :chdir when given and to the process working directory otherwise.

  • :separate_git_dir (String, nil) —

    alias for :repository

  • :server_option (String, Array<String>, nil) —

    protocol-v2 server options

  • :depth (Integer, String, nil) —

    create a shallow clone

  • :shallow_since (String, nil) —

    create a shallow clone by date

  • :shallow_exclude (String, Array<String>, nil) —

    exclude commits reachable from a ref

  • :single_branch (Boolean, nil) —

    clone one branch's history

  • :no_single_branch (Boolean, nil) —

    clone all branch history

  • :tags (Boolean, nil) —

    include tags in the clone

  • :no_tags (Boolean, nil) —

    exclude tags from the clone

  • :recurse_submodules (Boolean, String, Array<String>, nil) —

    initialize submodules after cloning

    Pass true to initialize all submodules, or pass a pathspec string or array for a subset.

  • :shallow_submodules (Boolean, nil) —

    use depth 1 for submodules

  • :no_shallow_submodules (Boolean, nil) —

    use full submodule history

  • :remote_submodules (Boolean, nil) —

    use submodule remote branches

  • :no_remote_submodules (Boolean, nil) —

    use recorded submodule SHAs

  • :jobs (Integer, String, nil) —

    submodule jobs to run concurrently

  • :sparse (Boolean, nil) —

    enable sparse checkout

  • :reject_shallow (Boolean, nil) —

    reject shallow source repositories

  • :no_reject_shallow (Boolean, nil) —

    allow shallow sources

  • :filter (String, nil) —

    specify a partial clone filter

  • :also_filter_submodules (Boolean, nil) —

    filter submodules too

  • :config (String, Array<String>, nil) —

    repository config entries

  • :bundle_uri (String, nil) —

    bundle URI to prefetch

  • :ref_format (String, nil) —

    ref storage format

  • :timeout (Numeric, nil) —

    command timeout in seconds

  • :repository (String, nil) —

    alternate git directory path

    Preferred facade spelling for git clone --separate-git-dir.

  • :git_ssh (String, nil, :use_global_config) —

    path to a custom SSH executable

    Pass :use_global_config (the default) to use Git.config.git_ssh.

  • :binary_path (String, :use_global_config) —

    path to the git binary

    Pass :use_global_config (the default) to use Git.config.binary_path.

  • :log (Logger, nil) —

    logger used for git operations

  • :index (String, nil) —

    a non-standard path to the index file

    Expanded before git runs, like directory: a relative path is expanded against :chdir when given, and against the process working directory otherwise.

  • :chdir (String, Pathname, nil) —

    run git clone from within this directory

    Expanded before git runs, so ~ is the home directory and a relative path is relative to the process working directory. directory, :repository, and :index are expanded against the expanded value.

Returns:

  • (Git::Repository) —

    a repository bound to the cloned working copy or bare repository

Raises:

  • (ArgumentError) —

    if unsupported options are provided, or if directory, :repository, :index, or :chdir begins with ~user for a user that does not exist

  • (Git::FailedError) —

    if git exits with a non-zero exit status

  • (Git::Error) —

    if a filesystem call made while resolving the paths fails

  • (Git::UnexpectedResultError) —

    if the cloned directory cannot be determined from git's output



231
232
233
234
235
236
237
238
# File 'lib/git/factories.rb', line 231

def clone(repository_url, directory = nil, options = {})
  opts, context_opts = prepare_clone_options(options)
  directory = expand_directory(directory, bare: context_opts[:bare], base: opts[:chdir]) if directory
  clone_result = run_clone_command(repository_url, directory, opts, context_opts)
  paths = resolve_paths_from_clone_result(clone_result, directory, opts, context_opts)

  from_paths(clone_repository_options(context_opts), paths)
end

#init(directory = '.', options = {}) ⇒ Git::Repository

Note:

If git exits non-zero after it starts writing, whatever it wrote is left in place: directory when git created it, and a git directory (.git, or the :repository path and the gitfile pointing at it) that git may not recognize as a repository. If git succeeds but the repository cannot be opened, the initialized repository is left in place.

Create an empty Git repository or reinitialize an existing one

Examples:

Initialize in the current directory

repository = Git.init

Initialize in a specific directory

repository = Git.init('/path/to/project')

Initialize a bare repository

repository = Git.init('/path/to/project.git', bare: true)

Parameters:

  • directory (String) (defaults to: '.') —

    the directory to initialize; defaults to '.'

    Expanded before git runs, so ~ is the home directory and a relative path is relative to the process working directory. git creates the expanded directory, and the returned repository is bound to it.

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

    options that control initialization

    Some options configure the returned Repository instance after the repository is initialized.

Options Hash (options):

  • :bare (Boolean, nil) —

    create a bare repository at directory

  • :initial_branch (String, nil) —

    the name for the initial branch

  • :repository (String, nil) —

    path for the .git directory

    Writes a gitfile in the working tree. Alias: :separate_git_dir. Expanded before git runs, like directory: ~ is the home directory and a relative path is relative to the process working directory.

  • :separate_git_dir (String, nil) —

    alias for :repository

  • :git_ssh (String, nil, :use_global_config) —

    path to a custom SSH executable

    Pass :use_global_config (the default) to use Git.config.git_ssh.

  • :binary_path (String, :use_global_config) —

    path to the git binary

    Pass :use_global_config (the default) to use Git.config.binary_path.

  • :log (Logger, nil) —

    logger used for git operations

  • :index (String, nil) —

    custom index path for the returned repository

    Expanded before git runs, like directory: ~ is the home directory and a relative path is relative to the process working directory.

Returns:

  • (Git::Repository) —

    a repository bound to the newly initialized repository

Raises:

  • (ArgumentError) —

    if options contains an unknown key

  • (ArgumentError) —

    if directory, :repository, or :index begins with ~user for a user that does not exist

  • (Git::FailedError) —

    if git exits with a non-zero exit status

  • (Git::Error) —

    if a filesystem call made while resolving the paths fails



314
315
316
317
318
319
320
321
# File 'lib/git/factories.rb', line 314

def init(directory = '.', options = {})
  reject_unknown_option_keys!(options.keys, INIT_ALLOWED_OPTS)
  options = normalize_init_options(options)
  directory = expand_directory(directory, bare: options[:bare])

  run_init_command(directory, options)
  open_after_init(directory, options)
end

#open(working_dir, options = {}) ⇒ Git::Repository

Note:

This method opens working copies only. To open a bare repository, use Git.bare.

Open a working copy at an existing path

Examples:

Open the working copy in the current directory

repository = Git.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. Expanded before use: ~ is the home directory and a relative path is relative to the process working directory.

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

    options that control how the repository is located

Options Hash (options):

  • :repository (String, nil) —

    a non-standard path to the .git directory

    When given, working_dir is used as-is (the working tree root is not auto-detected). A relative path is expanded against the process working directory, not against working_dir.

  • :index (String, nil) —

    a non-standard path to the index file

    A relative path is expanded against the process working directory.

  • :log (Logger, nil) —

    logger used for git operations

  • :git_ssh (String, nil, :use_global_config) —

    path to a custom SSH executable

    Pass :use_global_config (the default) to use Git.config.git_ssh.

  • :binary_path (String, :use_global_config) —

    path to the git binary

    Pass :use_global_config (the default) to use Git.config.binary_path.

Returns:

Raises:

  • (ArgumentError) —

    if options contains an unknown key

  • (ArgumentError) —

    if working_dir is not a directory or is not inside a git working tree, or if it begins with ~user for a user that does not exist

  • (Git::Error) —

    if the .git path is a gitdir pointer file that cannot be read, if the git binary cannot be found or fails to launch, or if a filesystem call made while resolving the paths fails



378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/git/factories.rb', line 378

def open(working_dir, options = {})
  reject_unknown_option_keys!(options.keys, OPEN_ALLOWED_OPTS)
  expanded_dir = PathResolver.expand_path(working_dir, PathResolver::WORKING_DIRECTORY_MESSAGE)
  raise ArgumentError, "'#{working_dir}' is not a directory" unless Dir.exist?(expanded_dir)

  working_dir = options[:repository] ? expanded_dir : resolve_open_working_dir(expanded_dir, options)

  paths = resolve_repository_paths(
    working_dir, bare: false, repository: options[:repository], index: options[:index]
  )

  from_paths(options, paths)
end