Merge pull request #10380 from SeekingMeaning/git-repo-branch-msg

utils/git_repository: add `::git_branch` and `::git_commit_message`
This commit is contained in:
Seeker 2021-01-21 20:00:27 -08:00 committed by GitHub
commit 3f72d4033d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 37 deletions

View File

@ -18,9 +18,7 @@ module GitRepositoryExtension
# Gets the URL of the Git origin remote. # Gets the URL of the Git origin remote.
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
def git_origin def git_origin
return if !git? || !Utils::Git.available? popen_git("config", "--get", "remote.origin.url")
Utils.popen_read(Utils::Git.git, "config", "--get", "remote.origin.url", chdir: self).chomp.presence
end end
# Sets the URL of the Git origin remote. # Sets the URL of the Git origin remote.
@ -47,26 +45,19 @@ module GitRepositoryExtension
# Gets the relative date of the last commit, e.g. "1 hour ago" # Gets the relative date of the last commit, e.g. "1 hour ago"
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
def git_last_commit def git_last_commit
return if !git? || !Utils::Git.available? popen_git("show", "-s", "--format=%cr", "HEAD")
Utils.popen_read(Utils::Git.git, "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
end end
# Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state. # 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)) } sig { params(safe: T::Boolean).returns(T.nilable(String)) }
def git_branch def git_branch(safe: false)
return if !git? || !Utils::Git.available? popen_git("rev-parse", "--abbrev-ref", "HEAD", safe: safe)
Utils.popen_read(Utils::Git.git, "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
end end
# Gets the name of the default origin HEAD branch. # Gets the name of the default origin HEAD branch.
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
def git_origin_branch def git_origin_branch
return if !git? || !Utils::Git.available? popen_git("symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD")&.split("/")&.last
Utils.popen_read(Utils::Git.git, "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD", chdir: self)
.chomp.presence&.split("/")&.last
end end
# Returns true if the repository's current branch matches the default origin branch. # Returns true if the repository's current branch matches the default origin branch.
@ -78,23 +69,19 @@ module GitRepositoryExtension
# Returns the date of the last commit, in YYYY-MM-DD format. # Returns the date of the last commit, in YYYY-MM-DD format.
sig { returns(T.nilable(String)) } sig { returns(T.nilable(String)) }
def git_last_commit_date def git_last_commit_date
return if !git? || !Utils::Git.available? popen_git("show", "-s", "--format=%cd", "--date=short", "HEAD")
Utils.popen_read(Utils::Git.git, "show", "-s", "--format=%cd", "--date=short", "HEAD", chdir: self).chomp.presence
end end
# Gets the full commit message of the specified commit, or of the HEAD commit if unspecified. # Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
sig { params(commit: String).returns(T.nilable(String)) } sig { params(commit: String, safe: T::Boolean).returns(T.nilable(String)) }
def git_commit_message(commit = "HEAD") def git_commit_message(commit = "HEAD", safe: false)
return if !git? || !Utils::Git.available? popen_git("log", "-1", "--pretty=%B", commit, "--", safe: safe, err: :out)&.strip
Utils.popen_read(Utils::Git.git, "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence
end end
private private
sig { params(args: T.untyped, safe: T::Boolean).returns(T.nilable(String)) } sig { params(args: T.untyped, safe: T::Boolean, err: T.nilable(Symbol)).returns(T.nilable(String)) }
def popen_git(*args, safe: false) def popen_git(*args, safe: false, err: nil)
unless git? unless git?
return unless safe return unless safe
@ -107,6 +94,6 @@ module GitRepositoryExtension
raise "Git is unavailable" raise "Git is unavailable"
end end
T.unsafe(Utils).popen_read(Utils::Git.git, *args, safe: safe, chdir: self).chomp.presence T.unsafe(Utils).popen_read(Utils::Git.git, *args, safe: safe, chdir: self, err: err).chomp.presence
end end
end end

View File

@ -37,20 +37,21 @@ describe Utils do
system "git", "init" system "git", "init"
Pathname("README.md").write("README") Pathname("README.md").write("README")
system "git", "add", "README.md" system "git", "add", "README.md"
system "git", "commit", "-m", "File added" system "git", "commit", "-m", commit_message
system "git", "checkout", "-b", branch_name
end end
end end
let(:commit_message) { "File added" }
let(:branch_name) { "test-branch" }
let(:head_revision) { HOMEBREW_CACHE.cd { `git rev-parse HEAD`.chomp } } let(:head_revision) { HOMEBREW_CACHE.cd { `git rev-parse HEAD`.chomp } }
let(:short_head_revision) { HOMEBREW_CACHE.cd { `git rev-parse --short HEAD`.chomp } } let(:short_head_revision) { HOMEBREW_CACHE.cd { `git rev-parse --short HEAD`.chomp } }
describe ".git_head" do describe "::git_head" do
it "returns the revision at HEAD if repo parameter is specified" 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)).to eq(head_revision)
expect(described_class.git_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5]) expect(described_class.git_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5])
end
it "returns the revision at HEAD if repo parameter is omitted" do
HOMEBREW_CACHE.cd do HOMEBREW_CACHE.cd do
expect(described_class.git_head).to eq(head_revision) expect(described_class.git_head).to eq(head_revision)
expect(described_class.git_head(length: 5)).to eq(head_revision[0...5]) expect(described_class.git_head(length: 5)).to eq(head_revision[0...5])
@ -60,13 +61,10 @@ describe Utils do
include_examples "git_repository helper function", :git_head include_examples "git_repository helper function", :git_head
end end
describe ".git_short_head" do describe "::git_short_head" do
it "returns the short revision at HEAD if repo parameter is specified" 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)).to eq(short_head_revision)
expect(described_class.git_short_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5]) expect(described_class.git_short_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5])
end
it "returns the short revision at HEAD if repo parameter is omitted" do
HOMEBREW_CACHE.cd do HOMEBREW_CACHE.cd do
expect(described_class.git_short_head).to eq(short_head_revision) expect(described_class.git_short_head).to eq(short_head_revision)
expect(described_class.git_short_head(length: 5)).to eq(head_revision[0...5]) expect(described_class.git_short_head(length: 5)).to eq(head_revision[0...5])
@ -75,4 +73,28 @@ describe Utils do
include_examples "git_repository helper function", :git_short_head include_examples "git_repository helper function", :git_short_head
end end
describe "::git_branch" do
include_examples "git_repository helper function", :git_branch
it "returns the current Git branch" do
expect(described_class.git_branch(HOMEBREW_CACHE)).to eq(branch_name)
HOMEBREW_CACHE.cd do
expect(described_class.git_branch).to eq(branch_name)
end
end
end
describe "::git_commit_message" do
include_examples "git_repository helper function", :git_commit_message
it "returns the commit message of HEAD" do
expect(described_class.git_commit_message(HOMEBREW_CACHE)).to eq(commit_message)
expect(described_class.git_commit_message(HOMEBREW_CACHE, commit: head_revision)).to eq(commit_message)
HOMEBREW_CACHE.cd do
expect(described_class.git_commit_message).to eq(commit_message)
expect(described_class.git_commit_message(commit: head_revision)).to eq(commit_message)
end
end
end
end end

