Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/vcpkg/base/fwd/system.process.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace vcpkg::System
{
struct CMakeVariable;
struct Command;
struct CommandLess;
}
2 changes: 2 additions & 0 deletions include/vcpkg/base/system.process.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <vcpkg/base/fwd/system.process.h>

#include <vcpkg/base/files.h>
#include <vcpkg/base/zstringview.h>

Expand Down
3 changes: 3 additions & 0 deletions include/vcpkg/vcpkgpaths.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <vcpkg/base/fwd/json.h>
#include <vcpkg/base/fwd/system.process.h>

#include <vcpkg/fwd/configuration.h>
#include <vcpkg/fwd/registries.h>
Expand Down Expand Up @@ -118,6 +119,8 @@ namespace vcpkg
const fs::path& get_tool_exe(const std::string& tool) const;
const std::string& get_tool_version(const std::string& tool) const;

System::Command git_cmd_builder(const fs::path& dot_git_dir, const fs::path& work_tree) const;

// Git manipulation in the vcpkg directory
ExpectedS<std::string> get_current_git_sha() const;
std::string get_current_git_sha_message() const;
Expand Down
23 changes: 3 additions & 20 deletions src/vcpkg/commands.porthistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,11 @@ namespace vcpkg::Commands::PortHistory
Versions::Scheme scheme;
};

const System::ExitCodeAndOutput run_git_command_inner(const VcpkgPaths& paths,
const fs::path& dot_git_directory,
const fs::path& working_directory,
const System::Command& cmd)
System::ExitCodeAndOutput run_git_command(const VcpkgPaths& paths, const System::Command& cmd)
{
const fs::path& git_exe = paths.get_tool_exe(Tools::GIT);
auto full_cmd = paths.git_cmd_builder(paths.root / ".git", paths.root).raw_arg(cmd.command_line());

auto full_cmd = System::Command(git_exe)
.string_arg(Strings::concat("--git-dir=", fs::u8string(dot_git_directory)))
.string_arg(Strings::concat("--work-tree=", fs::u8string(working_directory)))
.raw_arg(cmd.command_line());

auto output = System::cmd_execute_and_capture_output(full_cmd);
return output;
}

const System::ExitCodeAndOutput run_git_command(const VcpkgPaths& paths, const System::Command& cmd)
{
const fs::path& work_dir = paths.root;
const fs::path dot_git_dir = paths.root / ".git";

return run_git_command_inner(paths, dot_git_dir, work_dir, cmd);
return System::cmd_execute_and_capture_output(full_cmd);
}

vcpkg::Optional<HistoryVersion> get_version_from_text(const std::string& text,
Expand Down
22 changes: 11 additions & 11 deletions src/vcpkg/commands.portsdiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ namespace vcpkg::Commands::PortsDiff
{
std::error_code ec;
auto& fs = paths.get_filesystem();
const fs::path& git_exe = paths.get_tool_exe(Tools::GIT);
const fs::path dot_git_dir = paths.root / ".git";
const std::string ports_dir_name_as_string = fs::u8string(paths.builtin_ports_directory().filename());
const fs::path temp_checkout_path =
Expand All @@ -91,9 +90,7 @@ namespace vcpkg::Commands::PortsDiff
const auto checkout_this_dir =
Strings::format(R"(.\%s)", ports_dir_name_as_string); // Must be relative to the root of the repository

auto cmd = System::Command(git_exe)
.string_arg(Strings::format("--git-dir=%s", fs::u8string(dot_git_dir)))
.string_arg(Strings::format("--work-tree=%s", fs::u8string(temp_checkout_path)))
auto cmd = paths.git_cmd_builder(dot_git_dir, temp_checkout_path)
.string_arg("checkout")
.string_arg(git_commit_id)
.string_arg("-f")
Expand All @@ -102,8 +99,9 @@ namespace vcpkg::Commands::PortsDiff
.string_arg(checkout_this_dir)
.string_arg(".vcpkg-root");
System::cmd_execute_and_capture_output(cmd, System::get_clean_environment());
System::cmd_execute_and_capture_output(System::Command(git_exe).string_arg("reset"),
System::get_clean_environment());
System::cmd_execute_and_capture_output(
paths.git_cmd_builder(dot_git_dir, temp_checkout_path).string_arg("reset"),
System::get_clean_environment());
const auto ports_at_commit = Paragraphs::load_overlay_ports(fs, temp_checkout_path / ports_dir_name_as_string);
std::map<std::string, VersionT> names_and_versions;
for (auto&& port : ports_at_commit)
Expand All @@ -115,11 +113,14 @@ namespace vcpkg::Commands::PortsDiff
return names_and_versions;
}

static void check_commit_exists(const fs::path& git_exe, const std::string& git_commit_id)
static void check_commit_exists(const VcpkgPaths& paths, const std::string& git_commit_id)
{
static const std::string VALID_COMMIT_OUTPUT = "commit\n";

auto cmd = System::Command(git_exe).string_arg("cat-file").string_arg("-t").string_arg(git_commit_id);
auto cmd = paths.git_cmd_builder(paths.root / ".git", paths.root)
.string_arg("cat-file")
.string_arg("-t")
.string_arg(git_commit_id);
const System::ExitCodeAndOutput output = System::cmd_execute_and_capture_output(cmd);
Checks::check_exit(
VCPKG_LINE_INFO, output.output == VALID_COMMIT_OUTPUT, "Invalid commit id %s", git_commit_id);
Expand All @@ -137,14 +138,13 @@ namespace vcpkg::Commands::PortsDiff
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
(void)args.parse_arguments(COMMAND_STRUCTURE);
const fs::path& git_exe = paths.get_tool_exe(Tools::GIT);

const std::string git_commit_id_for_previous_snapshot = args.command_arguments.at(0);
const std::string git_commit_id_for_current_snapshot =
args.command_arguments.size() < 2 ? "HEAD" : args.command_arguments.at(1);

check_commit_exists(git_exe, git_commit_id_for_current_snapshot);
check_commit_exists(git_exe, git_commit_id_for_previous_snapshot);
check_commit_exists(paths, git_commit_id_for_current_snapshot);
check_commit_exists(paths, git_commit_id_for_previous_snapshot);

const std::map<std::string, VersionT> current_names_and_versions =
read_ports_from_commit(paths, git_commit_id_for_current_snapshot);
Expand Down
48 changes: 23 additions & 25 deletions src/vcpkg/vcpkgpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ namespace
return result;
}

System::Command git_cmd_builder(const VcpkgPaths& paths, const fs::path& dot_git_dir, const fs::path& work_tree)
{
return System::Command()
.path_arg(paths.get_tool_exe(Tools::GIT))
.string_arg(Strings::concat("--git-dir=", fs::u8string(dot_git_dir)))
.string_arg(Strings::concat("--work-tree=", fs::u8string(work_tree)));
}
} // unnamed namespace

