Method: Datadog::CI::Git::LocalRepository.relative_to_root
- Defined in:
- lib/datadog/ci/git/local_repository.rb
.relative_to_root(path) ⇒ Object
ATTENTION: this function is running in a hot path and should be optimized for performance
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/datadog/ci/git/local_repository.rb', line 35 def self.relative_to_root(path) return "" if path.nil? root_path = root return path if root_path.nil? if File.absolute_path?(path) # prefix_index is where the root path ends in the given path prefix_index = root_path.size # impossible case - absolute paths are returned from code coverage tool that always checks # that root is a prefix of the path return "" if path.size < prefix_index prefix_index += 1 if path[prefix_index] == File::SEPARATOR res = path[prefix_index..] else # prefix_to_root is a difference between the root path and the given path if @prefix_to_root == "" return path elsif @prefix_to_root return File.join(@prefix_to_root, path) end pathname = Pathname.new(File.(path)) root_path = Pathname.new(root_path) # relative_path_from is an expensive function res = pathname.relative_path_from(root_path).to_s unless defined?(@prefix_to_root) @prefix_to_root = res&.gsub(path, "") if res.end_with?(path) end end res || "" end |