From 4b5724ec02cd68f6ca24eda0602dbbe1b8de7a28 Mon Sep 17 00:00:00 2001 From: darix Date: Mon, 28 Aug 2017 18:24:56 +0200 Subject: [PATCH] Extend config/version.rb with more informations (#5061) This gives installations not using git checkouts to provide all the informations needed for the internal version checks and version display in the dashboard. The build:stamp rake task was extended to also add the new informations. --- lib/discourse.rb | 45 ++++++++++++++++++++++++++++------------ lib/discourse_updates.rb | 4 ++-- lib/tasks/build.rake | 11 +++++++--- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/lib/discourse.rb b/lib/discourse.rb index c2aa10a0eac..0832d499907 100644 --- a/lib/discourse.rb +++ b/lib/discourse.rb @@ -311,25 +311,44 @@ module Discourse def self.git_version return $git_version if $git_version - # load the version stamped by the "build:stamp" task - f = Rails.root.to_s + "/config/version" - require f if File.exists?("#{f}.rb") - - begin - $git_version ||= `git rev-parse HEAD`.strip - rescue - $git_version = Discourse::VERSION::STRING - end + git_cmd='git rev-parse HEAD' + self.load_version_or_git(git_cmd, Discourse::VERSION::STRING) { $git_version } end def self.git_branch return $git_branch if $git_branch + git_cmd='git rev-parse --abbrev-ref HEAD' + self.load_version_or_git(git_cmd, 'unknown') { $git_branch } + end - begin - $git_branch ||= `git rev-parse --abbrev-ref HEAD`.strip - rescue - $git_branch = "unknown" + def self.full_version + return $full_version if $full_version + git_cmd='git describe --dirty --match "v[0-9]*"' + self.load_version_or_git(git_cmd, 'unknown') { $full_version } + end + + def self.load_version_or_git(git_cmd, default_value) + version_file = "#{Rails.root}/config/version.rb" + version_value = false + + if File.exists?(version_file) + require version_file + version_value = yield end + + # file does not exist or does not define the expected global variable + unless version_value + begin + version_value = `#{git_cmd}`.strip + rescue # sollte noch ausspezifiziert werden… + version_value = default_value + end + end + if version_value.empty? + version_value = default_value + end + + version_value end # Either returns the site_contact_username user or the first admin. diff --git a/lib/discourse_updates.rb b/lib/discourse_updates.rb index dd568063608..8f9684dca1d 100644 --- a/lib/discourse_updates.rb +++ b/lib/discourse_updates.rb @@ -7,7 +7,7 @@ module DiscourseUpdates DiscourseVersionCheck.new( installed_version: Discourse::VERSION::STRING, installed_sha: (Discourse.git_version == 'unknown' ? nil : Discourse.git_version), - installed_describe: `git describe --dirty --match "v[0-9]*"`, + installed_describe: Discourse.full_version, git_branch: Discourse.git_branch, updated_at: nil ) @@ -17,7 +17,7 @@ module DiscourseUpdates critical_updates: critical_updates_available?, installed_version: Discourse::VERSION::STRING, installed_sha: (Discourse.git_version == 'unknown' ? nil : Discourse.git_version), - installed_describe: `git describe --dirty --match "v[0-9]*"`, + installed_describe: Discourse.full_version, missing_versions_count: missing_versions_count, git_branch: Discourse.git_branch, updated_at: updated_at diff --git a/lib/tasks/build.rake b/lib/tasks/build.rake index 0f698be15a4..443d675a61f 100644 --- a/lib/tasks/build.rake +++ b/lib/tasks/build.rake @@ -1,8 +1,13 @@ desc "stamp the current build with the git hash placed in version.rb" task "build:stamp" => :environment do - git_version = `git rev-parse HEAD`.strip + git_version = `git rev-parse HEAD`.strip + git_branch = `git rev-parse --abbrev-ref HEAD` + full_version = `git describe --dirty --match "v[0-9]*"` + File.open(Rails.root.to_s + '/config/version.rb', 'w') do |f| - f.write("$git_version = #{git_version.inspect}\n") + f.write("$git_version = #{git_version.inspect}\n") + f.write("$git_branch = #{git_branch.inspect}\n") + f.write("$full_version = #{full_version.inspect}\n") end - puts "Stamped current build with #{git_version}" + puts "Stamped current build with #{git_version} #{git_branch} #{full_version}" end