Skip to content

Commit

Permalink
add Utils::Git.root method that returns root of the current git repos…
Browse files Browse the repository at this point in the history
…itory
  • Loading branch information
anmarchenko committed Jan 4, 2024
1 parent 6568239 commit cba9dd1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/datadog/ci/utils/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/ci/utils/git.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions spec/datadog/ci/utils/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cba9dd1

Please sign in to comment.