Module: Utils::Git Private

Defined in:
Library/Homebrew/utils/git.rb

Overview

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

Helper functions for querying Git information.

Class Method Summary collapse

Class Method Details

.available?Boolean

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

Returns:

  • (Boolean)

11
12
13
# File 'Library/Homebrew/utils/git.rb', line 11

def available?
  version.present?
end

.cherry_pick!(repo, *args, resolve: false, verbose: false) ⇒ Object

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

Special case of git cherry-pick that permits non-verbose output and optional resolution on merge conflict.


147
148
149
150
151
152
153
154
155
156
157
# File 'Library/Homebrew/utils/git.rb', line 147

def cherry_pick!(repo, *args, resolve: false, verbose: false)
  cmd = [git, "-C", repo, "cherry-pick"] + args
  output = Utils.popen_read(*cmd, err: :out)
  if $CHILD_STATUS.success?
    puts output if verbose
    output
  else
    system git, "-C", repo, "cherry-pick", "--abort" unless resolve
    raise ErrorDuringExecution.new(cmd, status: $CHILD_STATUS, output: [[:stdout, output]])
  end
end

.clear_available_cacheObject

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


41
42
43
44
45
# File 'Library/Homebrew/utils/git.rb', line 41

def clear_available_cache
  remove_instance_variable(:@version) if defined?(@version)
  remove_instance_variable(:@path) if defined?(@path)
  remove_instance_variable(:@git) if defined?(@git)
end

.commit_message(repo, commit = nil) ⇒ Object

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


90
91
92
93
# File 'Library/Homebrew/utils/git.rb', line 90

def commit_message(repo, commit = nil)
  commit ||= "HEAD"
  Utils.safe_popen_read(git, "-C", repo, "log", "-1", "--pretty=%B", commit, "--", err: :out).strip
end

.current_branch(repo) ⇒ Object

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


141
142
143
# File 'Library/Homebrew/utils/git.rb', line 141

def current_branch(repo)
  Utils.popen_read("git", "-C", repo, "symbolic-ref", "--short", "HEAD").chomp.presence
end

.ensure_installed!Object

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


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'Library/Homebrew/utils/git.rb', line 95

def ensure_installed!
  return if available?

  # we cannot install brewed git if homebrew/core is unavailable.
  if CoreTap.instance.installed?
    begin
      oh1 "Installing #{Formatter.identifier("git")}"

      # Otherwise `git` will be installed from source in tests that need it. This is slow
      # and will also likely fail due to `OS::Linux` and `OS::Mac` being undefined.
      raise "Refusing to install Git on a generic OS." if ENV["HOMEBREW_TEST_GENERIC_OS"]

      safe_system HOMEBREW_BREW_FILE, "install", "git"
      clear_available_cache
    rescue
      raise "Git is unavailable"
    end
  end

  raise "Git is unavailable" unless available?
end

.file_at_commit(repo, file, commit) ⇒ Object

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


84
85
86
87
88
# File 'Library/Homebrew/utils/git.rb', line 84

def file_at_commit(repo, file, commit)
  relative_file = Pathname(file)
  relative_file = relative_file.relative_path_from(repo) if relative_file.absolute?
  Utils.popen_read(git, "-C", repo, "show", "#{commit}:#{relative_file}")
end

.gitObject

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


29
30
31
32
33
# File 'Library/Homebrew/utils/git.rb', line 29

def git
  return @git if defined?(@git)

  @git = HOMEBREW_SHIMS_PATH/"scm/git"
end

.last_revision_commit_of_file(repo, file, before_commit: nil) ⇒ Object

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


47
48
49
50
51
52
53
54
55
# File 'Library/Homebrew/utils/git.rb', line 47

def last_revision_commit_of_file(repo, file, before_commit: nil)
  args = if before_commit.nil?
    ["--skip=1"]
  else
    [before_commit.split("..").first]
  end

  Utils.popen_read(git, "-C", repo, "log", "--format=%h", "--abbrev=7", "--max-count=1", *args, "--", file).chomp
end

.last_revision_commit_of_files(repo, files, before_commit: nil) ⇒ Object

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


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'Library/Homebrew/utils/git.rb', line 57

def last_revision_commit_of_files(repo, files, before_commit: nil)
  args = if before_commit.nil?
    ["--skip=1"]
  else
    [before_commit.split("..").first]
  end

  # git log output format:
  #   <commit_hash>
  #   <file_path1>
  #   <file_path2>
  #   ...
  # return [<commit_hash>, [file_path1, file_path2, ...]]
  rev, *paths = Utils.popen_read(
    git, "-C", repo, "log",
    "--pretty=format:%h", "--abbrev=7", "--max-count=1",
    "--diff-filter=d", "--name-only", *args, "--", *files
  ).lines.map(&:chomp).reject(&:empty?)
  [rev, paths]
end

.last_revision_of_file(repo, file, before_commit: nil) ⇒ Object

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


78
79
80
81
82
# File 'Library/Homebrew/utils/git.rb', line 78

def last_revision_of_file(repo, file, before_commit: nil)
  relative_file = Pathname(file).relative_path_from(repo)
  commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
  file_at_commit(repo, file, commit_hash)
end

.origin_branch(repo) ⇒ Object

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


136
137
138
139
# File 'Library/Homebrew/utils/git.rb', line 136

def origin_branch(repo)
  Utils.popen_read(git, "-C", repo, "symbolic-ref", "-q", "--short",
                   "refs/remotes/origin/HEAD").chomp.presence
end

.pathObject

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


22
23
24
25
26
27
# File 'Library/Homebrew/utils/git.rb', line 22

def path
  return unless available?
  return @path if defined?(@path)

  @path = Utils.popen_read(git, "--homebrew=print-path").chomp.presence
end

.remote_exists?(url) ⇒ Boolean

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

Returns:

  • (Boolean)

35
36
37
38
39
# File 'Library/Homebrew/utils/git.rb', line 35

def remote_exists?(url)
  return true unless available?

  quiet_system "git", "ls-remote", url
end

.set_name_email!(author: true, committer: true) ⇒ Object

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


117
118
119
120
121
122
123
124
125
126
127
# File 'Library/Homebrew/utils/git.rb', line 117

def set_name_email!(author: true, committer: true)
  if Homebrew::EnvConfig.git_name
    ENV["GIT_AUTHOR_NAME"] = Homebrew::EnvConfig.git_name if author
    ENV["GIT_COMMITTER_NAME"] = Homebrew::EnvConfig.git_name if committer
  end

  return unless Homebrew::EnvConfig.git_email

  ENV["GIT_AUTHOR_EMAIL"] = Homebrew::EnvConfig.git_email if author
  ENV["GIT_COMMITTER_EMAIL"] = Homebrew::EnvConfig.git_email if committer
end

.setup_gpg!Object

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


129
130
131
132
133
134
# File 'Library/Homebrew/utils/git.rb', line 129

def setup_gpg!
  return unless Formula["gnupg"].optlinked?

  ENV["PATH"] = PATH.new(ENV["PATH"])
                    .prepend(Formula["gnupg"].opt_bin)
end

.versionObject

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


15
16
17
18
19
20
# File 'Library/Homebrew/utils/git.rb', line 15

def version
  return @version if defined?(@version)

  stdout, _, status = system_command(git, args: ["--version"], print_stderr: false)
  @version = status.success? ? stdout.chomp[/git version (\d+(?:\.\d+)*)/, 1] : nil
end