Class: Homebrew::Cmd::UpdateReport Private

Inherits:
AbstractCommand show all
Defined in:
cmd/update-report.rb,
sorbet/rbi/dsl/homebrew/cmd/update_report.rbi

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

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

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

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#apply_redirected_remotes ⇒ void

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

This method returns an undefined value.



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
# File 'cmd/update-report.rb', line 84

def apply_redirected_remotes
  redirected_remotes_file = ENV.fetch("HOMEBREW_REDIRECTED_REMOTES_FILE", nil)
  redirected_remotes_path = Pathname(redirected_remotes_file) if redirected_remotes_file.present?
  return unless redirected_remotes_path&.file?

  begin
    denied_redirects = []
    redirected_paths = []
    redirected_remotes_path.each_line do |line|
      tap_path, redirected_remote = line.chomp.split("\t", 2)
      next if tap_path.blank? || redirected_remote.blank?
      next unless (tap = Tap.from_path(tap_path))
      next if redirected_paths.include?(tap_path)

      redirected_paths << tap_path
      old_repository_var_suffix = tap.repository_var_suffix
      begin
        tap.apply_redirected_remote!(redirected_remote, quiet: args.quiet?)
      rescue TapRedirectNotAllowedError => e
        denied_redirects << e.message
        # update.sh may already have merged redirected content, so roll back every denied tap.
        before_revision = ENV.fetch("HOMEBREW_UPDATE_BEFORE#{old_repository_var_suffix}", nil)
        next if !before_revision&.match?(Reporter::REVISION_REGEX) || !tap.installed?

        begin
          git_args = ["-C", tap.path.to_s]
          branch = ENV.fetch("HOMEBREW_UPDATE_BRANCH#{old_repository_var_suffix}", nil).presence ||
                   tap.git_repository.origin_branch_name
          raise "Cannot identify the updated branch for #{tap.name}." if branch.blank?

          current_branch = Utils.popen_read("git", *git_args, "symbolic-ref", "--short", "-q",
                                            "HEAD").chomp
          updated_branch_checked_out = current_branch.blank? || current_branch == branch
          local_changes = updated_branch_checked_out &&
                          Utils.popen_read("git", *git_args, "status", "--porcelain",
                                           "--untracked-files=all").present?
          if local_changes
            if Utils.popen_read("git", *git_args, "ls-files", "-u").present?
              SystemCommand.safe_system "git", *git_args, "reset", "--mixed", "-q"
            end
            stash_before = Utils.popen_read("git", *git_args, "rev-parse", "-q", "--verify",
                                            "refs/stash").chomp
            SystemCommand.safe_system "git", *git_args, "-c", "core.hooksPath=#{File::NULL}",
                                      "stash", "push", "--include-untracked", "-q"
            stash_after = Utils.popen_read("git", *git_args, "rev-parse", "-q", "--verify",
                                           "refs/stash").chomp
            if stash_after == stash_before
              raise "Could not stash local changes in #{tap.path} before redirect rollback."
            end
          end
          if updated_branch_checked_out
            SystemCommand.safe_system "git", *git_args, "reset", "--hard", "-q", before_revision
          else
            SystemCommand.safe_system "git", *git_args, "update-ref", "refs/heads/#{branch}",
                                      before_revision
          end
          SystemCommand.safe_system "git", *git_args, "update-ref", "refs/remotes/origin/#{branch}",
                                    before_revision
          if local_changes && !SystemCommand.run("git", args: [*git_args, "-c", "core.hooksPath=#{File::NULL}",
                                                               "stash", "pop", "-q"]).success?
            stash_message = "Could not restore local changes for #{tap.name}. " \
                            "Rollback stash #{stash_after} contains the interrupted working tree. "
            stash_message << if stash_before.present?
              "Earlier stash #{stash_before} may contain prior edits; " \
                "inspect both before applying either."
            else
              "Inspect it before applying."
            end
            denied_redirects << stash_message
          end
        rescue => rollback_error
          denied_redirects << "Could not roll back #{tap.name}: #{rollback_error.message}\n" \
                              "Pre-update revision: #{before_revision}.\n" \
                              "Inspect its Git state and stash. " \
                              "After saving local changes, run " \
                              "`git -C #{tap.path.to_s.shellescape} reset --hard #{before_revision}` " \
                              "only if the updated branch is checked out."
        end
        next
      end
      new_repository_var_suffix = tap.repository_var_suffix
      next if old_repository_var_suffix == new_repository_var_suffix

      ["HOMEBREW_UPDATE_BEFORE", "HOMEBREW_UPDATE_AFTER"].each do |prefix|
        old_var = "#{prefix}#{old_repository_var_suffix}"
        old_value = ENV.fetch(old_var, nil)
        next if old_value.blank?

        ENV["#{prefix}#{new_repository_var_suffix}"] ||= old_value
      end
    end
    odie denied_redirects.join("\n\n") if denied_redirects.any?
  ensure
    redirected_remotes_path.unlink if redirected_remotes_path.exist?
  end
end

#args ⇒ Homebrew::Cmd::UpdateReport::Args

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



10
# File 'sorbet/rbi/dsl/homebrew/cmd/update_report.rbi', line 10

def args; end

#donation_message ⇒ void

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

This method returns an undefined value.



64
65
66
67
68
69
70
71
72
# File 'cmd/update-report.rb', line 64

def donation_message
  return if Settings.read("donationmessage") == "true"

  ohai "Homebrew is run entirely by unpaid volunteers. Please consider donating:"
  puts "  #{Formatter.url("https://github.com/Homebrew/brew#-donations")}\n\n"

  # Consider the message possibly missed if not a TTY.
  Settings.write "donationmessage", true if $stdout.tty?
end

#migrate_caskroom_caskfiles_to_json ⇒ void

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

This method returns an undefined value.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'cmd/update-report.rb', line 48

def migrate_caskroom_caskfiles_to_json
  return unless Cask::Caskroom.path.directory?

  Cask::Caskroom.path.glob("*/.metadata/*/*/Casks/*.{json,rb}").each do |caskfile|
    next if caskfile.dirname.dirname.dirname.extname == ".upgrading"
    # Other caskfiles fail to load as installed metadata, and the rollback after
    # failing through a renamed cask's old token symlink deletes the current file.
    next if caskfile != Cask::Caskroom.cask_installed_caskfile(Cask::CaskLoader.token_from_path(caskfile))

    Cask::Caskroom.migrate_caskfile_to_json(caskfile)
  rescue => e
    opoo "Failed to migrate #{caskfile} to JSON metadata: #{e}"
  end
end

#run ⇒ void

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

This method returns an undefined value.



39
40
41
42
43
44
45
# File 'cmd/update-report.rb', line 39

def run
  return output_update_report if $stdout.tty?

  Utils::Output.redirect_stdout($stderr) do
    output_update_report
  end
end

#warn_if_branch_not_updated(name, repository, updated_revision) ⇒ void

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

This method returns an undefined value.

update.sh updates the upstream branch and then checks out the original branch again, e.g. for developers, which leaves that branch unchanged.

Parameters:



77
78
79
80
81
# File 'cmd/update-report.rb', line 77

def warn_if_branch_not_updated(name, repository, updated_revision)
  return if updated_revision == repository.head_ref

  opoo "#{name} is on branch '#{repository.branch_name}'"
end