Module: Homebrew

Extended by:
FileUtils, Search
Defined in:
brew/Library/Homebrew/help.rb,
brew/Library/Homebrew/fetch.rb,
brew/Library/Homebrew/style.rb,
brew/Library/Homebrew/utils.rb,
brew/Library/Homebrew/cmd/sh.rb,
brew/Library/Homebrew/global.rb,
brew/Library/Homebrew/search.rb,
brew/Library/Homebrew/cleanup.rb,
brew/Library/Homebrew/cmd/cat.rb,
brew/Library/Homebrew/cmd/diy.rb,
brew/Library/Homebrew/cmd/log.rb,
brew/Library/Homebrew/cmd/pin.rb,
brew/Library/Homebrew/cmd/tap.rb,
brew/Library/Homebrew/install.rb,
brew/Library/Homebrew/options.rb,
brew/Library/Homebrew/cli/args.rb,
brew/Library/Homebrew/cmd/cask.rb,
brew/Library/Homebrew/cmd/deps.rb,
brew/Library/Homebrew/cmd/desc.rb,
brew/Library/Homebrew/cmd/help.rb,
brew/Library/Homebrew/cmd/home.rb,
brew/Library/Homebrew/cmd/info.rb,
brew/Library/Homebrew/cmd/link.rb,
brew/Library/Homebrew/cmd/list.rb,
brew/Library/Homebrew/cmd/uses.rb,
brew/Library/Homebrew/cmd/--env.rb,
brew/Library/Homebrew/cmd/fetch.rb,
brew/Library/Homebrew/cmd/style.rb,
brew/Library/Homebrew/cmd/unpin.rb,
brew/Library/Homebrew/cmd/untap.rb,
brew/Library/Homebrew/reinstall.rb,
brew/Library/Homebrew/cli/parser.rb,
brew/Library/Homebrew/cmd/config.rb,
brew/Library/Homebrew/cmd/doctor.rb,
brew/Library/Homebrew/cmd/leaves.rb,
brew/Library/Homebrew/cmd/search.rb,
brew/Library/Homebrew/cmd/switch.rb,
brew/Library/Homebrew/cmd/unlink.rb,
brew/Library/Homebrew/cmd/unpack.rb,
brew/Library/Homebrew/diagnostic.rb,
brew/Library/Homebrew/utils/gems.rb,
brew/Library/Homebrew/cmd/--cache.rb,
brew/Library/Homebrew/cmd/cleanup.rb,
brew/Library/Homebrew/cmd/command.rb,
brew/Library/Homebrew/cmd/install.rb,
brew/Library/Homebrew/cmd/migrate.rb,
brew/Library/Homebrew/cmd/missing.rb,
brew/Library/Homebrew/cmd/options.rb,
brew/Library/Homebrew/cmd/readall.rb,
brew/Library/Homebrew/cmd/tap-pin.rb,
brew/Library/Homebrew/cmd/upgrade.rb,
brew/Library/Homebrew/dev-cmd/irb.rb,
brew/Library/Homebrew/dev-cmd/man.rb,
brew/Library/Homebrew/cmd/--cellar.rb,
brew/Library/Homebrew/cmd/--prefix.rb,
brew/Library/Homebrew/cmd/commands.rb,
brew/Library/Homebrew/cmd/outdated.rb,
brew/Library/Homebrew/cmd/tap-info.rb,
brew/Library/Homebrew/dependencies.rb,
brew/Library/Homebrew/dev-cmd/edit.rb,
brew/Library/Homebrew/dev-cmd/prof.rb,
brew/Library/Homebrew/dev-cmd/pull.rb,
brew/Library/Homebrew/dev-cmd/ruby.rb,
brew/Library/Homebrew/dev-cmd/test.rb,
brew/Library/Homebrew/cmd/--version.rb,
brew/Library/Homebrew/cmd/analytics.rb,
brew/Library/Homebrew/cmd/gist-logs.rb,
brew/Library/Homebrew/cmd/reinstall.rb,
brew/Library/Homebrew/cmd/tap-unpin.rb,
brew/Library/Homebrew/cmd/uninstall.rb,
brew/Library/Homebrew/dev-cmd/audit.rb,
brew/Library/Homebrew/dev-cmd/tests.rb,
brew/Library/Homebrew/dev-cmd/bottle.rb,
brew/Library/Homebrew/dev-cmd/create.rb,
brew/Library/Homebrew/dev-cmd/mirror.rb,
brew/Library/Homebrew/cmd/postinstall.rb,
brew/Library/Homebrew/dev-cmd/extract.rb,
brew/Library/Homebrew/dev-cmd/formula.rb,
brew/Library/Homebrew/dev-cmd/linkage.rb,
brew/Library/Homebrew/dev-cmd/tap-new.rb,
brew/Library/Homebrew/formula_creator.rb,
brew/Library/Homebrew/missing_formula.rb,
brew/Library/Homebrew/os/linux/global.rb,
brew/Library/Homebrew/cmd/--repository.rb,
brew/Library/Homebrew/build_environment.rb,
brew/Library/Homebrew/cmd/update-report.rb,
brew/Library/Homebrew/formula_assertions.rb,
brew/Library/Homebrew/dev-cmd/update-test.rb,
brew/Library/Homebrew/dev-cmd/vendor-gems.rb,
brew/Library/Homebrew/os/linux/diagnostic.rb,
brew/Library/Homebrew/extend/os/mac/search.rb,
brew/Library/Homebrew/dev-cmd/bump-revision.rb,
brew/Library/Homebrew/dev-cmd/release-notes.rb,
brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb,
brew/Library/Homebrew/extend/os/linux/install.rb,
brew/Library/Homebrew/extend/os/mac/diagnostic.rb,
brew/Library/Homebrew/extend/os/linux/diagnostic.rb,
brew/Library/Homebrew/extend/os/mac/missing_formula.rb

Defined Under Namespace

Modules: Assertions, CLI, Diagnostic, Fetch, Help, Install, MissingFormula, Search, Style Classes: Cleanup, DependentsMessage, DeveloperDependentsMessage, FormulaAuditor, FormulaCreator, FormulaText, NondeveloperDependentsMessage, PatchPuller, ResourceAuditor

Constant Summary collapse

