Class: GitRepository Private
Overview
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.
Given a Pathname, provides methods for querying Git repository information.
Instance Attribute Summary collapse
- #pathname ⇒ Pathname readonly private
Instance Method Summary collapse
-
#branch_name(safe: false) ⇒ String?
private
Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
-
#commit_message(commit = "HEAD", safe: false) ⇒ String?
private
Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
-
#default_origin_branch? ⇒ Boolean
private
Returns true if the repository's current branch matches the default origin branch.
- #git_repository? ⇒ Boolean private
-
#head_info ⇒ Array<([String, nil], [String, nil], [String, nil])>
private
Gets the full commit hash of the HEAD commit, the relative date of the last commit and the currently checked-out branch (or HEAD if the repository is in a detached HEAD state) in a single Git invocation.
-
#head_ref(safe: false) ⇒ String?
private
Gets the full commit hash of the HEAD commit.
- #initialize(pathname) ⇒ void constructor private
-
#last_commit_date ⇒ String?
private
Returns the date of the last commit, in YYYY-MM-DD format.
-
#last_committed ⇒ String?
private
Gets the relative date of the last commit, e.g.
-
#origin_branch_name ⇒ String?
private
Gets the name of the default origin HEAD branch.
-
#origin_has_branch?(branch) ⇒ Boolean
private
Returns true if the given branch exists on origin.
-
#origin_url ⇒ String?
private
Gets the URL of the Git origin remote.
-
#rename_branch(old:, new:) ⇒ void
private
Change the name of a local branch.
- #set_head_origin_auto ⇒ void private
-
#set_upstream_branch(local:, origin:) ⇒ void
private
Set an upstream branch for a local branch to track.
-
#shallow? ⇒ Boolean
private
Check whether this repository is a shallow clone.
-
#short_head_ref(length: nil, safe: false) ⇒ String?
private
Gets a short commit hash of the HEAD commit.
Constructor Details
#initialize(pathname) ⇒ 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.
14 15 16 |
# File 'git_repository.rb', line 14 def initialize(pathname) @pathname = pathname end |
Instance Attribute Details
#pathname ⇒ Pathname (readonly)
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.
11 12 13 |
# File 'git_repository.rb', line 11 def pathname @pathname end |
Instance Method Details
#branch_name(safe: false) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
72 73 74 75 76 77 78 79 80 81 |
# File 'git_repository.rb', line 72 def branch_name(safe: false) ref = popen_git("rev-parse", "--symbolic-full-name", "HEAD", safe:) return if ref.blank? return "HEAD" if ref == "HEAD" refs_format = "refs/heads/" return ref.delete_prefix(refs_format) if ref.start_with?(refs_format) raise "Unexpected HEAD ref format: #{ref}" end |
#commit_message(commit = "HEAD", safe: false) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
132 133 134 |
# File 'git_repository.rb', line 132 def (commit = "HEAD", safe: false) popen_git("log", "-1", "--pretty=%B", commit, "--", safe:, err: :out)&.strip end |
#default_origin_branch? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns true if the repository's current branch matches the default origin branch.
109 110 111 |
# File 'git_repository.rb', line 109 def default_origin_branch? origin_branch_name == branch_name end |
#git_repository? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
19 20 21 |
# File 'git_repository.rb', line 19 def git_repository? pathname.join(".git").exist? end |
#head_info ⇒ Array<([String, nil], [String, nil], [String, nil])>
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.
Gets the full commit hash of the HEAD commit, the relative date of the last commit and the currently checked-out branch (or HEAD if the repository is in a detached HEAD state) in a single Git invocation.
61 62 63 64 65 66 67 68 |
# File 'git_repository.rb', line 61 def head_info output = popen_git("show", "-s", "--format=%H%n%cr%n%D", "HEAD") return [nil, nil, nil] if output.nil? head, last_committed, refs = output.lines(chomp: true) branch = refs&.[](/\AHEAD -> ([^,\n]+)/, 1) || "HEAD" [head, last_committed, branch] end |
#head_ref(safe: false) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the full commit hash of the HEAD commit.
40 41 42 |
# File 'git_repository.rb', line 40 def head_ref(safe: false) popen_git("rev-parse", "--verify", "--quiet", "HEAD", safe:) end |
#last_commit_date ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns the date of the last commit, in YYYY-MM-DD format.
115 116 117 |
# File 'git_repository.rb', line 115 def last_commit_date popen_git("show", "-s", "--format=%cd", "--date=short", "HEAD") end |
#last_committed ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the relative date of the last commit, e.g. "1 hour ago"
53 54 55 |
# File 'git_repository.rb', line 53 def last_committed popen_git("show", "-s", "--format=%cr", "HEAD") end |
#origin_branch_name ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the name of the default origin HEAD branch.
97 98 99 100 101 102 103 104 105 |
# File 'git_repository.rb', line 97 def origin_branch_name ref = popen_git("symbolic-ref", "-q", "refs/remotes/origin/HEAD") return if ref.blank? refs_format = "refs/remotes/origin/" return ref.delete_prefix(refs_format) if ref.start_with?(refs_format) raise "Unexpected origin/HEAD ref format: #{ref}" end |
#origin_has_branch?(branch) ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Returns true if the given branch exists on origin
121 122 123 |
# File 'git_repository.rb', line 121 def origin_has_branch?(branch) popen_git("ls-remote", "--heads", "origin", branch).present? end |
#origin_url ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets the URL of the Git origin remote.
34 35 36 |
# File 'git_repository.rb', line 34 def origin_url origin_url_from_config || popen_git("config", "--local", "--get", "remote.origin.url", no_global_config: true) end |
#rename_branch(old:, new:) ⇒ 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.
Change the name of a local branch
85 86 87 |
# File 'git_repository.rb', line 85 def rename_branch(old:, new:) popen_git("branch", "-m", old, new) end |
#set_head_origin_auto ⇒ 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.
126 127 128 |
# File 'git_repository.rb', line 126 def set_head_origin_auto popen_git("remote", "set-head", "origin", "--auto") end |
#set_upstream_branch(local:, origin:) ⇒ 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.
Set an upstream branch for a local branch to track
91 92 93 |
# File 'git_repository.rb', line 91 def set_upstream_branch(local:, origin:) popen_git("branch", "-u", "origin/#{origin}", local) end |
#shallow? ⇒ Boolean
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Check whether this repository is a shallow clone. A linked worktree keeps
the shallow marker in the shared Git directory, so ask Git rather than
looking for .git/shallow.
Keep in sync with shallow_repository in cmd/update.sh.
28 29 30 |
# File 'git_repository.rb', line 28 def shallow? popen_git("rev-parse", "--is-shallow-repository") == "true" end |
#short_head_ref(length: nil, safe: false) ⇒ String?
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
Gets a short commit hash of the HEAD commit.
46 47 48 49 |
# File 'git_repository.rb', line 46 def short_head_ref(length: nil, safe: false) short_arg = length.present? ? "--short=#{length}" : "--short" popen_git("rev-parse", short_arg, "--verify", "--quiet", "HEAD", safe:) end |