forked from prontolabs/pronto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get rid of Rugged monkey patches, move them to Pronto::Git namespace
- Loading branch information
Showing
9 changed files
with
150 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module Pronto | ||
module Git | ||
class Line < Struct.new(:line, :patch, :hunk) | ||
def addition? | ||
line.addition? | ||
end | ||
|
||
def deletion? | ||
line.deletion? | ||
end | ||
|
||
def content | ||
line.content | ||
end | ||
|
||
def new_lineno | ||
line.new_lineno | ||
end | ||
|
||
def old_lineno | ||
line.old_lineno | ||
end | ||
|
||
def line_origin | ||
line.line_origin | ||
end | ||
|
||
def position | ||
hunk_index = patch.hunks.find_index { |h| h.header == hunk.header } | ||
line_index = patch.lines.find_index(line) | ||
|
||
line_index + hunk_index + 1 | ||
end | ||
|
||
def commit | ||
@commit ||= begin | ||
repo.lookup(commit_sha) if commit_sha | ||
end | ||
end | ||
|
||
def commit_sha | ||
blame[:final_commit_id] if blame | ||
end | ||
|
||
def commit_line | ||
@commit_line ||= begin | ||
# TODO: Rugged does not seem to support diffing against multiple parents | ||
diff = commit.diff(reverse: true) unless commit.parents.count != 1 | ||
patches = diff ? diff.patches : [] | ||
|
||
# TODO This could be cleaner | ||
patches = patches.map { |patch| Git::Patch.new(patch, repo) } | ||
|
||
commit_patch = patches.find do |p| | ||
patch.new_file_full_path == p.new_file_full_path | ||
end | ||
|
||
lines = commit_patch ? commit_patch.lines : [] | ||
result = lines.find { |l| blame[:orig_start_line_number] == l.new_lineno } | ||
|
||
result || line # no commit_line means that it was just added | ||
end | ||
end | ||
|
||
def ==(other) | ||
line.content == other.content && | ||
line_origin == other.line_origin && | ||
old_lineno == other.old_lineno && | ||
new_lineno == other.new_lineno | ||
end | ||
|
||
private | ||
|
||
def repo | ||
patch.repo | ||
end | ||
|
||
def blame | ||
@blame ||= Rugged::Blame.new(repo, patch.delta.new_file[:path], | ||
min_line: new_lineno, max_line: new_lineno, | ||
track_copies_same_file: true, | ||
track_copies_any_commit_copies: true)[0] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'pathname' | ||
|
||
module Pronto | ||
module Git | ||
class Patch < Struct.new(:patch, :repo) | ||
def delta | ||
patch.delta | ||
end | ||
|
||
def hunks | ||
patch.hunks | ||
end | ||
|
||
def additions | ||
patch.stat[0] | ||
end | ||
|
||
def deletions | ||
patch.stat[1] | ||
end | ||
|
||
def lines | ||
@lines ||= begin | ||
patch.map do |hunk| | ||
hunk.lines.map { |line| Line.new(line, self, hunk) } | ||
end.flatten.compact | ||
end | ||
end | ||
|
||
def added_lines | ||
lines.select(&:addition?) | ||
end | ||
|
||
def deleted_lines | ||
lines.select(&:deletion?) | ||
end | ||
|
||
def new_file_full_path | ||
repo_path = Pathname.new(repo.path).parent | ||
repo_path.join(patch.delta.new_file[:path]) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
module Pronto | ||
module Git | ||
describe Line do | ||
let(:diff) { repository.diff('88558b7', '88558b7~5') } | ||
let(:patch) { Patch.new(diff.patches.last, repository) } | ||
let(:line) { patch.lines[2] } | ||
|
||
describe '#position' do | ||
subject { line.position } | ||
it { should == 3 } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +0,0 @@ | ||
require 'spec_helper' | ||
|
||
module Rugged | ||
class Diff | ||
describe Line do | ||
let(:diff) { repository.diff('88558b7', '88558b7~5') } | ||
let(:patch) { diff.patches.last } | ||
let(:line) { patch.lines[2] } | ||
|
||
describe '#position' do | ||
subject { line.position } | ||
it { should == 3 } | ||
end | ||
end | ||
end | ||
end | ||