DEFAULT_CELLAR =
"#{DEFAULT_PREFIX}/Cellar"
DEFAULT_REPOSITORY =
"#{DEFAULT_PREFIX}/Homebrew"
UNBREWED_EXCLUDE_FILES =
%w[.DS_Store].freeze
UNBREWED_EXCLUDE_PATHS =
%w[
  .github/*
  bin/brew
  completions/zsh/_brew
  docs/*
  lib/gdk-pixbuf-2.0/*
  lib/gio/*
  lib/node_modules/*
  lib/python[23].[0-9]/*
  lib/pypy/*
  lib/pypy3/*
  lib/ruby/gems/[12].*
  lib/ruby/site_ruby/[12].*
  lib/ruby/vendor_ruby/[12].*
  manpages/brew.1
  manpages/brew-cask.1
  share/pypy/*
  share/pypy3/*
  share/info/dir
  share/man/whatis
].freeze
PACKAGE_MANAGERS =
{
  macports: ->(query) { "https://www.macports.org/ports.php?by=name&substr=#{query}" },
  fink:     ->(query) { "http://pdb.finkproject.org/pdb/browse.php?summary=#{query}" },
  opensuse: ->(query) { "https://software.opensuse.org/search?q=#{query}" },
  fedora:   ->(query) { "https://apps.fedoraproject.org/packages/s/#{query}" },
  debian:   lambda { |query|
    "https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all&section=all"
  },
  ubuntu:   lambda { |query|
    "https://packages.ubuntu.com/search?keywords=#{query}&searchon=names&suite=all&section=all"
  },
}.freeze
SOURCE_PATH =
(HOMEBREW_LIBRARY_PATH/"manpages").freeze
TARGET_MAN_PATH =
(HOMEBREW_REPOSITORY/"manpages").freeze
TARGET_DOC_PATH =
(HOMEBREW_REPOSITORY/"docs").freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Search

query_regexp, search_casks, search_descriptions, search_formulae, search_taps

Class Attribute Details

.argsObject



78
79
80
# File 'brew/Library/Homebrew/global.rb', line 78

def args
  @args ||= OpenStruct.new
end

.auditing=(value) ⇒ Object (writeonly)

Sets the attribute auditing

Parameters:

  • value

    the value to set the attribute auditing to.



67
68
69
# File 'brew/Library/Homebrew/global.rb', line 67

def auditing=(value)
  @auditing = value
end

.failed=(value) ⇒ Object (writeonly)

Sets the attribute failed

Parameters:

  • value

    the value to set the attribute failed to.



67
68
69
# File 'brew/Library/Homebrew/global.rb', line 67

def failed=(value)
  @failed = value
end

.raise_deprecation_exceptions=(value) ⇒ Object (writeonly)

Sets the attribute raise_deprecation_exceptions

Parameters:

  • value

    the value to set the attribute raise_deprecation_exceptions to.



67
68
69
# File 'brew/Library/Homebrew/global.rb', line 67

def raise_deprecation_exceptions=(value)
  @raise_deprecation_exceptions = value
end

Class Method Details

.__cacheObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'brew/Library/Homebrew/cmd/--cache.rb', line 26

def __cache
  __cache_args.parse

  if ARGV.named.empty?
    puts HOMEBREW_CACHE
  else
    ARGV.formulae.each do |f|
      if Fetch.fetch_bottle?(f)
        puts f.bottle.cached_download
      else
        puts f.cached_download
      end
    end
  end
end

.__cache_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'brew/Library/Homebrew/cmd/--cache.rb', line 9

def __cache_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `--cache` [<options>] [<formula>]

      Display Homebrew's download cache. See also `HOMEBREW_CACHE`.

      If <formula> is provided, display the file or directory used to cache <formula>.
    EOS
    switch "-s", "--build-from-source",
           description: "Show the cache file used when building from source."
    switch "--force-bottle",
           description: "Show the cache file used when pouring a bottle."
    conflicts "--build-from-source", "--force-bottle"
  end
end

.__cellarObject



22
23
24
25
26
27
28
29
30
# File 'brew/Library/Homebrew/cmd/--cellar.rb', line 22

def __cellar
  __cellar_args.parse

  if ARGV.named.empty?
    puts HOMEBREW_CELLAR
  else
    puts ARGV.resolved_formulae.map(&:rack)
  end
end

.__cellar_argsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'brew/Library/Homebrew/cmd/--cellar.rb', line 8

def __cellar_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `--cellar` [<formula>]

      Display Homebrew's Cellar path. *Default:* `$(brew --prefix)/Cellar`, or if
      that directory doesn't exist, `$(brew --repository)/Cellar`.

      If <formula> is provided, display the location in the cellar where <formula>
      would be installed, without any sort of versioned directory as the last path.
    EOS
  end
end

.__envObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'brew/Library/Homebrew/cmd/--env.rb', line 29

def __env
  __env_args.parse

  ENV.activate_extensions!
  ENV.deps = ARGV.formulae if superenv?
  ENV.setup_build_environment
  ENV.universal_binary if ARGV.build_universal?

  shell = if args.plain?
    nil
  elsif args.shell.nil?
    # legacy behavior
    :bash unless $stdout.tty?
  elsif args.shell == "auto"
    Utils::Shell.parent || Utils::Shell.preferred
  elsif args.shell
    Utils::Shell.from_path(args.shell)
  end

  env_keys = build_env_keys(ENV)
  if shell.nil?
    dump_build_env ENV
  else
    env_keys.each do |key|
      puts Utils::Shell.export_value(key, ENV[key], shell)
    end
  end
end

.__env_argsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'brew/Library/Homebrew/cmd/--env.rb', line 11

def __env_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `--env` [<options>]

      Summarize Homebrew's build environment as a plain list.

      If the command's output is sent through a pipe and no shell is specified,
      the list is formatted for export to `bash`(1) unless `--plain` is passed.
    EOS
    flag   "--shell=",
           description: "Generate a list of environment variables for the specified shell, " \
                        "or `--shell=auto` to detect the current shell."
    switch "--plain",
           description: "Generate plain output even when piped."
  end
end

.__getsObject



112
113
114
115
# File 'brew/Library/Homebrew/dev-cmd/create.rb', line 112

def __gets
  gots = $stdin.gets.chomp
  gots.empty? ? nil : gots
end

.__prefixObject



22
23
24
25
26
27
28
29
30
31
32
# File 'brew/Library/Homebrew/cmd/--prefix.rb', line 22

def __prefix
  __prefix_args.parse

  if ARGV.named.empty?
    puts HOMEBREW_PREFIX
  else
    puts ARGV.resolved_formulae.map { |f|
      f.opt_prefix.exist? ? f.opt_prefix : f.installed_prefix
    }
  end
end

.__prefix_argsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'brew/Library/Homebrew/cmd/--prefix.rb', line 8

def __prefix_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `--prefix` [<formula>]

      Display Homebrew's install path. *Default:* `/usr/local` on macOS and
      `/home/linuxbrew/.linuxbrew` on Linux.

      If <formula> is provided, display the location in the cellar where <formula>
      is or would be installed.
    EOS
  end
end

.__repositoryObject



20
21
22
23
24
25
26
27
28
# File 'brew/Library/Homebrew/cmd/--repository.rb', line 20

def __repository
  __repository_args.parse

  if ARGV.named.empty?
    puts HOMEBREW_REPOSITORY
  else
    puts ARGV.named.map { |tap| Tap.fetch(tap).path }
  end
end

.__repository_argsObject



8
9
10
11
12
13
14
15
16
17
18
# File 'brew/Library/Homebrew/cmd/--repository.rb', line 8

def __repository_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `--repository`, `--repo` [<user>`/`<repo>]

      Display where Homebrew's `.git` directory is located.

      If <user>`/`<repo> are provided, display where tap <user>`/`<repo>'s directory is located.
    EOS
  end
end

.__versionObject



19
20
21
22
23
24
25
26
27
# File 'brew/Library/Homebrew/cmd/--version.rb', line 19

def __version
  __version_args.parse

  odie "This command does not take arguments." if ARGV.any?

  puts "Homebrew #{HOMEBREW_VERSION}"
  puts "#{CoreTap.instance.full_name} #{CoreTap.instance.version_string}"
  puts "#{Tap.default_cask_tap.full_name} #{Tap.default_cask_tap.version_string}" if Tap.default_cask_tap.installed?
end

.__version_argsObject



8
9
10
11
12
13
14
15
16
17
# File 'brew/Library/Homebrew/cmd/--version.rb', line 8

def __version_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `--version`

      Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask
      (if tapped) to standard output.
    EOS
  end
end

._system(cmd, *args, **options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'brew/Library/Homebrew/utils.rb', line 22

def _system(cmd, *args, **options)
  pid = fork do
    yield if block_given?
    args.map!(&:to_s)
    begin
      exec(cmd, *args, **options)
    rescue
      nil
    end
    exit! 1 # never gets here unless exec failed
  end
  Process.wait(pid)
  $CHILD_STATUS.success?
end

.alias_update_pair(formula, new_formula_version) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 429

def alias_update_pair(formula, new_formula_version)
  versioned_alias = formula.aliases.grep(/^.*@\d+(\.\d+)?$/).first
  return if versioned_alias.nil?

  name, old_alias_version = versioned_alias.split("@")
  new_alias_regex = (old_alias_version.split(".").length == 1) ? /^\d+/ : /^\d+\.\d+/
  new_alias_version, = *new_formula_version.to_s.match(new_alias_regex)
  return if Version.create(new_alias_version) <= Version.create(old_alias_version)

  [versioned_alias, "#{name}@#{new_alias_version}"]
end

.analyticsObject

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'brew/Library/Homebrew/cmd/analytics.rb', line 25

def analytics
  analytics_args.parse
  config_file = HOMEBREW_REPOSITORY/".git/config"

  raise UsageError if args.remaining.size > 1

  case args.remaining.first
  when nil, "state"
    analyticsdisabled =
      Utils.popen_read("git config --file=#{config_file} --get homebrew.analyticsdisabled").chomp
    uuid =
      Utils.popen_read("git config --file=#{config_file} --get homebrew.analyticsuuid").chomp
    if ENV["HOMEBREW_NO_ANALYTICS"]
      puts "Analytics is disabled (by HOMEBREW_NO_ANALYTICS)."
    elsif analyticsdisabled == "true"
      puts "Analytics is disabled."
    else
      puts "Analytics is enabled."
      puts "UUID: #{uuid}" if uuid.present?
    end
  when "on"
    safe_system "git", "config", "--file=#{config_file}",
                "--replace-all", "homebrew.analyticsdisabled", "false"
    safe_system "git", "config", "--file=#{config_file}",
                "--replace-all", "homebrew.analyticsmessage", "true"
  when "off"
    safe_system "git", "config", "--file=#{config_file}",
                "--replace-all", "homebrew.analyticsdisabled", "true"
    system "git", "config", "--file=#{config_file}", "--unset-all", "homebrew.analyticsuuid"
  when "regenerate-uuid"
    # it will be regenerated in next run.
    system "git", "config", "--file=#{config_file}", "--unset-all", "homebrew.analyticsuuid"
  else
    raise UsageError
  end
end

.analytics_argsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'brew/Library/Homebrew/cmd/analytics.rb', line 8

def analytics_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `analytics` [<subcommand>]

      If `on` or `off` is passed, turn Homebrew's analytics on or off respectively.

      If `state` is passed, display the current anonymous user behaviour analytics state.
      Read more at <https://docs.brew.sh/Analytics>.

      If `regenerate-uuid` is passed, regenerate the UUID used in Homebrew's analytics.
    EOS
    switch :verbose
    switch :debug
  end
end

.analytics_table(category, days, results, os_version: false, cask_install: false) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'brew/Library/Homebrew/cmd/info.rb', line 233

def analytics_table(category, days, results, os_version: false, cask_install: false)
  oh1 "#{category} (#{days} days)"
  total_count = results.values.inject("+")
  formatted_total_count = format_count(total_count)
  formatted_total_percent = format_percent(100)

  index_header = "Index"
  count_header = "Count"
  percent_header = "Percent"
  name_with_options_header = if os_version
    "macOS Version"
  elsif cask_install
    "Token"
  else
    "Name (with options)"
  end

  total_index_footer = "Total"
  max_index_width = results.length.to_s.length
  index_width = [
    index_header.length,
    total_index_footer.length,
    max_index_width,
  ].max
  count_width = [
    count_header.length,
    formatted_total_count.length,
  ].max
  percent_width = [
    percent_header.length,
    formatted_total_percent.length,
  ].max
  name_with_options_width = Tty.width -
                            index_width -
                            count_width -
                            percent_width -
                            10 # spacing and lines

  formatted_index_header =
    format "%#{index_width}s", index_header
  formatted_name_with_options_header =
    format "%-#{name_with_options_width}s",
           name_with_options_header[0..name_with_options_width-1]
  formatted_count_header =
    format "%#{count_width}s", count_header
  formatted_percent_header =
    format "%#{percent_width}s", percent_header
  puts "#{formatted_index_header} | #{formatted_name_with_options_header} | "\
       "#{formatted_count_header} |  #{formatted_percent_header}"

  columns_line = "#{"-"*index_width}:|-#{"-"*name_with_options_width}-|-"\
                 "#{"-"*count_width}:|-#{"-"*percent_width}:"
  puts columns_line

  index = 0
  results.each do |name_with_options, count|
    index += 1
    formatted_index = format "%0#{max_index_width}d", index
    formatted_index = format "%-#{index_width}s", formatted_index
    formatted_name_with_options =
      format "%-#{name_with_options_width}s",
             name_with_options[0..name_with_options_width-1]
    formatted_count = format "%#{count_width}s", format_count(count)
    formatted_percent = if total_count.zero?
      format "%#{percent_width}s", format_percent(0)
    else
      format "%#{percent_width}s",
             format_percent((count.to_i * 100) / total_count.to_f)
    end
    puts "#{formatted_index} | #{formatted_name_with_options} | " \
         "#{formatted_count} | #{formatted_percent}%"
    next if index > 10
  end
  return unless results.length > 1

  formatted_total_footer =
    format "%-#{index_width}s", total_index_footer
  formatted_blank_footer =
    format "%-#{name_with_options_width}s", ""
  formatted_total_count_footer =
    format "%#{count_width}s", formatted_total_count
  formatted_total_percent_footer =
    format "%#{percent_width}s", formatted_total_percent
  puts "#{formatted_total_footer} | #{formatted_blank_footer} | "\
       "#{formatted_total_count_footer} | #{formatted_total_percent_footer}%"
end

.argv_includes_ignores(argv) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'brew/Library/Homebrew/dependencies.rb', line 62

def argv_includes_ignores(argv)
  includes = []
  ignores = []

  if argv.include? "--include-build"
    includes << "build?"
  else
    ignores << "build?"
  end

  if argv.include? "--include-test"
    includes << "test?"
  else
    ignores << "test?"
  end

  if argv.include? "--include-optional"
    includes << "optional?"
  else
    ignores << "optional?"
  end

  ignores << "recommended?" if ARGV.include? "--skip-recommended"

  [includes, ignores]
end

.auditObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'brew/Library/Homebrew/dev-cmd/audit.rb', line 64

def audit
  audit_args.parse

  Homebrew.auditing = true
  inject_dump_stats!(FormulaAuditor, /^audit_/) if args.audit_debug?

  formula_count = 0
  problem_count = 0
  corrected_problem_count = 0
  new_formula_problem_count = 0
  new_formula = args.new_formula?
  strict = new_formula || args.strict?
  online = new_formula || args.online?

  ENV.activate_extensions!
  ENV.setup_build_environment

  if ARGV.named.empty?
    ff = Formula
    files = Tap.map(&:formula_dir)
  else
    ff = ARGV.resolved_formulae
    files = ARGV.resolved_formulae.map(&:path)
  end

  only_cops = args.only_cops
  except_cops = args.except_cops

  if only_cops && except_cops
    odie "--only-cops and --except-cops cannot be used simultaneously!"
  elsif (only_cops || except_cops) && (strict || args.only)
    odie "--only-cops/--except-cops and --strict/--only cannot be used simultaneously!"
  end

  options = { fix: args.fix?, realpath: true }

  if only_cops
    options[:only_cops] = only_cops
  elsif args.new_formula?
    nil
  elsif strict
    options[:except_cops] = [:NewFormulaAudit]
  elsif except_cops
    options[:except_cops] = except_cops
  elsif !strict
    options[:only_cops] = [:FormulaAudit]
  end

  # Check style in a single batch run up front for performance
  style_results = Style.check_style_json(files, options)

  new_formula_problem_lines = []
  ff.sort.each do |f|
    only = only_cops ? ["style"] : args.only
    options = { new_formula: new_formula, strict: strict, online: online, only: only, except: args.except }
    options[:style_offenses] = style_results.file_offenses(f.path)
    options[:display_cop_names] = args.display_cop_names?

    fa = FormulaAuditor.new(f, options)
    fa.audit
    next if fa.problems.empty? && fa.new_formula_problems.empty?

    fa.problems
    formula_count += 1
    problem_count += fa.problems.size
    problem_lines = format_problem_lines(fa.problems)
    corrected_problem_count = options[:style_offenses].count(&:corrected?)
    new_formula_problem_lines = format_problem_lines(fa.new_formula_problems)
    if args.display_filename?
      puts problem_lines.map { |s| "#{f.path}: #{s}" }
    else
      puts "#{f.full_name}:", problem_lines.map { |s| "  #{s}" }
    end
  end

  created_pr_comment = false
  if new_formula && !new_formula_problem_lines.empty?
    begin
      created_pr_comment = true if GitHub.create_issue_comment(new_formula_problem_lines.join("\n"))
    rescue *GitHub.api_errors => e
      opoo "Unable to create issue comment: #{e.message}"
    end
  end

  unless created_pr_comment
    new_formula_problem_count += new_formula_problem_lines.size
    puts new_formula_problem_lines.map { |s| "  #{s}" }
  end

  total_problems_count = problem_count + new_formula_problem_count
  problem_plural = "#{total_problems_count} #{"problem".pluralize(total_problems_count)}"
  formula_plural = "#{formula_count} #{"formula".pluralize(formula_count)}"
  corrected_problem_plural = "#{corrected_problem_count} #{"problem".pluralize(corrected_problem_count)}"
  errors_summary = "#{problem_plural} in #{formula_plural} detected"
  errors_summary += ", #{corrected_problem_plural} corrected" if corrected_problem_count.positive?

  if problem_count.positive? ||
     (new_formula_problem_count.positive? && !created_pr_comment)
    ofail errors_summary
  end
end

.audit_argsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'brew/Library/Homebrew/dev-cmd/audit.rb', line 18

def audit_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `audit` [<options>] [<formula>]

      Check <formula> for Homebrew coding style violations. This should be run before
      submitting a new formula. If no <formula> are provided, check all locally
      available formulae. Will exit with a non-zero status if any errors are
      found, which can be useful for implementing pre-commit hooks.
    EOS
    switch "--strict",
           description: "Run additional style checks, including RuboCop style checks."
    switch "--online",
           description: "Run additional slower style checks that require a network connection."
    switch "--new-formula",
           description: "Run various additional style checks to determine if a new formula is eligible "\
                        "for Homebrew. This should be used when creating new formula and implies "\
                        "`--strict` and `--online`."
    switch "--fix",
           description: "Fix style violations automatically using RuboCop's auto-correct feature."
    switch "--display-cop-names",
           description: "Include the RuboCop cop name for each violation in the output."
    switch "--display-filename",
           description: "Prefix every line of output with the file or formula name being audited, to "\
                        "make output easy to grep."
    switch "-D", "--audit-debug",
           description: "Enable debugging and profiling of audit methods."
    comma_array "--only",
                description: "Specify a comma-separated <method> list to only run the methods named "\
                             "`audit_`<method>."
    comma_array "--except",
                description: "Specify a comma-separated <method> list to skip running the methods named "\
                             "`audit_`<method>."
    comma_array "--only-cops",
                description: "Specify a comma-separated <cops> list to check for violations of only the listed "\
                             "RuboCop cops."
    comma_array "--except-cops",
                description: "Specify a comma-separated <cops> list to skip checking for violations of the listed "\
                             "RuboCop cops."
    switch :verbose
    switch :debug
    conflicts "--only", "--except"
    conflicts "--only-cops", "--except-cops"
  end
end

.auditing?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'brew/Library/Homebrew/global.rb', line 90

def auditing?
  @auditing == true
end

.backup(keg) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'brew/Library/Homebrew/reinstall.rb', line 58

def backup(keg)
  keg.unlink
  begin
    keg.rename backup_path(keg)
  rescue Errno::EACCES, Errno::ENOTEMPTY
    odie <<~EOS
      Could not rename #{keg.name} keg! Check/fix its permissions:
        sudo chown -R $(whoami) #{keg}
    EOS
  end
end

.backup_path(path) ⇒ Object



81
82
83
# File 'brew/Library/Homebrew/reinstall.rb', line 81

def backup_path(path)
  Pathname.new "#{path}.reinstall"
end

.bottleObject



89
90
91
92
93
94
95
96
97
98
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 89

def bottle
  bottle_args.parse

  return merge if args.merge?

  ensure_relocation_formulae_installed!
  ARGV.resolved_formulae.each do |f|
    bottle_formula f
  end
end

.bottle_argsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 44

def bottle_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS.freeze
      `bottle` [<options>] <formula>

      Generate a bottle (binary package) from a formula that was installed with
      `--build-bottle`.
      If the formula specifies a rebuild version, it will be incremented in the
      generated DSL. Passing `--keep-old` will attempt to keep it at its original
      value, while `--no-rebuild` will remove it.
    EOS
    switch "--skip-relocation",
           description: "Do not check if the bottle can be marked as relocatable."
    switch "--or-later",
           description: "Append `_or_later` to the bottle tag."
    switch "--force-core-tap",
           description: "Build a bottle even if <formula> is not in `homebrew/core` or any installed taps."
    switch "--no-rebuild",
           description: "If the formula specifies a rebuild version, remove it from the generated DSL."
    switch "--keep-old",
           description: "If the formula specifies a rebuild version, attempt to preserve its value in the "\
                        "generated DSL."
    switch "--json",
           description: "Write bottle information to a JSON file, which can be used as the value for "\
                        "`--merge`."
    switch "--merge",
           description: "Generate an updated bottle block for a formula and optionally merge it into the "\
                        "formula file. Instead of a formula name, requires the path to a JSON file generated with "\
                        "`brew bottle --json` <formula>."
    switch "--write",
           depends_on:  "--merge",
           description: "Write changes to the formula file. A new commit will be generated unless "\
                        "`--no-commit` is passed."
    switch "--no-commit",
           depends_on:  "--write",
           description: "When passed with `--write`, a new commit will not generated after writing changes "\
                        "to the formula file."
    flag   "--root-url=",
           description: "Use the specified <URL> as the root of the bottle's URL instead of Homebrew's default."
    switch :verbose
    switch :debug
    conflicts "--no-rebuild", "--keep-old"
  end
end

.bottle_formula(f) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 211

def bottle_formula(f)
  return ofail "Formula not installed or up-to-date: #{f.full_name}" unless f.installed?

  unless tap = f.tap
    return ofail "Formula not from core or any installed taps: #{f.full_name}" unless args.force_core_tap?

    tap = CoreTap.instance
  end

  if f.bottle_disabled?
    ofail "Formula has disabled bottle: #{f.full_name}"
    puts f.bottle_disable_reason
    return
  end

  return ofail "Formula not installed with '--build-bottle': #{f.full_name}" unless Utils::Bottles.built_as? f

  return ofail "Formula has no stable version: #{f.full_name}" unless f.stable

  if args.no_rebuild? || !f.tap
    rebuild = 0
  elsif args.keep_old?
    rebuild = f.bottle_specification.rebuild
  else
    ohai "Determining #{f.full_name} bottle rebuild..."
    versions = FormulaVersions.new(f)
    rebuilds = versions.bottle_version_map("origin/master")[f.pkg_version]
    rebuilds.pop if rebuilds.last.to_i.positive?
    rebuild = rebuilds.empty? ? 0 : rebuilds.max.to_i + 1
  end

  filename = Bottle::Filename.create(f, Utils::Bottles.tag, rebuild)
  bottle_path = Pathname.pwd/filename

  tar_filename = filename.to_s.sub(/.gz$/, "")
  tar_path = Pathname.pwd/tar_filename

  prefix = HOMEBREW_PREFIX.to_s
  repository = HOMEBREW_REPOSITORY.to_s
  cellar = HOMEBREW_CELLAR.to_s

  ohai "Bottling #{filename}..."

  formula_and_runtime_deps_names = [f.name] + f.runtime_dependencies.map(&:name)
  keg = Keg.new(f.prefix)
  relocatable = false
  skip_relocation = false

  keg.lock do
    original_tab = nil
    changed_files = nil

    begin
      keg.delete_pyc_files!

      changed_files = keg.replace_locations_with_placeholders unless args.skip_relocation?

      Tab.clear_cache
      tab = Tab.for_keg(keg)
      original_tab = tab.dup
      tab.poured_from_bottle = false
      tab.HEAD = nil
      tab.time = nil
      tab.changed_files = changed_files
      tab.write

      keg.find do |file|
        if file.symlink?
          # Ruby does not support `File.lutime` yet.
          # Shellout using `touch` to change modified time of symlink itself.
          system "/usr/bin/touch", "-h",
                 "-t", tab.source_modified_time.strftime("%Y%m%d%H%M.%S"), file
        else
          file.utime(tab.source_modified_time, tab.source_modified_time)
        end
      end

      cd cellar do
        safe_system "tar", "cf", tar_path, "#{f.name}/#{f.pkg_version}"
        tar_path.utime(tab.source_modified_time, tab.source_modified_time)
        relocatable_tar_path = "#{f}-bottle.tar"
        mv tar_path, relocatable_tar_path
        # Use gzip, faster to compress than bzip2, faster to uncompress than bzip2
        # or an uncompressed tarball (and more bandwidth friendly).
        safe_system "gzip", "-f", relocatable_tar_path
        mv "#{relocatable_tar_path}.gz", bottle_path
      end

      ohai "Detecting if #{filename} is relocatable..." if bottle_path.size > 1 * 1024 * 1024

      if Homebrew.default_prefix?(prefix)
        prefix_check = File.join(prefix, "opt")
      else
        prefix_check = prefix
      end

      # Ignore matches to source code, which is not required at run time.
      # These matches may be caused by debugging symbols.
      ignores = [%r{/include/|\.(c|cc|cpp|h|hpp)$}]
      any_go_deps = f.deps.any? do |dep|
        dep.name =~ Version.formula_optionally_versioned_regex(:go)
      end
      if any_go_deps
        go_regex =
          Version.formula_optionally_versioned_regex(:go, full: false)
        ignores << %r{#{Regexp.escape(HOMEBREW_CELLAR)}/#{go_regex}/[\d\.]+/libexec}
      end

      relocatable = true
      if args.skip_relocation?
        skip_relocation = true
      else
        relocatable = false if keg_contain?(prefix_check, keg, ignores, formula_and_runtime_deps_names)
        relocatable = false if keg_contain?(repository, keg, ignores)
        relocatable = false if keg_contain?(cellar, keg, ignores, formula_and_runtime_deps_names)
        if prefix != prefix_check
          relocatable = false if keg_contain_absolute_symlink_starting_with?(prefix, keg)
          relocatable = false if keg_contain?("#{prefix}/etc", keg, ignores)
          relocatable = false if keg_contain?("#{prefix}/var", keg, ignores)
          relocatable = false if keg_contain?("#{prefix}/share/vim", keg, ignores)
        end
        skip_relocation = relocatable && !keg.require_relocation?
      end
      puts if !relocatable && args.verbose?
    rescue Interrupt
      ignore_interrupts { bottle_path.unlink if bottle_path.exist? }
      raise
    ensure
      ignore_interrupts do
        original_tab&.write
        keg.replace_placeholders_with_locations changed_files unless args.skip_relocation?
      end
    end
  end

  root_url = args.root_url

  bottle = BottleSpecification.new
  bottle.tap = tap
  bottle.root_url(root_url) if root_url
  if relocatable
    if skip_relocation
      bottle.cellar :any_skip_relocation
    else
      bottle.cellar :any
    end
  else
    bottle.cellar cellar
    bottle.prefix prefix
  end
  bottle.rebuild rebuild
  sha256 = bottle_path.sha256
  bottle.sha256 sha256 => Utils::Bottles.tag

  old_spec = f.bottle_specification
  if args.keep_old? && !old_spec.checksums.empty?
    mismatches = [:root_url, :prefix, :cellar, :rebuild].reject do |key|
      old_spec.send(key) == bottle.send(key)
    end
    if old_spec.cellar == :any && bottle.cellar == :any_skip_relocation
      mismatches.delete(:cellar)
      bottle.cellar :any
    end
    unless mismatches.empty?
      bottle_path.unlink if bottle_path.exist?

      mismatches.map! do |key|
        old_value = old_spec.send(key).inspect
        value = bottle.send(key).inspect
        "#{key}: old: #{old_value}, new: #{value}"
      end

      odie <<~EOS.freeze
        --keep-old was passed but there are changes in:
        #{mismatches.join("\n")}
      EOS
    end
  end

  output = bottle_output bottle

  puts "./#{filename}"
  puts output

  return unless args.json?

  tag = Utils::Bottles.tag.to_s
  tag += "_or_later" if args.or_later?
  json = {
    f.full_name => {
      "formula" => {
        "pkg_version" => f.pkg_version.to_s,
        "path"        => f.path.to_s.delete_prefix("#{HOMEBREW_REPOSITORY}/"),
      },
      "bottle"  => {
        "root_url" => bottle.root_url,
        "prefix"   => bottle.prefix,
        "cellar"   => bottle.cellar.to_s,
        "rebuild"  => bottle.rebuild,
        "tags"     => {
          tag => {
            "filename"       => filename.bintray,
            "local_filename" => filename.to_s,
            "sha256"         => sha256,
          },
        },
      },
      "bintray" => {
        "package"    => Utils::Bottles::Bintray.package(f.name),
        "repository" => Utils::Bottles::Bintray.repository(tap),
      },
    },
  }
  File.open(filename.json, "w") do |file|
    file.write JSON.generate json
  end
end

.bottle_output(bottle) ⇒ Object



206
207
208
209
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 206

def bottle_output(bottle)
  erb = ERB.new BOTTLE_ERB
  erb.result(bottle.instance_eval { binding }).gsub(/^\s*$\n/, "")
end

.brief_build_info(f) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 79

def brief_build_info(f)
  build_time_str = f.logs.ctime.strftime("%Y-%m-%d %H:%M:%S")
  s = +<<~EOS
    Homebrew build logs for #{f.full_name} on #{OS_VERSION}
  EOS
  if args.with_hostname?
    hostname = Socket.gethostname
    s << "Host: #{hostname}\n"
  end
  s << "Build date: #{build_time_str}\n"
  s.freeze
end

.broken_dependents(kegs, formulae) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'brew/Library/Homebrew/cmd/upgrade.rb', line 258

def broken_dependents(kegs, formulae)
  formulae_to_reinstall = Set.new
  formulae_pinned_and_outdated = Set.new

  CacheStoreDatabase.use(:linkage) do |db|
    formulae.each do |formula|
      descendants = Set.new

      dependents = kegs.select do |keg|
        keg.runtime_dependencies
           .any? { |d| d["full_name"] == formula.full_name }
      end

      next if dependents.empty?

      dependents.each do |keg|
        f = keg.to_formula

        next if formulae_to_reinstall.include?(f)
        next if formulae_pinned_and_outdated.include?(f)

        checker = LinkageChecker.new(keg, cache_db: db)

        if checker.broken_library_linkage?
          if f.outdated?(fetch_head: args.fetch_HEAD?)
            # Outdated formulae = pinned formulae (see function above)
            formulae_pinned_and_outdated << f
          else
            formulae_to_reinstall << f
          end
        end

        descendants << f
      end

      descendants_to_reinstall, descendants_pinned = broken_dependents(kegs, descendants)

      formulae_to_reinstall.merge descendants_to_reinstall
      formulae_pinned_and_outdated.merge descendants_pinned
    end
  end

  [formulae_to_reinstall, formulae_pinned_and_outdated]
end

.build_env_keys(env) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'brew/Library/Homebrew/build_environment.rb', line 37

def build_env_keys(env)
  %w[
    CC CXX LD OBJC OBJCXX
    HOMEBREW_CC HOMEBREW_CXX
    CFLAGS CXXFLAGS CPPFLAGS LDFLAGS SDKROOT MAKEFLAGS
    CMAKE_PREFIX_PATH CMAKE_INCLUDE_PATH CMAKE_LIBRARY_PATH CMAKE_FRAMEWORK_PATH
    MACOSX_DEPLOYMENT_TARGET PKG_CONFIG_PATH PKG_CONFIG_LIBDIR
    HOMEBREW_DEBUG HOMEBREW_MAKE_JOBS HOMEBREW_VERBOSE
    HOMEBREW_SVN HOMEBREW_GIT
    HOMEBREW_SDKROOT
    MAKE GIT CPP
    ACLOCAL_PATH PATH CPATH
    LD_LIBRARY_PATH LD_RUN_PATH LD_PRELOAD LIBRARY_PATH
  ].select { |key| env.key?(key) }
end

.build_man_pageObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 63

def build_man_page
  template = (SOURCE_PATH/"brew.1.md.erb").read
  variables = OpenStruct.new

  variables[:commands] = generate_cmd_manpages("#{HOMEBREW_LIBRARY_PATH}/cmd/*.{rb,sh}")
  variables[:developer_commands] = generate_cmd_manpages("#{HOMEBREW_LIBRARY_PATH}/dev-cmd/{*.rb,sh}")
  variables[:global_options] = global_options_manpage

  readme = HOMEBREW_REPOSITORY/"README.md"
  variables[:lead] =
    readme.read[/(Homebrew's \[Project Leader.*\.)/, 1]
          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')
  variables[:plc] =
    readme.read[/(Homebrew's \[Project Leadership Committee.*\.)/, 1]
          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')
  variables[:tsc] =
    readme.read[/(Homebrew's \[Technical Steering Committee.*\.)/, 1]
          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')
  variables[:linux] =
    readme.read[%r{(Homebrew/brew's Linux maintainers .*\.)}, 1]
          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')
  variables[:maintainers] =
    readme.read[/(Homebrew's other current maintainers .*\.)/, 1]
          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')
  variables[:alumni] =
    readme.read[/(Former maintainers .*\.)/, 1]
          .gsub(/\[([^\]]+)\]\([^)]+\)/, '\1')

  ERB.new(template, nil, ">").result(variables.instance_eval { binding })
end

.bump_formula_prObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 69

def bump_formula_pr
  bump_formula_pr_args.parse

  # As this command is simplifying user run commands then let's just use a
  # user path, too.
  ENV["PATH"] = ENV["HOMEBREW_PATH"]

  # Use the user's browser, too.
  ENV["BROWSER"] = ENV["HOMEBREW_BROWSER"]

  formula = ARGV.formulae.first

  if formula
    check_for_duplicate_pull_requests(formula)
    checked_for_duplicates = true
  end

  new_url = args.url
  if new_url && !formula
    # Split the new URL on / and find any formulae that have the same URL
    # except for the last component, but don't try to match any more than the
    # first five components since sometimes the last component isn't the only
    # one to change.
    new_url_split = new_url.split("/")
    maximum_url_components_to_match = 5
    components_to_match = [new_url_split.count - 1, maximum_url_components_to_match].min
    base_url = new_url_split.first(components_to_match).join("/")
    base_url = /#{Regexp.escape(base_url)}/
    is_devel = args.devel?
    guesses = []
    Formula.each do |f|
      if is_devel && f.devel && f.devel.url && f.devel.url.match(base_url)
        guesses << f
      elsif f.stable&.url && f.stable.url.match(base_url)
        guesses << f
      end
    end
    if guesses.count == 1
      formula = guesses.shift
    elsif guesses.count > 1
      odie "Couldn't guess formula for sure; could be one of these:\n#{guesses}"
    end
  end
  raise FormulaUnspecifiedError unless formula

  check_for_duplicate_pull_requests(formula) unless checked_for_duplicates

  requested_spec, formula_spec = if args.devel?
    devel_message = " (devel)"
    [:devel, formula.devel]
  else
    [:stable, formula.stable]
  end
  odie "#{formula}: no #{requested_spec} specification found!" unless formula_spec

  hash_type, old_hash = if (checksum = formula_spec.checksum)
    [checksum.hash_type, checksum.hexdigest]
  end

  new_hash = args[hash_type] if hash_type
  new_tag = args.tag
  new_revision = args.revision
  new_mirror = args.mirror
  forced_version = args.version
  new_url_hash = if new_url && new_hash
    true
  elsif new_tag && new_revision
    false
  elsif !hash_type
    odie "#{formula}: no --tag=/--revision= arguments specified!"
  elsif !new_url
    odie "#{formula}: no --url= argument specified!"
  else
    new_mirror ||= case new_url
    when requested_spec != :devel && %r{.*ftp.gnu.org/gnu.*}
      new_url.sub "ftp.gnu.org/gnu", "ftpmirror.gnu.org"
    when %r{.*mirrors.ocf.berkeley.edu/debian.*}
      new_url.sub "mirrors.ocf.berkeley.edu/debian", "mirrorservice.org/sites/ftp.debian.org/debian"
    end
    resource = Resource.new { @url = new_url }
    resource.download_strategy = DownloadStrategyDetector.detect_from_url(new_url)
    resource.owner = Resource.new(formula.name)
    resource.version = forced_version if forced_version
    odie "No --version= argument specified!" unless resource.version
    resource_path = resource.fetch
    tar_file_extensions = %w[.tar .tb2 .tbz .tbz2 .tgz .tlz .txz .tZ]
    if tar_file_extensions.any? { |extension| new_url.include? extension }
      gnu_tar_gtar_path = HOMEBREW_PREFIX/"opt/gnu-tar/bin/gtar"
      gnu_tar_gtar = gnu_tar_gtar_path if gnu_tar_gtar_path.executable?
      tar = which("gtar") || gnu_tar_gtar || which("tar")
      if Utils.popen_read(tar, "-tf", resource_path) =~ %r{/.*\.}
        new_hash = resource_path.sha256
      else
        odie "#{resource_path} is not a valid tar file!"
      end
    else
      new_hash = resource_path.sha256
    end
  end

  old_formula_version = formula_version(formula, requested_spec)

  replacement_pairs = []
  if requested_spec == :stable && formula.revision.nonzero?
    replacement_pairs << [
      /^  revision \d+\n(\n(  head "))?/m,
      "\\2",
    ]
  end

  replacement_pairs += formula_spec.mirrors.map do |mirror|
    [
      / +mirror \"#{Regexp.escape(mirror)}\"\n/m,
      "",
    ]
  end

  replacement_pairs += if new_url_hash
    [
      [
        /#{Regexp.escape(formula_spec.url)}/,
        new_url,
      ],
      [
        old_hash,
        new_hash,
      ],
    ]
  else
    [
      [
        formula_spec.specs[:tag],
        new_tag,
      ],
      [
        formula_spec.specs[:revision],
        new_revision,
      ],
    ]
  end

  backup_file = File.read(formula.path) unless args.dry_run?

  if new_mirror
    replacement_pairs << [
      /^( +)(url \"#{Regexp.escape(new_url)}\"\n)/m,
      "\\1\\2\\1mirror \"#{new_mirror}\"\n",
    ]
  end

  if forced_version && forced_version != "0"
    if requested_spec == :stable
      if File.read(formula.path).include?("version \"#{old_formula_version}\"")
        replacement_pairs << [
          old_formula_version.to_s,
          forced_version,
        ]
      elsif new_mirror
        replacement_pairs << [
          /^( +)(mirror \"#{new_mirror}\"\n)/m,
          "\\1\\2\\1version \"#{forced_version}\"\n",
        ]
      else
        replacement_pairs << [
          /^( +)(url \"#{new_url}\"\n)/m,
          "\\1\\2\\1version \"#{forced_version}\"\n",
        ]
      end
    elsif requested_spec == :devel
      replacement_pairs << [
        /(  devel do.+?version \")#{old_formula_version}(\"\n.+?end\n)/m,
        "\\1#{forced_version}\\2",
      ]
    end
  elsif forced_version && forced_version == "0"
    if requested_spec == :stable
      replacement_pairs << [
        /^  version \"[\w\.\-\+]+\"\n/m,
        "",
      ]
    elsif requested_spec == :devel
      replacement_pairs << [
        /(  devel do.+?)^ +version \"[^\n]+\"\n(.+?end\n)/m,
        "\\1\\2",
      ]
    end
  end
  new_contents = inreplace_pairs(formula.path, replacement_pairs)

  new_formula_version = formula_version(formula, requested_spec, new_contents)

  if new_formula_version < old_formula_version
    formula.path.atomic_write(backup_file) unless args.dry_run?
    odie <<~EOS
      You probably need to bump this formula manually since changing the
      version from #{old_formula_version} to #{new_formula_version} would be a downgrade.
    EOS
  elsif new_formula_version == old_formula_version
    formula.path.atomic_write(backup_file) unless args.dry_run?
    odie <<~EOS
      You probably need to bump this formula manually since the new version
      and old version are both #{new_formula_version}.
    EOS
  end

  alias_rename = alias_update_pair(formula, new_formula_version)
  if alias_rename.present?
    ohai "renaming alias #{alias_rename.first} to #{alias_rename.last}"
    alias_rename.map! { |a| formula.tap.alias_dir/a }
  end

  run_audit(formula, alias_rename, backup_file)

  formula.path.parent.cd do
    branch = "#{formula.name}-#{new_formula_version}"
    git_dir = Utils.popen_read("git rev-parse --git-dir").chomp
    shallow = !git_dir.empty? && File.exist?("#{git_dir}/shallow")
    changed_files = [formula.path]
    changed_files += alias_rename if alias_rename.present?

    if args.dry_run?
      ohai "try to fork repository with GitHub API"
      ohai "git fetch --unshallow origin" if shallow
      ohai "git add #{alias_rename.first} #{alias_rename.last}" if alias_rename.present?
      ohai "git checkout --no-track -b #{branch} origin/master"
      ohai "git commit --no-edit --verbose --message='#{formula.name} " \
           "#{new_formula_version}#{devel_message}' -- #{changed_files.join(" ")}"
      ohai "git push --set-upstream $HUB_REMOTE #{branch}:#{branch}"
      ohai "git checkout --quiet -"
      ohai "create pull request with GitHub API"
    else

      begin
        response = GitHub.create_fork(formula.tap.full_name)
        # GitHub API responds immediately but fork takes a few seconds to be ready.
        sleep 3

        if system("git", "config", "--local", "--get-regexp", "remote\..*\.url", "git@github.com:.*")
          remote_url = response.fetch("ssh_url")
        else
          remote_url = response.fetch("clone_url")
        end
        username = response.fetch("owner").fetch("login")
      rescue GitHub::AuthenticationFailedError => e
        raise unless e.github_message =~ /forking is disabled/

        # If the repository is private, forking might be disabled.
        # Create branches in the repository itself instead.
        remote_url = Utils.popen_read("git remote get-url --push origin").chomp
        username = formula.tap.user
      rescue *GitHub.api_errors => e
        formula.path.atomic_write(backup_file) unless args.dry_run?
        odie "Unable to fork: #{e.message}!"
      end

      safe_system "git", "fetch", "--unshallow", "origin" if shallow
      safe_system "git", "add", *alias_rename if alias_rename.present?
      safe_system "git", "checkout", "--no-track", "-b", branch, "origin/master"
      safe_system "git", "commit", "--no-edit", "--verbose",
                  "--message=#{formula.name} #{new_formula_version}#{devel_message}",
                  "--", *changed_files
      safe_system "git", "push", "--set-upstream", remote_url, "#{branch}:#{branch}"
      safe_system "git", "checkout", "--quiet", "-"
      pr_message = <<~EOS
        Created with `brew bump-formula-pr`.
      EOS
      user_message = args.message
      if user_message
        pr_message += "\n" + <<~EOS
          ---

          #{user_message}
        EOS
      end
      pr_title = "#{formula.name} #{new_formula_version}#{devel_message}"

      begin
        url = GitHub.create_pull_request(formula.tap.full_name, pr_title,
                                         "#{username}:#{branch}", "master", pr_message)["html_url"]
        if args.no_browse?
          puts url
        else
          exec_browser url
        end
      rescue *GitHub.api_errors => e
        odie "Unable to open pull request: #{e.message}!"
      end
    end
  end
end

.bump_formula_pr_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 9

def bump_formula_pr_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `bump-formula-pr` [<options>] [<formula>]

      Create a pull request to update <formula> with a new URL or a new tag.

      If a <URL> is specified, the <SHA-256> checksum of the new download should also
      be specified. A best effort to determine the <SHA-256> and <formula> name will
      be made if either or both values are not supplied by the user.

      If a <tag> is specified, the Git commit <revision> corresponding to that tag
      must also be specified.

      *Note:* this command cannot be used to transition a formula from a
      URL-and-SHA-256 style specification into a tag-and-revision style specification,
      nor vice versa. It must use whichever style specification the formula already uses.
    EOS
    switch "--devel",
           description: "Bump the development rather than stable version. The development spec must already exist."
    switch "-n", "--dry-run",
           description: "Print what would be done rather than doing it."
    switch "--write",
           depends_on:  "--dry-run",
           description: "When passed along with `--dry-run`, perform a not-so-dry run by making the expected "\
                        "file modifications but not taking any Git actions."
    switch "--no-audit",
           description: "Don't run `brew audit` before opening the PR."
    switch "--strict",
           description: "Run `brew audit --strict` before opening the PR."
    switch "--no-browse",
           description: "Print the pull request URL instead of opening in a browser."
    flag   "--mirror=",
           description: "Use the specified <URL> as a mirror URL."
    flag   "--version=",
           description: "Use the specified <version> to override the value parsed from the URL or tag. Note "\
                        "that `--version=0` can be used to delete an existing version override from a "\
                        "formula if it has become redundant."
    flag   "--message=",
           description: "Append <message> to the default pull request message."
    flag   "--url=",
           description: "Specify the <URL> for the new download. If a <URL> is specified, the <SHA-256> "\
                        "checksum of the new download should also be specified."
    flag   "--sha256=",
           depends_on:  "--url=",
           description: "Specify the <SHA-256> checksum of the new download."
    flag   "--tag=",
           description: "Specify the new git commit <tag> for the formula."
    flag   "--revision=",
           required_for: "--tag=",
           description:  "Specify the new git commit <revision> corresponding to the specified <tag>."
    switch :force
    switch :quiet
    switch :verbose
    switch :debug
    conflicts "--no-audit", "--strict"
    conflicts "--url", "--tag"
  end
end

.bump_revisionObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'brew/Library/Homebrew/dev-cmd/bump-revision.rb', line 28

def bump_revision
  bump_revision_args.parse

  # As this command is simplifying user run commands then let's just use a
  # user path, too.
  ENV["PATH"] = ENV["HOMEBREW_PATH"]

  raise FormulaUnspecifiedError if ARGV.formulae.empty?
  raise "Multiple formulae given, only one is allowed." if ARGV.formulae.length > 1

  formula = ARGV.formulae.first
  current_revision = formula.revision

  if current_revision.zero?
    formula_spec = formula.stable
    hash_type, old_hash = if (checksum = formula_spec.checksum)
      [checksum.hash_type, checksum.hexdigest]
    end

    if hash_type
      # insert replacement revision after hash
      old = <<~EOS
        #{hash_type} "#{old_hash}"
      EOS
    else
      # insert replacement revision after :revision
      old = <<~EOS
        :revision => "#{formula_spec.specs[:revision]}"
      EOS
    end
    replacement = old + "  revision 1\n"

  else
    old = "revision #{current_revision}"
    replacement = "revision #{current_revision+1}"
  end

  if args.dry_run?
    ohai "replace #{old.inspect} with #{replacement.inspect}" unless Homebrew.args.quiet?
  else
    Utils::Inreplace.inreplace(formula.path) do |s|
      s.gsub!(old, replacement)
    end
  end

  message = "#{formula.name}: revision bump #{args.message}"
  if args.dry_run?
    ohai "git commit --no-edit --verbose --message=#{message} -- #{formula.path}"
  else
    formula.path.parent.cd do
      safe_system "git", "commit", "--no-edit", "--verbose",
                  "--message=#{message}", "--", formula.path
    end
  end
end

.bump_revision_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'brew/Library/Homebrew/dev-cmd/bump-revision.rb', line 9

def bump_revision_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `bump-revision` [<options>] <formula>

      Create a commit to increment the revision of <formula>. If no revision is
      present, "revision 1" will be added.
    EOS
    switch "-n", "--dry-run",
           description: "Print what would be done rather than doing it."
    flag   "--message=",
           description: "Append <message> to the default commit message."
    switch :force
    switch :quiet
    switch :verbose
    switch :debug
  end
end

.caskObject



8
9
10
# File 'brew/Library/Homebrew/cmd/cask.rb', line 8

def cask
  Cask::Cmd.run(*ARGV)
end

.catObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'brew/Library/Homebrew/cmd/cat.rb', line 18

def cat
  cat_args.parse
  # do not "fix" this to support multiple arguments, the output would be
  # unparsable, if the user wants to cat multiple formula they can call
  # brew cat multiple times.
  formulae = ARGV.formulae
  raise FormulaUnspecifiedError if formulae.empty?
  raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1

  cd HOMEBREW_REPOSITORY
  safe_system "cat", formulae.first.path, *ARGV.options_only
end

.cat_argsObject



8
9
10
11
12
13
14
15
16
# File 'brew/Library/Homebrew/cmd/cat.rb', line 8

def cat_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `cat` <formula>

      Display the source of <formula>.
    EOS
  end
end

.check_bintray_mirror(name, url) ⇒ Object



463
464
465
466
467
468
469
470
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 463

def check_bintray_mirror(name, url)
  headers, = curl_output("--connect-timeout", "15", "--location", "--head", url)
  status_code = headers.scan(%r{^HTTP\/.* (\d+)}).last.first
  return if status_code.start_with?("2")

  opoo "The Bintray mirror #{url} is not reachable (HTTP status code #{status_code})."
  opoo "Do you need to upload it with `brew mirror #{name}`?"
end

.check_bumps(patch_changes) ⇒ Object



274
275
276
277
278
279
280
281
282
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 274

def check_bumps(patch_changes)
  if patch_changes[:formulae].empty?
    odie "No changed formulae found to bump"
  elsif patch_changes[:formulae].length > 1
    odie "Can only bump one changed formula; bumped #{patch_changes[:formulae]}"
  elsif !patch_changes[:others].empty?
    odie "Can not bump if non-formula files are changed"
  end
end

.check_dependents(formulae) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'brew/Library/Homebrew/cmd/upgrade.rb', line 322

def check_dependents(formulae)
  return if formulae.empty?

  # First find all the outdated dependents.
  kegs = formulae_with_runtime_dependencies

  return if kegs.empty?

  oh1 "Checking dependents for outdated formulae" if args.verbose?
  upgradable, pinned = upgradable_dependents(kegs, formulae).map(&:to_a)

  upgradable.sort! { |a, b| depends_on(a, b) }

  pinned.sort! { |a, b| depends_on(a, b) }

  # Print the pinned dependents.
  unless pinned.empty?
    ohai "Not upgrading #{pinned.count} pinned #{"dependent".pluralize(pinned.count)}:"
    puts pinned.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", "
  end

  # Print the upgradable dependents.
  if upgradable.empty?
    ohai "No dependents to upgrade" if args.verbose?
  else
    ohai "Upgrading #{upgradable.count} #{"dependent".pluralize(upgradable.count)}:"
    formulae_upgrades = upgradable.map do |f|
      if f.optlinked?
        "#{f.full_specified_name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
      else
        "#{f.full_specified_name} #{f.pkg_version}"
      end
    end
    puts formulae_upgrades.join(", ")
  end

  upgrade_formulae(upgradable)

  # Assess the dependents tree again.
  kegs = formulae_with_runtime_dependencies

  oh1 "Checking dependents for broken library links" if args.verbose?
  reinstallable, pinned = broken_dependents(kegs, formulae).map(&:to_a)

  reinstallable.sort! { |a, b| depends_on(a, b) }

  pinned.sort! { |a, b| depends_on(a, b) }

  # Print the pinned dependents.
  unless pinned.empty?
    onoe "Not reinstalling #{pinned.count} broken and outdated, but pinned #{"dependent".pluralize(pinned.count)}:"
    $stderr.puts pinned.map { |f| "#{f.full_specified_name} #{f.pkg_version}" } * ", "
  end

  # Print the broken dependents.
  if reinstallable.empty?
    ohai "No broken dependents to reinstall" if args.verbose?
  else
    ohai "Reinstalling #{reinstallable.count} broken #{"dependent".pluralize(reinstallable.count)} from source:"
    puts reinstallable.map(&:full_specified_name).join(", ")
  end

  reinstallable.each do |f|
    begin
      reinstall_formula(f, build_from_source: true)
    rescue FormulaInstallationAlreadyAttemptedError
      # We already attempted to reinstall f as part of the dependency tree of
      # another formula. In that case, don't generate an error, just move on.
      nil
    rescue CannotInstallFormulaError => e
      ofail e
    rescue BuildError => e
      e.dump
      puts
      Homebrew.failed = true
    rescue DownloadError => e
      ofail e
    end
  end
end

.check_for_dependents(kegs) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'brew/Library/Homebrew/cmd/uninstall.rb', line 111

def check_for_dependents(kegs)
  return false unless result = Keg.find_some_installed_dependents(kegs)

  if ARGV.homebrew_developer?
    DeveloperDependentsMessage.new(*result).output
  else
    NondeveloperDependentsMessage.new(*result).output
  end

  true
end

.check_for_duplicate_pull_requests(formula) ⇒ Object



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 407

def check_for_duplicate_pull_requests(formula)
  pull_requests = fetch_pull_requests(formula)
  return unless pull_requests
  return if pull_requests.empty?

  duplicates_message = <<~EOS
    These open pull requests may be duplicates:
    #{pull_requests.map { |pr| "#{pr["title"]} #{pr["html_url"]}" }.join("\n")}
  EOS
  error_message = "Duplicate PRs should not be opened. Use --force to override this error."
  if Homebrew.args.force? && !Homebrew.args.quiet?
    opoo duplicates_message
  elsif !Homebrew.args.force? && Homebrew.args.quiet?
    odie error_message
  elsif !Homebrew.args.force?
    odie <<~EOS
      #{duplicates_message.chomp}
      #{error_message}
    EOS
  end
end

.cleanupObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'brew/Library/Homebrew/cmd/cleanup.rb', line 33

def cleanup
  cleanup_args.parse

  cleanup = Cleanup.new(*args.remaining, dry_run: args.dry_run?, scrub: args.s?, days: args.prune&.to_i)
  if args.prune_prefix?
    cleanup.prune_prefix_symlinks_and_directories
    return
  end

  cleanup.clean!

  unless cleanup.disk_cleanup_size.zero?
    disk_space = disk_usage_readable(cleanup.disk_cleanup_size)
    if args.dry_run?
      ohai "This operation would free approximately #{disk_space} of disk space."
    else
      ohai "This operation has freed approximately #{disk_space} of disk space."
    end
  end

  return if cleanup.unremovable_kegs.empty?

  ofail <<~EOS
    Could not cleanup old kegs! Fix your permissions on:
      #{cleanup.unremovable_kegs.join "\n  "}
  EOS
end

.cleanup_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'brew/Library/Homebrew/cmd/cleanup.rb', line 9

def cleanup_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `cleanup` [<options>] [<formula>|<cask>]

      Remove stale lock files and outdated downloads for all formulae and casks,
      and remove old versions of installed formulae. If arguments are specified,
      only do this for the given formulae and casks.
    EOS
    flag   "--prune=",
           description: "Remove all cache files older than specified <days>."
    switch "-n", "--dry-run",
           description: "Show what would be removed, but do not actually remove anything."
    switch "-s",
           description: "Scrub the cache, including downloads for even the latest versions. "\
                        "Note downloads for any installed formula or cask will still not be deleted. "\
                        "If you want to delete those too: `rm -rf \"$(brew --cache)\"`"
    switch "--prune-prefix",
           description: "Only prune the symlinks and directories from the prefix and remove no other files."
    switch :verbose
    switch :debug
  end
end

.cmd_arg_parser(cmd_path) ⇒ Object



177
178
179
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 177

def cmd_arg_parser(cmd_path)
  "#{cmd_path.basename.to_s.gsub(".rb", "").tr("-", "_")}_args".to_sym
end

.cmd_comment_manpage_lines(cmd_path) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 191

def cmd_comment_manpage_lines(cmd_path)
  comment_lines = cmd_path.read.lines.grep(/^#:/)
  return if comment_lines.empty?
  return if comment_lines.first.include?("@hide_from_man_page")

  lines = [format_usage_banner(comment_lines.first).chomp]
  comment_lines.slice(1..-1)
               .each do |line|
    line = line.slice(4..-2)
    unless line
      lines.last << "\n"
      next
    end

    # Omit the common global_options documented separately in the man page.
    next if line =~ /--(debug|force|help|quiet|verbose) /

    # Format one option or a comma-separated pair of short and long options.
    lines << line.gsub(/^ +(-+[a-z-]+), (-+[a-z-]+) +/, "* `\\1`, `\\2`:\n  ")
                 .gsub(/^ +(-+[a-z-]+) +/, "* `\\1`:\n  ")
  end
  lines.last << "\n"
  lines
end

.cmd_parser_manpage_lines(cmd_parser) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 181

def cmd_parser_manpage_lines(cmd_parser)
  lines = [format_usage_banner(cmd_parser.usage_banner_text)]
  lines += cmd_parser.processed_options.map do |short, long, _, desc|
    next if !long.nil? && cmd_parser.global_option?(cmd_parser.option_to_name(long), desc)

    generate_option_doc(short, long, desc)
  end.reject(&:blank?)
  lines
end

.commandObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'brew/Library/Homebrew/cmd/command.rb', line 21

def command
  command_args.parse
  abort "This command requires a command argument" if args.remaining.empty?

  cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(args.remaining.first, args.remaining.first)

  path = Commands.path(cmd)

  cmd_paths = PATH.new(ENV["PATH"]).append(Tap.cmd_directories) unless path
  path ||= which("brew-#{cmd}", cmd_paths)
  path ||= which("brew-#{cmd}.rb", cmd_paths)

  odie "Unknown command: #{cmd}" unless path
  puts path
end

.command_argsObject



9
10
11
12
13
14
15
16
17
18
19
# File 'brew/Library/Homebrew/cmd/command.rb', line 9

def command_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `command` <cmd>

      Display the path to the file being used when invoking `brew` <cmd>.
    EOS
    switch :verbose
    switch :debug
  end
end

.commandsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'brew/Library/Homebrew/cmd/commands.rb', line 25

def commands
  commands_args.parse

  if args.quiet?
    cmds = internal_commands
    cmds += external_commands
    cmds += internal_developer_commands
    cmds += HOMEBREW_INTERNAL_COMMAND_ALIASES.keys if args.include_aliases?
    puts Formatter.columns(cmds.sort)
    return
  end

  # Find commands in Homebrew/cmd
  puts "Built-in commands"
  puts Formatter.columns(internal_commands.sort)

  # Find commands in Homebrew/dev-cmd
  puts
  puts "Built-in developer commands"
  puts Formatter.columns(internal_developer_commands.sort)

  exts = external_commands
  return if exts.empty?

  # Find commands in the PATH
  puts
  puts "External commands"
  puts Formatter.columns(exts)
end

.commands_argsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'brew/Library/Homebrew/cmd/commands.rb', line 8

def commands_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `commands` [<options>]

      Show lists of built-in and external commands.
    EOS
    switch :quiet,
           description: "List only the names of commands without category headers."
    switch "--include-aliases",
           depends_on:  "--quiet",
           description: "Include aliases of internal commands."
    switch :verbose
    switch :debug
  end
end

.condense_requirements(deps) ⇒ Object



111
112
113
114
115
# File 'brew/Library/Homebrew/cmd/deps.rb', line 111

def condense_requirements(deps)
  return deps if args.include_requirements?

  deps.select { |dep| dep.is_a? Dependency }
end

.configObject

Raises:



22
23
24
25
26
27
# File 'brew/Library/Homebrew/cmd/config.rb', line 22

def config
  config_args.parse
  raise UsageError unless args.remaining.empty?

  SystemConfig.dump_verbose_config
end

.config_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'brew/Library/Homebrew/cmd/config.rb', line 9

def config_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `config`

      Show Homebrew and system configuration info useful for debugging. If you file
      a bug report, you will be required to provide this information.
    EOS
    switch :verbose
    switch :debug
  end
end

.convert_man_page(markup, target) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 99

def convert_man_page(markup, target)
  manual = target.basename(".1")
  organisation = "Homebrew"

  # Set the manpage date to the existing one if we're checking for changes.
  # This avoids the only change being e.g. a new date.
  date = if args.fail_if_changed? &&
            target.extname == ".1" && target.exist?
    /"(\d{1,2})" "([A-Z][a-z]+) (\d{4})" "#{organisation}" "#{manual}"/ =~ target.read
    Date.parse("#{Regexp.last_match(1)} #{Regexp.last_match(2)} #{Regexp.last_match(3)}")
  else
    Date.today
  end
  date = date.strftime("%Y-%m-%d")

  shared_args = %W[
    --pipe
    --organization=#{organisation}
    --manual=#{target.basename(".1")}
    --date=#{date}
  ]

  format_flag, format_desc = target_path_to_format(target)

  puts "Writing #{format_desc} to #{target}"
  Utils.popen(["ronn", format_flag] + shared_args, "rb+") do |ronn|
    ronn.write markup
    ronn.close_write
    ronn_output = ronn.read
    odie "Got no output from ronn!" unless ronn_output
    if format_flag == "--markdown"
      ronn_output = ronn_output.gsub(%r{<var>(.*?)</var>}, "*`\\1`*")
                               .gsub(/\n\n\n+/, "\n\n")
    elsif format_flag == "--roff"
      ronn_output = ronn_output.gsub(%r{<code>(.*?)</code>}, "\\fB\\1\\fR")
                               .gsub(%r{<var>(.*?)</var>}, "\\fI\\1\\fR")
                               .gsub(/(^\[?\\fB.+): /, "\\1\n    ")
    end
    target.atomic_write ronn_output
  end
end

.createObject

Create a formula from a tarball URL

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'brew/Library/Homebrew/dev-cmd/create.rb', line 48

def create
  create_args.parse

  raise UsageError if ARGV.named.empty?

  # Ensure that the cache exists so we can fetch the tarball
  HOMEBREW_CACHE.mkpath

  url = ARGV.named.first # Pull the first (and only) url from ARGV

  version = args.set_version
  name = args.set_name
  tap = args.tap

  fc = FormulaCreator.new
  fc.name = name
  fc.version = version
  fc.tap = Tap.fetch(tap || "homebrew/core")
  raise TapUnavailableError, tap unless fc.tap.installed?

  fc.url = url

  fc.mode = if args.cmake?
    :cmake
  elsif args.autotools?
    :autotools
  elsif args.meson?
    :meson
  end

  if fc.name.nil? || fc.name.strip.empty?
    stem = Pathname.new(url).stem
    print "Formula name [#{stem}]: "
    fc.name = __gets || stem
    fc.update_path
  end

  # Don't allow blacklisted formula, or names that shadow aliases,
  # unless --force is specified.
  unless args.force?
    if reason = MissingFormula.blacklisted_reason(fc.name)
      raise <<~EOS
        #{fc.name} is blacklisted for creation.
        #{reason}
        If you really want to create this formula use --force.
      EOS
    end

    if Formula.aliases.include? fc.name
      realname = Formulary.canonical_name(fc.name)
      raise <<~EOS
        The formula #{realname} is already aliased to #{fc.name}
        Please check that you are not creating a duplicate.
        To force creation use --force.
      EOS
    end
  end

  fc.generate!

  puts "Please `brew audit --new-formula #{fc.name}` before submitting, thanks."
  exec_editor fc.path
end

.create_argsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'brew/Library/Homebrew/dev-cmd/create.rb', line 11

def create_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `create` [<options>] <URL>

      Generate a formula for the downloadable file at <URL> and open it in the editor.
      Homebrew will attempt to automatically derive the formula name and version, but
      if it fails, you'll have to make your own template. The `wget` formula serves as
      a simple example. For the complete API, see:
      <http://www.rubydoc.info/github/Homebrew/brew/master/Formula>
    EOS
    switch "--autotools",
           description: "Create a basic template for an Autotools-style build."
    switch "--cmake",
           description: "Create a basic template for a CMake-style build."
    switch "--meson",
           description: "Create a basic template for a Meson-style build."
    switch "--no-fetch",
           description: "Homebrew will not download <URL> to the cache and will thus not add its SHA-256 "\
                        "to the formula for you, nor will it check the GitHub API for GitHub projects "\
                        "(to fill out its description and homepage)."
    switch "--HEAD",
           description: "Indicate that <URL> points to the package's repository rather than a file."
    flag   "--set-name=",
           description: "Explicitly set the <name> of the new formula."
    flag   "--set-version=",
           description: "Explicitly set the <version> of the new formula."
    flag   "--tap=",
           description: "Generate the new formula within the given tap, specified as <user>`/`<repo>."
    switch :force
    switch :verbose
    switch :debug
    conflicts "--autotools", "--cmake", "--meson"
  end
end

.create_gist(files, description) ⇒ Object



129
130
131
132
133
134
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 129

def create_gist(files, description)
  url = "https://api.github.com/gists"
  data = { "public" => !create_private?, "files" => files, "description" => description }
  scopes = GitHub::CREATE_GIST_SCOPES
  GitHub.open_api(url, data: data, scopes: scopes)["html_url"]
end

.create_issue(repo, title, body) ⇒ Object



136
137
138
139
140
141
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 136

def create_issue(repo, title, body)
  url = "https://api.github.com/repos/#{repo}/issues"
  data = { "title" => title, "body" => body }
  scopes = GitHub::CREATE_ISSUE_FORK_OR_PR_SCOPES
  GitHub.open_api(url, data: data, scopes: scopes)["html_url"]
end

.create_private?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 125

def create_private?
  args.private?
end

.current_versions_from_info_external(formula_name) ⇒ Object

Get current formula versions without loading formula definition in this process. Returns info as a hash (type => version), for pull.rb’s internal use. Uses special key :nonexistent => true for nonexistent formulae.



407
408
409
410
411
412
413
414
415
416
417
418
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 407

def current_versions_from_info_external(formula_name)
  info = FormulaInfo.lookup(formula_name)
  versions = {}
  if info
    [:stable, :devel, :head].each do |spec_type|
      versions[spec_type] = info.version(spec_type)
    end
  else
    versions[:nonexistent] = true
  end
  versions
end

.decorate_dependencies(dependencies) ⇒ Object



383
384
385
386
387
388
389
390
391
392
# File 'brew/Library/Homebrew/cmd/info.rb', line 383

def decorate_dependencies(dependencies)
  deps_status = dependencies.map do |dep|
    if dep.satisfied?([])
      pretty_installed(dep_display_s(dep))
    else
      pretty_uninstalled(dep_display_s(dep))
    end
  end
  deps_status.join(", ")
end

.decorate_requirements(requirements) ⇒ Object



394
395
396
397
398
399
400
# File 'brew/Library/Homebrew/cmd/info.rb', line 394

def decorate_requirements(requirements)
  req_status = requirements.map do |req|
    req_s = req.display_s
    req.satisfied? ? pretty_installed(req_s) : pretty_uninstalled(req_s)
  end
  req_status.join(", ")
end

.default_prefix?(prefix = HOMEBREW_PREFIX) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'brew/Library/Homebrew/global.rb', line 69

def Homebrew.default_prefix?(prefix = HOMEBREW_PREFIX)
  prefix.to_s == DEFAULT_PREFIX
end

.dep_display_name(dep) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'brew/Library/Homebrew/cmd/deps.rb', line 117

def dep_display_name(dep)
  str = if dep.is_a? Requirement
    if args.include_requirements?
      ":#{dep.display_s}"
    else
      # This shouldn't happen, but we'll put something here to help debugging
      "::#{dep.name}"
    end
  elsif args.full_name?
    dep.to_formula.full_name
  else
    dep.name
  end

  if args.annotate?
    str = "#{str} " if args.tree?
    str = "#{str} [build]" if dep.build?
    str = "#{str} [test]" if dep.test?
    str = "#{str} [optional]" if dep.optional?
    str = "#{str} [recommended]" if dep.recommended?
  end

  str
end

.dep_display_s(dep) ⇒ Object



402
403
404
405
406
# File 'brew/Library/Homebrew/cmd/info.rb', line 402

def dep_display_s(dep)
  return dep.name if dep.option_tags.empty?

  "#{dep.name} #{dep.option_tags.map { |o| "--#{o}" }.join(" ")}"
end

.depsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'brew/Library/Homebrew/cmd/deps.rb', line 59

def deps
  deps_args.parse
  mode = OpenStruct.new(
    installed?:  args.installed?,
    tree?:       args.tree?,
    all?:        args.all?,
    topo_order?: args.n?,
    union?:      args.union?,
    for_each?:   args.for_each?,
    recursive?:  !args.send("1?"),
  )

  if mode.tree?
    if mode.installed?
      puts_deps_tree Formula.installed.sort, mode.recursive?
    else
      raise FormulaUnspecifiedError if args.remaining.empty?

      puts_deps_tree ARGV.formulae, mode.recursive?
    end
    return
  elsif mode.all?
    puts_deps Formula.sort, mode.recursive?
    return
  elsif !args.remaining.empty? && mode.for_each?
    puts_deps ARGV.formulae, mode.recursive?
    return
  end

  @only_installed_arg = mode.installed? &&
                        mode.recursive? &&
                        !args.include_build? &&
                        !args.include_test? &&
                        !args.include_optional? &&
                        !args.skip_recommended?

  if args.remaining.empty?
    raise FormulaUnspecifiedError unless mode.installed?

    puts_deps Formula.installed.sort, mode.recursive?
    return
  end

  all_deps = deps_for_formulae(ARGV.formulae, mode.recursive?, &(mode.union? ? :| : :&))
  all_deps = condense_requirements(all_deps)
  all_deps.select!(&:installed?) if mode.installed?
  all_deps.map!(&method(:dep_display_name))
  all_deps.uniq!
  all_deps.sort! unless mode.topo_order?
  puts all_deps
end

.deps_argsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'brew/Library/Homebrew/cmd/deps.rb', line 10

def deps_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `deps` [<options>] [<formula>]

      Show dependencies for <formula>. Additional options specific to <formula>
      may be appended to the command. When given multiple formula arguments,
      show the intersection of dependencies for each formula.
    EOS
    switch "-n",
           description: "Sort dependencies in topological order."
    switch "--1",
           description: "Only show dependencies one level down, instead of recursing."
    switch "--union",
           description: "Show the union of dependencies for multiple <formula>, instead of the intersection."
    switch "--full-name",
           description: "List dependencies by their full name."
    switch "--include-build",
           description: "Include `:build` dependencies for <formula>."
    switch "--include-optional",
           description: "Include `:optional` dependencies for <formula>."
    switch "--include-test",
           description: "Include `:test` dependencies for <formula> (non-recursive)."
    switch "--skip-recommended",
           description: "Skip `:recommended` dependencies for <formula>."
    switch "--include-requirements",
           description: "Include requirements in addition to dependencies for <formula>."
    switch "--tree",
           description: "Show dependencies as a tree. When given multiple formula arguments, "\
                        "show individual trees for each formula."
    switch "--annotate",
           description: "Mark any build, test, optional, or recommended dependencies as "\
                        "such in the output."
    switch "--installed",
           description: "List dependencies for formulae that are currently installed. If <formula> is "\
                        "specified, list only its dependencies that are currently installed."
    switch "--all",
           description: "List dependencies for all available formulae."
    switch "--for-each",
           description: "Switch into the mode used by the `--all` option, but only list dependencies "\
                        "for each provided <formula>, one formula per line. This is used for "\
                        "debugging the `--installed`/`--all` display mode."
    switch :verbose
    switch :debug
    conflicts "--installed", "--all"
    formula_options
  end
end

.deps_for_formula(f, recursive = false) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'brew/Library/Homebrew/cmd/deps.rb', line 142

def deps_for_formula(f, recursive = false)
  includes, ignores = argv_includes_ignores(ARGV)

  deps = f.runtime_dependencies if @only_installed_arg

  if recursive
    deps ||= recursive_includes(Dependency,  f, includes, ignores)
    reqs   = recursive_includes(Requirement, f, includes, ignores)
  else
    deps ||= reject_ignores(f.deps, ignores, includes)
    reqs   = reject_ignores(f.requirements, ignores, includes)
  end

  deps + reqs.to_a
end

.deps_for_formulae(formulae, recursive = false, &block) ⇒ Object



158
159
160
# File 'brew/Library/Homebrew/cmd/deps.rb', line 158

def deps_for_formulae(formulae, recursive = false, &block)
  formulae.map { |f| deps_for_formula(f, recursive) }.reduce(&block)
end

.descObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'brew/Library/Homebrew/cmd/desc.rb', line 36

def desc
  desc_args.parse

  search_type = []
  search_type << :either if args.search
  search_type << :name   if args.name
  search_type << :desc   if args.description
  if search_type.size > 1
    odie "Pick one, and only one, of -s/--search, -n/--name, or -d/--description."
  elsif search_type.present? && ARGV.named.empty?
    odie "You must provide a search term."
  end

  results = if search_type.empty?
    raise FormulaUnspecifiedError if ARGV.named.empty?

    desc = {}
    ARGV.formulae.each { |f| desc[f.full_name] = f.desc }
    Descriptions.new(desc)
  else
    arg = ARGV.named.join(" ")
    string_or_regex = query_regexp(arg)
    CacheStoreDatabase.use(:descriptions) do |db|
      cache_store = DescriptionCacheStore.new(db)
      Descriptions.search(string_or_regex, search_type.first, cache_store)
    end
  end

  results.print
end

.desc_argsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'brew/Library/Homebrew/cmd/desc.rb', line 13

def desc_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `desc` [<options>] (<text>|`/`<text>`/`|<formula>)

      Display <formula>'s name and one-line description.
      Formula descriptions are cached; the cache is created on the
      first search, making that search slower than subsequent ones.
    EOS
    flag   "-s", "--search=",
           description: "Search both names and descriptions for <text>. If <text> is flanked by "\
                        "slashes, it is interpreted as a regular expression."
    flag   "-n", "--name=",
           description: "Search just names for <text>. If <text> is flanked by slashes, it is "\
                        "interpreted as a regular expression."
    flag   "-d", "--description=",
           description: "Search just descriptions for <text>. If <text> is flanked by slashes, "\
                        "it is interpreted as a regular expression."
    switch :verbose
    conflicts "--search=", "--name=", "--description="
  end
end

.detect_name(path, version) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'brew/Library/Homebrew/cmd/diy.rb', line 56

def detect_name(path, version)
  basename = path.basename.to_s
  detected_name = basename[/(.*?)-?#{Regexp.escape(version)}/, 1] || basename
  canonical_name = Formulary.canonical_name(detected_name)

  odie <<~EOS if detected_name != canonical_name
    The detected name #{detected_name.inspect} exists in Homebrew as an alias
    of #{canonical_name.inspect}. Consider using the canonical name instead:
      brew diy --name=#{canonical_name}

    To continue using the detected name, pass it explicitly:
      brew diy --name=#{detected_name}
  EOS

  detected_name
end

.detect_version(path) ⇒ Object



48
49
50
51
52
53
54
# File 'brew/Library/Homebrew/cmd/diy.rb', line 48

def detect_version(path)
  version = path.version.to_s

  raise "Couldn't determine version, set it with --version=<version>" if version.empty?

  version
end

.diyObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'brew/Library/Homebrew/cmd/diy.rb', line 27

def diy
  diy_args.parse

  path = Pathname.getwd

  version = args.version || detect_version(path)
  name = args.name || detect_name(path, version)

  prefix = HOMEBREW_CELLAR/name/version

  if File.file? "CMakeLists.txt"
    puts "-DCMAKE_INSTALL_PREFIX=#{prefix}"
  elsif File.file? "configure"
    puts "--prefix=#{prefix}"
  elsif File.file? "meson.build"
    puts "-Dprefix=#{prefix}"
  else
    raise "Couldn't determine build system. You can manually put files into #{prefix}"
  end
end

.diy_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'brew/Library/Homebrew/cmd/diy.rb', line 9

def diy_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `diy` [<options>]

      Automatically determine the installation prefix for non-Homebrew software.
      Using the output from this command, you can install your own software into
      the Cellar and then link it into Homebrew's prefix with `brew link`.
    EOS
    flag   "--name=",
           description: "Explicitly set the <name> of the package being installed."
    flag   "--version=",
           description: "Explicitly set the <version> of the package being installed."
    switch :verbose
    switch :debug
  end
end

.doctorObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'brew/Library/Homebrew/cmd/doctor.rb', line 29

def doctor
  doctor_args.parse

  inject_dump_stats!(Diagnostic::Checks, /^check_*/) if args.audit_debug?

  checks = Diagnostic::Checks.new

  if args.list_checks?
    puts checks.all.sort
    exit
  end

  if ARGV.named.empty?
    slow_checks = %w[
      check_for_broken_symlinks
      check_missing_deps
    ]
    methods = (checks.all.sort - slow_checks) + slow_checks
  else
    methods = ARGV.named
  end

  first_warning = true
  methods.each do |method|
    $stderr.puts "Checking #{method}" if args.debug?
    unless checks.respond_to?(method)
      Homebrew.failed = true
      puts "No check available by the name: #{method}"
      next
    end

    out = checks.send(method)
    next if out.nil? || out.empty?

    if first_warning
      $stderr.puts <<~EOS
        #{Tty.bold}Please note that these warnings are just used to help the Homebrew maintainers
        with debugging if you file an issue. If everything you use Homebrew for is
        working fine: please don't worry or file an issue; just ignore this. Thanks!#{Tty.reset}
      EOS
    end

    $stderr.puts
    opoo out
    Homebrew.failed = true
    first_warning = false
  end

  puts "Your system is ready to brew." unless Homebrew.failed?
