summaryrefslogtreecommitdiff
path: root/lib/gitlab/version_info.rb
diff options
context:
space:
mode:
authorSato Hiroyuki <sathiroyuki@gmail.com>2013-05-08 14:19:51 +0900
committerSato Hiroyuki <sathiroyuki@gmail.com>2013-05-08 17:36:48 +0900
commit862e0ff6b846d9207c3adf9e86b5b84420595935 (patch)
treed06940ebe66d59a59550229004f10b8d25d0682e /lib/gitlab/version_info.rb
parent2e9599b746d5858eea0e4edcd5db4969b74c885c (diff)
downloadgitlab-ce-862e0ff6b846d9207c3adf9e86b5b84420595935.tar.gz
Add Gitlab::VersionInfo class to fix and simplify version check.
It returns "yes" if required version is "1.7.10" and current version is "1.6.10", because the patch version of current version equals to that of required version.
Diffstat (limited to 'lib/gitlab/version_info.rb')
-rw-r--r--lib/gitlab/version_info.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/lib/gitlab/version_info.rb b/lib/gitlab/version_info.rb
new file mode 100644
index 00000000000..31b72720972
--- /dev/null
+++ b/lib/gitlab/version_info.rb
@@ -0,0 +1,54 @@
+module Gitlab
+ class VersionInfo
+ include Comparable
+
+ attr_reader :major, :minor, :patch
+
+ def self.parse(str)
+ if m = str.match(/(\d+)\.(\d+)\.(\d+)/)
+ VersionInfo.new(m[1].to_i, m[2].to_i, m[3].to_i)
+ else
+ VersionInfo.new
+ end
+ end
+
+ def initialize(major = 0, minor = 0, patch = 0)
+ @major = major
+ @minor = minor
+ @patch = patch
+ end
+
+ def <=>(other)
+ return unless other.is_a? VersionInfo
+ return unless valid? && other.valid?
+
+ if other.major < @major
+ 1
+ elsif @major < other.major
+ -1
+ elsif other.minor < @minor
+ 1
+ elsif @minor < other.minor
+ -1
+ elsif other.patch < @patch
+ 1
+ elsif @patch < other.patch
+ -1
+ else
+ 0
+ end
+ end
+
+ def to_s
+ if valid?
+ "%d.%d.%d" % [@major, @minor, @patch]
+ else
+ "Unknown"
+ end
+ end
+
+ def valid?
+ @major >= 0 && @minor >= 0 && @patch >= 0 && @major + @minor + @patch > 0
+ end
+ end
+end