Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: add git_repository #10245

Merged
merged 3 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Library/Homebrew/.rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Style/Documentation:
- 'utils.rb'
- 'utils/fork.rb'
- 'utils/gems.rb'
- 'utils/git_repository.rb'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can docs be added (as these are public methods) instead of this being added to todo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I fix this style error?

Library/Homebrew/utils/git_repository.rb:4:1: C: Missing top-level module documentation comment.
module Utils
^^^^^^

What about utils/popen and utils/fork (which both extend Utils)? Would adding top-level documentation comments cause conflicts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do I fix this style error?

Ah, yes, I see. Would need to perhaps define that module in utils.rb and document it? I think it's fine to punt on that, then.

- 'utils/popen.rb'
- 'utils/shell.rb'
- 'version.rb'
Expand Down
42 changes: 22 additions & 20 deletions Library/Homebrew/extend/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,59 @@ def git?
# Gets the URL of the Git origin remote.
sig { returns(T.nilable(String)) }
def git_origin
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "config", "--get", "remote.origin.url", chdir: self).chomp.presence
Utils.popen_read(Utils::Git.git, "config", "--get", "remote.origin.url", chdir: self).chomp.presence
end

# Sets the URL of the Git origin remote.
sig { params(origin: String).returns(T.nilable(T::Boolean)) }
def git_origin=(origin)
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

safe_system "git", "remote", "set-url", "origin", origin, chdir: self
safe_system Utils::Git.git, "remote", "set-url", "origin", origin, chdir: self
end

# Gets the full commit hash of the HEAD commit.
sig { returns(T.nilable(String)) }
def git_head
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence
Utils.popen_read(Utils::Git.git, "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence
end

# Gets a short commit hash of the HEAD commit.
sig { returns(T.nilable(String)) }
def git_short_head
return unless git? && Utils::Git.available?
sig { params(length: T.nilable(Integer)).returns(T.nilable(String)) }
def git_short_head(length: nil)
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "rev-parse", "--short=4", "--verify", "-q", "HEAD", chdir: self).chomp.presence
short_arg = length&.to_s&.prepend("=")
Utils.popen_read(Utils::Git.git, "rev-parse", "--short#{short_arg}", "--verify", "-q", "HEAD", chdir: self)
.chomp.presence
end

# Gets the relative date of the last commit, e.g. "1 hour ago"
sig { returns(T.nilable(String)) }
def git_last_commit
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
Utils.popen_read(Utils::Git.git, "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
end

# Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
sig { returns(T.nilable(String)) }
def git_branch
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
Utils.popen_read(Utils::Git.git, "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
end

# Gets the name of the default origin HEAD branch.
sig { returns(T.nilable(String)) }
def git_origin_branch
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD", chdir: self)
Utils.popen_read(Utils::Git.git, "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD", chdir: self)
.chomp.presence&.split("/")&.last
end

Expand All @@ -81,16 +83,16 @@ def git_default_origin_branch?
# Returns the date of the last commit, in YYYY-MM-DD format.
sig { returns(T.nilable(String)) }
def git_last_commit_date
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "show", "-s", "--format=%cd", "--date=short", "HEAD", chdir: self).chomp.presence
Utils.popen_read(Utils::Git.git, "show", "-s", "--format=%cd", "--date=short", "HEAD", chdir: self).chomp.presence
end

# Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
sig { params(commit: String).returns(T.nilable(String)) }
def git_commit_message(commit = "HEAD")
return unless git? && Utils::Git.available?
return if !git? || !Utils::Git.available?

Utils.popen_read("git", "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence
Utils.popen_read(Utils::Git.git, "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence
end
end
2 changes: 1 addition & 1 deletion Library/Homebrew/tap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def git_head
def git_short_head
raise TapUnavailableError, name unless installed?

path.git_short_head
path.git_short_head(length: 4)
end

# Time since last git commit for this {Tap}.
Expand Down
32 changes: 32 additions & 0 deletions Library/Homebrew/test/utils/git_repository_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# typed: false
# frozen_string_literal: true

require "utils/git_repository"

describe Utils do
before do
HOMEBREW_CACHE.cd do
system "git", "init"
Pathname("README.md").write("README")
system "git", "add", "README.md"
system "git", "commit", "-m", "File added"
end
end

let(:head_revision) { HOMEBREW_CACHE.cd { `git rev-parse HEAD`.chomp } }
let(:short_head_revision) { HOMEBREW_CACHE.cd { `git rev-parse --short HEAD`.chomp } }

describe ".git_head" do
it "returns the revision at HEAD" do
expect(described_class.git_head(HOMEBREW_CACHE)).to eq(head_revision)
expect(described_class.git_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5])
end
end

describe ".git_short_head" do
it "returns the short revision at HEAD" do
expect(described_class.git_short_head(HOMEBREW_CACHE)).to eq(short_head_revision)
expect(described_class.git_short_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5])
end
end
end
1 change: 1 addition & 0 deletions Library/Homebrew/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "utils/formatter"
require "utils/gems"
require "utils/git"
require "utils/git_repository"
require "utils/github"
require "utils/inreplace"
require "utils/link"
Expand Down
20 changes: 20 additions & 0 deletions Library/Homebrew/utils/git_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# typed: strict
# frozen_string_literal: true

module Utils
extend T::Sig

sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) }
def self.git_head(repo, length: nil)
return git_short_head(repo, length: length) if length.present?

repo = Pathname(repo).extend(GitRepositoryExtension)
repo.git_head
end

sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) }
def self.git_short_head(repo, length: nil)
repo = Pathname(repo).extend(GitRepositoryExtension)
repo.git_short_head(length: length)
end
end