From d74d7c7cfb49ead619d21e35f07b1feba8295118 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Mon, 15 Apr 2013 19:02:05 +0300 Subject: [PATCH] Update app code to use Gitlab::Git::Diff --- app/models/note.rb | 6 +++--- lib/gitlab/git/commit.rb | 6 +++++- lib/gitlab/git/repository.rb | 11 +++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/note.rb b/app/models/note.rb index 8274b8665..7b7e6e99d 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -68,8 +68,8 @@ class Note < ActiveRecord::Base def diff if noteable.diffs.present? noteable.diffs.select do |d| - if d.b_path - Digest::SHA1.hexdigest(d.b_path) == diff_file_index + if d.new_path + Digest::SHA1.hexdigest(d.new_path) == diff_file_index end end.first end @@ -80,7 +80,7 @@ class Note < ActiveRecord::Base end def diff_file_name - diff.b_path + diff.new_path end def diff_new_line diff --git a/lib/gitlab/git/commit.rb b/lib/gitlab/git/commit.rb index 27b866893..e5e48ec48 100644 --- a/lib/gitlab/git/commit.rb +++ b/lib/gitlab/git/commit.rb @@ -9,7 +9,7 @@ module Gitlab :author_name, :author_email, :parent_ids, :committer_name, :committer_email - delegate :parents, :diffs, :tree, :stats, :to_patch, + delegate :parents, :tree, :stats, :to_patch, to: :raw_commit def initialize(raw_commit, head = nil) @@ -96,6 +96,10 @@ module Gitlab committed_date end + def diffs + raw_commit.diffs.map { |diff| Gitlab::Git::Diff.new(diff) } + end + private def init_from_grit(grit) diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb index b8eba8881..96fd61007 100644 --- a/lib/gitlab/git/repository.rb +++ b/lib/gitlab/git/repository.rb @@ -191,6 +191,17 @@ module Gitlab "#{type}:#{path_with_namespace}" end + def diffs_between(source_branch, target_branch) + # 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.git.native(:merge_base, {}, [target_branch, source_branch]).strip + repo.diff(common_commit, source_branch).map { |diff| Gitlab::Git::Diff.new(diff) } + + rescue Grit::Git::GitTimeout + [Gitlab::Git::Diff::BROKEN_DIFF] + end + protected def decorate_commit(commit, ref = nil) -- 2.11.0