Skip to content

Commit

Permalink
optimise .relative_to_root more
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Oct 1, 2024
1 parent 9d8e7e8 commit b12f028
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/datadog/ci/git/local_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ def self.relative_to_root(path)
return path if root_path.nil?

if File.absolute_path?(path)
res = path.gsub(root, "")
if res[0] == File::SEPARATOR
res = res[1..]
end
prefix_index = root_path.size

# impossible case
return "" if path.size < prefix_index

prefix_index += 1 if path[prefix_index] == File::SEPARATOR
res = path[prefix_index..]
else
if @prefix && @prefix != ""
return "#{@prefix}#{path}"
if @prefix_to_root == ""
return path
elsif @prefix_to_root
return File.join(@prefix_to_root, path)
end

pathname = Pathname.new(File.expand_path(path))
Expand All @@ -52,12 +57,8 @@ def self.relative_to_root(path)
# relative_path_from is an expensive function
res = pathname.relative_path_from(root_path).to_s

unless defined?(@prefix)
@prefix = if res.end_with?(path)
res&.gsub(path, "")
else
nil
end
unless defined?(@prefix_to_root)
@prefix_to_root = res&.gsub(path, "") if res.end_with?(path)
end
end

Expand Down

0 comments on commit b12f028

Please sign in to comment.