tap: add methods to get git related information

These methods will be used in `brew --version`, `brew config`
and `brew doctor` after core/formula separation.

Closes Homebrew/homebrew#49796.

Signed-off-by: Xu Cheng <xucheng@me.com>
This commit is contained in:
Xu Cheng 2016-03-05 20:03:43 +08:00
parent a2d0d88bf1
commit 9f96e41b40
2 changed files with 68 additions and 7 deletions

View File

@ -83,7 +83,7 @@ class Tap
# e.g. `https://github.com/user/homebrew-repo`
def remote
@remote ||= if installed?
if git?
if git? && Utils.git_available?
path.cd do
Utils.popen_read("git", "config", "--get", "remote.origin.url").chomp
end
@ -98,6 +98,34 @@ class Tap
(path/".git").exist?
end
# git HEAD for this {Tap}.
def git_head
raise TapUnavailableError, name unless installed?
return unless git? && Utils.git_available?
path.cd { Utils.popen_read("git", "rev-parse", "--verify", "-q", "HEAD").chuzzle }
end
# git HEAD in short format for this {Tap}.
def git_short_head
raise TapUnavailableError, name unless installed?
return unless git? && Utils.git_available?
path.cd { Utils.popen_read("git", "rev-parse", "--short=4", "--verify", "-q", "HEAD").chuzzle }
end
# time since git last commit for this {Tap}.
def git_last_commit
raise TapUnavailableError, name unless installed?
return unless git? && Utils.git_available?
path.cd { Utils.popen_read("git", "show", "-s", "--format=%cr", "HEAD").chuzzle }
end
# git last commit date for this {Tap}.
def git_last_commit_date
raise TapUnavailableError, name unless installed?
return unless git? && Utils.git_available?
path.cd { Utils.popen_read("git", "show", "-s", "--format=%cd", "--date=short", "HEAD").chuzzle }
end
# The issues URL of this {Tap}.
# e.g. `https://github.com/user/homebrew-repo/issues`
def issues_url

View File

@ -1,6 +1,8 @@
require "testing_env"
class TapTest < Homebrew::TestCase
include FileUtils
def setup
@path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
@path.mkpath
@ -16,7 +18,7 @@ class TapTest < Homebrew::TestCase
EOS
@alias_file = @path/"Aliases/bar"
@alias_file.parent.mkpath
FileUtils.ln_s @formula_file, @alias_file
ln_s @formula_file, @alias_file
(@path/"formula_renames.json").write <<-EOS.undent
{ "oldname": "foo" }
EOS
@ -25,14 +27,21 @@ class TapTest < Homebrew::TestCase
EOS
@cmd_file = @path/"cmd/brew-tap-cmd.rb"
@cmd_file.parent.mkpath
FileUtils.touch @cmd_file
FileUtils.chmod 0755, @cmd_file
touch @cmd_file
chmod 0755, @cmd_file
@manpage_file = @path/"man/man1/brew-tap-cmd.1"
@manpage_file.parent.mkpath
FileUtils.touch @manpage_file
touch @manpage_file
end
def setup_git_repo
env = ENV.to_hash
%w[AUTHOR COMMITTER].each do |role|
ENV["GIT_#{role}_NAME"] = "brew tests"
ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost"
ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100"
end
@path.cd do
shutup do
system "git", "init"
@ -41,6 +50,8 @@ class TapTest < Homebrew::TestCase
system "git", "commit", "-m", "init"
end
end
ensure
ENV.replace(env)
end
def teardown
@ -74,7 +85,7 @@ class TapTest < Homebrew::TestCase
t = Tap.new("someone", "foo")
path = Tap::TAP_DIRECTORY/"someone/homebrew-foo"
path.mkpath
FileUtils.cd path do
cd path do
shutup { system "git", "init" }
system "git", "remote", "add", "origin",
"https://github.com/someone/homebrew-foo"
@ -123,6 +134,26 @@ class TapTest < Homebrew::TestCase
refute_predicate version_tap, :private?
end
def test_remote_not_git_repo
assert_nil @tap.remote
end
def test_remote_git_not_available
setup_git_repo
Utils.stubs(:git_available?).returns(false)
assert_nil @tap.remote
end
def test_git_variant
touch @path/"README"
setup_git_repo
assert_equal "e1893a6bd191ba895c71b652ff8376a6114c7fa7", @tap.git_head
assert_equal "e189", @tap.git_short_head
assert_match %r{years ago}, @tap.git_last_commit
assert_equal "2009-05-21", @tap.git_last_commit_date
end
def test_private_remote
skip "HOMEBREW_GITHUB_API_TOKEN is required" unless ENV["HOMEBREW_GITHUB_API_TOKEN"]
assert_predicate @tap, :private?
@ -174,6 +205,8 @@ class TapTest < Homebrew::TestCase
end
class CoreFormulaRepositoryTest < Homebrew::TestCase
include FileUtils
def setup
@repo = CoreFormulaRepository.new
end
@ -204,7 +237,7 @@ class CoreFormulaRepositoryTest < Homebrew::TestCase
EOS
@alias_file = @repo.alias_dir/"bar"
@alias_file.parent.mkpath
FileUtils.ln_s @formula_file, @alias_file
ln_s @formula_file, @alias_file
assert_equal [@formula_file], @repo.formula_files
assert_equal ["foo"], @repo.formula_names