Commit 2ddd6d952b48fd0bd3364d1a22c0a4fbb4ad56f7

Authored by Dmitriy Zaporozhets
2 parents 7069fac3 862e0ff6

Merge pull request #3880 from hiroponz/fix-check-git-version

Add Gitlab::VersionInfo class to fix and simplify version check.
lib/gitlab/version_info.rb 0 → 100644
@@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
  1 +module Gitlab
  2 + class VersionInfo
  3 + include Comparable
  4 +
  5 + attr_reader :major, :minor, :patch
  6 +
  7 + def self.parse(str)
  8 + if m = str.match(/(\d+)\.(\d+)\.(\d+)/)
  9 + VersionInfo.new(m[1].to_i, m[2].to_i, m[3].to_i)
  10 + else
  11 + VersionInfo.new
  12 + end
  13 + end
  14 +
  15 + def initialize(major = 0, minor = 0, patch = 0)
  16 + @major = major
  17 + @minor = minor
  18 + @patch = patch
  19 + end
  20 +
  21 + def <=>(other)
  22 + return unless other.is_a? VersionInfo
  23 + return unless valid? && other.valid?
  24 +
  25 + if other.major < @major
  26 + 1
  27 + elsif @major < other.major
  28 + -1
  29 + elsif other.minor < @minor
  30 + 1
  31 + elsif @minor < other.minor
  32 + -1
  33 + elsif other.patch < @patch
  34 + 1
  35 + elsif @patch < other.patch
  36 + -1
  37 + else
  38 + 0
  39 + end
  40 + end
  41 +
  42 + def to_s
  43 + if valid?
  44 + "%d.%d.%d" % [@major, @minor, @patch]
  45 + else
  46 + "Unknown"
  47 + end
  48 + end
  49 +
  50 + def valid?
  51 + @major >= 0 && @minor >= 0 && @patch >= 0 && @major + @minor + @patch > 0
  52 + end
  53 + end
  54 +end
lib/tasks/gitlab/check.rake
@@ -655,39 +655,25 @@ namespace :gitlab do @@ -655,39 +655,25 @@ namespace :gitlab do
655 end 655 end
656 656
657 def check_gitlab_shell 657 def check_gitlab_shell
658 - required_version = '1.4.0' 658 + required_version = Gitlab::VersionInfo.new(1, 4, 0)
  659 + current_version = Gitlab::VersionInfo.parse(gitlab_shell_version)
659 660
660 - print "GitLab Shell version? ... "  
661 - if gitlab_shell_version.strip == required_version  
662 - puts "OK (#{required_version})".green 661 + print "GitLab Shell version >= #{required_version} ? ... "
  662 + if required_version <= current_version
  663 + puts "OK (#{current_version})".green
663 else 664 else
664 - puts "FAIL. Please update gitlab-shell to v#{required_version}".red 665 + puts "FAIL. Please update gitlab-shell to #{required_version} from #{current_version}".red
665 end 666 end
666 end 667 end
667 668
668 def check_git_version 669 def check_git_version
669 - required_version_major = 1  
670 - required_version_minor = 7  
671 - required_version_patch = 10  
672 -  
673 - required_version = "%d.%d.%d" %[required_version_major, required_version_minor, required_version_patch] 670 + required_version = Gitlab::VersionInfo.new(1, 7, 10)
  671 + current_version = Gitlab::VersionInfo.parse(run("git --version"))
674 672
675 print "Git version >= #{required_version} ? ... " 673 print "Git version >= #{required_version} ? ... "
676 674
677 - if m = run_and_match("git --version", /git version ((\d+)\.(\d+)\.(\d+))/)  
678 - current_version = m[1]  
679 - major = m[2].to_i  
680 - minor = m[3].to_i  
681 - patch = m[4].to_i  
682 - unless major <= required_version_major && minor <= required_version_minor && patch < required_version_patch  
683 - satisfying_git_version = true  
684 - end  
685 - else  
686 - current_version = "Unknown"  
687 - end  
688 -  
689 - if satisfying_git_version  
690 - puts "yes".green 675 + if required_version <= current_version
  676 + puts "yes (#{current_version})".green
