Prevent git errors from showing in command output

This would be a very rare case, but one time the git command failed, printing out an obscure "fatal" message to stderr which caused confusion.
This commit is contained in:
David Cook
2023-07-28 12:07:49 +10:00
parent 774bb7a607
commit febe6501c0
2 changed files with 18 additions and 2 deletions

View File

@@ -24,6 +24,7 @@ require_relative "../lib/open_food_network/i18n_config"
require_relative '../lib/spree/core/environment'
require_relative '../lib/spree/core/mail_interceptor'
require_relative "../lib/i18n_digests"
require_relative "../lib/git_utils"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
@@ -34,8 +35,8 @@ end
module Openfoodnetwork
class Application < Rails::Application
# Store a description of the current version, with linebreak trimmed
config.x.git_version = `git describe --tags --dirty=-modified`.strip
# Store a description of the current version
config.x.git_version = GitUtils::git_version
config.after_initialize do
# We need this here because the test env file loads before the Spree engine is loaded

15
lib/git_utils.rb Normal file
View File

@@ -0,0 +1,15 @@
class GitUtils
# Generate a description of the git version based on latest tag.
# Eg: "v4.4.4-156-g8afcd82-modified"
# - tag name
# - number of commits since tag
# - commit ID
# - "modified" if uncommitted changes
#
def self.git_version
# Capture stderr so that confusing errors aren't shown in comand output
stdout, _stderr, _status = Open3.capture3("git describe --tags --dirty=-modified")
# Strip trailing linebreak
stdout.strip
end
end