namespace vcpkg
Expand Down Expand Up @@ -522,6 +515,15 @@ If you wish to silence this error and use classic mode, you can:
return m_pimpl->m_tool_cache->get_tool_version(*this, tool);
}

System::Command VcpkgPaths::git_cmd_builder(const fs::path& dot_git_dir, const fs::path& work_tree) const
{
return System::Command(get_tool_exe(Tools::GIT))
.string_arg(Strings::concat("--git-dir=", fs::u8string(dot_git_dir)))
.string_arg(Strings::concat("--work-tree=", fs::u8string(work_tree)))
.string_arg("-c")
.string_arg("core.autocrlf=false");
}

void VcpkgPaths::git_checkout_subpath(const VcpkgPaths& paths,
StringView commit_sha,
const fs::path& subpath,
Expand All @@ -538,7 +540,7 @@ If you wish to silence this error and use classic mode, you can:
// All git commands are run with: --git-dir={dot_git_dir} --work-tree={work_tree_temp}
// git clone --no-checkout --local --no-hardlinks {vcpkg_root} {dot_git_dir}
// note that `--no-hardlinks` is added because otherwise, git fails to clone in some cases
System::Command clone_cmd_builder = git_cmd_builder(paths, dot_git_dir, work_tree)
System::Command clone_cmd_builder = paths.git_cmd_builder(dot_git_dir, work_tree)
.string_arg("clone")
.string_arg("--no-checkout")
.string_arg("--local")
Expand All @@ -552,7 +554,7 @@ If you wish to silence this error and use classic mode, you can:
clone_output.output);

// git checkout {commit-sha} -- {subpath}
System::Command checkout_cmd_builder = git_cmd_builder(paths, dot_git_dir, work_tree)
System::Command checkout_cmd_builder = paths.git_cmd_builder(dot_git_dir, work_tree)
.string_arg("checkout")
.string_arg(commit_sha)
.string_arg("--")
Expand Down Expand Up @@ -588,7 +590,7 @@ If you wish to silence this error and use classic mode, you can:

