Skip to content

Instantly share code, notes, and snippets.

@darix
Last active August 17, 2017 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darix/4c6ba244186a6634be1ac6f0adf77f0a to your computer and use it in GitHub Desktop.
Save darix/4c6ba244186a6634be1ac6f0adf77f0a to your computer and use it in GitHub Desktop.
diff --git a/lib/discourse.rb b/lib/discourse.rb
index c2aa10a0ea..0832d49990 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
+
+ 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
- begin
- $git_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
- rescue
- $git_branch = "unknown"
+ 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 dd56806360..8f9684dca1 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 0f698be15a..443d675a61 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment