2018-12-23 12:14:25 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Gitaly note: JV: no RPC's here.
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
# Ephemeral (per request) storage for environment variables that some Git
|
2018-05-09 12:01:36 +05:30
|
|
|
# commands need during internal API calls made from Git push hooks.
|
2017-08-17 22:00:37 +05:30
|
|
|
#
|
|
|
|
# For example, in pre-receive hooks, new objects are put in a temporary
|
|
|
|
# $GIT_OBJECT_DIRECTORY. Without it set, the new objects cannot be retrieved
|
|
|
|
# (this would break push rules for instance).
|
|
|
|
#
|
|
|
|
# This class is thread-safe via RequestStore.
|
2018-05-09 12:01:36 +05:30
|
|
|
class HookEnv
|
2018-03-17 18:26:18 +05:30
|
|
|
WHITELISTED_VARIABLES = %w[
|
|
|
|
GIT_OBJECT_DIRECTORY_RELATIVE
|
|
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE
|
2017-08-17 22:00:37 +05:30
|
|
|
].freeze
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def self.set(gl_repository, env)
|
2018-12-05 23:21:45 +05:30
|
|
|
return unless Gitlab::SafeRequestStore.active?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
raise "missing gl_repository" if gl_repository.blank?
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
Gitlab::SafeRequestStore[:gitlab_git_env] ||= {}
|
|
|
|
Gitlab::SafeRequestStore[:gitlab_git_env][gl_repository] = whitelist_git_env(env)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def self.all(gl_repository)
|
2018-12-05 23:21:45 +05:30
|
|
|
return {} unless Gitlab::SafeRequestStore.active?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
h = Gitlab::SafeRequestStore.fetch(:gitlab_git_env) { {} }
|
2018-05-09 12:01:36 +05:30
|
|
|
h.fetch(gl_repository, {})
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
def self.to_env_hash(gl_repository)
|
2018-03-17 18:26:18 +05:30
|
|
|
env = {}
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
all(gl_repository).compact.each do |key, value|
|
2018-03-17 18:26:18 +05:30
|
|
|
value = value.join(File::PATH_SEPARATOR) if value.is_a?(Array)
|
|
|
|
env[key.to_s] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
env
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.whitelist_git_env(env)
|
2018-03-17 18:26:18 +05:30
|
|
|
env.select { |key, _| WHITELISTED_VARIABLES.include?(key.to_s) }.with_indifferent_access
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|