Skip to content
Snippets Groups Projects
Commit a0d22d35 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Merge branch 'dz-straight-diffs' into 'master'

Straight diffs support

Based on https://gitlab.com/gitlab-org/gitlab_git/merge_requests/41. 

All credentials to @ben.boeckel :smiley:

See merge request !125
parents fc105f0d 595ed5d8
No related branches found
No related tags found
1 merge request!125Straight diffs support
Pipeline #
v 10.6.8
- Add straight parameter to Compare#initialize and straight option to Diff.between
v 10.6.7
- Commit.find returns nil and no longer throws an error on an empty repository
 
Loading
Loading
module Gitlab
module Git
class Compare
attr_reader :head, :base
attr_reader :head, :base, :straight
 
def initialize(repository, base, head)
def initialize(repository, base, head, straight = false)
@repository = repository
@straight = straight
 
unless base && head
@commits = []
Loading
Loading
@@ -34,6 +35,7 @@ module Gitlab
end
 
paths = options.delete(:paths) || []
options[:straight] = @straight
Gitlab::Git::Diff.between(@repository, @head.id, @base.id, options, *paths)
end
end
Loading
Loading
Loading
Loading
@@ -21,13 +21,22 @@ module Gitlab
 
class << self
def between(repo, head, base, options = {}, *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)
straight = options.delete(:straight) || false
common_commit = if straight
base
else
# 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"
repo.merge_base_commit(head, base)
end
 
options ||= {}
break_rewrites = options[:break_rewrites]
actual_options = filter_diff_options(options)
repo.diff(common_commit, head, actual_options, *paths)
end
Loading
Loading
Loading
Loading
@@ -2,7 +2,8 @@ require "spec_helper"
 
describe Gitlab::Git::Compare do
let(:repository) { Gitlab::Git::Repository.new(TEST_REPO_PATH) }
let(:compare) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID) }
let(:compare) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, false) }
let(:compare_straight) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, true) }
 
describe :commits do
subject do
Loading
Loading
@@ -12,6 +13,7 @@ describe Gitlab::Git::Compare do
it 'has 8 elements' do
expect(subject.size).to eq(8)
end
it { is_expected.to include(SeedRepo::Commit::PARENT_ID) }
it { is_expected.not_to include(SeedRepo::BigCommit::PARENT_ID) }
 
Loading
Loading
@@ -48,6 +50,7 @@ describe Gitlab::Git::Compare do
it 'has 10 elements' do
expect(subject.size).to eq(10)
end
it { is_expected.to include('files/ruby/popen.rb') }
it { is_expected.not_to include('LICENSE') }
 
Loading
Loading
@@ -77,4 +80,30 @@ describe Gitlab::Git::Compare do
it { is_expected.to eq(true) }
end
end
describe :commits_straight do
subject do
compare_straight.commits.map(&:id)
end
it 'has 8 elements' do
expect(subject.size).to eq(8)
end
it { is_expected.to include(SeedRepo::Commit::PARENT_ID) }
it { is_expected.not_to include(SeedRepo::BigCommit::PARENT_ID) }
end
describe :diffs_straight do
subject do
compare_straight.diffs.map(&:new_path)
end
it 'has 10 elements' do
expect(subject.size).to eq(10)
end
it { is_expected.to include('files/ruby/popen.rb') }
it { is_expected.not_to include('LICENSE') }
end
end
Loading
Loading
@@ -94,6 +94,26 @@ EOT
end
end
 
describe 'straight diffs' do
let(:options) { { :straight => true } }
let(:diffs) { Gitlab::Git::Diff.between(repository, 'feature', 'master', options) }
subject { diffs }
describe '#size' do
subject { super().size }
it { is_expected.to eq(24) }
end
context :diff do
subject { diffs.first }
it { should be_kind_of Gitlab::Git::Diff }
its(:new_path) { should == '.DS_Store' }
its(:diff) { should include 'Binary files /dev/null and b/.DS_Store differ' }
end
end
describe :between do
let(:diffs) { Gitlab::Git::Diff.between(repository, 'feature', 'master') }
subject { diffs }
Loading
Loading
@@ -102,6 +122,7 @@ EOT
 
describe '#size' do
subject { super().size }
it { is_expected.to eq(1) }
end
 
Loading
Loading
@@ -112,11 +133,13 @@ EOT
 
describe '#new_path' do
subject { super().new_path }
it { is_expected.to eq('files/ruby/feature.rb') }
end
 
describe '#diff' do
subject { super().diff }
it { is_expected.to include '+class Feature' }
end
end
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment