Commit cc570b6c authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '46600-fix-gitlab-revision-when-not-in-git-repo' into 'master'

Resolve "gitlab 10.8 assumes that installation is a git working copy."

Closes #46600

See merge request gitlab-org/gitlab-ce!19125
parents f5e6300b d50a80a2
...@@ -618,7 +618,7 @@ module Ci ...@@ -618,7 +618,7 @@ module Ci
variables.append(key: 'GITLAB_FEATURES', value: project.licensed_features.join(',')) variables.append(key: 'GITLAB_FEATURES', value: project.licensed_features.join(','))
variables.append(key: 'CI_SERVER_NAME', value: 'GitLab') variables.append(key: 'CI_SERVER_NAME', value: 'GitLab')
variables.append(key: 'CI_SERVER_VERSION', value: Gitlab::VERSION) variables.append(key: 'CI_SERVER_VERSION', value: Gitlab::VERSION)
variables.append(key: 'CI_SERVER_REVISION', value: Gitlab::REVISION) variables.append(key: 'CI_SERVER_REVISION', value: Gitlab.revision)
variables.append(key: 'CI_JOB_NAME', value: name) variables.append(key: 'CI_JOB_NAME', value: name)
variables.append(key: 'CI_JOB_STAGE', value: stage) variables.append(key: 'CI_JOB_STAGE', value: stage)
variables.append(key: 'CI_COMMIT_SHA', value: sha) variables.append(key: 'CI_COMMIT_SHA', value: sha)
......
...@@ -132,7 +132,7 @@ ...@@ -132,7 +132,7 @@
GitLab GitLab
%span.pull-right %span.pull-right
= Gitlab::VERSION = Gitlab::VERSION
= "(#{Gitlab::REVISION})" = "(#{Gitlab.revision})"
%p %p
GitLab Shell GitLab Shell
%span.pull-right %span.pull-right
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
Community Edition Community Edition
- if user_signed_in? - if user_signed_in?
%span= Gitlab::VERSION %span= Gitlab::VERSION
%small= link_to Gitlab::REVISION, Gitlab::COM_URL + namespace_project_commits_path('gitlab-org', 'gitlab-ce', Gitlab::REVISION) %small= link_to Gitlab.revision, Gitlab::COM_URL + namespace_project_commits_path('gitlab-org', 'gitlab-ce', Gitlab.revision)
= version_status_badge = version_status_badge
%hr %hr
......
---
title: Replace Gitlab::REVISION with Gitlab.revision and handle installations without
a .git directory
merge_request: 19125
author:
type: fixed
...@@ -3,7 +3,7 @@ if defined?(Rails::Console) ...@@ -3,7 +3,7 @@ if defined?(Rails::Console)
# note that this will not print out when using `spring` # note that this will not print out when using `spring`
justify = 15 justify = 15
puts "-------------------------------------------------------------------------------------" puts "-------------------------------------------------------------------------------------"
puts " Gitlab:".ljust(justify) + "#{Gitlab::VERSION} (#{Gitlab::REVISION})" puts " Gitlab:".ljust(justify) + "#{Gitlab::VERSION} (#{Gitlab.revision})"
puts " Gitlab Shell:".ljust(justify) + Gitlab::Shell.new.version puts " Gitlab Shell:".ljust(justify) + Gitlab::Shell.new.version
puts " #{Gitlab::Database.adapter_name}:".ljust(justify) + Gitlab::Database.version puts " #{Gitlab::Database.adapter_name}:".ljust(justify) + Gitlab::Database.version
puts "-------------------------------------------------------------------------------------" puts "-------------------------------------------------------------------------------------"
......
...@@ -13,7 +13,7 @@ def configure_sentry ...@@ -13,7 +13,7 @@ def configure_sentry
if sentry_enabled if sentry_enabled
Raven.configure do |config| Raven.configure do |config|
config.dsn = Gitlab::CurrentSettings.current_application_settings.sentry_dsn config.dsn = Gitlab::CurrentSettings.current_application_settings.sentry_dsn
config.release = Gitlab::REVISION config.release = Gitlab.revision
# Sanitize fields based on those sanitized from Rails. # Sanitize fields based on those sanitized from Rails.
config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s) config.sanitize_fields = Rails.application.config.filter_parameters.map(&:to_s)
......
...@@ -113,7 +113,7 @@ module API ...@@ -113,7 +113,7 @@ module API
{ {
api_version: API.version, api_version: API.version,
gitlab_version: Gitlab::VERSION, gitlab_version: Gitlab::VERSION,
gitlab_rev: Gitlab::REVISION, gitlab_rev: Gitlab.revision,
redis: redis_ping redis: redis_ping
} }
end end
......
...@@ -6,7 +6,7 @@ module API ...@@ -6,7 +6,7 @@ module API
detail 'This feature was introduced in GitLab 8.13.' detail 'This feature was introduced in GitLab 8.13.'
end end
get '/version' do get '/version' do
{ version: Gitlab::VERSION, revision: Gitlab::REVISION } { version: Gitlab::VERSION, revision: Gitlab.revision }
end end
end end
end end
...@@ -13,11 +13,26 @@ module Gitlab ...@@ -13,11 +13,26 @@ module Gitlab
@_migrations_hash ||= Digest::MD5.hexdigest(ActiveRecord::Migrator.get_all_versions.to_s) @_migrations_hash ||= Digest::MD5.hexdigest(ActiveRecord::Migrator.get_all_versions.to_s)
end end
def self.revision
@_revision ||= begin
if File.exist?(root.join("REVISION"))
File.read(root.join("REVISION")).strip.freeze
else
result = Gitlab::Popen.popen_with_detail(%W[#{config.git.bin_path} log --pretty=format:%h -n 1])
if result.status.success?
result.stdout.chomp.freeze
else
"Unknown".freeze
end
end
end
end
COM_URL = 'https://gitlab.com'.freeze COM_URL = 'https://gitlab.com'.freeze
APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))} APP_DIRS_PATTERN = %r{^/?(app|config|ee|lib|spec|\(\w*\))}
SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z} SUBDOMAIN_REGEX = %r{\Ahttps://[a-z0-9]+\.gitlab\.com\z}
VERSION = File.read(root.join("VERSION")).strip.freeze VERSION = File.read(root.join("VERSION")).strip.freeze
REVISION = Gitlab::Popen.popen(%W(#{config.git.bin_path} log --pretty=format:%h -n 1)).first.chomp.freeze
def self.com? def self.com?
# Check `gl_subdomain?` as well to keep parity with gitlab.com # Check `gl_subdomain?` as well to keep parity with gitlab.com
......
...@@ -15,7 +15,7 @@ module Gitlab ...@@ -15,7 +15,7 @@ module Gitlab
gon.user_color_scheme = Gitlab::ColorSchemes.for_user(current_user).css_class gon.user_color_scheme = Gitlab::ColorSchemes.for_user(current_user).css_class
gon.sentry_dsn = Gitlab::CurrentSettings.clientside_sentry_dsn if Gitlab::CurrentSettings.clientside_sentry_enabled gon.sentry_dsn = Gitlab::CurrentSettings.clientside_sentry_dsn if Gitlab::CurrentSettings.clientside_sentry_enabled
gon.gitlab_url = Gitlab.config.gitlab.url gon.gitlab_url = Gitlab.config.gitlab.url
gon.revision = Gitlab::REVISION gon.revision = Gitlab.revision
gon.gitlab_logo = ActionController::Base.helpers.asset_path('gitlab_logo.png') gon.gitlab_logo = ActionController::Base.helpers.asset_path('gitlab_logo.png')
gon.sprite_icons = IconsHelper.sprite_icon_path gon.sprite_icons = IconsHelper.sprite_icon_path
gon.sprite_file_icons = IconsHelper.sprite_file_icons_path gon.sprite_file_icons = IconsHelper.sprite_file_icons_path
......
...@@ -47,7 +47,7 @@ namespace :gitlab do ...@@ -47,7 +47,7 @@ namespace :gitlab do
puts "" puts ""
puts "GitLab information".color(:yellow) puts "GitLab information".color(:yellow)
puts "Version:\t#{Gitlab::VERSION}" puts "Version:\t#{Gitlab::VERSION}"
puts "Revision:\t#{Gitlab::REVISION}" puts "Revision:\t#{Gitlab.revision}"
puts "Directory:\t#{Rails.root}" puts "Directory:\t#{Rails.root}"
puts "DB Adapter:\t#{database_adapter}" puts "DB Adapter:\t#{database_adapter}"
puts "URL:\t\t#{Gitlab.config.gitlab.url}" puts "URL:\t\t#{Gitlab.config.gitlab.url}"
......
...@@ -8,6 +8,66 @@ describe Gitlab do ...@@ -8,6 +8,66 @@ describe Gitlab do
expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__))) expect(described_class.root).to eq(Pathname.new(File.expand_path('../..', __dir__)))
end end
end end
describe '.revision' do
let(:cmd) { %W[#{described_class.config.git.bin_path} log --pretty=format:%h -n 1] }
around do |example|
described_class.instance_variable_set(:@_revision, nil)
example.run
described_class.instance_variable_set(:@_revision, nil)
end
context 'when a REVISION file exists' do
before do
expect(File).to receive(:exist?)
.with(described_class.root.join('REVISION'))
.and_return(true)
end
it 'returns the actual Git revision' do
expect(File).to receive(:read)
.with(described_class.root.join('REVISION'))
.and_return("abc123\n")
expect(described_class.revision).to eq('abc123')
end
it 'memoizes the revision' do
expect(File).to receive(:read)
.once
.with(described_class.root.join('REVISION'))
.and_return("abc123\n")
2.times { described_class.revision }
end
end
context 'when no REVISION file exist' do
context 'when the Git command succeeds' do
before do
expect(Gitlab::Popen).to receive(:popen_with_detail)
.with(cmd)
.and_return(Gitlab::Popen::Result.new(cmd, 'abc123', '', double(success?: true)))
end
it 'returns the actual Git revision' do
expect(described_class.revision).to eq('abc123')
end
end
context 'when the Git command fails' do
before do
expect(Gitlab::Popen).to receive(:popen_with_detail)
.with(cmd)
.and_return(Gitlab::Popen::Result.new(cmd, '', 'fatal: Not a git repository', double('Process::Status', success?: false)))
end
it 'returns "Unknown"' do
expect(described_class.revision).to eq('Unknown')
end
end
end
end
describe '.com?' do describe '.com?' do
it 'is true when on GitLab.com' do it 'is true when on GitLab.com' do
......
...@@ -1546,7 +1546,7 @@ describe Ci::Build do ...@@ -1546,7 +1546,7 @@ describe Ci::Build do
{ key: 'GITLAB_FEATURES', value: project.licensed_features.join(','), public: true }, { key: 'GITLAB_FEATURES', value: project.licensed_features.join(','), public: true },
{ key: 'CI_SERVER_NAME', value: 'GitLab', public: true }, { key: 'CI_SERVER_NAME', value: 'GitLab', public: true },
{ key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true }, { key: 'CI_SERVER_VERSION', value: Gitlab::VERSION, public: true },
{ key: 'CI_SERVER_REVISION', value: Gitlab::REVISION, public: true }, { key: 'CI_SERVER_REVISION', value: Gitlab.revision, public: true },
{ key: 'CI_JOB_NAME', value: 'test', public: true }, { key: 'CI_JOB_NAME', value: 'test', public: true },
{ key: 'CI_JOB_STAGE', value: 'test', public: true }, { key: 'CI_JOB_STAGE', value: 'test', public: true },
{ key: 'CI_COMMIT_SHA', value: build.sha, public: true }, { key: 'CI_COMMIT_SHA', value: build.sha, public: true },
......
...@@ -18,7 +18,7 @@ describe API::Version do ...@@ -18,7 +18,7 @@ describe API::Version do
expect(response).to have_gitlab_http_status(200) expect(response).to have_gitlab_http_status(200)
expect(json_response['version']).to eq(Gitlab::VERSION) expect(json_response['version']).to eq(Gitlab::VERSION)
expect(json_response['revision']).to eq(Gitlab::REVISION) expect(json_response['revision']).to eq(Gitlab.revision)
end end
end end
end end
......
...@@ -22,6 +22,6 @@ describe 'admin/dashboard/index.html.haml' do ...@@ -22,6 +22,6 @@ describe 'admin/dashboard/index.html.haml' do
it "includes revision of GitLab" do it "includes revision of GitLab" do
render render
expect(rendered).to have_content "#{Gitlab::VERSION} (#{Gitlab::REVISION})" expect(rendered).to have_content "#{Gitlab::VERSION} (#{Gitlab.revision})"
end end
end end
...@@ -39,7 +39,7 @@ describe 'help/index' do ...@@ -39,7 +39,7 @@ describe 'help/index' do
def stub_version(version, revision) def stub_version(version, revision)
stub_const('Gitlab::VERSION', version) stub_const('Gitlab::VERSION', version)
stub_const('Gitlab::REVISION', revision) allow(Gitlab).to receive(:revision).and_return(revision)
end end
def stub_helpers def stub_helpers
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment