From cba9dd110d038f2ec21e483241177eb0f469948b Mon Sep 17 00:00:00 2001 From: Andrey Marchenko Date: Thu, 4 Jan 2024 13:21:42 +0100 Subject: [PATCH] add Utils::Git.root method that returns root of the current git repository --- lib/datadog/ci/utils/git.rb | 6 ++++++ sig/datadog/ci/utils/git.rbs | 2 ++ spec/datadog/ci/utils/git_spec.rb | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/lib/datadog/ci/utils/git.rb b/lib/datadog/ci/utils/git.rb index dd1750ff..4f89a56e 100644 --- a/lib/datadog/ci/utils/git.rb +++ b/lib/datadog/ci/utils/git.rb @@ -19,6 +19,12 @@ def self.is_git_tag?(ref) !ref.nil? && ref.include?("tags/") end + def self.root + return @@root if defined?(@@root) + + @@root = exec_git_command("git rev-parse --show-toplevel") + end + def self.exec_git_command(cmd) out, status = Open3.capture2e(cmd) diff --git a/sig/datadog/ci/utils/git.rbs b/sig/datadog/ci/utils/git.rbs index 26257c8e..4ef0a783 100644 --- a/sig/datadog/ci/utils/git.rbs +++ b/sig/datadog/ci/utils/git.rbs @@ -2,6 +2,8 @@ module Datadog module CI module Utils module Git + @@root: String? + def self?.normalize_ref: (untyped name) -> (nil | untyped) def self?.is_git_tag?: (untyped ref) -> untyped diff --git a/spec/datadog/ci/utils/git_spec.rb b/spec/datadog/ci/utils/git_spec.rb index c17700c5..0c6cb14f 100644 --- a/spec/datadog/ci/utils/git_spec.rb +++ b/spec/datadog/ci/utils/git_spec.rb @@ -46,4 +46,22 @@ it { is_expected.to be_truthy } end end + + describe ".root" do + subject { described_class.root } + + it { is_expected.to eq(Dir.pwd) } + + context "caches the result" do + before do + expect(Open3).to receive(:capture2e).never + end + + it "returns the same result" do + 2.times do + expect(described_class.root).to eq(Dir.pwd) + end + end + end + end end