Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Sep 6, 2019
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
5 changes: 5 additions & 0 deletions toolsrc/include/vcpkg/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions toolsrc/src/vcpkg/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
91 changes: 91 additions & 0 deletions toolsrc/src/vcpkg/commands.porthistory.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions toolsrc/src/vcpkg/help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! @vicroms It seems that history here should also be changed to x-history?

" 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"
Expand Down
1 change: 1 addition & 0 deletions toolsrc/vcpkglib/vcpkglib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
3 changes: 3 additions & 0 deletions toolsrc/vcpkglib/vcpkglib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down