2017-07-12 10:31:36 -04:00
|
|
|
# Gitaly note: JV: no RPC's here.
|
|
|
|
|
2017-04-05 03:25:54 -04:00
|
|
|
module Gitlab
|
|
|
|
module Git
|
|
|
|
# Ephemeral (per request) storage for environment variables that some Git
|
2018-03-30 05:19:46 -04:00
|
|
|
# commands need during internal API calls made from Git push hooks.
|
2017-04-05 03:25:54 -04:00
|
|
|
#
|
|
|
|
# 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-03-30 05:19:46 -04:00
|
|
|
class HookEnv
|
2017-10-05 10:51:31 -04:00
|
|
|
WHITELISTED_VARIABLES = %w[
|
|
|
|
GIT_OBJECT_DIRECTORY_RELATIVE
|
|
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE
|
2017-04-05 03:25:54 -04:00
|
|
|
].freeze
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
def self.set(gl_repository, env)
|
2017-04-05 03:25:54 -04:00
|
|
|
return unless RequestStore.active?
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
raise "missing gl_repository" if gl_repository.blank?
|
|
|
|
|
|
|
|
RequestStore.store[:gitlab_git_env] ||= {}
|
|
|
|
RequestStore.store[:gitlab_git_env][gl_repository] = whitelist_git_env(env)
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
def self.all(gl_repository)
|
2017-04-05 03:25:54 -04:00
|
|
|
return {} unless RequestStore.active?
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
h = RequestStore.fetch(:gitlab_git_env) { {} }
|
|
|
|
h.fetch(gl_repository, {})
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
def self.to_env_hash(gl_repository)
|
2017-10-12 07:54:59 -04:00
|
|
|
env = {}
|
|
|
|
|
2018-03-30 05:19:46 -04:00
|
|
|
all(gl_repository).compact.each do |key, value|
|
2017-10-12 07:54:59 -04:00
|
|
|
value = value.join(File::PATH_SEPARATOR) if value.is_a?(Array)
|
|
|
|
env[key.to_s] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
env
|
|
|
|
end
|
|
|
|
|
2017-04-05 03:25:54 -04:00
|
|
|
def self.whitelist_git_env(env)
|
2017-10-05 10:51:31 -04:00
|
|
|
env.select { |key, _| WHITELISTED_VARIABLES.include?(key.to_s) }.with_indifferent_access
|
2017-04-05 03:25:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|