Class: GitRepository Private

Inherits:
Object show all
Defined in:
git_repository.rb

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.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

[View source]

14
15
16
# File 'git_repository.rb', line 14

def initialize(pathname)
  @pathname = pathname
end

Instance Attribute Details

#pathnamePathname (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.

Returns:


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.

Parameters:

  • safe (Boolean) (defaults to: false)

Returns:

[View source]

65
66
67
# File 'git_repository.rb', line 65

def branch_name(safe: false)
  popen_git("rev-parse", "--abbrev-ref", "HEAD", safe:)
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.

Parameters:

  • commit (String) (defaults to: "HEAD")
  • safe (Boolean) (defaults to: false)

Returns:

[View source]

112
113
114
# File 'git_repository.rb', line 112

def commit_message(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.

Returns:

  • (Boolean, nil)
[View source]

89
90
91
# File 'git_repository.rb', line 89

def default_origin_branch?
  origin_branch_name == branch_name
end

#git_repo?Boolean

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

Returns:

  • (Boolean)
[View source]

24
25
26
27
28
# File 'git_repository.rb', line 24

def git_repo?
  # delete this whole function when removing odisabled
  odisabled "GitRepository#git_repo?", "GitRepository#git_repository?"
  git_repository?
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.

Returns:

  • (Boolean)
[View source]

19
20
21
# File 'git_repository.rb', line 19

def git_repository?
  pathname.join(".git").exist?
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.

Parameters:

  • safe (Boolean) (defaults to: false)

Returns:

[View source]

46
47
48
# File 'git_repository.rb', line 46

def head_ref(safe: false)
  popen_git("rev-parse", "--verify", "--quiet", "HEAD", safe:)
end

#last_commit_dateString?

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.

Returns:

[View source]

95
96
97
# File 'git_repository.rb', line 95

def last_commit_date
  popen_git("show", "-s", "--format=%cd", "--date=short", "HEAD")
end

#last_committedString?

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"

Returns:

[View source]

59
60
61
# File 'git_repository.rb', line 59

def last_committed
  popen_git("show", "-s", "--format=%cr", "HEAD")
end

#origin_branch_nameString?

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.

Returns:

[View source]

83
84
85
# File 'git_repository.rb', line 83

def origin_branch_name
  popen_git("symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD")&.split("/")&.last
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

Parameters:

Returns:

  • (Boolean)
[View source]

101
102
103
# File 'git_repository.rb', line 101

def origin_has_branch?(branch)
  popen_git("ls-remote", "--heads", "origin", branch).present?
end

#origin_urlString?

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.

Returns:

[View source]

32
33
34
# File 'git_repository.rb', line 32

def origin_url
  popen_git("config", "--get", "remote.origin.url")
end

#origin_url=(origin) ⇒ 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.

Sets the URL of the Git origin remote.

Parameters:

Returns:

  • (Boolean, nil)
[View source]

38
39
40
41
42
# File 'git_repository.rb', line 38

def origin_url=(origin)
  return if !git_repository? || !Utils::Git.available?

  safe_system Utils::Git.git, "remote", "set-url", "origin", origin, chdir: pathname
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

Parameters:

[View source]

71
72
73
# File 'git_repository.rb', line 71

def rename_branch(old:, new:)
  popen_git("branch", "-m", old, new)
end

#set_head_origin_autovoid

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.

[View source]

106
107
108
# File 'git_repository.rb', line 106

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

Parameters:

[View source]

77
78
79
# File 'git_repository.rb', line 77

def set_upstream_branch(local:, origin:)
  popen_git("branch", "-u", "origin/#{origin}", local)
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.

Parameters:

  • length (Integer, nil) (defaults to: nil)
  • safe (Boolean) (defaults to: false)

Returns:

[View source]

52
53
54
55
# File 'git_repository.rb', line 52

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