Module: Datadog::CI::Git::LocalRepository
- Defined in:
- lib/datadog/ci/git/local_repository.rb
Class Method Summary collapse
-
.base_commit_sha(base_branch: nil) ⇒ Object
On best effort basis determines the git sha of the most likely base branch for the current PR.
- .current_folder_name ⇒ Object
- .filter_invalid_commits(commits) ⇒ Object
-
.get_changed_files_from_diff(base_commit) ⇒ Object
Returns a Set of normalized file paths changed since the given base_commit.
- .get_upstream_branch ⇒ Object
- .git_branch ⇒ Object
- .git_commit_message ⇒ Object
- .git_commit_sha ⇒ Object
- .git_commit_users ⇒ Object
-
.git_commits ⇒ Object
returns maximum of 1000 latest commits in the last month.
- .git_commits_rev_list(included_commits:, excluded_commits:) ⇒ Object
- .git_generate_packfiles(included_commits:, excluded_commits:, path:) ⇒ Object
- .git_repository_url ⇒ Object
- .git_root ⇒ Object
- .git_shallow_clone? ⇒ Boolean
- .git_tag ⇒ Object
- .git_unshallow ⇒ Object
- .log_failure(e, action) ⇒ Object
-
.relative_to_root(path) ⇒ Object
ATTENTION: this function is running in a hot path and must be optimized for performance.
- .repository_name ⇒ Object
- .root ⇒ Object
Class Method Details
.base_commit_sha(base_branch: nil) ⇒ Object
On best effort basis determines the git sha of the most likely base branch for the current PR.
360 361 362 363 364 365 366 367 368 |
# File 'lib/datadog/ci/git/local_repository.rb', line 360 def self.base_commit_sha(base_branch: nil) Telemetry.git_command(Ext::Telemetry::Command::BASE_COMMIT_SHA) BaseBranchShaDetector.base_branch_sha(base_branch) rescue => e Telemetry.track_error(e, Ext::Telemetry::Command::BASE_COMMIT_SHA) log_failure(e, "git base ref") nil end |
.current_folder_name ⇒ Object
78 79 80 |
# File 'lib/datadog/ci/git/local_repository.rb', line 78 def self.current_folder_name File.basename(root) end |
.filter_invalid_commits(commits) ⇒ Object
377 378 379 |
# File 'lib/datadog/ci/git/local_repository.rb', line 377 def self.filter_invalid_commits(commits) commits.filter { |commit| Utils::Git.valid_commit_sha?(commit) } end |
.get_changed_files_from_diff(base_commit) ⇒ Object
Returns a Set of normalized file paths changed since the given base_commit. If base_commit is nil, returns nil. On error, returns nil.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/datadog/ci/git/local_repository.rb', line 313 def self.get_changed_files_from_diff(base_commit) return nil if base_commit.nil? Datadog.logger.debug { "calculating git diff from base_commit: #{base_commit}" } Telemetry.git_command(Ext::Telemetry::Command::DIFF) begin # 1. Run the git diff command # @type var output: String? output = nil duration_ms = Core::Utils::Time.measure(:float_millisecond) do output = CLI.exec_git_command(["diff", "-U0", "--word-diff=porcelain", base_commit, "HEAD"], timeout: CLI::LONG_TIMEOUT) end Telemetry.git_command_ms(Ext::Telemetry::Command::DIFF, duration_ms) Datadog.logger.debug { "git diff output: #{output}" } return nil if output.nil? # 2. Parse the output to extract which files changed changed_files = Set.new output.each_line do |line| # Match lines like: diff --git a/foo/bar.rb b/foo/bar.rb # This captures git changes on file level match = /^diff --git a\/(?<file>.+?) b\//.match(line) if match && match[:file] changed_file = match[:file] # Normalize to repo root normalized_changed_file = relative_to_root(changed_file) changed_files << normalized_changed_file unless normalized_changed_file.nil? || normalized_changed_file.empty? Datadog.logger.debug { "matched changed_file: #{changed_file} from line: #{line}" } Datadog.logger.debug { "normalized_changed_file: #{normalized_changed_file}" } end end changed_files rescue => e Telemetry.track_error(e, Ext::Telemetry::Command::DIFF) log_failure(e, "get changed files from diff") nil end end |
.get_upstream_branch ⇒ Object
370 371 372 373 374 375 |
# File 'lib/datadog/ci/git/local_repository.rb', line 370 def self.get_upstream_branch CLI.exec_git_command(["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"]) rescue => e Datadog.logger.debug { "Error getting upstream: #{e}" } nil end |
.git_branch ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/datadog/ci/git/local_repository.rb', line 113 def self.git_branch Telemetry.git_command(Ext::Telemetry::Command::GET_BRANCH) # @type var res: String? res = nil duration_ms = Core::Utils::Time.measure(:float_millisecond) do res = CLI.exec_git_command(["rev-parse", "--abbrev-ref", "HEAD"]) end Telemetry.git_command_ms(Ext::Telemetry::Command::GET_BRANCH, duration_ms) res rescue => e log_failure(e, "git branch") Telemetry.track_error(e, Ext::Telemetry::Command::GET_BRANCH) nil end |
.git_commit_message ⇒ Object
137 138 139 140 141 142 |
# File 'lib/datadog/ci/git/local_repository.rb', line 137 def self. CLI.exec_git_command(["log", "-n", "1", "--format=%B"]) rescue => e log_failure(e, "git commit message") nil end |
.git_commit_sha ⇒ Object
106 107 108 109 110 111 |
# File 'lib/datadog/ci/git/local_repository.rb', line 106 def self.git_commit_sha CLI.exec_git_command(["rev-parse", "HEAD"]) rescue => e log_failure(e, "git commit sha") nil end |
.git_commit_users ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/datadog/ci/git/local_repository.rb', line 144 def self.git_commit_users # Get committer and author information in one command. output = CLI.exec_git_command(["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 , , , committer_name, committer_email, = output.split("\t").each(&:strip!) = User.new(, , ) committer = User.new(committer_name, committer_email, ) [, committer] rescue => e log_failure(e, "git commit users") nil_user = NilUser.new [nil_user, nil_user] end |
.git_commits ⇒ Object
returns maximum of 1000 latest commits in the last month
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/datadog/ci/git/local_repository.rb', line 170 def self.git_commits Telemetry.git_command(Ext::Telemetry::Command::GET_LOCAL_COMMITS) # @type var output: String? output = nil duration_ms = Core::Utils::Time.measure(:float_millisecond) do output = CLI.exec_git_command(["log", "--format=%H", "-n", "1000", "--since=\"1 month ago\""], timeout: CLI::LONG_TIMEOUT) end Telemetry.git_command_ms(Ext::Telemetry::Command::GET_LOCAL_COMMITS, duration_ms) return [] if output.nil? output.split("\n") rescue => e log_failure(e, "git commits") Telemetry.track_error(e, Ext::Telemetry::Command::GET_LOCAL_COMMITS) [] end |
.git_commits_rev_list(included_commits:, excluded_commits:) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/datadog/ci/git/local_repository.rb', line 191 def self.git_commits_rev_list(included_commits:, excluded_commits:) Telemetry.git_command(Ext::Telemetry::Command::GET_OBJECTS) included_commits_list = filter_invalid_commits(included_commits) excluded_commits_list = filter_invalid_commits(excluded_commits).map { |sha| "^#{sha}" } # @type var res: String? res = nil duration_ms = Core::Utils::Time.measure(:float_millisecond) do cmd = [ "rev-list", "--objects", "--no-object-names", "--filter=blob:none", "--since=\"1 month ago\"" ] + excluded_commits_list + included_commits_list res = CLI.exec_git_command(cmd, timeout: CLI::LONG_TIMEOUT) end Telemetry.git_command_ms(Ext::Telemetry::Command::GET_OBJECTS, duration_ms) res rescue => e log_failure(e, "git commits rev list") Telemetry.track_error(e, Ext::Telemetry::Command::GET_OBJECTS) nil end |
.git_generate_packfiles(included_commits:, excluded_commits:, path:) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/datadog/ci/git/local_repository.rb', line 220 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) Telemetry.git_command(Ext::Telemetry::Command::PACK_OBJECTS) duration_ms = Core::Utils::Time.measure(:float_millisecond) do CLI.exec_git_command( ["pack-objects", "--compression=9", "--max-pack-size=3m", "#{path}/#{basename}"], stdin: commit_tree, timeout: CLI::LONG_TIMEOUT ) end Telemetry.git_command_ms(Ext::Telemetry::Command::PACK_OBJECTS, duration_ms) basename rescue => e log_failure(e, "git generate packfiles") Telemetry.track_error(e, Ext::Telemetry::Command::PACK_OBJECTS) nil end |
.git_repository_url ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/datadog/ci/git/local_repository.rb', line 82 def self.git_repository_url Telemetry.git_command(Ext::Telemetry::Command::GET_REPOSITORY) # @type var res: String? res = nil duration_ms = Core::Utils::Time.measure(:float_millisecond) do res = CLI.exec_git_command(["ls-remote", "--get-url"]) end Telemetry.git_command_ms(Ext::Telemetry::Command::GET_REPOSITORY, duration_ms) res rescue => e log_failure(e, "git repository url") Telemetry.track_error(e, Ext::Telemetry::Command::GET_REPOSITORY) nil end |
.git_root ⇒ Object
99 100 101 102 103 104 |
# File 'lib/datadog/ci/git/local_repository.rb', line 99 def self.git_root CLI.exec_git_command(["rev-parse", "--show-toplevel"]) rescue => e log_failure(e, "git root path") nil end |
.git_shallow_clone? ⇒ Boolean
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/datadog/ci/git/local_repository.rb', line 246 def self.git_shallow_clone? Telemetry.git_command(Ext::Telemetry::Command::CHECK_SHALLOW) res = false duration_ms = Core::Utils::Time.measure(:float_millisecond) do res = CLI.exec_git_command(["rev-parse", "--is-shallow-repository"]) == "true" end Telemetry.git_command_ms(Ext::Telemetry::Command::CHECK_SHALLOW, duration_ms) res rescue => e log_failure(e, "git shallow clone") Telemetry.track_error(e, Ext::Telemetry::Command::CHECK_SHALLOW) false end |
.git_tag ⇒ Object
130 131 132 133 134 135 |
# File 'lib/datadog/ci/git/local_repository.rb', line 130 def self.git_tag CLI.exec_git_command(["tag", "--points-at", "HEAD"]) rescue => e log_failure(e, "git tag") nil end |
.git_unshallow ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/datadog/ci/git/local_repository.rb', line 262 def self.git_unshallow Telemetry.git_command(Ext::Telemetry::Command::UNSHALLOW) # @type var res: String? res = nil default_remote = CLI.exec_git_command(["config", "--default", "origin", "--get", "clone.defaultRemoteName"])&.strip head_commit = git_commit_sha upstream_branch = get_upstream_branch # Build array of remotes to try, filtering out nil values unshallow_remotes = [] unshallow_remotes << head_commit if head_commit unshallow_remotes << upstream_branch if upstream_branch unshallow_remotes << nil # Ensure the loop runs at least once, even if no valid remotes are available. This acts as a fallback mechanism. duration_ms = Core::Utils::Time.measure(:float_millisecond) do unshallow_remotes.each do |remote| unshallowing_errored = false res = begin # @type var cmd: Array[String] cmd = [ "fetch", "--shallow-since=\"1 month ago\"", "--update-shallow", "--filter=blob:none", "--recurse-submodules=no", default_remote ] cmd << remote if remote CLI.exec_git_command(cmd, timeout: CLI::UNSHALLOW_TIMEOUT) rescue => e log_failure(e, "git unshallow") Telemetry.track_error(e, Ext::Telemetry::Command::UNSHALLOW) unshallowing_errored = true nil end # If the command succeeded, break and return the result break [] if res && !unshallowing_errored end end Telemetry.git_command_ms(Ext::Telemetry::Command::UNSHALLOW, duration_ms) res end |
.log_failure(e, action) ⇒ Object
381 382 383 384 385 |
# File 'lib/datadog/ci/git/local_repository.rb', line 381 def self.log_failure(e, action) Datadog.logger.debug( "Unable to perform #{action}: #{e.class.name} #{e.} at #{Array(e.backtrace).first}" ) end |
.relative_to_root(path) ⇒ Object
ATTENTION: this function is running in a hot path and must be optimized for performance
26 27 28 29 30 31 32 33 34 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 |
# File 'lib/datadog/ci/git/local_repository.rb', line 26 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 |
.repository_name ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/datadog/ci/git/local_repository.rb', line 64 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 |
.root ⇒ Object
18 19 20 21 22 |
# File 'lib/datadog/ci/git/local_repository.rb', line 18 def self.root return @root if defined?(@root) @root = git_root || Dir.pwd end |