Class: GitDownloadStrategy

Inherits:
VCSDownloadStrategy show all
Defined in:
download_strategy/git_download_strategy.rb

Overview

Strategy for downloading a Git repository.

Direct Known Subclasses

GitHubGitDownloadStrategy

Constant Summary collapse

MINIMUM_COMMIT_HASH_LENGTH =
7

Constants inherited from VCSDownloadStrategy

VCSDownloadStrategy::REF_TYPES

Constants inherited from AbstractDownloadStrategy

AbstractDownloadStrategy::HOMEBREW_CONTROLLED_STRATEGIES

Instance Attribute Summary

Attributes inherited from VCSDownloadStrategy

#cached_location

Attributes inherited from AbstractDownloadStrategy

#cache, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from VCSDownloadStrategy

#command_sandbox, #commit_outdated?, #fetch_last_commit, #fetching?, #head?, #sandbox_write_path

Methods inherited from AbstractDownloadStrategy

#basename, #cached_location, #chdir, #clear_cache, expand_deferred_environment_for?, #fetched_size, #ohai, #quiet!, #quiet?, #stage, #total_size

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Methods included from SystemCommand::Mixin

#command_sandbox, #system_command, #system_command!

Methods included from Context

current, current=, #debug?, #deferred_environment_expansion?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(url, name, version, **meta) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'download_strategy/git_download_strategy.rb', line 15

def initialize(url, name, version, **meta)
  # Needs to be before the call to `super`, as the VCSDownloadStrategy's
  # constructor calls `cache_tag` and sets the cache path.
  @only_path = meta[:only_path]

  if @only_path.present?
    # "Cone" mode of sparse checkout requires patterns to be directories
    @only_path = T.let("/#{@only_path}", String) unless @only_path.start_with?("/")
    @only_path = T.let("#{@only_path}/", String) unless @only_path.end_with?("/")
  end

  super
  @ref_type ||= T.let(:branch, T.nilable(Symbol))
  @ref ||= T.let("master", T.untyped)
end

Class Method Details

.sandboxed_env ⇒ Hash{String => String}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Preserve user configuration, helper executables and SSH agents during downloads.

Returns:



105
106
107
108
109
110
111
112
113
# File 'download_strategy/git_download_strategy.rb', line 105

def self.sandboxed_env
  {
    "GIT_TERMINAL_PROMPT" => "0",
    "HOME"                => Dir.home(ENV.fetch("USER")),
    "PATH"                => PATH.new(ENV.fetch("PATH"), ORIGINAL_PATHS).to_s,
    "SSH_ASKPASS"         => "/usr/bin/false",
    "SSH_ASKPASS_REQUIRE" => "force",
  }.merge(ENV.slice("SSH_AUTH_SOCK"))
end

.sandboxed_git(sandbox, *args, timeout: nil) ⇒ SystemCommand::Result

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Capture non-interactive Git output using the download environment and permissions.

Parameters:

Returns:



93
94
95
96
97
98
99
100
101
# File 'download_strategy/git_download_strategy.rb', line 93

def self.sandboxed_git(sandbox, *args, timeout: nil)
  require "utils/git"

  sandbox.capture(
    "/usr/bin/env",
    args: [*sandboxed_env.map { |key, value| "#{key}=#{value}" }, Utils::Git.git, *args],
    timeout:, must_succeed: false, print_stderr: false, debug: false, verbose: false
  )
end

Instance Method Details

#clone_args ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'download_strategy/git_download_strategy.rb', line 72

def clone_args
  # Fetch submodules separately without the top-level token, even when Git configuration enables recursion.
  args = ["clone", NO_RECURSE_SUBMODULES]

  case @ref_type
  when :branch, :tag
    args << "--branch" << @ref
  end

  args << "--no-checkout" << "--filter=blob:none" if partial_clone_sparse_checkout?

  args << "--config" << "advice.detachedHead=false" # Silences “detached head” warning.
  args << "--config" << "core.fsmonitor=false" # Prevent `fsmonitor` from watching this repository.
  args << "--end-of-options" << @url << cached_location.to_s
end

#fetch(timeout: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • timeout (Float, Integer, nil) (defaults to: nil)


32
33
34
35
36
# File 'download_strategy/git_download_strategy.rb', line 32

def fetch(timeout: nil)
  require "utils/git"
  Utils::Git.ensure_installed!
  super
end

#last_commit ⇒ String

Return last commit's unique identifier for the repository if fetched locally.

Returns:



57
58
59
60
61
# File 'download_strategy/git_download_strategy.rb', line 57

def last_commit
  args = ["--git-dir", git_dir, "rev-parse", "--short=#{MINIMUM_COMMIT_HASH_LENGTH}", "HEAD"]
  @last_commit ||= system_command("git", args:, env: local_git_env, print_stderr: false).stdout.chomp.presence
  @last_commit || ""
end

#ref? ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'download_strategy/git_download_strategy.rb', line 64

def ref?
  silent_command("git",
                 args: ["--git-dir", git_dir, "rev-parse", "-q", "--verify", "--end-of-options",
                        "#{@ref}^{commit}"])
    .success?
end

#source_modified_time ⇒ Time

Returns the most recent modified time for all files in the current working directory after stage.

Returns:



42
43
44
45
46
47
48
# File 'download_strategy/git_download_strategy.rb', line 42

def source_modified_time
  result = system_command("git", args: ["--git-dir", git_dir, "show", "-s", "--format=%cD"],
                                 env:  local_git_env, print_stderr: false)
  raise "Failed to read the Git commit time:\n#{result.stderr}" unless result.success?

  Time.parse(result.stdout)
end

#source_revision ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



51
# File 'download_strategy/git_download_strategy.rb', line 51

def source_revision = current_revision.presence