Module: Datadog::CI::Git::Packfiles

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

Class Method Summary collapse

Class Method Details

.generate(included_commits:, excluded_commits:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
63
64
65
66
# File 'lib/datadog/ci/git/packfiles.rb', line 12

def self.generate(included_commits:, excluded_commits:)
  # @type var current_process_tmp_folder: String?
  current_process_tmp_folder = nil

  Dir.mktmpdir do |tmpdir|
    prefix = LocalRepository.git_generate_packfiles(
      included_commits: included_commits,
      excluded_commits: excluded_commits,
      path: tmpdir
    )

    if prefix.nil?
      # git pack-files command fails if tmpdir is mounted on
      # a different device from the current process directory
      #
      # @type var current_process_tmp_folder: String
      current_process_tmp_folder = File.join(Dir.pwd, "tmp", "packfiles")
      FileUtils.mkdir_p(current_process_tmp_folder)

      prefix = LocalRepository.git_generate_packfiles(
        included_commits: included_commits,
        excluded_commits: excluded_commits,
        path: current_process_tmp_folder
      )

      if prefix.nil?
        Datadog.logger.debug("Packfiles generation failed twice, aborting")
        break
      end

      tmpdir = current_process_tmp_folder
    end

    packfiles = Dir.entries(tmpdir) - %w[. ..]
    if packfiles.empty?
      Datadog.logger.debug("Empty packfiles list, aborting process")
      break
    end

    packfiles.each do |packfile_name|
      next unless packfile_name.start_with?(prefix)
      next unless packfile_name.end_with?(".pack")

      packfile_path = File.join(tmpdir, packfile_name)

      yield packfile_path
    end
  end
rescue => e
  Datadog.logger.debug("Packfiles could not be generated, error: #{e}")
ensure
  if current_process_tmp_folder && File.exist?(current_process_tmp_folder)
    FileUtils.remove_entry(current_process_tmp_folder)
  end
end