ExpectedS<std::string> VcpkgPaths::get_current_git_sha() const
{
auto cmd = git_cmd_builder(*this, this->root / fs::u8path(".git"), this->root);
auto cmd = git_cmd_builder(this->root / fs::u8path(".git"), this->root);
cmd.string_arg("rev-parse").string_arg("HEAD");
auto output = System::cmd_execute_and_capture_output(cmd);
if (output.exit_code != 0)
Expand Down Expand Up @@ -617,8 +619,7 @@ If you wish to silence this error and use classic mode, you can:
{
// All git commands are run with: --git-dir={dot_git_dir} --work-tree={work_tree_temp}
// git clone --no-checkout --local {vcpkg_root} {dot_git_dir}
System::Command showcmd =
git_cmd_builder(*this, dot_git_dir, dot_git_dir).string_arg("show").string_arg(treeish);
System::Command showcmd = git_cmd_builder(dot_git_dir, dot_git_dir).string_arg("show").string_arg(treeish);

auto output = System::cmd_execute_and_capture_output(showcmd);
if (output.exit_code == 0)
Expand All @@ -636,7 +637,7 @@ If you wish to silence this error and use classic mode, you can:
const auto local_repo = this->root / fs::u8path(".git");
const auto path_with_separator =
Strings::concat(fs::u8string(this->builtin_ports_directory()), Files::preferred_separator);
const auto git_cmd = git_cmd_builder(*this, local_repo, this->root)
const auto git_cmd = git_cmd_builder(local_repo, this->root)
.string_arg("ls-tree")
.string_arg("-d")
.string_arg("HEAD")
Expand Down Expand Up @@ -773,9 +774,7 @@ If you wish to silence this error and use classic mode, you can:
expected_right_tag};
}

auto tar_cmd_builder = git_cmd_builder(*this, dot_git_dir, dot_git_dir)
.string_arg("-c")
.string_arg("core.autocrlf=false")
auto tar_cmd_builder = git_cmd_builder(dot_git_dir, dot_git_dir)
.string_arg("archive")
.string_arg(git_tree)
.string_arg("-o")
Expand Down Expand Up @@ -832,7 +831,7 @@ If you wish to silence this error and use classic mode, you can:
fs.create_directories(work_tree, VCPKG_LINE_INFO);
auto dot_git_dir = m_pimpl->registries_dot_git_dir;

System::Command init_registries_git_dir = git_cmd_builder(*this, dot_git_dir, work_tree).string_arg("init");
System::Command init_registries_git_dir = git_cmd_builder(dot_git_dir, work_tree).string_arg("init");
auto init_output = System::cmd_execute_and_capture_output(init_registries_git_dir);
if (init_output.exit_code != 0)
{
Expand All @@ -847,7 +846,7 @@ If you wish to silence this error and use classic mode, you can:
std::error_code ec;
Files::ExclusiveFileLock guard(Files::ExclusiveFileLock::Wait::Yes, fs, lock_file, ec);

System::Command fetch_git_ref = git_cmd_builder(*this, dot_git_dir, work_tree)
System::Command fetch_git_ref = git_cmd_builder(dot_git_dir, work_tree)
.string_arg("fetch")
.string_arg("--update-shallow")
.string_arg("--")
Expand All @@ -869,7 +868,7 @@ If you wish to silence this error and use classic mode, you can:
}

System::Command get_fetch_head =
git_cmd_builder(*this, dot_git_dir, work_tree).string_arg("rev-parse").string_arg("FETCH_HEAD");
git_cmd_builder(dot_git_dir, work_tree).string_arg("rev-parse").string_arg("FETCH_HEAD");
auto fetch_head_output = System::cmd_execute_and_capture_output(get_fetch_head);
if (fetch_head_output.exit_code != 0)
{
Expand All @@ -884,10 +883,9 @@ If you wish to silence this error and use classic mode, you can:
const fs::path& relative_path) const
{
auto revision = Strings::format("%s:%s", hash, fs::generic_u8string(relative_path));
System::Command git_show =
git_cmd_builder(*this, m_pimpl->registries_dot_git_dir, m_pimpl->registries_work_tree_dir)
.string_arg("show")
.string_arg(revision);
System::Command git_show = git_cmd_builder(m_pimpl->registries_dot_git_dir, m_pimpl->registries_work_tree_dir)
.string_arg("show")
.string_arg(revision);

auto git_show_output = System::cmd_execute_and_capture_output(git_show);
if (git_show_output.exit_code != 0)
Expand All @@ -901,7 +899,7 @@ If you wish to silence this error and use classic mode, you can:
{
auto revision = Strings::format("%s:%s", hash, fs::generic_u8string(relative_path));
System::Command git_rev_parse =
git_cmd_builder(*this, m_pimpl->registries_dot_git_dir, m_pimpl->registries_work_tree_dir)
git_cmd_builder(m_pimpl->registries_dot_git_dir, m_pimpl->registries_work_tree_dir)
.string_arg("rev-parse")
.string_arg(revision);

Expand Down Expand Up @@ -931,7 +929,7 @@ If you wish to silence this error and use classic mode, you can:
fs.create_directory(git_tree_temp, VCPKG_LINE_INFO);

auto dot_git_dir = m_pimpl->registries_dot_git_dir;
System::Command git_archive = git_cmd_builder(*this, dot_git_dir, m_pimpl->registries_work_tree_dir)
System::Command git_archive = git_cmd_builder(dot_git_dir, m_pimpl->registries_work_tree_dir)
.string_arg("archive")
.string_arg("--format")
.string_arg("tar")
Expand Down