Skip to content

Commit

Permalink
Merge pull request #11 from CatanaCorp/ec-git-apply
Browse files Browse the repository at this point in the history
Add naive implementations of git apply and git apply -R
  • Loading branch information
Edouard-chin authored Feb 21, 2024
2 parents 4704e6d + bdaf4f3 commit d96cfbe
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ inherit_gem:
AllCops:
NewCops: disable
SuggestExtensions: false
Exclude:
- "test/data/**/*"

Style/Documentation:
Enabled: false
42 changes: 42 additions & 0 deletions lib/github_diff_parser/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,48 @@ def previous_line_number_is_now(line_number)
end
end

# A naive implementation of `$ git apply`.
#
# @param previous_content [String] The previous content related to this diff.
# @return [String] The content after applying this diff to the `previous_content`.
def apply(previous_content)
lines = previous_content.lines
offset = 0

self.lines.each do |line|
if line.addition?
lines.insert(line.current_number - 1, line.content)
offset += 1
elsif line.deletion?
lines.delete_at(line.previous_number - 1 + offset)
offset -= 1
end
end

lines.join
end

# A naive implementation of `$ git apply -R`.
#
# @param current_content [String] The current content related to this diff.
# @return [String] The content after reverting this diff to the `current_content`.
def revert(current_content)
lines = current_content.lines
offset = 0

self.lines.each do |line|
if line.addition?
lines.delete_at(line.current_number - 1 + offset)
offset -= 1
elsif line.deletion?
lines.insert(line.previous_number - 1, line.content)
offset += 1
end
end

lines.join
end

private

# Check if a line was shifted. A line is considered shifted if its number is superior to the first hunk's start
Expand Down
130 changes: 130 additions & 0 deletions test/data/octokit.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
diff --git a/test/data/octokit_before.rb b/test/data/octokit_before.rb
index 3fd0a6c..1e8ff37 100644
--- a/test/data/octokit_before.rb
+++ b/test/data/octokit_before.rb
@@ -17,24 +17,11 @@ module Octokit
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def commit_comment_reactions(repo, id, options = {})
- get "#{Repository.path repo}/comments/#{id}/reactions", options
- end
+ get "something/something"

- # Create a reaction for a commit comment
- #
- # @param repo [Integer, String, Hash, Repository] A GitHub repository
- # @param id [Integer] The id of the commit comment
- # @param reaction [String] The Reaction
- # @see https://developer.github.com/v3/reactions/#create-reaction-for-a-commit-comment
- # @see https://developer.github.com/v3/reactions/#reaction-types
- #
- # @example
- # @client.create_commit_comment_reactions("octokit/octokit.rb", 1)
- #
- # @return [<Sawyer::Resource>] Hash representing the reaction
- def create_commit_comment_reaction(repo, id, reaction, options = {})
- options = options.merge(content: reaction)
- post "#{Repository.path repo}/comments/#{id}/reactions", options
+ <<~CODE
+ 1 + 1
+ CODE
end

# List reactions for an issue
@@ -47,8 +34,8 @@ module Octokit
# @client.issue_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
- def issue_reactions(repo, number, options = {})
- get "#{Repository.path repo}/issues/#{number}/reactions", options
+ def issues_reactions(repo, number, opts = {})
+ get "#{Repository.path repo}/issues/#{number}/reactions", opts
end

# Create reaction for an issue
@@ -61,7 +48,7 @@ module Octokit
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
- # @client.create_issue_reaction("octokit/octokit.rb", 1)
+ # @client.create_issue_reaction("octokit/octokit.rb", 2)
#
# @return [<Sawyer::Resource>] Hash representing the reaction.
def create_issue_reaction(repo, number, reaction, options = {})
@@ -69,6 +56,10 @@ module Octokit
post "#{Repository.path repo}/issues/#{number}/reactions", options
end

+ def hello
+ "world"
+ end
+
# List reactions for an issue comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
@@ -135,6 +126,55 @@ module Octokit
post "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
end

+ # Delete a reaction
+ #
+ # @param repo [Integer, String, Hash, Repository] A GitHub repository
+ # @param issue_id [Integer] The Issue comment id
+ # @param reaction_id [Integer] The Reaction id
+ #
+ # @see https://docs.github.com/en/rest/reactions/reactions#delete-an-issue-reaction
+ #
+ # @example
+ # @client.delete_issue_reaction("octokit/octokit.rb", 1, 2)
+ #
+ # @return [Boolean] Return true if reaction was deleted, false otherwise.
+ def delete_issue_reaction(repo, issue_id, reaction_id, options = {})
+ boolean_from_response :delete, "#{Repository.path repo}/issues/#{issue_id}/reactions/#{reaction_id}", options
+ end
+
+ # List reactions for a release
+ #
+ # @param repo [Integer, String, Hash, Repository] A GitHub repository
+ # @param id [Integer] The Release id
+ #
+ # @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-a-release
+ #
+ # @example
+ # @client.release_reactions("octokit/octokit.rb", 1)
+ #
+ # @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
+ def release_reactions(repo, release_id, options = {})
+ get "#{Repository.path repo}/releases/#{release_id}/reactions", options
+ end
+
+ # Create reaction for a release
+ #
+ # @param repo [Integer, String, Hash, Repository] A GitHub repository
+ # @param id [Integer] The Release id
+ # @param reaction [String] The Reaction
+ #
+ # @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-a-release
+ # @see https://developer.github.com/v3/reactions/#reaction-types
+ #
+ # @example
+ # @client.create_release_reaction("octokit/octokit.rb", 1)
+ #
+ # @return [<Sawyer::Resource>] Hash representing the reaction.
+ def create_release_reaction(repo, release_id, reaction, options = {})
+ options = options.merge(content: reaction)
+ post "#{Repository.path repo}/releases/#{release_id}/reactions", options
+ end
+
# Delete a reaction for a release
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
@@ -153,3 +193,9 @@ module Octokit
end
end
end
+
+module OctoOcto
+ def foofoo
+ Error = Class.new(StandardError)
+ end
+end
201 changes: 201 additions & 0 deletions test/data/octokit_after.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# frozen_string_literal: true

