Module: Datadog::CI::Utils::Git

Defined in:
lib/datadog/ci/utils/git.rb

Class Method Summary collapse

Class Method Details

.current_folder_nameObject



62
63
64
65
66
67
68
69
# File 'lib/datadog/ci/utils/git.rb', line 62

def self.current_folder_name
  root_folder = root
  if root_folder.nil?
    File.basename(Dir.pwd)
  else
    File.basename(root_folder)
  end
end

.exec_git_command(cmd) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/datadog/ci/utils/git.rb', line 71

def self.exec_git_command(cmd)
  out, status = Open3.capture2e(cmd)

  raise "Failed to run git command #{cmd}: #{out}" unless status.success?

  # Sometimes Encoding.default_external is somehow set to US-ASCII which breaks
  # commit messages with UTF-8 characters like emojis
  # We force output's encoding to be UTF-8 in this case
  # This is safe to do as UTF-8 is compatible with US-ASCII
  if Encoding.default_external == Encoding::US_ASCII
    out = out.force_encoding(Encoding::UTF_8)
  end
  out.strip! # There's always a "\n" at the end of the command output

  return nil if out.empty?

  out
end

.is_git_tag?(ref) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/datadog/ci/utils/git.rb', line 19

def self.is_git_tag?(ref)
  !ref.nil? && ref.include?("tags/")
end

.normalize_ref(ref) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/datadog/ci/utils/git.rb', line 10

def self.normalize_ref(ref)
  return nil if ref.nil?

  refs = %r{^refs/(heads/)?}
  origin = %r{^origin/}
  tags = %r{^tags/}
  ref.gsub(refs, "").gsub(origin, "").gsub(tags, "")
end

.relative_to_root(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datadog/ci/utils/git.rb', line 34

def self.relative_to_root(path)
  return nil if path.nil?

  git_root = root
  return path if git_root.nil?

  path = Pathname.new(File.expand_path(path))
  git_root = Pathname.new(git_root)

  path.relative_path_from(git_root).to_s
end

.repository_nameObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/ci/utils/git.rb', line 46

def self.repository_name
  return @repository_name if defined?(@repository_name)

  git_remote_url = exec_git_command("git ls-remote --get-url origin")

  # return git repository name from remote url without .git extension
  last_path_segment = git_remote_url.split("/").last if git_remote_url
  @repository_name = last_path_segment.gsub(".git", "") if last_path_segment
  @repository_name ||= current_folder_name
rescue => e
  Datadog.logger.debug(
    "Unable to get git remote: #{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
  )
  @repository_name = current_folder_name
end

.rootObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/datadog/ci/utils/git.rb', line 23

def self.root
  return @root if defined?(@root)

  @root = exec_git_command("git rev-parse --show-toplevel")
rescue => e
  Datadog.logger.debug(
    "Unable to read git root: #{e.class.name} #{e.message} at #{Array(e.backtrace).first}"
  )
  @root = nil
end