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
|
|
|
|
# commands may need.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
class Env
|
2017-10-05 10:51:31 -04:00
|
|
|
WHITELISTED_VARIABLES = %w[
|
2017-04-05 03:25:54 -04:00
|
|
|
GIT_OBJECT_DIRECTORY
|
2017-10-05 10:51:31 -04:00
|
|
|
GIT_OBJECT_DIRECTORY_RELATIVE
|
2017-04-05 03:25:54 -04:00
|
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES
|
2017-10-05 10:51:31 -04:00
|
|
|
GIT_ALTERNATE_OBJECT_DIRECTORIES_RELATIVE
|
2017-04-05 03:25:54 -04:00
|
|
|
].freeze
|
|
|
|
|
|
|
|
def self.set(env)
|
|
|
|
return unless RequestStore.active?
|
|
|
|
|
|
|
|
RequestStore.store[:gitlab_git_env] = whitelist_git_env(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.all
|
|
|
|
return {} unless RequestStore.active?
|
|
|
|
|
|
|
|
RequestStore.fetch(:gitlab_git_env) { {} }
|
|
|
|
end
|
|
|
|
|
2017-10-12 07:54:59 -04:00
|
|
|
def self.to_env_hash
|
|
|
|
env = {}
|
|
|
|
|
|
|
|
all.compact.each do |key, value|
|
|
|
|
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.[](key)
|
|
|
|
all[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|