end

.doctor_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'brew/Library/Homebrew/cmd/doctor.rb', line 9

def doctor_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `doctor` [<options>]

      Check your system for potential problems. Will exit with a non-zero status
      if any potential problems are found. Please note that these warnings are just
      used to help the Homebrew maintainers with debugging if you file an issue. If
      everything you use Homebrew for is working fine: please don't worry or file
      an issue; just ignore this.
    EOS
    switch "--list-checks",
           description: "List all audit methods."
    switch "-D", "--audit-debug",
           description: "Enable debugging and profiling of audit methods."
    switch :verbose
    switch :debug
  end
end

.dump_build_env(env, f = $stdout) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'brew/Library/Homebrew/build_environment.rb', line 53

def dump_build_env(env, f = $stdout)
  keys = build_env_keys(env)
  keys -= %w[CC CXX OBJC OBJCXX] if env["CC"] == env["HOMEBREW_CC"]

  keys.each do |key|
    value = env[key]
    s = +"#{key}: #{value}"
    case key
    when "CC", "CXX", "LD"
      s << " => #{Pathname.new(value).realpath}" if File.symlink?(value)
    end
    s.freeze
    f.puts s
  end
end

.dump_options_for_formula(f) ⇒ Object



120
121
122
123
124
125
126
# File 'brew/Library/Homebrew/options.rb', line 120

