Skip to content

Commit d96cfbe

Browse files
authored
Merge pull request #11 from CatanaCorp/ec-git-apply
Add naive implementations of git apply and git apply -R
2 parents 4704e6d + bdaf4f3 commit d96cfbe

7 files changed

Lines changed: 555 additions & 1 deletion

File tree

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ inherit_gem:
44
AllCops:
55
NewCops: disable
66
SuggestExtensions: false
7+
Exclude:
8+
- "test/data/**/*"
79

810
Style/Documentation:
911
Enabled: false

lib/github_diff_parser/diff.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,48 @@ def previous_line_number_is_now(line_number)
152152
end
153153
end
154154

155+
# A naive implementation of `$ git apply`.
156+
#
157+
# @param previous_content [String] The previous content related to this diff.
158+
# @return [String] The content after applying this diff to the `previous_content`.
159+
def apply(previous_content)
160+
lines = previous_content.lines
161+
offset = 0
162+
163+
self.lines.each do |line|
164+
if line.addition?
165+
lines.insert(line.current_number - 1, line.content)
166+
offset += 1
167+
elsif line.deletion?
168+
lines.delete_at(line.previous_number - 1 + offset)
169+
offset -= 1
170+
end
171+
end
172+
173+
lines.join
174+
end
175+
176+
# A naive implementation of `$ git apply -R`.
177+
#
178+
# @param current_content [String] The current content related to this diff.
179+
# @return [String] The content after reverting this diff to the `current_content`.
180+
def revert(current_content)
181+
lines = current_content.lines
182+
offset = 0
183+
184+
self.lines.each do |line|
185+
if line.addition?
186+
lines.delete_at(line.current_number - 1 + offset)
187+
offset -= 1
188+
elsif line.deletion?
189+
lines.insert(line.previous_number - 1, line.content)
190+
offset += 1
191+
end
192+
end
193+
194+
lines.join
195+
end
196+
155197
private
156198

157199
# Check if a line was shifted. A line is considered shifted if its number is superior to the first hunk's start

test/data/octokit.diff

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
diff --git a/test/data/octokit_before.rb b/test/data/octokit_before.rb
2+
index 3fd0a6c..1e8ff37 100644
3+
--- a/test/data/octokit_before.rb
4+
+++ b/test/data/octokit_before.rb
5+
@@ -17,24 +17,11 @@ module Octokit
6+
#
7+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
8+
def commit_comment_reactions(repo, id, options = {})
9+
- get "#{Repository.path repo}/comments/#{id}/reactions", options
10+
- end
11+
+ get "something/something"
12+
13+
- # Create a reaction for a commit comment
14+
- #
15+
- # @param repo [Integer, String, Hash, Repository] A GitHub repository
16+
- # @param id [Integer] The id of the commit comment
17+
- # @param reaction [String] The Reaction
18+
- # @see https://developer.github.com/v3/reactions/#create-reaction-for-a-commit-comment
19+
- # @see https://developer.github.com/v3/reactions/#reaction-types
20+
- #
21+
- # @example
22+
- # @client.create_commit_comment_reactions("octokit/octokit.rb", 1)
23+
- #
24+
- # @return [<Sawyer::Resource>] Hash representing the reaction
25+
- def create_commit_comment_reaction(repo, id, reaction, options = {})
26+
- options = options.merge(content: reaction)
27+
- post "#{Repository.path repo}/comments/#{id}/reactions", options
28+
+ <<~CODE
29+
+ 1 + 1
30+
+ CODE
31+
end
32+
33+
# List reactions for an issue
34+
@@ -47,8 +34,8 @@ module Octokit
35+
# @client.issue_reactions("octokit/octokit.rb", 1)
36+
#
37+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
38+
- def issue_reactions(repo, number, options = {})
39+
- get "#{Repository.path repo}/issues/#{number}/reactions", options
40+
+ def issues_reactions(repo, number, opts = {})
41+
+ get "#{Repository.path repo}/issues/#{number}/reactions", opts
42+
end
43+
44+
# Create reaction for an issue
45+
@@ -61,7 +48,7 @@ module Octokit
46+
# @see https://developer.github.com/v3/reactions/#reaction-types
47+
#
48+
# @example
49+
- # @client.create_issue_reaction("octokit/octokit.rb", 1)
50+
+ # @client.create_issue_reaction("octokit/octokit.rb", 2)
51+
#
52+
# @return [<Sawyer::Resource>] Hash representing the reaction.
53+
def create_issue_reaction(repo, number, reaction, options = {})
54+
@@ -69,6 +56,10 @@ module Octokit
55+
post "#{Repository.path repo}/issues/#{number}/reactions", options
56+
end
57+
58+
+ def hello
59+
+ "world"
60+
+ end
61+
+
62+
# List reactions for an issue comment
63+
#
64+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
65+
@@ -135,6 +126,55 @@ module Octokit
66+
post "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
67+
end
68+
69+
+ # Delete a reaction
70+
+ #
71+
+ # @param repo [Integer, String, Hash, Repository] A GitHub repository
72+
+ # @param issue_id [Integer] The Issue comment id
73+
+ # @param reaction_id [Integer] The Reaction id
74+
+ #
75+
+ # @see https://docs.github.com/en/rest/reactions/reactions#delete-an-issue-reaction
76+
+ #
77+
+ # @example
78+
+ # @client.delete_issue_reaction("octokit/octokit.rb", 1, 2)
79+
+ #
80+
+ # @return [Boolean] Return true if reaction was deleted, false otherwise.
81+
+ def delete_issue_reaction(repo, issue_id, reaction_id, options = {})
82+
+ boolean_from_response :delete, "#{Repository.path repo}/issues/#{issue_id}/reactions/#{reaction_id}", options
83+
+ end
84+
+
85+
+ # List reactions for a release
86+
+ #
87+
+ # @param repo [Integer, String, Hash, Repository] A GitHub repository
88+
+ # @param id [Integer] The Release id
89+
+ #
90+
+ # @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-a-release
91+
+ #
92+
+ # @example
93+
+ # @client.release_reactions("octokit/octokit.rb", 1)
94+
+ #
95+
+ # @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
96+
+ def release_reactions(repo, release_id, options = {})
97+
+ get "#{Repository.path repo}/releases/#{release_id}/reactions", options
98+
+ end
99+
+
100+
+ # Create reaction for a release
101+
+ #
102+
+ # @param repo [Integer, String, Hash, Repository] A GitHub repository
103+
+ # @param id [Integer] The Release id
104+
+ # @param reaction [String] The Reaction
105+
+ #
106+
+ # @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-a-release
107+
+ # @see https://developer.github.com/v3/reactions/#reaction-types
108+
+ #
109+
+ # @example
110+
+ # @client.create_release_reaction("octokit/octokit.rb", 1)
111+
+ #
112+
+ # @return [<Sawyer::Resource>] Hash representing the reaction.
113+
+ def create_release_reaction(repo, release_id, reaction, options = {})
114+
+ options = options.merge(content: reaction)
115+
+ post "#{Repository.path repo}/releases/#{release_id}/reactions", options
116+
+ end
117+
+
118+
# Delete a reaction for a release
119+
#
120+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
121+
@@ -153,3 +193,9 @@ module Octokit
122+
end
123+
end
124+
end
125+
+
126+
+module OctoOcto
127+
+ def foofoo
128+
+ Error = Class.new(StandardError)
129+
+ end
130+
+end

test/data/octokit_after.rb

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# frozen_string_literal: true
2+
3+
module Octokit
4+
class Client
5+
# Methods for the Reacions API
6+
#
7+
# @see https://developer.github.com/v3/reactions/
8+
module Reactions
9+
# List reactions for a commit comment
10+
#
11+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
12+
# @param id [Integer] The id of the commit comment
13+
# @see https://developer.github.com/v3/reactions/#list-reactions-for-a-commit-comment
14+
#
15+
# @example
16+
# @client.commit_comment_reactions("octokit/octokit.rb", 1)
17+
#
18+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
19+
def commit_comment_reactions(repo, id, options = {})
20+
get "something/something"
21+
22+
<<~CODE
23+
1 + 1
24+
CODE
25+
end
26+
27+
# List reactions for an issue
28+
#
29+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
30+
# @param number [Integer] The Issue number
31+
# @see https://developer.github.com/v3/reactions/#list-reactions-for-an-issue
32+
#
33+
# @example
34+
# @client.issue_reactions("octokit/octokit.rb", 1)
35+
#
36+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
37+
def issues_reactions(repo, number, opts = {})
38+
get "#{Repository.path repo}/issues/#{number}/reactions", opts
39+
end
40+
41+
# Create reaction for an issue
42+
#
43+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
44+
# @param number [Integer] The Issue number
45+
# @param reaction [String] The Reaction
46+
#
47+
# @see https://developer.github.com/v3/reactions/#create-reaction-for-an-issue
48+
# @see https://developer.github.com/v3/reactions/#reaction-types
49+
#
50+
# @example
51+
# @client.create_issue_reaction("octokit/octokit.rb", 2)
52+
#
53+
# @return [<Sawyer::Resource>] Hash representing the reaction.
54+
def create_issue_reaction(repo, number, reaction, options = {})
55+
options = options.merge(content: reaction)
56+
post "#{Repository.path repo}/issues/#{number}/reactions", options
57+
end
58+
59+
def hello
60+
"world"
61+
end
62+
63+
# List reactions for an issue comment
64+
#
65+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
66+
# @param id [Integer] The Issue comment id
67+
#
68+
# @see https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
69+
#
70+
# @example
71+
# @client.issue_comment_reactions("octokit/octokit.rb", 1)
72+
#
73+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
74+
def issue_comment_reactions(repo, id, options = {})
75+
get "#{Repository.path repo}/issues/comments/#{id}/reactions", options
76+
end
77+
78+
# Create reaction for an issue comment
79+
#
80+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
81+
# @param id [Integer] The Issue comment id
82+
# @param reaction [String] The Reaction
83+
#
84+
# @see https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
85+
# @see https://developer.github.com/v3/reactions/#reaction-types
86+
#
87+
# @example
88+
# @client.create_issue_comment_reaction("octokit/octokit.rb", 1)
89+
#
90+
# @return [<Sawyer::Resource>] Hashes representing the reaction.
91+
def create_issue_comment_reaction(repo, id, reaction, options = {})
92+
options = options.merge(content: reaction)
93+
post "#{Repository.path repo}/issues/comments/#{id}/reactions", options
94+
end
95+
96+
# List reactions for a pull request review comment
97+
#
98+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
99+
# @param id [Integer] The Issue comment id
100+
#
101+
# @see https://developer.github.com/v3/reactions/#list-reactions-for-a-pull-request-review-comment
102+
#
103+
# @example
104+
# @client.pull_request_review_comment_reactions("octokit/octokit.rb", 1)
105+
#
106+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
107+
def pull_request_review_comment_reactions(repo, id, options = {})
108+
get "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
109+
end
110+
111+
# Create reaction for a pull request review comment
112+
#
113+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
114+
# @param id [Integer] The Issue comment id
115+
# @param reaction [String] The Reaction
116+
#
117+
# @see https://developer.github.com/v3/reactions/#create-reaction-for-a-pull-request-review-comment
118+
# @see https://developer.github.com/v3/reactions/#reaction-types
119+
#
120+
# @example
121+
# @client.create_pull_request_reiew_comment_reaction("octokit/octokit.rb", 1)
122+
#
123+
# @return [<Sawyer::Resource>] Hash representing the reaction.
124+
def create_pull_request_review_comment_reaction(repo, id, reaction, options = {})
125+
options = options.merge(content: reaction)
126+
post "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
127+
end
128+
129+
# Delete a reaction
130+
#
131+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
132+
# @param issue_id [Integer] The Issue comment id
133+
# @param reaction_id [Integer] The Reaction id
134+
#
135+
# @see https://docs.github.com/en/rest/reactions/reactions#delete-an-issue-reaction
136+
#
137+
# @example
138+
# @client.delete_issue_reaction("octokit/octokit.rb", 1, 2)
139+
#
140+
# @return [Boolean] Return true if reaction was deleted, false otherwise.
141+
def delete_issue_reaction(repo, issue_id, reaction_id, options = {})
142+
boolean_from_response :delete, "#{Repository.path repo}/issues/#{issue_id}/reactions/#{reaction_id}", options
143+
end
144+
145+
# List reactions for a release
146+
#
147+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
148+
# @param id [Integer] The Release id
149+
#
150+
# @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-a-release
151+
#
152+
# @example
153+
# @client.release_reactions("octokit/octokit.rb", 1)
154+
#
155+
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
156+
def release_reactions(repo, release_id, options = {})
157+
get "#{Repository.path repo}/releases/#{release_id}/reactions", options
158+
end
159+
160+
# Create reaction for a release
161+
#
162+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
163+
# @param id [Integer] The Release id
164+
# @param reaction [String] The Reaction
165+
#
166+
# @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-a-release
167+
# @see https://developer.github.com/v3/reactions/#reaction-types
168+
#
169+
# @example
170+
# @client.create_release_reaction("octokit/octokit.rb", 1)
171+
#
172+
# @return [<Sawyer::Resource>] Hash representing the reaction.
173+
def create_release_reaction(repo, release_id, reaction, options = {})
174+
options = options.merge(content: reaction)
175+
post "#{Repository.path repo}/releases/#{release_id}/reactions", options
176+
end
177+
178+
# Delete a reaction for a release
179+
#
180+
# @param repo [Integer, String, Hash, Repository] A GitHub repository
181+
# @param issue_id [Integer] The Release id
182+
# @param reaction_id [Integer] The Reaction id
183+
#
184+
# @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#delete-a-release-reaction
185+
#
186+
# @example
187+
# @client.delete_release_reaction("octokit/octokit.rb", 1, 2)
188+
#
189+
# @return [Boolean] Return true if reaction was deleted, false otherwise.
190+
def delete_release_reaction(repo, release_id, reaction_id, options = {})
191+
boolean_from_response :delete, "#{Repository.path repo}/releases/#{release_id}/reactions/#{reaction_id}", options
192+
end
193+
end
194+
end
195+
end
196+
197+
module OctoOcto
198+
def foofoo
199+
Error = Class.new(StandardError)
200+
end
201+
end

0 commit comments

Comments
 (0)