diff --git a/include/vcpkg/base/fwd/system.process.h b/include/vcpkg/base/fwd/system.process.h new file mode 100644 index 0000000000..2eaf78acd4 --- /dev/null +++ b/include/vcpkg/base/fwd/system.process.h @@ -0,0 +1,8 @@ +#pragma once + +namespace vcpkg::System +{ + struct CMakeVariable; + struct Command; + struct CommandLess; +} diff --git a/include/vcpkg/base/system.process.h b/include/vcpkg/base/system.process.h index 5b6f8cd558..bbf332a75b 100644 --- a/include/vcpkg/base/system.process.h +++ b/include/vcpkg/base/system.process.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include diff --git a/include/vcpkg/vcpkgpaths.h b/include/vcpkg/vcpkgpaths.h index 505015a53b..f43485518b 100644 --- a/include/vcpkg/vcpkgpaths.h +++ b/include/vcpkg/vcpkgpaths.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -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 get_current_git_sha() const; std::string get_current_git_sha_message() const; diff --git a/src/vcpkg/commands.porthistory.cpp b/src/vcpkg/commands.porthistory.cpp index 7d7fcf3d56..0fe0caa962 100644 --- a/src/vcpkg/commands.porthistory.cpp +++ b/src/vcpkg/commands.porthistory.cpp @@ -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 get_version_from_text(const std::string& text, diff --git a/src/vcpkg/commands.portsdiff.cpp b/src/vcpkg/commands.portsdiff.cpp index 18e3a9d910..f40f193c4d 100644 --- a/src/vcpkg/commands.portsdiff.cpp +++ b/src/vcpkg/commands.portsdiff.cpp @@ -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 = @@ -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") @@ -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 names_and_versions; for (auto&& port : ports_at_commit) @@ -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); @@ -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 current_names_and_versions = read_ports_from_commit(paths, git_commit_id_for_current_snapshot); diff --git a/src/vcpkg/vcpkgpaths.cpp b/src/vcpkg/vcpkgpaths.cpp index c4af929c0a..d1ac80a744 100644 --- a/src/vcpkg/vcpkgpaths.cpp +++ b/src/vcpkg/vcpkgpaths.cpp @@ -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 @@ -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, @@ -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") @@ -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("--") @@ -588,7 +590,7 @@ If you wish to silence this error and use classic mode, you can: ExpectedS 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) @@ -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) @@ -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") @@ -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") @@ -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) { @@ -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("--") @@ -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) { @@ -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) @@ -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); @@ -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")