def dump_options_for_formula(f)
  f.options.sort_by(&:flag).each do |opt|
    puts "#{opt.flag}\n\t#{opt.description}"
  end
  puts "--devel\n\tInstall development version #{f.devel.version}" if f.devel
  puts "--HEAD\n\tInstall HEAD version" if f.head
end

.editObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'brew/Library/Homebrew/dev-cmd/edit.rb', line 23

def edit
  edit_args.parse

  unless (HOMEBREW_REPOSITORY/".git").directory?
    raise <<~EOS
      Changes will be lost!
      The first time you `brew update`, all local changes will be lost; you should
      thus `brew update` before you `brew edit`!
    EOS
  end

  # If no brews are listed, open the project root in an editor.
  paths = [HOMEBREW_REPOSITORY] if ARGV.named.empty?

  # Don't use ARGV.formulae as that will throw if the file doesn't parse
  paths ||= ARGV.named.map do |name|
    path = Formulary.path(name)
    raise FormulaUnavailableError, name if !path.file? && !args.force?

    path
  end

  exec_editor(*paths)
end

.edit_argsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'brew/Library/Homebrew/dev-cmd/edit.rb', line 9

def edit_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `edit` [<formula>]

      Open <formula> in the editor set by `EDITOR` or `HOMEBREW_EDITOR`, or open the
      Homebrew repository for editing if no formula is provided.
    EOS
    switch :force
    switch :verbose
    switch :debug
  end
