Module: Datadog::CI::Git::LocalRepository

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

Constant Summary collapse

COMMAND_RETRY_COUNT =
3

Class Method Summary collapse

Class Method Details

.current_folder_nameObject



46
47
48
# File 'lib/datadog/ci/git/local_repository.rb', line 46

def self.current_folder_name
  File.basename(root)
end

.git_branchObject



71
72
73
74
75
76
# File 'lib/datadog/ci/git/local_repository.rb', line 71

def self.git_branch
  exec_git_command("git rev-parse --abbrev-ref HEAD")
rescue => e
  log_failure(e, "git branch")
  nil
end

.git_commit_messageObject



85
86
87
88
89
90
# File 'lib/datadog/ci/git/local_repository.rb', line 85

def self.git_commit_message
  exec_git_command("git show -s --format=%s")
rescue => e
  log_failure(e, "git commit message")
  nil
end

.git_commit_shaObject



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

def self.git_commit_sha
  exec_git_command("git rev-parse HEAD")
rescue => e
  log_failure(e, "git commit sha")
  nil
end

.git_commit_usersObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/datadog/ci/git/local_repository.rb', line 92

def self.git_commit_users
  # Get committer and author information in one command.
  output = exec_git_command("git show -s --format='%an\t%ae\t%at\t%cn\t%ce\t%ct'")
  unless output
    Datadog.logger.debug(
      "Unable to read git commit users: git command output is nil"
    )
    nil_user = NilUser.new
    return [nil_user, nil_user]
  end

  author_name, author_email, author_timestamp,
    committer_name, committer_email, committer_timestamp = output.split("\t").each(&:strip!)

  author = User.new(author_name, author_email, author_timestamp)
  committer = User.new(committer_name, committer_email, committer_timestamp)

  [author, committer]
rescue => e
  log_failure(e, "git commit users")

  nil_user = NilUser.new
  [nil_user, nil_user]
end

.git_commitsObject

returns maximum of 1000 latest commits in the last month



118
119
120
121
122
123
124
125
126
# File 'lib/datadog/ci/git/local_repository.rb', line 118

def self.git_commits
  output = exec_git_command("git log --format=%H -n 1000 --since=\"1 month ago\"")
  return [] if output.nil?

  output.split("\n")
rescue => e
  log_failure(e, "git commits")
  []
end

.git_commits_rev_list(included_commits:, excluded_commits:) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/datadog/ci/git/local_repository.rb', line 128

def self.git_commits_rev_list(included_commits:, excluded_commits:)
  included_commits = filter_invalid_commits(included_commits).join(" ")
  excluded_commits = filter_invalid_commits(excluded_commits).map! { |sha| "^#{sha}" }.join(" ")

  exec_git_command(
    "git rev-list " \
    "--objects " \
    "--no-object-names " \
    "--filter=blob:none " \
    "--since=\"1 month ago\" " \
    "#{excluded_commits} #{included_commits}"
  )
rescue => e
  log_failure(e, "git commits rev list")
  nil
end

.git_generate_packfiles(included_commits:, excluded_commits:, path:) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/datadog/ci/git/local_repository.rb', line 145

def self.git_generate_packfiles(included_commits:, excluded_commits:, path:)
  return nil unless File.exist?(path)

  commit_tree = git_commits_rev_list(included_commits: included_commits, excluded_commits: excluded_commits)
  return nil if commit_tree.nil?

  basename = SecureRandom.hex(4)

  exec_git_command(
    "git pack-objects --compression=9 --max-pack-size=3m #{path}/#{basename}",
    stdin: commit_tree
  )

  basename
rescue => e
  log_failure(e, "git generate packfiles")
  nil
end

.git_repository_urlObject



50
51
52
53
54
55
# File 'lib/datadog/ci/git/local_repository.rb', line 50

def self.git_repository_url
  exec_git_command("git ls-remote --get-url")
rescue => e
  log_failure(e, "git repository url")
  nil
end

.git_rootObject



57
58
59
60
61
62
# File 'lib/datadog/ci/git/local_repository.rb', line 57

def self.git_root
  exec_git_command("git rev-parse --show-toplevel")
rescue => e
  log_failure(e, "git root path")
  nil
end

.git_shallow_clone?Boolean

Returns:

  • (Boolean)


164
165
166
167
168
169
# File 'lib/datadog/ci/git/local_repository.rb', line 164

def self.git_shallow_clone?
  exec_git_command("git rev-parse --is-shallow-repository") == "true"
rescue => e
  log_failure(e, "git shallow clone")
  false
end

.git_tagObject



78
79
80
81
82
83
# File 'lib/datadog/ci/git/local_repository.rb', line 78

def self.git_tag
  exec_git_command("git tag --points-at HEAD")
rescue => e
  log_failure(e, "git tag")
  nil
end

.git_unshallowObject



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/datadog/ci/git/local_repository.rb', line 171

def self.git_unshallow
  exec_git_command(
    "git fetch " \
    "--shallow-since=\"1 month ago\" " \
    "--update-shallow " \
    "--filter=\"blob:none\" " \
    "--recurse-submodules=no " \
    "$(git config --default origin --get clone.defaultRemoteName) $(git rev-parse HEAD)"
  )
rescue => e
  log_failure(e, "git unshallow")
  nil
end

.relative_to_root(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/datadog/ci/git/local_repository.rb', line 20

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

  root_path = root
  return path if root_path.nil?

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

  path.relative_path_from(root_path).to_s
end

.repository_nameObject



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

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

  git_remote_url = git_repository_url

  # 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
  log_failure(e, "git repository name")
  @repository_name = current_folder_name
end

.rootObject



14
15
16
17
18
# File 'lib/datadog/ci/git/local_repository.rb', line 14

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

  @root = git_root || Dir.pwd
end