Commit b6bd7bbe authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '297480-fix-sort-same-name' into 'master'

Fix diff sorting when 2 files have the same name

See merge request gitlab-org/gitlab!52202
parents cc378fd1 b86db935
......@@ -3,6 +3,10 @@
module Gitlab
module Diff
class FileCollectionSorter
B_FOLLOWS_A = 1
A_FOLLOWS_B = -1
EQUIVALENT = 0
attr_reader :diffs
def initialize(diffs)
......@@ -29,14 +33,16 @@ module Gitlab
a_part = a_parts.shift
b_part = b_parts.shift
return 1 if a_parts.size < b_parts.size && a_parts.empty?
return -1 if a_parts.size > b_parts.size && b_parts.empty?
return B_FOLLOWS_A if a_parts.size < b_parts.size && a_parts.empty?
return A_FOLLOWS_B if a_parts.size > b_parts.size && b_parts.empty?
comparison = a_part <=> b_part
return comparison unless comparison == 0
return comparison unless comparison == EQUIVALENT
return compare_path_parts(a_parts, b_parts) if a_parts.any? && b_parts.any?
compare_path_parts(a_parts, b_parts)
# If A and B have the same name (e.g. symlink change), they are identical so return 0
EQUIVALENT
end
end
end
......
......@@ -5,11 +5,14 @@ require 'spec_helper'
RSpec.describe Gitlab::Diff::FileCollectionSorter do
let(:diffs) do
[
double(new_path: 'README', old_path: 'README'),
double(new_path: '.dir/test', old_path: '.dir/test'),
double(new_path: '', old_path: '.file'),
double(new_path: '1-folder/A-file.ext', old_path: '1-folder/A-file.ext'),
double(new_path: '1-folder/README', old_path: '1-folder/README'),
double(new_path: nil, old_path: '1-folder/M-file.ext'),
double(new_path: '1-folder/Z-file.ext', old_path: '1-folder/Z-file.ext'),
double(new_path: '1-folder/README', old_path: '1-folder/README'),
double(new_path: '', old_path: '1-folder/nested/A-file.ext'),
double(new_path: '1-folder/nested/M-file.ext', old_path: '1-folder/nested/M-file.ext'),
double(new_path: nil, old_path: '1-folder/nested/Z-file.ext'),
......@@ -19,7 +22,8 @@ RSpec.describe Gitlab::Diff::FileCollectionSorter do
double(new_path: nil, old_path: '2-folder/nested/A-file.ext'),
double(new_path: 'A-file.ext', old_path: 'A-file.ext'),
double(new_path: '', old_path: 'M-file.ext'),
double(new_path: 'Z-file.ext', old_path: 'Z-file.ext')
double(new_path: 'Z-file.ext', old_path: 'Z-file.ext'),
double(new_path: 'README', old_path: 'README')
]
end
......@@ -36,6 +40,8 @@ RSpec.describe Gitlab::Diff::FileCollectionSorter do
'1-folder/nested/Z-file.ext',
'1-folder/A-file.ext',
'1-folder/M-file.ext',
'1-folder/README',
'1-folder/README',
'1-folder/Z-file.ext',
'2-folder/nested/A-file.ext',
'2-folder/A-file.ext',
......@@ -44,6 +50,8 @@ RSpec.describe Gitlab::Diff::FileCollectionSorter do
'.file',
'A-file.ext',
'M-file.ext',
'README',
'README',
'Z-file.ext'
])
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