end

.ensure_relocation_formulae_installed!Object



100
101
102
103
104
105
106
107
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 100

def ensure_relocation_formulae_installed!
  Keg.relocation_formulae.each do |f|
    next if Formula[f].installed?

    ohai "Installing #{f}..."
    safe_system HOMEBREW_BREW_FILE, "install", f
  end
end

.external_commandsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'brew/Library/Homebrew/cmd/commands.rb', line 63

def external_commands
  cmd_paths = PATH.new(ENV["PATH"]).append(Tap.cmd_directories)
  cmd_paths.each_with_object([]) do |path, cmds|
    Dir["#{path}/brew-*"].each do |file|
      next unless File.executable?(file)

      cmd = File.basename(file, ".rb")[5..-1]
      next if cmd.include?(".")

      cmds << cmd
    end
  end.sort
end

.extractObject

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'brew/Library/Homebrew/dev-cmd/extract.rb', line 97

def extract
  extract_args.parse

  # Expect exactly two named arguments: formula and tap
  raise UsageError if args.remaining.length != 2

  if args.remaining.first !~ HOMEBREW_TAP_FORMULA_REGEX
    name = args.remaining.first.downcase
    source_tap = CoreTap.instance
  else
    name = Regexp.last_match(3).downcase
    source_tap = Tap.fetch(Regexp.last_match(1), Regexp.last_match(2))
    raise TapFormulaUnavailableError.new(source_tap, name) unless source_tap.installed?
  end

  destination_tap = Tap.fetch(args.remaining.second)
  odie "Cannot extract formula to homebrew/core!" if destination_tap.core_tap?
  odie "Cannot extract formula to the same tap!" if destination_tap == source_tap
  destination_tap.install unless destination_tap.installed?

  repo = source_tap.path
  pattern = if source_tap.core_tap?
    [repo/"Formula/#{name}.rb"]
  else
    # A formula can technically live in the root directory of a tap or in any of its subdirectories
    [repo/"#{name}.rb", repo/"**/#{name}.rb"]
  end

  if args.version
    ohai "Searching repository history"
    version = args.version
    rev = "HEAD"
    test_formula = nil
    result = ""
    loop do
      rev, (path,) = Git.last_revision_commit_of_files(repo, pattern, before_commit: "#{rev}~1")
      odie "Could not find #{name}! The formula or version may not have existed." if rev.nil?

      file = repo/path
      result = Git.last_revision_of_file(repo, file, before_commit: rev)
      if result.empty?
        ohai "Skipping revision #{rev} - file is empty at this revision" if ARGV.debug?
        next
      end

      test_formula = formula_at_revision(repo, name, file, rev)
      break if test_formula.nil? || test_formula.version == version

      ohai "Trying #{test_formula.version} from revision #{rev} against desired #{version}" if ARGV.debug?
    end
    odie "Could not find #{name}! The formula or version may not have existed." if test_formula.nil?
  else
    # Search in the root directory of <repo> as well as recursively in all of its subdirectories
    files = Dir[repo/"{,**/}"].map do |dir|
      Pathname.glob(["#{dir}/#{name}.rb"]).find(&:file?)
    end.compact

    if files.empty?
      ohai "Searching repository history"
      rev, (path,) = Git.last_revision_commit_of_files(repo, pattern)
      odie "Could not find #{name}! The formula or version may not have existed." if rev.nil?
      file = repo/path
      version = formula_at_revision(repo, name, file, rev).version
      result = Git.last_revision_of_file(repo, file)
    else
      file = files.first.realpath
      rev = "HEAD"
      version = Formulary.factory(file).version
      result = File.read(file)
    end
  end

  # The class name has to be renamed to match the new filename,
  # e.g. Foo version 1.2.3 becomes FooAT123 and resides in Foo@1.2.3.rb.
  class_name = Formulary.class_s(name)
  versioned_name = Formulary.class_s("#{name}@#{version}")
  result.gsub!("class #{class_name} < Formula", "class #{versioned_name} < Formula")

  path = destination_tap.path/"Formula/#{name}@#{version}.rb"
  if path.exist?
    unless ARGV.force?
      odie <<~EOS
        Destination formula already exists: #{path}
        To overwrite it and continue anyways, run:
          brew extract --force --version=#{version} #{name} #{destination_tap.name}
      EOS
    end
    ohai "Overwriting existing formula at #{path}" if ARGV.debug?
    path.delete
  end
  ohai "Writing formula for #{name} from revision #{rev} to #{path}"
  path.write result
end

.extract_argsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'brew/Library/Homebrew/dev-cmd/extract.rb', line 79

def extract_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `extract` [<options>] <formula> <tap>

      Look through repository history to find the most recent version of <formula> and
      create a copy in <tap>`/Formula/`<formula>`@`<version>`.rb`. If the tap is not
      installed yet, attempt to install/clone the tap before continuing. To extract
      a formula from a tap that is not `homebrew/core` use its fully-qualified form of
      <user>`/`<repo>`/`<formula>.
    EOS
    flag   "--version=",
           description: "Extract the specified <version> of <formula> instead of the most recent."
    switch :force
    switch :debug
  end
end

.failed?Boolean

Returns:

  • (Boolean)


73
74
75
76
# File 'brew/Library/Homebrew/global.rb', line 73

def failed?
  @failed ||= false
  @failed == true
end

.fetchObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'brew/Library/Homebrew/cmd/fetch.rb', line 45

def fetch
  fetch_args.parse

  raise FormulaUnspecifiedError if ARGV.named.empty?

  if args.deps?
    bucket = []
    ARGV.formulae.each do |f|
      bucket << f
      bucket.concat f.recursive_dependencies.map(&:to_formula)
    end
    bucket.uniq!
  else
    bucket = ARGV.formulae
  end

  puts "Fetching: #{bucket * ", "}" if bucket.size > 1
  bucket.each do |f|
    f.print_tap_action verb: "Fetching"

    fetched_bottle = false
    if Fetch.fetch_bottle?(f)
      begin
        fetch_formula(f.bottle)
      rescue Interrupt
        raise
      rescue => e
        raise if ARGV.homebrew_developer?

        fetched_bottle = false
        onoe e.message
        opoo "Bottle fetch failed: fetching the source."
      else
        fetched_bottle = true
      end
    end

    next if fetched_bottle

    fetch_formula(f)

    f.resources.each do |r|
      fetch_resource(r)
      r.patches.each { |p| fetch_patch(p) if p.external? }
    end

    f.patchlist.each { |p| fetch_patch(p) if p.external? }
  end
end

.fetch_argsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'brew/Library/Homebrew/cmd/fetch.rb', line 10

def fetch_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `fetch` [<options>] <formula>

      Download a bottle (if available) or source packages for <formula>.
      For tarballs, also print SHA-256 checksums.
    EOS
    switch "--HEAD",
           description: "Fetch HEAD version instead of stable version."
    switch "--devel",
           description: "Fetch development version instead of stable version."
    switch :force,
           description: "Remove a previously cached version and re-fetch."
    switch :verbose,
           description: "Do a verbose VCS checkout, if the URL represents a VCS. This is useful for "\
                        "seeing if an existing VCS cache has been updated."
    switch "--retry",
           description: "Retry if downloading fails or re-download if the checksum of a previously cached "\
                        "version no longer matches."
    switch "--deps",
           description: "Also download dependencies for any listed <formula>."
    switch "-s", "--build-from-source",
           description: "Download source packages rather than a bottle."
    switch "--build-bottle",
           description: "Download source packages (for eventual bottling) rather than a bottle."
    switch "--force-bottle",
           description: "Download a bottle if it exists for the current or newest version of macOS, "\
                        "even if it would not be used during installation."
    switch :debug
    conflicts "--devel", "--HEAD"
    conflicts "--build-from-source", "--build-bottle", "--force-bottle"
  end
end

.fetch_bottles_patch(bottle_commit_url, args, bottle_branch, branch, orig_revision) ⇒ Object



296
297
298
299
300
301
302
303
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 296

def fetch_bottles_patch(bottle_commit_url, args, bottle_branch, branch, orig_revision)
  safe_system "git", "checkout", "--quiet", "-B", bottle_branch, orig_revision
  PatchPuller.new(bottle_commit_url, args, "bottle commit").pull_patch
  safe_system "git", "rebase", "--quiet", branch
  safe_system "git", "checkout", "--quiet", branch
  safe_system "git", "merge", "--quiet", "--ff-only", "--no-edit", bottle_branch
  safe_system "git", "branch", "--quiet", "-D", bottle_branch
end

.fetch_fetchable(f) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'brew/Library/Homebrew/cmd/fetch.rb', line 129

def fetch_fetchable(f)
  f.clear_cache if args.force?

  already_fetched = f.cached_download.exist?

  begin
    download = f.fetch(verify_download_integrity: false)
  rescue DownloadError
    retry if retry_fetch? f
    raise
  end

  return unless download.file?

  puts "Downloaded to: #{download}" unless already_fetched
  puts Checksum::TYPES.map { |t| "#{t.to_s.upcase}: #{download.send(t)}" }

  f.verify_download_integrity(download)
end

.fetch_formula(f) ⇒ Object



103
104
105
106
107
108
# File 'brew/Library/Homebrew/cmd/fetch.rb', line 103

def fetch_formula(f)
  fetch_fetchable f
rescue ChecksumMismatchError => e
  retry if retry_fetch? f
  opoo "Formula reports different #{e.hash_type}: #{e.expected}"
end

.fetch_merge_patch(url, args, issue) ⇒ Object



305
306
307
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 305

def fetch_merge_patch(url, args, issue)
  PatchPuller.new(url, args, "merge commit").pull_merge_commit(issue)
end

.fetch_patch(p) ⇒ Object



110
111
112
113
114
115
# File 'brew/Library/Homebrew/cmd/fetch.rb', line 110

def fetch_patch(p)
  fetch_fetchable p
rescue ChecksumMismatchError => e
  Homebrew.failed = true
  opoo "Patch reports different #{e.hash_type}: #{e.expected}"
end

.fetch_pull_requests(formula) ⇒ Object



397
398
399
400
401
402
403
404
405
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 397

def fetch_pull_requests(formula)
  GitHub.issues_for_formula(formula.name, tap: formula.tap).select do |pr|
    pr["html_url"].include?("/pull/") &&
      /(^|\s)#{Regexp.quote(formula.name)}(:|\s|$)/i =~ pr["title"]
  end
rescue GitHub::RateLimitExceededError => e
  opoo e.message
  []
end

.fetch_resource(r) ⇒ Object



95
96
97
98
99
100
101
# File 'brew/Library/Homebrew/cmd/fetch.rb', line 95

def fetch_resource(r)
  puts "Resource: #{r.name}"
  fetch_fetchable r
rescue ChecksumMismatchError => e
  retry if retry_fetch? r
  opoo "Resource #{r.name} reports different #{e.hash_type}: #{e.expected}"
end

.files_changed_in_patch(patchfile, tap) ⇒ Object

List files changed by a patch, partitioned in to those that are (probably) formula definitions, and those which aren’t. Only applies to patches on Homebrew core or taps, based simply on relative pathnames of affected files.



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 386

def files_changed_in_patch(patchfile, tap)
  files = []
  formulae = []
  others = []
  File.foreach(patchfile) do |line|
    files << Regexp.last_match(1) if line =~ %r{^\+\+\+ b/(.*)}
  end
  files.each do |file|
    if tap&.formula_file?(file)
      formula_name = File.basename(file, ".rb")
      formulae << formula_name unless formulae.include?(formula_name)
    else
      others << file
    end
  end
  { files: files, formulae: formulae, others: others }
end

.filtered_listObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'brew/Library/Homebrew/cmd/list.rb', line 125

def filtered_list
  names = if ARGV.named.empty?
    Formula.racks
  else
    racks = ARGV.named.map { |n| Formulary.to_rack(n) }
    racks.select do |rack|
      Homebrew.failed = true unless rack.exist?
      rack.exist?
    end
  end
  if args.pinned?
    pinned_versions = {}
    names.sort.each do |d|
      keg_pin = (HOMEBREW_PINNED_KEGS/d.basename.to_s)
      pinned_versions[d] = keg_pin.readlink.basename.to_s if keg_pin.exist? || keg_pin.symlink?
    end
    pinned_versions.each do |d, version|
      puts d.basename.to_s.concat(args.versions? ? " #{version}" : "")
    end
  else # --versions without --pinned
    names.sort.each do |d|
      versions = d.subdirs.map { |pn| pn.basename.to_s }
      next if args.multiple? && versions.length < 2

      puts "#{d.basename} #{versions * " "}"
    end
  end
end

.find_internal_commands(directory) ⇒ Object



77
78
79
80
81
# File 'brew/Library/Homebrew/cmd/commands.rb', line 77

def find_internal_commands(directory)
  Pathname.glob(directory/"*")
          .select(&:file?)
          .map { |f| f.basename.to_s.sub(/\.(?:rb|sh)$/, "") }
end

.force_auto_update?Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'brew/Library/Homebrew/cmd/tap.rb', line 71

def force_auto_update?
  # if no relevant flag is present, return nil, meaning "no change"
  true if args.force_auto_update?
end

.format_count(count) ⇒ Object



408
409
410
# File 'brew/Library/Homebrew/cmd/info.rb', line 408

def format_count(count)
  count.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end

.format_long_opt(opt) ⇒ Object



234
235
236
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 234

def format_long_opt(opt)
  "`#{opt}`" unless opt.nil?
end

.format_percent(percent) ⇒ Object



412
413
414
# File 'brew/Library/Homebrew/cmd/info.rb', line 412

def format_percent(percent)
  format "%.2f", percent
end

.format_problem_lines(problems) ⇒ Object



166
167
168
# File 'brew/Library/Homebrew/dev-cmd/audit.rb', line 166

def format_problem_lines(problems)
  problems.uniq.map { |p| "* #{p.chomp.gsub("\n", "\n    ")}" }
end

.format_short_opt(opt) ⇒ Object



230
231
232
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 230

def format_short_opt(opt)
  "`#{opt}`" unless opt.nil?
end

.format_usage_banner(usage_banner) ⇒ Object



238
239
240
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 238

