Skip to content
This repository was archived by the owner on May 12, 2018. It is now read-only.

Allow to specify diff paths #19

Merged
merged 1 commit into from
Jan 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/gitlab_git/compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ def initialize(repository, from, to, limit = 100)
@commits, @diffs = [], []
@commit = nil
@same = false
@limit = limit
@repository = repository

return unless from && to

base = Gitlab::Git::Commit.find(repository, from.try(:strip))
head = Gitlab::Git::Commit.find(repository, to.try(:strip))
@base = Gitlab::Git::Commit.find(repository, from.try(:strip))
@head = Gitlab::Git::Commit.find(repository, to.try(:strip))

return unless base && head
return unless @base && @head

if base.id == head.id
if @base.id == @head.id
@same = true
return
end

@commit = head
@commits = Gitlab::Git::Commit.between(repository, base.id, head.id)
@diffs = if @commits.size > limit
[]
else
Gitlab::Git::Diff.between(repository, head.id, base.id) rescue []
end
@commit = @head
@commits = Gitlab::Git::Commit.between(repository, @base.id, @head.id)
end

def diffs(paths = nil)
return [] if @commits.size > @limit
Gitlab::Git::Diff.between(@repository, @head.id, @base.id, *paths) rescue []
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gitlab_git/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class Diff
attr_accessor :new_file, :renamed_file, :deleted_file

class << self
def between(repo, head, base)
def between(repo, head, base, *paths)
# Only show what is new in the source branch compared to the target branch, not the other way around.
# The linex below with merge_base is equivalent to diff with three dots (git diff branch1...branch2)
# From the git documentation: "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B"
common_commit = repo.merge_base_commit(head, base)

repo.diff(common_commit, head).map do |diff|
repo.diff(common_commit, head, *paths).map do |diff|
Gitlab::Git::Diff.new(diff)
end
rescue Grit::Git::GitTimeout
Expand Down
4 changes: 2 additions & 2 deletions lib/gitlab_git/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ def merge_base_commit(from, to)
grit.git.native(:merge_base, {}, [to, from]).strip
end

def diff(from, to)
grit.diff(from, to)
def diff(from, to, *paths)
grit.diff(from, to, *paths)
end

# Returns commits collection
Expand Down