691 else 677 else
692 puts "no".red 678 puts "no".red
693 try_fixing_it( 679 try_fixing_it(
spec/lib/gitlab/version_info_spec.rb 0 → 100644
@@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
  1 +require 'spec_helper'
  2 +
  3 +describe 'Gitlab::VersionInfo', no_db: true do
  4 + before do
  5 + @unknown = Gitlab::VersionInfo.new
  6 + @v0_0_1 = Gitlab::VersionInfo.new(0, 0, 1)
  7 + @v0_1_0 = Gitlab::VersionInfo.new(0, 1, 0)
  8 + @v1_0_0 = Gitlab::VersionInfo.new(1, 0, 0)
  9 + @v1_0_1 = Gitlab::VersionInfo.new(1, 0, 1)
  10 + @v1_1_0 = Gitlab::VersionInfo.new(1, 1, 0)
  11 + @v2_0_0 = Gitlab::VersionInfo.new(2, 0, 0)
  12 + end
  13 +
  14 + context '>' do
  15 + it { @v2_0_0.should > @v1_1_0 }
  16 + it { @v1_1_0.should > @v1_0_1 }
  17 + it { @v1_0_1.should > @v1_0_0 }
  18 + it { @v1_0_0.should > @v0_1_0 }
  19 + it { @v0_1_0.should > @v0_0_1 }
  20 + end
  21 +
  22 + context '>=' do
  23 + it { @v2_0_0.should >= Gitlab::VersionInfo.new(2, 0, 0) }
  24 + it { @v2_0_0.should >= @v1_1_0 }
  25 + end
  26 +
  27 + context '<' do
  28 + it { @v0_0_1.should < @v0_1_0 }
  29 + it { @v0_1_0.should < @v1_0_0 }
  30 + it { @v1_0_0.should < @v1_0_1 }
  31 + it { @v1_0_1.should < @v1_1_0 }
  32 + it { @v1_1_0.should < @v2_0_0 }
  33 + end
  34 +
  35 + context '<=' do
  36 + it { @v0_0_1.should <= Gitlab::VersionInfo.new(0, 0, 1) }
  37 + it { @v0_0_1.should <= @v0_1_0 }
  38 + end
  39 +
  40 + context '==' do
  41 + it { @v0_0_1.should == Gitlab::VersionInfo.new(0, 0, 1) }
  42 + it { @v0_1_0.should == Gitlab::VersionInfo.new(0, 1, 0) }
  43 + it { @v1_0_0.should == Gitlab::VersionInfo.new(1, 0, 0) }
  44 + end
  45 +
  46 + context '!=' do
  47 + it { @v0_0_1.should_not == @v0_1_0 }
  48 + end
  49 +
  50 + context 'unknown' do
  51 + it { @unknown.should_not be @v0_0_1 }
  52 + it { @unknown.should_not be Gitlab::VersionInfo.new }
  53 + it { expect{@unknown > @v0_0_1}.to raise_error(ArgumentError) }
  54 + it { expect{@unknown < @v0_0_1}.to raise_error(ArgumentError) }
  55 + end
  56 +
  57 + context 'parse' do
  58 + it { Gitlab::VersionInfo.parse("1.0.0").should == @v1_0_0 }
  59 + it { Gitlab::VersionInfo.parse("1.0.0.1").should == @v1_0_0 }
  60 + it { Gitlab::VersionInfo.parse("git 1.0.0b1").should == @v1_0_0 }
  61 + it { Gitlab::VersionInfo.parse("git 1.0b1").should_not be_valid }
  62 + end
  63 +
  64 + context 'to_s' do
  65 + it { @v1_0_0.to_s.should == "1.0.0" }
  66 + it { @unknown.to_s.should == "Unknown" }
  67 + end
  68 +end
  69 +