def format_usage_banner(usage_banner)
  usage_banner&.sub(/^(#: *\* )?/, "### ")
end

.formulaObject



21
22
23
24
25
26
27
# File 'brew/Library/Homebrew/dev-cmd/formula.rb', line 21

def formula
  formula_args.parse

  raise FormulaUnspecifiedError if ARGV.named.empty?

  ARGV.resolved_formulae.each { |f| puts f.path }
end

.formula_argsObject



9
10
11
12
13
14
15
16
17
18
19
# File 'brew/Library/Homebrew/dev-cmd/formula.rb', line 9

def formula_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `formula` <formula>

      Display the path where <formula> is located.
    EOS
    switch :verbose
    switch :debug
  end
end

.formula_version(formula, spec, contents = nil) ⇒ Object



387
388
389
390
391
392
393
394
395
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 387

def formula_version(formula, spec, contents = nil)
  name = formula.name
  path = formula.path
  if contents
    Formulary.from_contents(name, path, contents, spec).version
  else
    Formulary::FormulaLoader.new(name, path).get_formula(spec).version
  end
end

.formulae_api_json(endpoint) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
# File 'brew/Library/Homebrew/cmd/info.rb', line 221

def formulae_api_json(endpoint)
  return if ENV["HOMEBREW_NO_ANALYTICS"] || ENV["HOMEBREW_NO_GITHUB_API"]

  output, = curl_output("--max-time", "5",
                        "https://formulae.brew.sh/api/#{endpoint}")
  return if output.blank?

  JSON.parse(output)
rescue JSON::ParserError
  nil
end

.full_clone?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'brew/Library/Homebrew/cmd/tap.rb', line 67

def full_clone?
  args.full? || ARGV.homebrew_developer?
end

.gem_user_bindirObject



16
17
18
19
# File 'brew/Library/Homebrew/utils/gems.rb', line 16

def gem_user_bindir
  require "rubygems"
  "#{Gem.user_dir}/bin"
end

.generate_cmd_manpages(glob) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 150

def generate_cmd_manpages(glob)
  cmd_paths = Pathname.glob(glob).sort
  man_page_lines = []
  man_args = Homebrew.args
  # preserve existing manpage order
  cmd_paths.sort_by(&method(:sort_key_for_path))
           .each do |cmd_path|
    cmd_args_method_name = cmd_arg_parser(cmd_path)

    cmd_man_page_lines = begin
      cmd_parser = Homebrew.send(cmd_args_method_name)
      next if cmd_parser.hide_from_man_page

      cmd_parser_manpage_lines(cmd_parser).join
    rescue NoMethodError => e
      raise if e.name != cmd_args_method_name

      nil
    end
    cmd_man_page_lines ||= cmd_comment_manpage_lines(cmd_path)

    man_page_lines << cmd_man_page_lines
  end
  Homebrew.args = man_args
  man_page_lines.compact.join("\n")
end

.generate_option_doc(short, long, desc) ⇒ Object



225
226
227
228
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 225

def generate_option_doc(short, long, desc)
  comma = (short && long) ? ", " : ""
  "* #{format_short_opt(short)}" + comma + "#{format_long_opt(long)}:" + "\n  " + desc + "\n"
end

.gist_logsObject



143
144
145
146
147
148
149
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 143

def gist_logs
  raise FormulaUnspecifiedError if ARGV.resolved_formulae.length != 1

  Install.perform_preinstall_checks(all_fatal: true)
  Install.perform_build_from_source_checks(all_fatal: true)
  gistify_logs(ARGV.resolved_formulae.first)
end

.gist_logs_argsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 13

def gist_logs_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `gist-logs` [<options>] <formula>

      Upload logs for a failed build of <formula> to a new Gist. Presents an
      error message if no logs are found.
    EOS
    switch "--with-hostname",
           description: "Include the hostname in the Gist."
    switch "-n", "--new-issue",
           description: "Automatically create a new issue in the appropriate GitHub repository "\
                        "after creating the Gist."
    switch "-p", "--private",
           description: "The Gist will be marked private and will not appear in listings but will "\
                        "be accessible with its link."
    switch :verbose
    switch :debug
  end
end

.gistify_logs(f) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'brew/Library/Homebrew/cmd/gist-logs.rb', line 34

def gistify_logs(f)
  gist_logs_args.parse

  files = load_logs(f.logs)
  build_time = f.logs.ctime
  timestamp = build_time.strftime("%Y-%m-%d_%H-%M-%S")

  s = StringIO.new
  SystemConfig.dump_verbose_config s
  # Dummy summary file, asciibetically first, to control display title of gist
  files["# #{f.name} - #{timestamp}.txt"] = { content: brief_build_info(f) }
  files["00.config.out"] = { content: s.string }
  files["00.doctor.out"] = { content: Utils.popen_read("#{HOMEBREW_PREFIX}/bin/brew", "doctor", err: :out) }
  unless f.core_formula?
    tap = <<~EOS
      Formula: #{f.name}
          Tap: #{f.tap}
         Path: #{f.path}
    EOS
    files["00.tap.out"] = { content: tap }
  end

  if GitHub.api_credentials_type == :none
    puts <<~EOS
      You can create a new personal access token:
        #{GitHub::ALL_SCOPES_URL}
      #{Utils::Shell.set_variable_in_profile("HOMEBREW_GITHUB_API_TOKEN", "your_token_here")}

    EOS
    login!
  end

  # Description formatted to work well as page title when viewing gist
  if f.core_formula?
    descr = "#{f.name} on #{OS_VERSION} - Homebrew build logs"
  else
    descr = "#{f.name} (#{f.full_name}) on #{OS_VERSION} - Homebrew build logs"
  end
  url = create_gist(files, descr)

  url = create_issue(f.tap, "#{f.name} failed to build on #{MacOS.full_version}", url) if args.new_issue?

  puts url if url
end

.git_log(cd_dir, path = nil, tap = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'brew/Library/Homebrew/cmd/log.rb', line 40

def git_log(cd_dir, path = nil, tap = nil)
  cd cd_dir
  repo = Utils.popen_read("git rev-parse --show-toplevel").chomp
  if tap
    name = tap.to_s
    git_cd = "$(brew --repo #{tap})"
  elsif cd_dir == HOMEBREW_REPOSITORY
    name = "Homebrew/brew"
    git_cd = "$(brew --repo)"
  else
    name, git_cd = cd_dir
  end

  if File.exist? "#{repo}/.git/shallow"
    opoo <<~EOS
      #{name} is a shallow clone so only partial output will be shown.
      To get a full clone run:
        git -C "#{git_cd}" fetch --unshallow
    EOS
  end
  args = ARGV.options_only
  args += ["--follow", "--", path] unless path.nil?
  system "git", "log", *args
end

.github_info(f) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'brew/Library/Homebrew/cmd/info.rb', line 127

def github_info(f)
  if f.tap
    if remote = f.tap.remote
      path = f.path.relative_path_from(f.tap.path)
      github_remote_path(remote, path)
    else
      f.path
    end
  else
    f.path
  end
end

.github_remote_path(remote, path) ⇒ Object



119
120
121
122
123
124
125
# File 'brew/Library/Homebrew/cmd/info.rb', line 119

def github_remote_path(remote, path)
  if remote =~ %r{^(?:https?://|git(?:@|://))github\.com[:/](.+)/(.+?)(?:\.git)?$}
    "https://github.com/#{Regexp.last_match(1)}/#{Regexp.last_match(2)}/blob/master/#{path}"
  else
    "#{remote}/#{path}"
  end
end

.global_options_manpageObject



216
217
218
219
220
221
222
223
# File 'brew/Library/Homebrew/dev-cmd/man.rb', line 216

def global_options_manpage
  lines = ["These options are applicable across all sub-commands.\n"]
  lines += Homebrew::CLI::Parser.global_options.values.map do |names, _, desc|
    short, long = names
    generate_option_doc(short, long, desc)
  end
  lines.join("\n")
end

.handle_unsatisfied_dependents(kegs_by_rack) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'brew/Library/Homebrew/cmd/uninstall.rb', line 101

def handle_unsatisfied_dependents(kegs_by_rack)
  return if args.ignore_dependencies?

  all_kegs = kegs_by_rack.values.flatten(1)
  check_for_dependents all_kegs
rescue MethodDeprecatedError
  # Silently ignore deprecations when uninstalling.
  nil
end

.head_revision(_url, fetched) ⇒ Object



292
293
294
# File 'brew/Library/Homebrew/dev-cmd/pull.rb', line 292

def head_revision(_url, fetched)
  Utils.popen_read("git", "rev-parse", fetched ? "FETCH_HEAD" : "HEAD").strip
end

.homeObject



20
21
22
23
24
25
26
27
28
# File 'brew/Library/Homebrew/cmd/home.rb', line 20

def home
  home_args.parse

  if args.remaining.empty?
    exec_browser HOMEBREW_WWW
  else
    exec_browser(*ARGV.formulae.map(&:homepage))
  end
end

.home_argsObject



8
9
10
11
12
13
14
15
16
17
18
# File 'brew/Library/Homebrew/cmd/home.rb', line 8

def home_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `home` [<formula>]

      Open <formula>'s homepage in a browser, or open Homebrew's own homepage
      if no formula is provided.
    EOS
    switch :debug
  end
end

.infoObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'brew/Library/Homebrew/cmd/info.rb', line 57

def info
  info_args.parse
  if args.json
    raise UsageError, "invalid JSON version: #{args.json}" unless ["v1", true].include? args.json

    print_json
  elsif args.github?
    exec_browser(*ARGV.formulae.map { |f| github_info(f) })
  else
    print_info
  end
end

.info_argsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'brew/Library/Homebrew/cmd/info.rb', line 15

def info_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `info` [<options>] [<formula>]

      Display brief statistics for your Homebrew installation.

      If <formula> is provided, show summary of information about <formula>.
    EOS
    switch "--analytics",
           description: "List global Homebrew analytics data or, if specified, installation and "\
                        "build error data for <formula> (provided neither `HOMEBREW_NO_ANALYTICS` "\
                        "nor `HOMEBREW_NO_GITHUB_API` are set)."
    flag   "--days",
           depends_on:  "--analytics",
           description: "How many days of global analytics data to retrieve. "\
                        "The value for <days> must be `30`, `90` or `365`. The default is `30`."
    flag   "--category",
           depends_on:  "--analytics",
           description: "Which type of global analytics data to retrieve. "\
                        "The value for <category> must be `install`, `install-on-request`, "\
                        "`cask-install`, `build-error` or `os-version`. The default is `install`."
    switch "--github",
           description: "Open the GitHub source page for <formula> in a browser. "\
                        "To view formula history locally: `brew log -p` <formula>"
    flag   "--json",
           description: "Print a JSON representation of <formula>. Currently the default and only accepted "\
                        "value for <version> is `v1`. See the docs for examples of using the JSON "\
                        "output: <https://docs.brew.sh/Querying-Brew>"
    switch "--installed",
           depends_on:  "--json",
           description: "Print JSON of formulae that are currently installed."
    switch "--all",
           depends_on:  "--json",
           description: "Print JSON of all available formulae."
    switch :verbose,
           description: "Show more verbose analytics data for <formula>."
    switch :debug
    conflicts "--installed", "--all"
  end
end

.info_formula(f) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'brew/Library/Homebrew/cmd/info.rb', line 140

def info_formula(f)
  specs = []

  if stable = f.stable
    s = "stable #{stable.version}"
    s += " (bottled)" if stable.bottled?
    specs << s
  end

  if devel = f.devel
    specs << "devel #{devel.version}"
  end

  specs << "HEAD" if f.head

  attrs = []
  attrs << "pinned at #{f.pinned_version}" if f.pinned?
  attrs << "keg-only" if f.keg_only?

  puts "#{f.full_name}: #{specs * ", "}#{" [#{attrs * ", "}]" unless attrs.empty?}"
  puts f.desc if f.desc
  puts Formatter.url(f.homepage) if f.homepage

  conflicts = f.conflicts.map do |c|
    reason = " (because #{c.reason})" if c.reason
    "#{c.name}#{reason}"
  end.sort!
  unless conflicts.empty?
    puts <<~EOS
      Conflicts with:
        #{conflicts.join("\n  ")}
    EOS
  end

  kegs = f.installed_kegs
  heads, versioned = kegs.partition { |k| k.version.head? }
  kegs = [
    *heads.sort_by { |k| -Tab.for_keg(k).time.to_i },
    *versioned.sort_by(&:version),
  ]
  if kegs.empty?
    puts "Not installed"
  else
    kegs.each do |keg|
      puts "#{keg} (#{keg.abv})#{" *" if keg.linked?}"
      tab = Tab.for_keg(keg).to_s
      puts "  #{tab}" unless tab.empty?
    end
  end

  puts "From: #{Formatter.url(github_info(f))}"

  unless f.deps.empty?
    ohai "Dependencies"
    %w[build required recommended optional].map do |type|
      deps = f.deps.send(type).uniq
      puts "#{type.capitalize}: #{decorate_dependencies deps}" unless deps.empty?
    end
  end

  unless f.requirements.to_a.empty?
    ohai "Requirements"
    %w[build required recommended optional].map do |type|
      reqs = f.requirements.select(&:"#{type}?")
      next if reqs.to_a.empty?

      puts "#{type.capitalize}: #{decorate_requirements(reqs)}"
    end
  end

  if !f.options.empty? || f.head || f.devel
    ohai "Options"
    Homebrew.dump_options_for_formula f
  end

  caveats = Caveats.new(f)
  ohai "Caveats", caveats.to_s unless caveats.empty?

  output_formula_analytics(f)
end

.inject_dump_stats!(the_module, pattern) ⇒ Object

rubocop:disable Style/GlobalVars



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'brew/Library/Homebrew/utils.rb', line 43

def inject_dump_stats!(the_module, pattern)
  @injected_dump_stat_modules ||= {}
  @injected_dump_stat_modules[the_module] ||= []
  injected_methods = @injected_dump_stat_modules[the_module]
  the_module.module_eval do
    instance_methods.grep(pattern).each do |name|
      next if injected_methods.include? name

      method = instance_method(name)
      define_method(name) do |*args, &block|
        begin
          time = Time.now
          method.bind(self).call(*args, &block)
        ensure
          $times[name] ||= 0
          $times[name] += Time.now - time
        end
      end
    end
  end

  return unless $times.nil?

  $times = {}
  at_exit do
    col_width = [$times.keys.map(&:size).max.to_i + 2, 15].max
    $times.sort_by { |_k, v| v }.each do |method, time|
      puts format("%-*s %0.4f sec", col_width, "#{method}:", time)
    end
  end
end

.inreplace_pairs(path, replacement_pairs) ⇒ Object



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'brew/Library/Homebrew/dev-cmd/bump-formula-pr.rb', line 360

def inreplace_pairs(path, replacement_pairs)
  if args.dry_run?
    contents = path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read }
    contents.extend(StringInreplaceExtension)
    replacement_pairs.each do |old, new|
      ohai "replace #{old.inspect} with #{new.inspect}" unless Homebrew.args.quiet?
      raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old

      contents.gsub!(old, new)
    end
    raise Utils::InreplaceError, path => contents.errors unless contents.errors.empty?

    path.atomic_write(contents) if args.write?
    contents
  else
    Utils::Inreplace.inreplace(path) do |s|
      replacement_pairs.each do |old, new|
        ohai "replace #{old.inspect} with #{new.inspect}" unless Homebrew.args.quiet?
        raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old

        s.gsub!(old, new)
      end
    end
    path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read }
  end
end

.installObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'brew/Library/Homebrew/cmd/install.rb', line 94

def install
  ARGV.named.each do |name|
    next if File.exist?(name)
    next if name !~ HOMEBREW_TAP_FORMULA_REGEX && name !~ HOMEBREW_CASK_TAP_CASK_REGEX

    tap = Tap.fetch(Regexp.last_match(1), Regexp.last_match(2))
    tap.install unless tap.installed?
  end

  install_args.parse
  raise FormulaUnspecifiedError if args.remaining.empty?

  if args.ignore_dependencies?
    opoo <<~EOS
      #{Tty.bold}--ignore-dependencies is an unsupported Homebrew developer flag!#{Tty.reset}
      Adjust your PATH to put any preferred versions of applications earlier in the
      PATH rather than using this unsupported flag!

    EOS
  end

  formulae = []

  unless ARGV.casks.empty?
    cask_args = []
    cask_args << "--force" if args.force?
    cask_args << "--debug" if args.debug?
    cask_args << "--verbose" if args.verbose?

    ARGV.casks.each do |c|
      ohai "brew cask install #{c} #{cask_args.join " "}"
      system("#{HOMEBREW_PREFIX}/bin/brew", "cask", "install", c, *cask_args)
    end
  end

  # if the user's flags will prevent bottle only-installations when no
  # developer tools are available, we need to stop them early on
  FormulaInstaller.prevent_build_flags unless DevelopmentTools.installed?

  ARGV.formulae.each do |f|
    # head-only without --HEAD is an error
    if !Homebrew.args.HEAD? && f.stable.nil? && f.devel.nil?
      raise <<~EOS
        #{f.full_name} is a head-only formula
        Install with `brew install --HEAD #{f.full_name}`
      EOS
    end

    # devel-only without --devel is an error
    if !args.devel? && f.stable.nil? && f.head.nil?
      raise <<~EOS
        #{f.full_name} is a devel-only formula
        Install with `brew install --devel #{f.full_name}`
      EOS
    end

    if !(args.HEAD? || args.devel?) && f.stable.nil?
      raise "#{f.full_name} has no stable download, please choose --devel or --HEAD"
    end

    # --HEAD, fail with no head defined
    raise "No head is defined for #{f.full_name}" if args.head? && f.head.nil?

    # --devel, fail with no devel defined
    raise "No devel block is defined for #{f.full_name}" if args.devel? && f.devel.nil?

    installed_head_version = f.latest_head_version
    if installed_head_version &&
       !f.head_version_outdated?(installed_head_version, fetch_head: args.fetch_HEAD?)
      new_head_installed = true
    end
    prefix_installed = f.prefix.exist? && !f.prefix.children.empty?

    if f.keg_only? && f.any_version_installed? && f.optlinked? && !args.force?
      # keg-only install is only possible when no other version is
      # linked to opt, because installing without any warnings can break
      # dependencies. Therefore before performing other checks we need to be
      # sure --force flag is passed.
      if f.outdated?
        optlinked_version = Keg.for(f.opt_prefix).version
        onoe <<~EOS
          #{f.full_name} #{optlinked_version} is already installed
          To upgrade to #{f.version}, run `brew upgrade #{f.name}`
        EOS
      elsif args.only_dependencies?
        formulae << f
      else
        opoo <<~EOS
          #{f.full_name} #{f.pkg_version} is already installed and up-to-date
          To reinstall #{f.pkg_version}, run `brew reinstall #{f.name}`
        EOS
      end
    elsif (args.HEAD? && new_head_installed) || prefix_installed
      # After we're sure that --force flag is passed for linked to opt
      # keg-only we need to be sure that the version we're attempting to
      # install is not already installed.

      installed_version = if args.HEAD?
        f.latest_head_version
      else
        f.pkg_version
      end

      msg = "#{f.full_name} #{installed_version} is already installed"
      linked_not_equals_installed = f.linked_version != installed_version
      if f.linked? && linked_not_equals_installed
        msg = <<~EOS
          #{msg}
          The currently linked version is #{f.linked_version}
          You can use `brew switch #{f} #{installed_version}` to link this version.
        EOS
      elsif !f.linked? || f.keg_only?
        msg = <<~EOS
          #{msg}, it's just not linked
          You can use `brew link #{f}` to link this version.
        EOS
      elsif args.only_dependencies?
        msg = nil
        formulae << f
      else
        msg = <<~EOS
          #{msg} and up-to-date
          To reinstall #{f.pkg_version}, run `brew reinstall #{f.name}`
        EOS
      end
      opoo msg if msg
    elsif !f.any_version_installed? && old_formula = f.old_installed_formulae.first
      msg = "#{old_formula.full_name} #{old_formula.installed_version} already installed"
      if !old_formula.linked? && !old_formula.keg_only?
        msg = <<~EOS
          #{msg}, it's just not linked.
          You can use `brew link #{old_formula.full_name}` to link this version.
        EOS
      end
      opoo msg
    elsif f.migration_needed? && !args.force?
      # Check if the formula we try to install is the same as installed
      # but not migrated one. If --force is passed then install anyway.
      opoo <<~EOS
        #{f.oldname} is already installed, it's just not migrated
        You can migrate this formula with `brew migrate #{f}`
        Or you can force install it with `brew install #{f} --force`
      EOS
    else
      # If none of the above is true and the formula is linked, then
      # FormulaInstaller will handle this case.
      formulae << f
    end

    # Even if we don't install this formula mark it as no longer just
    # installed as a dependency.
    next unless f.opt_prefix.directory?

    keg = Keg.new(f.opt_prefix.resolved_path)
    tab = Tab.for_keg(keg)
    unless tab.installed_on_request
      tab.installed_on_request = true
      tab.write
    end
  end

  return if formulae.empty?

  Install.perform_preinstall_checks

  formulae.each do |f|
    Migrator.migrate_if_needed(f)
    install_formula(f)
    Cleanup.install_formula_clean!(f)
  end
  Homebrew.messages.display_messages
rescue FormulaUnreadableError, FormulaClassUnavailableError,
       TapFormulaUnreadableError, TapFormulaClassUnavailableError => e
  # Need to rescue before `FormulaUnavailableError` (superclass of this)
  # is handled, as searching for a formula doesn't make sense here (the
  # formula was found, but there's a problem with its implementation).
  ofail e.message
rescue FormulaUnavailableError => e
  if e.name == "updog"
    ofail "What's updog?"
    return
  end

  ofail e.message
  if (reason = MissingFormula.reason(e.name))
    $stderr.puts reason
    return
  end

  ohai "Searching for similarly named formulae..."
  formulae_search_results = search_formulae(e.name)
  case formulae_search_results.length
  when 0
    ofail "No similarly named formulae found."
  when 1
    puts "This similarly named formula was found:"
    puts formulae_search_results
    puts "To install it, run:\n  brew install #{formulae_search_results.first}"
  else
    puts "These similarly named formulae were found:"
    puts Formatter.columns(formulae_search_results)
    puts "To install one of them, run (for example):\n  brew install #{formulae_search_results.first}"
  end

  # Do not search taps if the formula name is qualified
  return if e.name.include?("/")

  ohai "Searching taps..."
  taps_search_results = search_taps(e.name)[:formulae]
  case taps_search_results.length
  when 0
    ofail "No formulae found in taps."
  when 1
    puts "This formula was found in a tap:"
    puts taps_search_results
    puts "To install it, run:\n  brew install #{taps_search_results.first}"
  else
    puts "These formulae were found in taps:"
    puts Formatter.columns(taps_search_results)
    puts "To install one of them, run (for example):\n  brew install #{taps_search_results.first}"
  end
end

.install_argsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'brew/Library/Homebrew/cmd/install.rb', line 16

def install_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `install` [<options>] <formula>

      Install <formula>. Additional options specific to <formula> may be appended to the command.

      Unless `HOMEBREW_NO_INSTALL_CLEANUP` is set, `brew cleanup` will then be run for the
      installed formulae or, every 30 days, for all formulae.
    EOS
    switch :debug,
           description: "If brewing fails, open an interactive debugging session with access to IRB "\
                        "or a shell inside the temporary build directory."
    flag   "--env=",
           description: "If `std` is passed, use the standard build environment instead of superenv. "\
                        "If `super` is passed, use superenv even if the formula specifies the "\
                        "standard build environment."
    switch "--ignore-dependencies",
           description: "An unsupported Homebrew development flag to skip installing any dependencies of "\
                        "any kind. If the dependencies are not already present, the formula will have issues. "\
                        "If you're not developing Homebrew, consider adjusting your PATH rather than "\
                        "using this flag."
    switch "--only-dependencies",
           description: "Install the dependencies with specified options but do not install the "\
                        "formula itself."
    flag   "--cc=",
           description: "Attempt to compile using the specified <compiler>, which should be the "\
                        "name of the compiler's executable, e.g. `gcc-7` for GCC 7. "\
                        "In order to use LLVM's clang, specify `llvm_clang`. To use the "\
                        "Apple-provided clang, specify `clang`. This option will only accept "\
                        "compilers that are provided by Homebrew or bundled with macOS. "\
                        "Please do not file issues if you encounter errors while using this option."
    switch "-s", "--build-from-source",
           description: "Compile <formula> from source even if a bottle is provided. "\
                        "Dependencies will still be installed from bottles if they are available."
    switch "--force-bottle",
           description: "Install from a bottle if it exists for the current or newest version of "\
                        "macOS, even if it would not normally be used for installation."
    switch "--include-test",
           description: "Install testing dependencies required to run `brew test` <formula>."
    switch "--devel",
           description: "If <formula> defines it, install the development version."
    switch "--HEAD",
           description: "If <formula> defines it, install the HEAD version, aka. master, trunk, unstable."
    switch "--fetch-HEAD",
           description: "Fetch the upstream repository to detect if the HEAD installation of the "\
                        "formula is outdated. Otherwise, the repository's HEAD will only be checked for "\
                        "updates when a new stable or development version has been released."
    switch "--keep-tmp",
           description: "Retain the temporary files created during installation."
    switch "--build-bottle",
           description: "Prepare the formula for eventual bottling during installation, skipping any "\
                        "post-install steps."
    flag   "--bottle-arch=",
           depends_on:  "--build-bottle",
           description: "Optimise bottles for the specified architecture rather than the oldest "\
                        "architecture supported by the version of macOS the bottles are built on."
    switch :force,
           description: "Install without checking for previously installed keg-only or "\
                        "non-migrated versions."
    switch :verbose,
           description: "Print the verification and postinstall steps."
    switch "--display-times",
           env:         :display_install_times,
           description: "Print install times for each formula at the end of the run."
    switch "-i", "--interactive",
           description: "Download and patch <formula>, then open a shell. This allows the user to "\
                        "run `./configure --help` and otherwise determine how to turn the software "\
                        "package into a Homebrew package."
    switch "-g", "--git",
           description: "Create a Git repository, useful for creating patches to the software."
    conflicts "--ignore-dependencies", "--only-dependencies"
    conflicts "--devel", "--HEAD"
    conflicts "--build-from-source", "--build-bottle", "--force-bottle"
    formula_options
  end
end

.install_bundler!Object



82
83
84
85
86
# File 'brew/Library/Homebrew/utils/gems.rb', line 82

def install_bundler!
  require "rubygems"
  setup_gem_environment!(gem_home: Gem.user_dir, gem_bindir: gem_user_bindir)
  install_gem_setup_path!("bundler", version: ">=2", executable: "bundle", setup_gem_environment: false)
end

.install_bundler_gems!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'brew/Library/Homebrew/utils/gems.rb', line 88

def install_bundler_gems!
  install_bundler!

  ENV["BUNDLE_GEMFILE"] = "#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/Gemfile"
  @bundle_installed ||= begin
    bundle = "#{gem_user_bindir}/bundle"
    bundle_check_output = `#{bundle} check 2>&1`
    bundle_check_failed = !$CHILD_STATUS.exitstatus.zero?

    # for some reason sometimes the exit code lies so check the output too.
    if bundle_check_failed || bundle_check_output.include?("Install missing gems")
      unless system bundle, "install"
        odie_if_defined <<~EOS
          failed to run `#{bundle} install`!
        EOS
      end
    else
      true
    end
  end

  setup_gem_environment!
end

.install_core_tap_if_necessaryObject



148
149
150
151
152
153
154
155
156
157
158
# File 'brew/Library/Homebrew/cmd/update-report.rb', line 148

def install_core_tap_if_necessary
  return if ENV["HOMEBREW_UPDATE_TEST"]

  core_tap = CoreTap.instance
  return if core_tap.installed?

  CoreTap.ensure_installed!
  revision = core_tap.git_head
  ENV["HOMEBREW_UPDATE_BEFORE_HOMEBREW_HOMEBREW_CORE"] = revision
  ENV["HOMEBREW_UPDATE_AFTER_HOMEBREW_HOMEBREW_CORE"] = revision
end

.install_formula(f) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'brew/Library/Homebrew/cmd/install.rb', line 317

def install_formula(f)
  f.print_tap_action
  build_options = f.build

  fi = FormulaInstaller.new(f)
  fi.options              = build_options.used_options
  fi.ignore_deps          = args.ignore_dependencies?
  fi.only_deps            = args.only_dependencies?
  fi.build_bottle         = args.build_bottle?
  fi.interactive          = args.interactive?
  fi.git                  = args.git?
  fi.prelude
  fi.install
  fi.finish
rescue FormulaInstallationAlreadyAttemptedError
  # We already attempted to install f as part of the dependency tree of
  # another formula. In that case, don't generate an error, just move on.
  nil
rescue CannotInstallFormulaError => e
  ofail e.message
end

.install_gem!(name, version: nil, setup_gem_environment: true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'brew/Library/Homebrew/utils/gems.rb', line 57

def install_gem!(name, version: nil, setup_gem_environment: true)
  setup_gem_environment! if setup_gem_environment
  return unless Gem::Specification.find_all_by_name(name, version).empty?

  # Shell out to `gem` to avoid RubyGems requires for e.g. loading JSON.
  ohai_if_defined "Installing '#{name}' gem"
  install_args = %W[--no-document #{name}]
  install_args << "--version" << version if version
  return if system "#{ruby_bindir}/gem", "install", *install_args

  odie_if_defined "failed to install the '#{name}' gem."
end

.install_gem_setup_path!(name, version: nil, executable: name, setup_gem_environment: true) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'brew/Library/Homebrew/utils/gems.rb', line 70

def install_gem_setup_path!(name, version: nil, executable: name, setup_gem_environment: true)
  install_gem!(name, version: version, setup_gem_environment: setup_gem_environment)
  return if ENV["PATH"].split(":").any? do |path|
    File.executable?("#{path}/#{executable}")
  end

  odie_if_defined <<~EOS
    the '#{name}' gem is installed but couldn't find '#{executable}' in the PATH:
      #{ENV["PATH"]}
  EOS
end

.internal_commandsObject



55
56
57
# File 'brew/Library/Homebrew/cmd/commands.rb', line 55

def internal_commands
  find_internal_commands HOMEBREW_LIBRARY_PATH/"cmd"
end

.internal_developer_commandsObject



59
60
61
# File 'brew/Library/Homebrew/cmd/commands.rb', line 59

def internal_developer_commands
  find_internal_commands HOMEBREW_LIBRARY_PATH/"dev-cmd"
end

.irbObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'brew/Library/Homebrew/dev-cmd/irb.rb', line 35

def irb
  # work around IRB modifying ARGV.
  irb_args.parse(ARGV.dup)

  if args.examples?
    puts "'v8'.f # => instance of the v8 formula"
    puts ":hub.f.installed?"
    puts ":lua.f.methods - 1.methods"
    puts ":mpd.f.recursive_dependencies.reject(&:installed?)"
    return
  end

  if args.pry?
    Homebrew.install_gem_setup_path! "pry"
    require "pry"
    Pry.config.prompt_name = "brew"
  else
    require "irb"
  end

  require "formula"
  require "keg"
  require "cask/all"

  ohai "Interactive Homebrew Shell"
  puts "Example commands available with: brew irb --examples"
  if args.pry?
    Pry.start
  else
    IRB.start
  end
end

.irb_argsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'brew/Library/Homebrew/dev-cmd/irb.rb', line 20

def irb_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `irb` [<options>]

      Enter the interactive Homebrew Ruby shell.
    EOS
    switch "--examples",
           description: "Show several examples."
    switch "--pry",
           env:         :pry,
           description: "Use Pry instead of IRB. Implied if `HOMEBREW_PRY` is set."
  end
end

.keg_contain?(string, keg, ignores, formula_and_runtime_deps_names = nil) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 109

def keg_contain?(string, keg, ignores, formula_and_runtime_deps_names = nil)
  @put_string_exists_header, @put_filenames = nil

  print_filename = lambda do |str, filename|
    unless @put_string_exists_header
      opoo "String '#{str}' still exists in these files:"
      @put_string_exists_header = true
    end

    @put_filenames ||= []

    return if @put_filenames.include?(filename)

    puts Formatter.error(filename.to_s)
    @put_filenames << filename
  end

  result = false

  keg.each_unique_file_matching(string) do |file|
    next if Metafiles::EXTENSIONS.include?(file.extname) # Skip document files.

    linked_libraries = Keg.file_linked_libraries(file, string)
    result ||= !linked_libraries.empty?

    if args.verbose?
      print_filename.call(string, file) unless linked_libraries.empty?
      linked_libraries.each do |lib|
        puts " #{Tty.bold}-->#{Tty.reset} links to #{lib}"
      end
    end

    text_matches = []

    # Use strings to search through the file for each string
    Utils.popen_read("strings", "-t", "x", "-", file.to_s) do |io|
      until io.eof?
        str = io.readline.chomp
        next if ignores.any? { |i| i =~ str }
        next unless str.include? string

        offset, match = str.split(" ", 2)
        next if linked_libraries.include? match # Don't bother reporting a string if it was found by otool

        # Do not report matches to files that do not exist.
        next unless File.exist? match

        # Do not report matches to build dependencies.
        if formula_and_runtime_deps_names.present?
          begin
            keg_name = Keg.for(Pathname.new(match)).name
            next unless formula_and_runtime_deps_names.include? keg_name
          rescue NotAKegError
            nil
          end
        end

        result = true
        text_matches << [match, offset]
      end
    end

    next unless args.verbose? && !text_matches.empty?

    print_filename.call(string, file)
    text_matches.first(MAXIMUM_STRING_MATCHES).each do |match, offset|
      puts " #{Tty.bold}-->#{Tty.reset} match '#{match}' at offset #{Tty.bold}0x#{offset}#{Tty.reset}"
    end

    if text_matches.size > MAXIMUM_STRING_MATCHES
      puts "Only the first #{MAXIMUM_STRING_MATCHES} matches were output"
    end
  end

  keg_contain_absolute_symlink_starting_with?(string, keg) || result
end

Returns:

  • (Boolean)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'brew/Library/Homebrew/dev-cmd/bottle.rb', line 186

def keg_contain_absolute_symlink_starting_with?(string, keg)
  absolute_symlinks_start_with_string = []
  keg.find do |pn|
    next unless pn.symlink? && (link = pn.readlink).absolute?

    absolute_symlinks_start_with_string << pn if link.to_s.start_with?(string)
  end

  if args.verbose?
    unless absolute_symlinks_start_with_string.empty?
      opoo "Absolute symlink starting with #{string}:"
      absolute_symlinks_start_with_string.each do |pn|
        puts "  #{pn} -> #{pn.resolved_path}"
      end
    end
  end

  !absolute_symlinks_start_with_string.empty?
end

.leavesObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'brew/Library/Homebrew/cmd/leaves.rb', line 21

def leaves
  leaves_args.parse

  installed = Formula.installed.sort

  deps_of_installed = installed.flat_map do |f|
    f.runtime_dependencies.map do |dep|
      begin
        dep.to_formula.full_name
      rescue FormulaUnavailableError
        dep.name
      end
    end
  end

  leaves = installed.map(&:full_name) - deps_of_installed
  leaves.each(&method(:puts))
end

.leaves_argsObject



10
11
12
13
14
15
16
17
18
19
# File 'brew/Library/Homebrew/cmd/leaves.rb', line 10

def leaves_args
  Homebrew::CLI::Parser.new do
    usage_banner <<~EOS
      `leaves`

      List installed formulae that are not dependencies of another installed formula.
    EOS
    switch :debug
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'brew/Library/Homebrew/cmd/link.rb', line 31

def link
  link_args.parse

  raise KegUnspecifiedError if ARGV.named.empty?

  mode = OpenStruct.new

  mode.overwrite = true if args.overwrite?
  mode.dry_run = true if args.dry_run?

  ARGV.kegs.each do |keg|
    keg_only = Formulary.keg_only?(keg.rack)

    if keg.linked?
      opoo "Already linked: #{keg}"
      name_and_flag = if keg_only
        "--force #{keg.name}"
      else
        keg.name
      end
      puts "To relink: brew unlink #{keg.name} && brew link #{name_and_flag}"
      next
    end

    if mode.dry_run
      if mode.overwrite
        puts "Would remove:"
      else
        puts "Would link:"
      end
      keg.link(mode)
      puts_keg_only_path_message(keg) if keg_only
      next
    end

    if keg_only
      if Homebrew.default_prefix?
        f = keg.to_formula
        if f.keg_only_reason.reason == :provided_by_macos
          caveats = Caveats.new(f)
          opoo <<~EOS
            Refusing to link macOS-provided software: #{keg.name}
            #{caveats.keg_only_text(skip_reason: true).strip}
          EOS
          next
        end
      end

      unless args.force?
        opoo "#{keg.name} is keg-only and must be linked with --force"
        puts_keg_only_path_message(keg)
        next
      end
    end

    keg.lock do
      print "Linking #{keg}... "
      puts if args.verbose?

      begin
        n = keg.link(mode</