diff options
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | app/models/repository.rb | 20 | ||||
-rw-r--r-- | spec/models/repository_spec.rb | 21 |
3 files changed, 39 insertions, 3 deletions
diff --git a/CHANGELOG b/CHANGELOG index e38c8b363e7..585e6c42043 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -42,6 +42,7 @@ v 8.3.0 (unreleased) - Fix sidebar tooltips when collapsed - Prevent possible XSS attack with award-emoji - Upgraded Sidekiq to 4.x + - Accept COPYING,COPYING.lesser, and licence as license file (Zeger-Jan van de Weg) v 8.2.3 - Fix application settings cache not expiring after changes (Stan Hu) diff --git a/app/models/repository.rb b/app/models/repository.rb index 1edec52c09e..2c25f4ce451 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -271,9 +271,25 @@ class Repository def license cache.fetch(:license) do - tree(:head).blobs.find do |file| - file.name =~ /\Alicense/i + licenses = tree(:head).blobs.find_all do |file| + file.name =~ /\A(copying|license|licence)/i + end + + preferences = [ + /\Alicen[sc]e\z/i, # LICENSE, LICENCE + /\Alicen[sc]e\./i, # LICENSE.md, LICENSE.txt + /\Acopying\z/i, # COPYING + /\Acopying\.(?!lesser)/i, # COPYING.txt + /Acopying.lesser/i # COPYING.LESSER + ] + + license = nil + preferences.each do |r| + license = licenses.find { |l| l.name =~ r } + break if license end + + license end end diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index e6c415da267..afbf62035ac 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -103,6 +103,26 @@ describe Repository, models: true do end + describe "#license" do + before do + repository.send(:cache).expire(:license) + TestBlob = Struct.new(:name) + end + + it 'test selection preference' do + files = [TestBlob.new('file'), TestBlob.new('license'), TestBlob.new('copying')] + expect(repository.tree).to receive(:blobs).and_return(files) + + expect(repository.license.name).to eq('license') + end + + it 'also accepts licence instead of license' do + expect(repository.tree).to receive(:blobs).and_return([TestBlob.new('licence')]) + + expect(repository.license.name).to eq('licence') + end + end + describe :add_branch do context 'when pre hooks were successful' do it 'should run without errors' do @@ -199,5 +219,4 @@ describe Repository, models: true do end end end - end |