module Octokit
class Client
# Methods for the Reacions API
#
# @see https://developer.github.com/v3/reactions/
module Reactions
# List reactions for a commit comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The id of the commit comment
# @see https://developer.github.com/v3/reactions/#list-reactions-for-a-commit-comment
#
# @example
# @client.commit_comment_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def commit_comment_reactions(repo, id, options = {})
get "something/something"

<<~CODE
1 + 1
CODE
end

# List reactions for an issue
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param number [Integer] The Issue number
# @see https://developer.github.com/v3/reactions/#list-reactions-for-an-issue
#
# @example
# @client.issue_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def issues_reactions(repo, number, opts = {})
get "#{Repository.path repo}/issues/#{number}/reactions", opts
end

# Create reaction for an issue
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param number [Integer] The Issue number
# @param reaction [String] The Reaction
#
# @see https://developer.github.com/v3/reactions/#create-reaction-for-an-issue
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_issue_reaction("octokit/octokit.rb", 2)
#
# @return [<Sawyer::Resource>] Hash representing the reaction.
def create_issue_reaction(repo, number, reaction, options = {})
options = options.merge(content: reaction)
post "#{Repository.path repo}/issues/#{number}/reactions", options
end

def hello
"world"
end

# List reactions for an issue comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
#
# @see https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
#
# @example
# @client.issue_comment_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def issue_comment_reactions(repo, id, options = {})
get "#{Repository.path repo}/issues/comments/#{id}/reactions", options
end

# Create reaction for an issue comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
# @param reaction [String] The Reaction
#
# @see https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_issue_comment_reaction("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hashes representing the reaction.
def create_issue_comment_reaction(repo, id, reaction, options = {})
options = options.merge(content: reaction)
post "#{Repository.path repo}/issues/comments/#{id}/reactions", options
end

# List reactions for a pull request review comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
#
# @see https://developer.github.com/v3/reactions/#list-reactions-for-a-pull-request-review-comment
#
# @example
# @client.pull_request_review_comment_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def pull_request_review_comment_reactions(repo, id, options = {})
get "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
end

# Create reaction for a pull request review comment
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Issue comment id
# @param reaction [String] The Reaction
#
# @see https://developer.github.com/v3/reactions/#create-reaction-for-a-pull-request-review-comment
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_pull_request_reiew_comment_reaction("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hash representing the reaction.
def create_pull_request_review_comment_reaction(repo, id, reaction, options = {})
options = options.merge(content: reaction)
post "#{Repository.path repo}/pulls/comments/#{id}/reactions", options
end

# Delete a reaction
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param issue_id [Integer] The Issue comment id
# @param reaction_id [Integer] The Reaction id
#
# @see https://docs.github.com/en/rest/reactions/reactions#delete-an-issue-reaction
#
# @example
# @client.delete_issue_reaction("octokit/octokit.rb", 1, 2)
#
# @return [Boolean] Return true if reaction was deleted, false otherwise.
def delete_issue_reaction(repo, issue_id, reaction_id, options = {})
boolean_from_response :delete, "#{Repository.path repo}/issues/#{issue_id}/reactions/#{reaction_id}", options
end

# List reactions for a release
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Release id
#
# @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#list-reactions-for-a-release
#
# @example
# @client.release_reactions("octokit/octokit.rb", 1)
#
# @return [Array<Sawyer::Resource>] Array of Hashes representing the reactions.
def release_reactions(repo, release_id, options = {})
get "#{Repository.path repo}/releases/#{release_id}/reactions", options
end

# Create reaction for a release
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param id [Integer] The Release id
# @param reaction [String] The Reaction
#
# @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#create-reaction-for-a-release
# @see https://developer.github.com/v3/reactions/#reaction-types
#
# @example
# @client.create_release_reaction("octokit/octokit.rb", 1)
#
# @return [<Sawyer::Resource>] Hash representing the reaction.
def create_release_reaction(repo, release_id, reaction, options = {})
options = options.merge(content: reaction)
post "#{Repository.path repo}/releases/#{release_id}/reactions", options
end

# Delete a reaction for a release
#
# @param repo [Integer, String, Hash, Repository] A GitHub repository
# @param issue_id [Integer] The Release id
# @param reaction_id [Integer] The Reaction id
#
# @see https://docs.github.com/en/free-pro-team@latest/rest/reactions/reactions?apiVersion=2022-11-28#delete-a-release-reaction
#
# @example
# @client.delete_release_reaction("octokit/octokit.rb", 1, 2)
#
# @return [Boolean] Return true if reaction was deleted, false otherwise.
def delete_release_reaction(repo, release_id, reaction_id, options = {})
boolean_from_response :delete, "#{Repository.path repo}/releases/#{release_id}/reactions/#{reaction_id}", options
end
end
end
end

module OctoOcto
def foofoo
Error = Class.new(StandardError)
end
end
Loading

0 comments on commit d96cfbe

Please sign in to comment.