From 5fd280f3d3264aec3656cb61cd8728f2ca4d61ce Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Thu, 12 Nov 2015 17:00:39 +0100 Subject: [PATCH] Licence also accepted as license file --- CHANGELOG | 2 +- app/models/repository.rb | 22 +++++++++++++++------- spec/models/repository_spec.rb | 11 ++++++++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 69a293d70d6..891004f2ffb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -32,7 +32,7 @@ v 8.2.0 (unreleased) - Fix incoming email config defaults - MR target branch is now visible on a list view when it is different from project's default one - Improve Continuous Integration graphs page - - Accept COPYING as licence file (Zeger-Jan van de Weg) + - Accept COPYING,COPYING.lesser, and licence as license file (Zeger-Jan van de Weg) v 8.1.4 - Fix bug where manually merged branches in a MR would end up with an empty diff (Stan Hu) diff --git a/app/models/repository.rb b/app/models/repository.rb index 622d6303bfe..cc70aa2b737 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -244,16 +244,24 @@ class Repository def license cache.fetch(:license) do licenses = tree(:head).blobs.find_all do |file| - file.name =~ /\A(copying|license)/i + file.name =~ /\A(copying|license|licence)/i end - # If `licence`, `copying` and `copying.lesser` are found, return in the - # following order: licence, copying, copying.lesser - regex = [/\Alicense(\..*)?\z/i, /\Acopying(\..{0,3})?\z/i, /\Acopying.lesser/i] + 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 + ] - regex.map do |re| - licenses.find { |l| l.name =~ re } - end.compact.first + 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 04437d64fee..49af0229bb5 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -102,13 +102,22 @@ describe Repository do end describe "#license" do - it 'test selection preference' 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 end