View File

@ -4,6 +4,7 @@
module Utils module Utils
extend T::Sig extend T::Sig
# Gets the full commit hash of the HEAD commit.
sig { sig {
params( params(
repo: T.any(String, Pathname), repo: T.any(String, Pathname),
@ -18,6 +19,7 @@ module Utils
repo.git_head(safe: safe) repo.git_head(safe: safe)
end end
# Gets a short commit hash of the HEAD commit.
sig { sig {
params( params(
repo: T.any(String, Pathname), repo: T.any(String, Pathname),
@ -29,4 +31,29 @@ module Utils
repo = Pathname(repo).extend(GitRepositoryExtension) repo = Pathname(repo).extend(GitRepositoryExtension)
repo.git_short_head(length: length, safe: safe) repo.git_short_head(length: length, safe: safe)
end end
# Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
sig {
params(
repo: T.any(String, Pathname),
safe: T::Boolean,
).returns(T.nilable(String))
}
def self.git_branch(repo = Pathname.pwd, safe: true)
repo = Pathname(repo).extend(GitRepositoryExtension)
repo.git_branch(safe: safe)
end
# Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
sig {
params(
repo: T.any(String, Pathname),
commit: String,
safe: T::Boolean,
).returns(T.nilable(String))
}
def self.git_commit_message(repo = Pathname.pwd, commit: "HEAD", safe: true)
repo = Pathname(repo).extend(GitRepositoryExtension)
repo.git_commit_message(commit, safe: safe)
end
end end