Commit 4d4aefc5 authored by Douwe Maan's avatar Douwe Maan

Merge branch '30973-fix-network-graph-ordering' into 'master'

Fix ordering of commits in the network graph.

Closes #30973

See merge request !10936
parents 4d3bd41d 3f1c7d85
......@@ -107,7 +107,8 @@ module Network
def find_commits(skip = 0)
opts = {
max_count: self.class.max_count,
skip: skip
skip: skip,
order: :date
}
opts[:ref] = @commit.id if @filter_ref
......
---
title: Fix ordering of commits in the network graph
merge_request: 10936
author:
......@@ -494,7 +494,9 @@ module Gitlab
# :contains is the commit contained by the refs from which to begin (SHA1 or name)
# :max_count is the maximum number of commits to fetch
# :skip is the number of commits to skip
# :order is the commits order and allowed value is :date(default) or :topo
# :order is the commits order and allowed value is :none (default), :date, or :topo
# commit ordering types are documented here:
# http://www.rubydoc.info/github/libgit2/rugged/Rugged#SORT_NONE-constant)
#
def find_commits(options = {})
actual_options = options.dup
......@@ -522,11 +524,8 @@ module Gitlab
end
end
if actual_options[:order] == :topo
walker.sorting(Rugged::SORT_TOPO)
else
walker.sorting(Rugged::SORT_NONE)
end
sort_type = rugged_sort_type(actual_options[:order])
walker.sorting(sort_type)
commits = []
offset = actual_options[:skip]
......@@ -1273,6 +1272,18 @@ module Gitlab
def gitaly_ref_client
@gitaly_ref_client ||= Gitlab::GitalyClient::Ref.new(self)
end
# Returns the `Rugged` sorting type constant for a given
# sort type key. Valid keys are `:none`, `:topo`, and `:date`
def rugged_sort_type(key)
@rugged_sort_types ||= {
none: Rugged::SORT_NONE,
topo: Rugged::SORT_TOPO,
date: Rugged::SORT_DATE
}
@rugged_sort_types.fetch(key, Rugged::SORT_NONE)
end
end
end
end
......@@ -1031,6 +1031,35 @@ describe Gitlab::Git::Repository, seed_helper: true do
end
end
describe '#find_commits' do
it 'should return a return a collection of commits' do
commits = repository.find_commits
expect(commits).not_to be_empty
expect(commits).to all( be_a_kind_of(Gitlab::Git::Commit) )
end
context 'while applying a sort order based on the `order` option' do
it "allows ordering topologically (no parents shown before their children)" do
expect_any_instance_of(Rugged::Walker).to receive(:sorting).with(Rugged::SORT_TOPO)
repository.find_commits(order: :topo)
end
it "allows ordering by date" do
expect_any_instance_of(Rugged::Walker).to receive(:sorting).with(Rugged::SORT_DATE)
repository.find_commits(order: :date)
end
it "applies no sorting by default" do
expect_any_instance_of(Rugged::Walker).to receive(:sorting).with(Rugged::SORT_NONE)
repository.find_commits
end
end
end
describe '#branches with deleted branch' do
before(:each) do
ref = double()
......
......@@ -9,4 +9,25 @@ describe Network::Graph, models: true do
expect(graph.notes).to eq( { note_on_commit.commit_id => 1 } )
end
describe "#commits" do
let(:graph) { described_class.new(project, 'refs/heads/master', project.repository.commit, nil) }
it "returns a list of commits" do
commits = graph.commits
expect(commits).not_to be_empty
expect(commits).to all( be_kind_of(Network::Commit) )
end
it "sorts the commits by commit date (descending)" do
# Remove duplicate timestamps because they make it harder to
# assert that the commits are sorted as expected.
commits = graph.commits.uniq(&:date)
sorted_commits = commits.sort_by(&:date).reverse
expect(commits).not_to be_empty
expect(commits.map(&:id)).to eq(sorted_commits.map(&:id))
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment