Class: Datadog::CI::Git::Diff
- Inherits:
-
Object
- Object
- Datadog::CI::Git::Diff
- Defined in:
- lib/datadog/ci/git/diff.rb
Constant Summary collapse
- FILE_CHANGE_REGEX =
/^diff --git a\/(?<file>.+?) b\//.freeze
- LINES_CHANGE_REGEX =
/^@@ -\d+(?:,\d+)? \+(?<start>\d+)(?:,(?<count>\d+))? @@/.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(changed_files: {}) ⇒ Diff
constructor
A new instance of Diff.
-
#inspect ⇒ Object
for debug purposes.
-
#lines_changed?(file_path, start_line: nil, end_line: nil) ⇒ Boolean
Check if any lines in the given range are changed for the specified file.
-
#size ⇒ Object
for debug purposes.
Constructor Details
#initialize(changed_files: {}) ⇒ Diff
Returns a new instance of Diff.
13 14 15 |
# File 'lib/datadog/ci/git/diff.rb', line 13 def initialize(changed_files: {}) @changed_files = changed_files # Hash of file_path => ChangedLines end |
Class Method Details
.parse_diff_output(output) ⇒ Object
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/datadog/ci/git/diff.rb', line 45 def self.parse_diff_output(output) return new if output.nil? || output.empty? changed_files = {} current_file = nil 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 = FILE_CHANGE_REGEX.match(line) if match && match[:file] # this path here is already relative from the git root changed_file = match[:file] unless changed_file.nil? || changed_file.empty? current_file = changed_file changed_files[current_file] ||= ChangedLines.new end Datadog.logger.debug { "matched changed_file: #{changed_file} from git diff line: #{line}" } next end # Match lines like: @@ -1,2 +3,4 @@ match = LINES_CHANGE_REGEX.match(line) if match && match[:start] && current_file start_line = match[:start].to_i line_count = 1 # Default to 1 line if count not specified line_count = match[:count].to_i if match[:count] end_line = start_line + line_count - 1 changed_files[current_file].add_interval(start_line, end_line) Datadog.logger.debug { "Added interval [#{start_line}, #{end_line}] for file: #{current_file}" } end end new(changed_files: changed_files) end |
Instance Method Details
#empty? ⇒ Boolean
31 32 33 |
# File 'lib/datadog/ci/git/diff.rb', line 31 def empty? @changed_files.empty? end |
#inspect ⇒ Object
for debug purposes
41 42 43 |
# File 'lib/datadog/ci/git/diff.rb', line 41 def inspect @changed_files.inspect end |
#lines_changed?(file_path, start_line: nil, end_line: nil) ⇒ Boolean
Check if any lines in the given range are changed for the specified file
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/datadog/ci/git/diff.rb', line 18 def lines_changed?(file_path, start_line: nil, end_line: nil) changed_lines = @changed_files[file_path] unless changed_lines Datadog.logger.debug { "No changes found for file: #{file_path}" } return false end # If either start_line or end_line is nil, return true if file is present return true if start_line.nil? || end_line.nil? changed_lines.overlaps?(start_line, end_line) end |
#size ⇒ Object
for debug purposes
36 37 38 |
# File 'lib/datadog/ci/git/diff.rb', line 36 def size @changed_files.size end |