github_diff_parser 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26631fa3bed73629263398b5a5956aeb3f680ecdc3d990a86ff3c68cbb4e93fc
4
+ data.tar.gz: 1268de53cf2e150fb1667955c0b4b5b24e045c9b413c81441310f98f4c564046
5
+ SHA512:
6
+ metadata.gz: b12c80f92786394271ec785c6b22db9111d20c17c3ee859b23cae2f54a9ef757b01d1cac2666df3b512eeae88524e37646efe98ba51ddcf94c3188d87bc7d42c
7
+ data.tar.gz: 53d34cc940811a050a26f214ab18db26acd638adaf26c717a740977cf01394682f0c3b165843449c09587b75dc54cf8e1dcc1d9419b1bda3794b751ddedd6fc2
data/.rubocop.yml ADDED
@@ -0,0 +1,9 @@
1
+ inherit_gem:
2
+ rubocop-shopify: rubocop.yml
3
+
4
+ AllCops:
5
+ NewCops: disable
6
+ SuggestExtensions: false
7
+
8
+ Style/Documentation:
9
+ Enabled: false
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at chin.edouard@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in github_diff_parser.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ github_diff_parser (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ byebug (11.1.3)
11
+ minitest (5.15.0)
12
+ parallel (1.22.1)
13
+ parser (3.1.2.0)
14
+ ast (~> 2.4.1)
15
+ rainbow (3.1.1)
16
+ rake (13.0.6)
17
+ regexp_parser (2.3.0)
18
+ rexml (3.2.5)
19
+ rubocop (1.27.0)
20
+ parallel (~> 1.10)
21
+ parser (>= 3.1.0.0)
22
+ rainbow (>= 2.2.2, < 4.0)
23
+ regexp_parser (>= 1.8, < 3.0)
24
+ rexml
25
+ rubocop-ast (>= 1.16.0, < 2.0)
26
+ ruby-progressbar (~> 1.7)
27
+ unicode-display_width (>= 1.4.0, < 3.0)
28
+ rubocop-ast (1.17.0)
29
+ parser (>= 3.1.1.0)
30
+ rubocop-shopify (2.5.0)
31
+ rubocop (~> 1.25)
32
+ ruby-progressbar (1.11.0)
33
+ unicode-display_width (2.1.0)
34
+
35
+ PLATFORMS
36
+ x86_64-darwin-20
37
+ x86_64-linux
38
+
39
+ DEPENDENCIES
40
+ byebug
41
+ github_diff_parser!
42
+ minitest (~> 5.0)
43
+ rake (~> 13.0)
44
+ rubocop-shopify
45
+
46
+ BUNDLED WITH
47
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Edouard CHIN
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ ### GithubDiffParser
2
+
3
+ A Ruby Gem to parse the output of a Git Diff with an emphasises on actual line numbers **before** and **after** the changes apply. This gem matches the information you get when reviewing a Pull Request on GitHub in split mode.
4
+
5
+ ## Usage
6
+
7
+ #### Given this diff
8
+
9
+ ```diff
10
+ diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb
11
+ index 70c4937c418b2..bd87fe1057f90 100644
12
+ --- a/actionmailer/lib/action_mailer/railtie.rb
13
+ +++ b/actionmailer/lib/action_mailer/railtie.rb
14
+ @@ -23,7 +23,6 @@ class Railtie < Rails::Railtie # :nodoc:
15
+ options.stylesheets_dir ||= paths["public/stylesheets"].first
16
+ options.show_previews = Rails.env.development? if options.show_previews.nil?
17
+ options.cache_store ||= Rails.cache
18
+ - options.smtp_settings ||= {}
19
+
20
+ if options.show_previews
21
+ options.preview_path ||= defined?(Rails.root) ? "#{Rails.root}/test/mailers/previews" : nil
22
+ @@ -46,9 +45,15 @@ class Railtie < Rails::Railtie # :nodoc:
23
+ self.delivery_job = delivery_job.constantize
24
+ end
25
+
26
+ - if smtp_timeout = options.delete(:smtp_timeout)
27
+ - options.smtp_settings[:open_timeout] ||= smtp_timeout
28
+ - options.smtp_settings[:read_timeout] ||= smtp_timeout
29
+ + if options.smtp_settings
30
+ + self.smtp_settings = options.smtp_settings
31
+ + end
32
+ +
33
+ + smtp_timeout = options.delete(:smtp_timeout)
34
+ +
35
+ + if self.smtp_settings && smtp_timeout
36
+ + self.smtp_settings[:open_timeout] ||= smtp_timeout
37
+ + self.smtp_settings[:read_timeout] ||= smtp_timeout
38
+ end
39
+
40
+ options.each { |k, v| send("#{k}=", v) }
41
+
42
+ ```
43
+
44
+ ```ruby
45
+ parsed_diffs = GithubDiffParser.parse(diff) # Returns an array of `GithubDiffParser:Diff` objects. Each object corresponding to a diff section in the Git Diff. In this example we only have one.
46
+
47
+ parsed_diffs.deleted_mode? # If this diff deleting a file. No in this example
48
+ parsed_diffs.new_mode? # If this diff adding a new file. No in this example
49
+ parsed_diffs.rename_mode? # If this diff renaming or moving a file. No in this example
50
+
51
+ hunks = parsed_diffs.hunks # Returns an array of `GithubDiffParser::Hunk` objects. In this example we have two.
52
+ lines = parsed_diffs.hunks.first.lines # Returns an array of `GithubDiffParser::Line` objects. Each object represent a line that belongs to the hunk. A line in a git diff can be either a contextual, addition or a deletion line.
53
+
54
+ lines.first.previous_number # Return the line number before the changes. In this example it's 23
55
+ lines.first.current_number # Return the line number after the changes. In this example it's 23
56
+ lines.first.patch_position # Return the position of the line in this patch. In this example it's 1
57
+ lines.first.contextual? # Returns true, the first line is a contextual line
58
+ ```
59
+
60
+ See the API documentation for more example.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "github_diff_parser"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GithubDiffParser
4
+ class Diff
5
+ # @return [String] (see #initialize)
6
+ attr_reader :previous_filename
7
+
8
+ # @return [String] (see #initialize)
9
+ attr_reader :new_filename
10
+
11
+ # @return [Array<GithubDiffParser::Hunk>] the hunks belonging to this diff
12
+ attr_reader :hunks
13
+
14
+ # @private
15
+ attr_writer :file_mode
16
+
17
+ # @param previous_filename [String] the original filename. Represented by "diff --git /a filename"
18
+ # @param new_filename [String] the new filename. Represented by "diff --git /b filename"
19
+ def initialize(previous_filename, new_filename)
20
+ @previous_filename = previous_filename
21
+ @new_filename = new_filename
22
+ @hunks = []
23
+ end
24
+
25
+ # Add a Git Hunk to the diff.
26
+ #
27
+ # @param previous_lino_start [String] the starting line number of the hunk for the original file
28
+ # @param new_lino_start [String] the starting line number of the hunk for the new file
29
+ #
30
+ # @example Representation of the previous_lino_start and new_lino_start in a Git Diff
31
+ # @@ -6,5 +6,6 @@ def test1 # => The first 6 is the previous_lino_start, the second is the new_lino_start
32
+ def add_hunk(previous_lino_start, new_lino_start)
33
+ hunks << Hunk.new(previous_lino_start, new_lino_start)
34
+ end
35
+
36
+ # Add a line belonging to the previously processed Git Hunk.
37
+ #
38
+ # @param line_content [String] the line content itself
39
+ # @param type [Symbol] the type of the line. Can be either :addition, :deletion or :contextual
40
+ # @raise [GithubDiffParser::InvalidDiff] if we are trying to add a line but the Diff doesn't contain any Hunk.
41
+ def add_line(line_content, type:)
42
+ last_hunk = hunks.last
43
+ raise InvalidDiff, "Couldn't find the Git diff Range Header." unless last_hunk
44
+
45
+ patch_position = hunks.flat_map(&:lines).count + hunks.count
46
+
47
+ last_hunk.add_line(line_content, patch_position, type: type)
48
+ end
49
+
50
+ # Check if this Diff is set to deleted mode.
51
+ #
52
+ # @example When the diff is set to deleted mode
53
+ # diff --git a/package.json b/package.json
54
+ # deleted file mode 100644 # => This indicates the diff is in deletion mode.
55
+ # index 3ffb801..0000000
56
+ # --- a/package.json
57
+ # +++ /dev/null
58
+ # @@ -1,11 +0,0 @@
59
+ # -{
60
+ #
61
+ # @return [Boolean]
62
+ def deleted_mode?
63
+ @file_mode == "deleted"
64
+ end
65
+
66
+ # Check if this Diff is set to new mode.
67
+ #
68
+ # @example When the diff is set to new mode
69
+ # diff --git a/blabla.rb b/blabla.rb
70
+ # new file mode 100644 # => This indicate the diff is in new mode.
71
+ # index 0000000..d3dfbe4
72
+ # --- /dev/null
73
+ # +++ b/blabla.rb
74
+ # @@ -0,0 +1,10 @@
75
+ # +Hello World
76
+ #
77
+ # @return [Boolean]
78
+ def new_mode?
79
+ @file_mode == "new"
80
+ end
81
+
82
+ # Check if this Diff is set to rename mode.
83
+ #
84
+ # @example When the diff is set to rename mode
85
+ # diff --git a/blabla.rb b/app/my_file.rb
86
+ # similarity index 100%
87
+ # rename from blabla.rb
88
+ # rename to app/my_file.rb
89
+ #
90
+ # @return [Boolean]
91
+ def rename_mode?
92
+ previous_filename != new_filename
93
+ end
94
+
95
+ # A utility method that returns the current number of a line who might not be present in the diff.
96
+ # This is useful if you need to keep track of the updated line numbers in a file for every changes.
97
+ #
98
+ # @param line_number [Integer]
99
+ #
100
+ # @return [Integer]
101
+ def previous_line_number_is_now(line_number)
102
+ return line_number unless line_shifted?(line_number)
103
+
104
+ applicable_hunk = last_applicable_hunk_for_line(line_number)
105
+ line = applicable_hunk.find_previous_line(line_number)
106
+
107
+ if line
108
+ line.current_number
109
+ else
110
+ line_number + last_line_offset(applicable_hunk)
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ # Check if a line was shifted. A line is considered shifted if its number is superior to the first hunk's start
117
+ # range.
118
+ #
119
+ # @param line_number [Integer]
120
+ #
121
+ # @return [Boolean]
122
+ def line_shifted?(line_number)
123
+ first_hunk = hunks.first
124
+
125
+ line_number > first_hunk.new_file_start_line
126
+ end
127
+
128
+ # Find the last hunk that shifts the line. We need the last because we know it's the one that will shift the line
129
+ # the most.
130
+ #
131
+ # @param line_number [Integer]
132
+ #
133
+ # @return [GithubDiffParser::Hunk]
134
+ def last_applicable_hunk_for_line(line_number)
135
+ hunks.reverse_each.find do |hunk|
136
+ line_number >= hunk.previous_file_start_line
137
+ end
138
+ end
139
+
140
+ # Calculate the number difference of the last line. This method is called when we can't find the desired line number
141
+ # in the Hunk, which means the line we are searching for is not part of the diff.
142
+ #
143
+ # @param hunk [GithubDiffParser::Hunk]
144
+ #
145
+ # @return [Integer]
146
+ def last_line_offset(hunk)
147
+ last_line = hunk.lines.last
148
+
149
+ last_line.current_number - last_line.previous_number
150
+ end
151
+ end
152
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GithubDiffParser
4
+ class Hunk
5
+ # @return [Array<GithubDiffParser::Line>] all the contextual, added and removed lines belonging to this Hunk.
6
+ attr_reader :lines
7
+
8
+ # @return [Integer] (see #initialize)
9
+ attr_reader :previous_file_start_line
10
+
11
+ # @return [Integer] (see #initialize)
12
+ attr_reader :new_file_start_line
13
+
14
+ # @param previous_file_start_line [String] the starting line number of the hunk for the original file
15
+ # @param new_file_start_line [String] the starting line number of the hunk for the new file
16
+ #
17
+ # @example Representation of the previous_file_start_line and new_file_start_line in a Git Diff
18
+ # @@ -6,5 +6,6 @@ def test1 # => The first 6 is the previous_file_start_line the second is the new_file_start_line
19
+ def initialize(previous_file_start_line, new_file_start_line)
20
+ @previous_file_start_line = Integer(previous_file_start_line)
21
+ @new_file_start_line = Integer(new_file_start_line)
22
+ @lines = []
23
+ end
24
+
25
+ # Add a line to this Hunk
26
+ #
27
+ # @param line_content [String] the line content itself
28
+ # @param patch_position [Integer] the the position of this line in the patch
29
+ # @param type [Symbol] the type of the line. Can be either :addition, :deletion or :contextual
30
+ def add_line(line_content, patch_position, type:)
31
+ case type
32
+ when :deletion
33
+ number = @previous_file_start_line + contextual_lines.count + deletion_lines.count
34
+ line = Line.new(line_content, number, nil, patch_position, type)
35
+ when :addition
36
+ number = @new_file_start_line + contextual_lines.count + addition_lines.count
37
+ line = Line.new(line_content, nil, number, patch_position, type)
38
+ when :contextual
39
+ before = @previous_file_start_line + contextual_lines.count + deletion_lines.count
40
+ now = @new_file_start_line + contextual_lines.count + addition_lines.count
41
+ line = Line.new(line_content, before, now, patch_position, type)
42
+ end
43
+
44
+ @lines << line
45
+ end
46
+
47
+ # Get all the contextual lines for this Hunk.
48
+ #
49
+ # @return [Array<GithubDiffParser::Line>]
50
+ def contextual_lines
51
+ @lines.select(&:contextual?)
52
+ end
53
+
54
+ # Get all the addition lines for this Hunk.
55
+ #
56
+ # @return [Array<GithubDiffParser::Line>]
57
+ def addition_lines
58
+ @lines.select(&:addition?)
59
+ end
60
+
61
+ # Get all the deletion lines for this Hunk.
62
+ #
63
+ # @return [Array<GithubDiffParser::Line>]
64
+ def deletion_lines
65
+ @lines.select(&:deletion?)
66
+ end
67
+
68
+ # Find a line in the Hunk by it's previous line number.
69
+ #
70
+ # @param line_number [Integer]
71
+ #
72
+ # @return [GithubDiffParser::Line, nil]
73
+ def find_previous_line(line_number)
74
+ lines.find { |line| line.previous_number == line_number }
75
+ end
76
+
77
+ # Find a line in the Hunk by it's current line number.
78
+ #
79
+ # @param line_number [Integer]
80
+ #
81
+ # @return [GithubDiffParser::Line, nil]
82
+ def find_current_line(line_number)
83
+ lines.find { |line| line.current_number == line_number }
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GithubDiffParser
4
+ class Line
5
+ # @return [String] the line content itself
6
+ attr_reader :content
7
+
8
+ # @return [Integer] the previous line number before the changes. This match
9
+ # the number shown by GitHub on the left side when reviewing a Pull Request in split mode.
10
+ attr_reader :previous_number
11
+
12
+ # @return [Integer] the current line number before the changes. This match
13
+ # the number shown by GitHub on the right side when reviewing a Pull Request in split mode.
14
+ attr_reader :current_number
15
+
16
+ # @return [Integer] the position of this line in the patch. When using the GitHub API and
17
+ # you want to write a comment on a given line change, you'll use this.
18
+ attr_reader :patch_position
19
+
20
+ # @param content [String] the line content itself
21
+ # @param previous_number [Ingeter] (see #previous_number)
22
+ # @param current_number [Ingeter] (see #current_number)
23
+ # @param patch_position [Ingeter] (see #patch_position)
24
+ # @param type [Symbol] the type of the line. Can be either :addition, :deletion or :contextual
25
+ def initialize(content, previous_number, current_number, patch_position, type)
26
+ @content = content
27
+ @previous_number = previous_number
28
+ @current_number = current_number
29
+ @patch_position = patch_position
30
+ @type = type
31
+ end
32
+
33
+ # Check if this line is a contextual line. A contextual line in a Git diff always start with a space (" ")
34
+ #
35
+ # @return [Boolean]
36
+ def contextual?
37
+ @type == :contextual
38
+ end
39
+
40
+ # Check if this line is an addition line. An addition line in a Git diff always start with a plus ("+")
41
+ #
42
+ # @return [Boolean]
43
+ def addition?
44
+ @type == :addition
45
+ end
46
+
47
+ # Check if this line is a deletion line. A deletion line in a Git diff always start with a minus ("-")
48
+ #
49
+ # @return [Boolean]
50
+ def deletion?
51
+ @type == :deletion
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GithubDiffParser
4
+ class Parser
5
+ # @param git_diff [String] the Git Diff output
6
+ def initialize(git_diff)
7
+ @git_diff = git_diff
8
+ @parsed_diffs = []
9
+ @current_diff = nil
10
+ end
11
+
12
+ # Parse and process the Git Diff output.
13
+ #
14
+ # @return [Array<GithubDiffParser::Diff>]
15
+ # @raise [GithubDiffParser::InvalidDiff] if the Diff output is malformatted
16
+ def process
17
+ @git_diff.each_line do |line|
18
+ case line
19
+ when Regexes::DIFF_HEADER
20
+ process_new_diff(Regexp.last_match)
21
+ when Regexes::MODE_HEADER
22
+ process_diff_file_mode(Regexp.last_match)
23
+ when Regexes::ORIGINAL_FILE_HEADER, Regexes::NEW_FILE_HEADER
24
+ validate_diff
25
+ when Regexes::RANGE_HEADER
26
+ add_hunk_to_diff(Regexp.last_match)
27
+ when Regexes::LINE_DIFF
28
+ add_line_to_hunk(Regexp.last_match)
29
+ end
30
+ end
31
+
32
+ validate_diff
33
+
34
+ @parsed_diffs << @current_diff
35
+ end
36
+
37
+ private
38
+
39
+ # Called when encountering a `diff --git a/file b/file` in the Git Diff output.
40
+ # Worth to note that a Git Diff output will most likely contain multiple diff section. Each will
41
+ # be represented by a new GithubDiffParser::Diff object.
42
+ #
43
+ # @param match_data [MatchData]
44
+ def process_new_diff(match_data)
45
+ @parsed_diffs << @current_diff if @current_diff
46
+
47
+ @current_diff = Diff.new(match_data[:previous_filename], match_data[:new_filename])
48
+ end
49
+
50
+ # Called when encountering a `new file mode 100644` or `delete file mode 100644` in the Git Diff output.
51
+ #
52
+ # @param match_data [MatchData]
53
+ #
54
+ # @raise [GithubDiffParser::InvalidDiff] if the parser didn't process the `diff --git` header first.
55
+ def process_diff_file_mode(match_data)
56
+ validate_diff
57
+
58
+ @current_diff.file_mode = match_data[:file_mode]
59
+ end
60
+
61
+ # Called when encountering a `@@ -0,0 +1,10 @@` in the Git Diff output.
62
+ #
63
+ # @param match_data [MatchData]
64
+ #
65
+ # @raise [GithubDiffParser::InvalidDiff] if the parser didn't process the `diff --git` header first.
66
+ def add_hunk_to_diff(match_data)
67
+ validate_diff
68
+
69
+ @current_diff.add_hunk(match_data[:previous_lino_start], match_data[:new_lino_start])
70
+ end
71
+
72
+ # Called when encountering a `-text` or `+text` or ` text` in the Git Diff output.
73
+ #
74
+ # @param match_data [MatchData]
75
+ #
76
+ # @raise [GithubDiffParser::InvalidDiff] if the parser didn't process the `diff --git` header first.
77
+ def add_line_to_hunk(match_data)
78
+ validate_diff
79
+
80
+ @current_diff.add_line(match_data[:line], type: map_line_type(match_data[:type]))
81
+ end
82
+
83
+ # Validate that the Git Diff output contains a `diff --git header` before attempting to add Hunk or Lines
84
+ #
85
+ # @raise [GithubDiffParser::InvalidDiff]
86
+ def validate_diff
87
+ message = "Couldn't find the Git diff header. A valid git diff has to start with 'diff --git'"
88
+
89
+ raise InvalidDiff, message if @current_diff.nil?
90
+ end
91
+
92
+ # Map the type of the line.
93
+ #
94
+ # @param token [String] either '+', '-' or ' '
95
+ #
96
+ # @raise [GithubDiffParser::InvalidDiff] if a line starts with a unknown token
97
+ def map_line_type(token)
98
+ mapping = { "+" => :addition, "-" => :deletion, " " => :contextual }
99
+
100
+ mapping.fetch(token)
101
+ rescue KeyError
102
+ raise InvalidDiff, "Unexpected token: '#{token}' found at beginning of line. Expecting '+', '-', ' '"
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GithubDiffParser
4
+ module Regexes
5
+ # This Regexp is used to match the very first line of a diff.
6
+ #
7
+ # @example Possible header on a diff.
8
+ #
9
+ # diff --git a/app/my_file.rb b/app/my_file.rb <-- Match this line -->
10
+ # index d3dfbe4..ac0e8b3 100644
11
+ # --- a/app/my_file.rb
12
+ # +++ b/app/my_file.rb
13
+ # @@ -5,6 +5,6 @@ def test1
14
+ DIFF_HEADER = %r{
15
+ \A # Start of line
16
+ diff\s--git\s # Match 'diff --git '
17
+ a/(?<previous_filename>.*?)\s # Match 'a/filename.json ' and capture the "filename.json" part
18
+ b/(?<new_filename>.*?) # Match 'b/filename.json ' and capture the "filename.json" part
19
+ \Z # End of line
20
+ }x
21
+
22
+ # This Regexp is used to match the header containing the original filename.
23
+ #
24
+ # @example Possible header on a diff.
25
+ #
26
+ # diff --git a/app/my_file.rb b/app/my_file.rb
27
+ # index d3dfbe4..ac0e8b3 100644
28
+ # --- a/app/my_file.rb <-- Match this line -->
29
+ # +++ b/app/my_file.rb
30
+ # @@ -5,6 +5,6 @@ def test1
31
+ ORIGINAL_FILE_HEADER = %r{
32
+ \A # Start of line
33
+ ---\s.* # Match '--- a/filename.rb' and capture the "filename.rb" part
34
+ \Z # End of line
35
+ }x
36
+
37
+ # This Regexp is used to match the header containing the new filename.
38
+ #
39
+ # @example Possible header on a diff.
40
+ #
41
+ # diff --git a/app/my_file.rb b/app/my_file.rb
42
+ # index d3dfbe4..ac0e8b3 100644
43
+ # --- a/app/my_file.rb
44
+ # +++ b/app/my_file.rb <-- Match this line -->
45
+ # @@ -5,6 +5,6 @@ def test1
46
+ NEW_FILE_HEADER = %r{
47
+ \A # Start of line
48
+ \+\+\+\s.* # Match '+++ b/filename.rb' and capture the "filename.rb" part
49
+ \Z # End of line
50
+ }x
51
+
52
+ # This Regexp is used to match the header indicating when a new file is added or removed.
53
+ #
54
+ # @example Possible header on a diff when adding a file.
55
+ #
56
+ # diff --git a/blabla.rb b/blabla.rb
57
+ # new file mode 100644 <-- Match this line -->
58
+ # index 0000000..d3dfbe4
59
+ # --- /dev/null
60
+ # +++ b/blabla.rb
61
+ # @@ -0,0 +1,10 @@
62
+ #
63
+ # @example Possible header on a diff when deleting a file.
64
+ #
65
+ # diff --git a/package.json b/package.json
66
+ # deleted file mode 100644 <-- Match this line -->
67
+ # index 3ffb801..0000000
68
+ # --- a/package.json
69
+ # +++ /dev/null
70
+ # @@ -1,11 +0,0 @@
71
+ MODE_HEADER = %r{
72
+ \A # Start of line
73
+ (?<file_mode>new|deleted) # Match 'new' or 'deleted' and capture the group
74
+ \sfile\smode\s\d+ # Match ' file mode 100655'
75
+ \Z # End of line
76
+ }x
77
+
78
+ # This Regexp is used to match the hunk's range of a diff.
79
+ #
80
+ # @example Possible hunk range on a diff.
81
+ #
82
+ # diff --git a/app/my_file.rb b/app/my_file.rb
83
+ # index d3dfbe4..ac0e8b3 100644
84
+ # --- a/app/my_file.rb
85
+ # +++ b/app/my_file.rb
86
+ # @@ -5,6 +5,6 @@ def test1 <-- Match this line -->
87
+ #
88
+ # @example Alternative match
89
+ #
90
+ # diff --git a/app/my_file.rb b/app/my_file.rb
91
+ # index d3dfbe4..ac0e8b3 100644
92
+ # --- a/app/my_file.rb
93
+ # +++ b/app/my_file.rb
94
+ # @@ -5 +5 @@ def test1 <-- Match this line -->
95
+ RANGE_HEADER = %r{
96
+ \A # Start of line
97
+ @@\s # Match '@@ '
98
+ -(?<previous_lino_start>\d+)(,\d+)?\s # Match '-1,11 ' or match '-1 ' and capture the '1' part
99
+ \+(?<new_lino_start>\d+)(,\d+)?\s # Match '+1,34 ' or match '+1 ' and capture the '1' part
100
+ @@.* # Match '@@ Any text'
101
+ \Z # End of line
102
+ }x
103
+
104
+ # This Regexp is used to match added lines.
105
+ #
106
+ # @example Diff when a line is added.
107
+ #
108
+ # diff --git a/app/my_file.rb b/app/my_file.rb
109
+ # index d3dfbe4..03d99f2 100644
110
+ # --- a/app/my_file.rb
111
+ # +++ b/app/my_file.rb
112
+ # @@ -6,5 +6,6 @@ def test1
113
+ #
114
+ # def test2
115
+ # "This file is just added"
116
+ # + "This is a new line" <-- Match this line -->
117
+ # end
118
+ # end
119
+ #
120
+ # @example Diff when a line is removed.
121
+ #
122
+ # diff --git a/app/my_file.rb b/app/my_file.rb
123
+ # index 03d99f2..d3dfbe4 100644
124
+ # --- a/app/my_file.rb
125
+ # +++ b/app/my_file.rb
126
+ # @@ -6,6 +6,5 @@ def test1
127
+ #
128
+ # def test2
129
+ # "This file is just added"
130
+ # - "This is a new line" <-- Match this line -->
131
+ # end
132
+ # end
133
+ #
134
+ # @example Diff containing a contextual line.
135
+ #
136
+ # diff --git a/app/my_file.rb b/app/my_file.rb
137
+ # index 03d99f2..d3dfbe4 100644
138
+ # --- a/app/my_file.rb
139
+ # +++ b/app/my_file.rb
140
+ # @@ -6,6 +6,5 @@ def test1
141
+ # <-- Match this line -->
142
+ # def test2 <-- Match this line -->
143
+ # "This file is just added" <-- Match this line -->
144
+ # - "This is a new line"
145
+ # end <-- Match this line -->
146
+ # end <-- Match this line -->
147
+ LINE_DIFF = %r{
148
+ \A # Start of line
149
+ (?<type> # Named group <type>
150
+ \+ # Match '+' (Considered as an addition line)
151
+ | # OR
152
+ - # Match '-' (Considered as a deletion line)
153
+ | # OR
154
+ \s # Match empty space ' ' (Considered as a contextual line)
155
+ ) # End of named group
156
+ (?<line>.*) # Match the content of the line itself
157
+ \Z # End of line
158
+ }x
159
+ end
160
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GithubDiffParser
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "github_diff_parser/version"
4
+
5
+ module GithubDiffParser
6
+ InvalidDiff = Class.new(ArgumentError)
7
+
8
+ autoload :Parser, "github_diff_parser/parser"
9
+ autoload :Regexes, "github_diff_parser/regexes"
10
+ autoload :Diff, "github_diff_parser/diff"
11
+ autoload :Hunk, "github_diff_parser/hunk"
12
+ autoload :Line, "github_diff_parser/line"
13
+
14
+ extend self
15
+
16
+ # Parse the output of a unified Git Diff.
17
+ #
18
+ # @param string [String] the output of a Git Diff
19
+ # @raise [GithubDiffParser::InvalidDiff] if the +string+ is not
20
+ # a correctly formatter Git Diff.
21
+ #
22
+ # @return [Array<GitubDiffParser::Diff>]
23
+ def parse(string)
24
+ Parser.new(string).process
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_diff_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Edouard CHIN
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-shopify
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Ruby Gem to parse unified git diff output.
42
+ email:
43
+ - chin.edouard@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rubocop.yml"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/github_diff_parser.rb
58
+ - lib/github_diff_parser/diff.rb
59
+ - lib/github_diff_parser/hunk.rb
60
+ - lib/github_diff_parser/line.rb
61
+ - lib/github_diff_parser/parser.rb
62
+ - lib/github_diff_parser/regexes.rb
63
+ - lib/github_diff_parser/version.rb
64
+ homepage: https://github.com/Edouard-chin/git_diff_parser
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ allowed_push_host: https://rubygems.org
69
+ homepage_uri: https://github.com/Edouard-chin/git_diff_parser
70
+ source_code_uri: https://github.com/Edouard-chin/git_diff_parser
71
+ rubygems_mfa_required: 'true'
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.6.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.2.32
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A Ruby Gem to parse unified git diff output.
91
+ test_files: []