This instance will be upgraded to Heptapod 0.23.2 on 2021-08-05 at 11:00 UTC+2 (a few minutes of down time)

Commit d3d2ae8f authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

HgGitRepository: factorized minimal environ for hg

Wanted to do this a long time ago, much more comfortable
with this now that we have extensive tests.

Will be needed to add `HGMODULEPOLICY` to the game

--HG--
branch : heptapod
parent eac0a704e209
...@@ -9,8 +9,8 @@ module Mercurial ...@@ -9,8 +9,8 @@ module Mercurial
BookmarksUnsupportedError = Class.new(StandardError) BookmarksUnsupportedError = Class.new(StandardError)
class << self class << self
# Suitable value for HGRCPATH so that structural and default settings # Application-wide HGRC files, in a suitable form for the HGRCPATH
# are loaded and an application wide can override them. # environment variable.
def hgrc_path def hgrc_path
Gitlab.config.mercurial.hgrc.join(":") Gitlab.config.mercurial.hgrc.join(":")
end end
......
...@@ -74,11 +74,9 @@ def hg_full_path ...@@ -74,11 +74,9 @@ def hg_full_path
end end
def hg_config_item_bool?(full_dotted_name) def hg_config_item_bool?(full_dotted_name)
env = { 'HGRCPATH' => Gitlab::Mercurial.hgrc_path }
val_str, status = popen( val_str, status = popen(
[Gitlab.config.mercurial.bin_path, [Gitlab.config.mercurial.bin_path, 'config', full_dotted_name],
'config', full_dotted_name], @hgpath, hg_env_minimal)
@hgpath, env)
return false if status == 1 # status 1 means value is missing return false if status == 1 # status 1 means value is missing
raise HgError, "Could not read `#{full_dotted_name}` config item for #{@hgpath}" unless status == 0 raise HgError, "Could not read `#{full_dotted_name}` config item for #{@hgpath}" unless status == 0
...@@ -107,13 +105,12 @@ def create_hg_repository(namespace_fs_path:, in_subgroup:) ...@@ -107,13 +105,12 @@ def create_hg_repository(namespace_fs_path:, in_subgroup:)
"in_subgroup: #{in_subgroup}") "in_subgroup: #{in_subgroup}")
begin begin
out, status = popen( out, status = popen(
[Gitlab.config.mercurial.bin_path, "init", @hgpath], [Gitlab.config.mercurial.bin_path, "init", @hgpath],
# Rails.root can be a Mercurial repo, that can lead to # Rails.root can be a Mercurial repo, that can lead to
# complications. Let's run from a path that's very unlikely # complications. Let's run from a path that's very unlikely
# to be one: # to be one:
@hg_storage_root, @hg_storage_root, hg_env_minimal)
{ 'HGRCPATH' => Gitlab::Mercurial.hgrc_path,
})
if status != 0 if status != 0
raise HgError, "Could not execute `hg init` for #{@hgpath}: #{out}" raise HgError, "Could not execute `hg init` for #{@hgpath}: #{out}"
end end
...@@ -149,10 +146,9 @@ def import_repository(url, **create_args) ...@@ -149,10 +146,9 @@ def import_repository(url, **create_args)
create_repository(**create_args) create_repository(**create_args)
hg_exe = Gitlab.config.mercurial.bin_path hg_exe = Gitlab.config.mercurial.bin_path
hg_env = {'HGUSER' => 'Heptapod system', hg_env = hg_env_minimal
'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes', hg_env.update({ 'HGUSER' => 'Heptapod system',
'HGRCPATH' => Gitlab::Mercurial.hgrc_path, 'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes' })
}
Rails.logger.info("#{logprefix} Pulling") Rails.logger.info("#{logprefix} Pulling")
pid = Process.spawn(hg_env, pid = Process.spawn(hg_env,
hg_exe, "pull", "-q", hg_exe, "pull", "-q",
...@@ -193,10 +189,9 @@ def import_repository(url, **create_args) ...@@ -193,10 +189,9 @@ def import_repository(url, **create_args)
# #
# See heptapod#192 for details about implications of various options # See heptapod#192 for details about implications of various options
def hg_optim def hg_optim
hg_env = {'HGUSER' => 'Heptapod system', hg_env = hg_env_minimal
'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes', hg_env.update({ 'HGUSER' => 'Heptapod system',
'HGRCPATH' => Gitlab::Mercurial.hgrc_path, 'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes' })
}
output, status = popen( output, status = popen(
[Gitlab.config.mercurial.bin_path, [Gitlab.config.mercurial.bin_path,
"debugupgraderepo", "debugupgraderepo",
...@@ -212,10 +207,9 @@ def hg_optim ...@@ -212,10 +207,9 @@ def hg_optim
# Run `hg recover`. # Run `hg recover`.
# This is idempotent and reasonably fast if there's nothing to recover # This is idempotent and reasonably fast if there's nothing to recover
def hg_recover def hg_recover
hg_env = {'HGUSER' => 'Heptapod system', hg_env = hg_env_minimal
'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes', hg_env.update({ 'HGUSER' => 'Heptapod system',
'HGRCPATH' => Gitlab::Mercurial.hgrc_path, 'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes' })
}
output, status = popen( output, status = popen(
[Gitlab.config.mercurial.bin_path, [Gitlab.config.mercurial.bin_path,
"recover", "recover",
...@@ -358,18 +352,17 @@ def hg_git_invalidate_maps! ...@@ -358,18 +352,17 @@ def hg_git_invalidate_maps!
# obtained through `hg share`, hence taking advantage of working dir # obtained through `hg share`, hence taking advantage of working dir
# revisions (`.`) # revisions (`.`)
def hgsha_from_rev(hgrev, path = nil) def hgsha_from_rev(hgrev, path = nil)
hg_env = {'HGRCPATH' => Gitlab::Mercurial.hgrc_path,
}
path = @hgpath if path.nil? path = @hgpath if path.nil?
sha, status = popen([Gitlab.config.mercurial.bin_path, sha, status = popen([Gitlab.config.mercurial.bin_path,
'log', '--limit', '1', '-r', hgrev, 'log', '--limit', '1', '-r', hgrev,
'-T', '{node}'], '-T', '{node}'],
path, hg_env) path, hg_env_minimal)
if status != 0 if status != 0
raise HgRevisionError, raise HgRevisionError,
"Could not find changeset #{hgrev} in #{@hgpath}" "Could not find changeset #{hgrev} in #{@hgpath}"
end end
sha sha
end end
...@@ -378,14 +371,19 @@ def hg_changeset_branch(hgsha) ...@@ -378,14 +371,19 @@ def hg_changeset_branch(hgsha)
out, status = popen([Gitlab.config.mercurial.bin_path, out, status = popen([Gitlab.config.mercurial.bin_path,
'log', '-r', hgsha, '-T', '{branch}'], 'log', '-r', hgsha, '-T', '{branch}'],
@hgpath, @hgpath,
{'HGRCPATH' => Gitlab::Mercurial.hgrc_path, hg_env_minimal)
})
if status != 0 if status != 0
raise HgError, "Could not get hg branch of changeset #{hgsha}: #{out}" raise HgError, "Could not get hg branch of changeset #{hgsha}: #{out}"
end end
out.strip out.strip
end end
def hg_env_minimal
{ 'HGRCPATH' => Gitlab::Mercurial.hgrc_path }
end
# Return a suitable environment for write operations (merge etc.) # Return a suitable environment for write operations (merge etc.)
# #
# This method assumes that write permission is already granted, # This method assumes that write permission is already granted,
...@@ -399,7 +397,8 @@ def hg_env_for_write(user, force_system_user: false) ...@@ -399,7 +397,8 @@ def hg_env_for_write(user, force_system_user: false)
"without force_system_user flag" "without force_system_user flag"
end end
env = { env = hg_env_minimal
env.update({
# `name` and `email` are as in `committer` (which # `name` and `email` are as in `committer` (which
# does not have `username`) but the purpose here is # does not have `username`) but the purpose here is
# different: we want to pass them # different: we want to pass them
...@@ -414,9 +413,8 @@ def hg_env_for_write(user, force_system_user: false) ...@@ -414,9 +413,8 @@ def hg_env_for_write(user, force_system_user: false)
'HEPTAPOD_USERINFO_EMAIL' => user&.email, 'HEPTAPOD_USERINFO_EMAIL' => user&.email,
'HEPTAPOD_HG_NATIVE' => 'no', 'HEPTAPOD_HG_NATIVE' => 'no',
'GL_REPOSITORY' => @gl_repository, 'GL_REPOSITORY' => @gl_repository,
'HGUSER' => nil, 'HGUSER' => nil
'HGRCPATH' => Gitlab::Mercurial.hgrc_path })
}
unless @hg_project_for_perms.nil? unless @hg_project_for_perms.nil?
env.update( env.update(
...@@ -505,7 +503,7 @@ def hg_call(args, user, path, env: nil, for_write: false, ...@@ -505,7 +503,7 @@ def hg_call(args, user, path, env: nil, for_write: false,
if for_write if for_write
env = hg_env_for_write(user, force_system_user: force_system_user) env = hg_env_for_write(user, force_system_user: force_system_user)
else else
env = { 'HGRCPATH' => Gitlab::Mercurial.hgrc_path } env = hg_env_minimal
end end
end end
username = hg_prepare_user_encoding(env, user, "") username = hg_prepare_user_encoding(env, user, "")
...@@ -638,7 +636,7 @@ def hg_tmp_workdir(update_rev, prefix: nil) ...@@ -638,7 +636,7 @@ def hg_tmp_workdir(update_rev, prefix: nil)
begin begin
hg_exe = Gitlab.config.mercurial.bin_path hg_exe = Gitlab.config.mercurial.bin_path
env = { 'HGRCPATH' => Gitlab::Mercurial.hgrc_path } env = hg_env_minimal
output, status = popen([hg_exe, 'share', output, status = popen([hg_exe, 'share',
@hgpath, share_path, @hgpath, share_path,
'--noupdate', '--bookmarks'], '--noupdate', '--bookmarks'],
...@@ -686,8 +684,7 @@ def hg_merge_changeset(user, source_hgsha, target_hgsha, message, ...@@ -686,8 +684,7 @@ def hg_merge_changeset(user, source_hgsha, target_hgsha, message,
hg_exe = Gitlab.config.mercurial.bin_path hg_exe = Gitlab.config.mercurial.bin_path
hg_env = hg_env =
if user.nil? if user.nil?
# TODO time to have a method for minimal hg_env hg_env_minimal
{ 'HGRCPATH' => Gitlab::Mercurial.hgrc_path }
else else
hg_env_for_write(user) hg_env_for_write(user)
end end
...@@ -1101,10 +1098,9 @@ def rebase_in_progress?(rebase_id) ...@@ -1101,10 +1098,9 @@ def rebase_in_progress?(rebase_id)
def pull_force_topic(url, hgsha, topic) def pull_force_topic(url, hgsha, topic)
# necessary to amend changesets # necessary to amend changesets
# TODO do better and think of encoding, as well # TODO do better and think of encoding, as well
hg_env = {'HGUSER' => 'Heptapod system', hg_env = hg_env_minimal
'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes', hg_env.update({ 'HGUSER' => 'Heptapod system',
'HGRCPATH' => Gitlab::Mercurial.hgrc_path, 'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes' })
}
hg_exe = Gitlab.config.mercurial.bin_path hg_exe = Gitlab.config.mercurial.bin_path
_output, status = popen( _output, status = popen(
[hg_exe, [hg_exe,
...@@ -1474,10 +1470,12 @@ def rpath_to_namespace_if_hgrc_included(new_disk_path) ...@@ -1474,10 +1470,12 @@ def rpath_to_namespace_if_hgrc_included(new_disk_path)
HG_CONFIG_TRUE = ["1", "yes", "true", "on"].freeze HG_CONFIG_TRUE = ["1", "yes", "true", "on"].freeze
def get_hg_heptapod_config(local: false) def get_hg_heptapod_config(local: false)
hg_env = {'HGUSER' => 'Heptapod system', hg_env = hg_env_minimal
'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes',
'HGRCPATH' => local ? "" : Gitlab::Mercurial.hgrc_path, hg_env.update({ 'HGUSER' => 'Heptapod system',
} 'HEPTAPOD_SKIP_ALL_GITLAB_HOOKS' => 'yes' })
del hg_env['HGRCPATH'] if local
hg_exe = Gitlab.config.mercurial.bin_path hg_exe = Gitlab.config.mercurial.bin_path
out, status = popen([hg_exe, 'config', 'heptapod'], out, status = popen([hg_exe, 'config', 'heptapod'],
@hgpath, hg_env) @hgpath, hg_env)
......
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