[x-history] Prints CONTROL version history of a port 👻 (#7377)

* [port-history] Print port CONTROL version history

* Add commands.porthistory.cpp to VS project

* Get most recent commit for each version

* Apply clang-format

* Fix output format

* Rename command to x-history
This commit is contained in:
Victor Romero 2019-09-06 11:35:56 -07:00 committed by GitHub
parent 33f15e40e8
commit 84ba23ad33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 102 additions and 0 deletions

View File

@ -100,6 +100,11 @@ namespace vcpkg::Commands
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}
namespace PortHistory
{
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}
namespace Autocomplete
{
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);

View File

@ -47,6 +47,7 @@ namespace vcpkg::Commands
{"autocomplete", &Autocomplete::perform_and_exit},
{"hash", &Hash::perform_and_exit},
{"fetch", &Fetch::perform_and_exit},
{"x-history", &PortHistory::perform_and_exit},
{"x-vsinstances", &X_VSInstances::perform_and_exit},
};
return t;

View File

@ -0,0 +1,91 @@
#include "pch.h"
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/base/system.print.h>
#include <vcpkg/base/system.process.h>
#include <vcpkg/base/util.h>
namespace vcpkg::Commands::PortHistory
{
struct PortControlVersion
{
std::string commit_id;
std::string version;
std::string date;
};
static System::ExitCodeAndOutput run_git_command(const VcpkgPaths& paths, const std::string& cmd)
{
const fs::path& git_exe = paths.get_tool_exe(Tools::GIT);
const fs::path dot_git_dir = paths.root / ".git";
const std::string full_cmd =
Strings::format(R"("%s" --git-dir="%s" %s)", git_exe.u8string(), dot_git_dir.u8string(), cmd);
auto output = System::cmd_execute_and_capture_output(full_cmd);
Checks::check_exit(VCPKG_LINE_INFO, output.exit_code == 0, "Failed to run command: %s", full_cmd);
return output;
}
static std::string get_version_from_commit(const VcpkgPaths& paths,
const std::string& commit_id,
const std::string& port_name)
{
const std::string cmd = Strings::format(R"(show %s:ports/%s/CONTROL)", commit_id, port_name);
auto output = run_git_command(paths, cmd);
const auto version = Strings::find_at_most_one_enclosed(output.output, "Version: ", "\n");
Checks::check_exit(VCPKG_LINE_INFO, version.has_value(), "CONTROL file does not have a 'Version' field");
return version.get()->to_string();
}
static std::vector<PortControlVersion> read_versions_from_log(const VcpkgPaths& paths, const std::string& port_name)
{
const std::string cmd =
Strings::format(R"(log --format="%%H %%cd" --date=short --left-only -- ports/%s/.)", port_name);
auto output = run_git_command(paths, cmd);
auto commits = Util::fmap(
Strings::split(output.output, "\n"), [](const std::string& line) -> auto {
auto parts = Strings::split(line, " ");
return std::make_pair(parts[0], parts[1]);
});
std::vector<PortControlVersion> ret;
std::string last_version;
for (auto&& commit_date_pair : commits)
{
const std::string version = get_version_from_commit(paths, commit_date_pair.first, port_name);
if (last_version != version)
{
ret.emplace_back(PortControlVersion{commit_date_pair.first, version, commit_date_pair.second});
last_version = version;
}
}
return ret;
}
const CommandStructure COMMAND_STRUCTURE = {
Help::create_example_string("history <port>"),
1,
1,
{},
nullptr,
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
std::string port_name = args.command_arguments.at(0);
std::vector<PortControlVersion> versions = read_versions_from_log(paths, port_name);
System::print2(" version date vcpkg commit\n");
for (auto&& version : versions)
{
System::printf("%20.20s %s %s\n", version.version, version.date, version.commit_id);
}
Checks::exit_success(VCPKG_LINE_INFO);
}
}

View File

@ -91,6 +91,7 @@ namespace vcpkg::Help
" vcpkg list List installed packages\n"
" vcpkg update Display list of packages for updating\n"
" vcpkg upgrade Rebuild all outdated packages\n"
" vcpkg history <pkg> Shows the history of CONTROL versions of a package\n"
" vcpkg hash <file> [alg] Hash a file by specific algorithm, default SHA512\n"
" vcpkg help topics Display the list of help topics\n"
" vcpkg help <topic> Display help for a specific topic\n"

View File

@ -238,6 +238,7 @@
<ClCompile Include="..\src\vcpkg\commands.integrate.cpp" />
<ClCompile Include="..\src\vcpkg\commands.list.cpp" />
<ClCompile Include="..\src\vcpkg\commands.owns.cpp" />
<ClCompile Include="..\src\vcpkg\commands.porthistory.cpp" />
<ClCompile Include="..\src\vcpkg\commands.portsdiff.cpp" />
<ClCompile Include="..\src\vcpkg\commands.search.cpp" />
<ClCompile Include="..\src\vcpkg\commands.upgrade.cpp" />

View File

@ -219,6 +219,9 @@
<ClCompile Include="..\src\vcpkg\base\system.print.cpp">
<Filter>Source Files\vcpkg\base</Filter>
</ClCompile>
<ClCompile Include="..\src\vcpkg\commands.porthistory.cpp">
<Filter>Source Files\vcpkg</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\include\pch.h">