Skip to content

Commit

Permalink
x-add-version: Add option to automatically commit result
Browse files Browse the repository at this point in the history
  • Loading branch information
autoantwort committed Jun 14, 2021
1 parent 295e3e7 commit d25433c
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/vcpkg/vcpkgpaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vcpkg/base/lazy.h>
#include <vcpkg/base/optional.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/system.process.h>
#include <vcpkg/base/util.h>

namespace vcpkg
Expand Down Expand Up @@ -151,6 +152,10 @@ namespace vcpkg
StringView git_tree,
const fs::path& dot_git_dir) const;
ExpectedS<std::string> git_show(const std::string& treeish, const fs::path& dot_git_dir) const;
System::ExitCodeAndOutput git_commit(const fs::path& dot_git_dir,
std::vector<fs::path>&& files,
const std::string& message,
bool amend) const;

const Downloads::DownloadManager& get_download_manager() const;

Expand Down
51 changes: 50 additions & 1 deletion src/vcpkg/commands.add-version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,29 @@ namespace vcpkg::Commands::AddVersion
static constexpr StringLiteral OPTION_ALL = "all";
static constexpr StringLiteral OPTION_OVERWRITE_VERSION = "overwrite-version";
static constexpr StringLiteral OPTION_SKIP_FORMATTING_CHECK = "skip-formatting-check";
static constexpr StringLiteral OPTION_COMMIT = "commit";
static constexpr StringLiteral OPTION_COMMIT_AMEND = "amend";
static constexpr StringLiteral OPTION_COMMIT_MESSAGE = "commit-message";
static constexpr StringLiteral OPTION_VERBOSE = "verbose";

const CommandSwitch COMMAND_SWITCHES[] = {
{OPTION_ALL, "Process versions for all ports."},
{OPTION_OVERWRITE_VERSION, "Overwrite `git-tree` of an existing version."},
{OPTION_SKIP_FORMATTING_CHECK, "Skips the formatting check of vcpkg.json files."},
{OPTION_COMMIT, "Commits the results."},
{OPTION_COMMIT_AMEND, "Amend the result to the last commit instead of creating a new one."},
{OPTION_VERBOSE, "Print success messages instead of just errors."},
};

const CommandSetting COMMAND_SETTINGS[] = {
{OPTION_COMMIT_MESSAGE, "The commit message when creating a new commit."},
};

const CommandStructure COMMAND_STRUCTURE{
create_example_string(R"###(x-add-version <port name>)###"),
0,
1,
{{COMMAND_SWITCHES}, {}, {}},
{{COMMAND_SWITCHES}, {COMMAND_SETTINGS}, {}},
nullptr,
};

Expand All @@ -290,6 +299,29 @@ namespace vcpkg::Commands::AddVersion
const bool overwrite_version = Util::Sets::contains(parsed_args.switches, OPTION_OVERWRITE_VERSION);
const bool skip_formatting_check = Util::Sets::contains(parsed_args.switches, OPTION_SKIP_FORMATTING_CHECK);
const bool verbose = Util::Sets::contains(parsed_args.switches, OPTION_VERBOSE);
const bool commit = Util::Sets::contains(parsed_args.switches, OPTION_COMMIT);
const bool amend = Util::Sets::contains(parsed_args.switches, OPTION_COMMIT_AMEND);

std::optional<std::string> commit_message;
const auto iter_commit_message = parsed_args.settings.find(OPTION_COMMIT_MESSAGE);
if (iter_commit_message != parsed_args.settings.end())
{
commit_message.emplace(iter_commit_message->second);
if (commit_message.value().empty())
{
System::print2(System::Color::error, "Error: The specified commit message must be not empty.\n.");
Checks::exit_fail(VCPKG_LINE_INFO);
}
}

if ((amend || commit_message) && !commit)
{
System::printf(System::Color::warning,
"Warning: `--%s` or `--%s` was specified, assuming `--%s`\n",
OPTION_COMMIT_AMEND,
OPTION_COMMIT_MESSAGE,
OPTION_COMMIT);
}

auto& fs = paths.get_filesystem();
auto baseline_path = paths.current_registry_versions / fs::u8path("baseline.json");
Expand Down Expand Up @@ -341,6 +373,7 @@ namespace vcpkg::Commands::AddVersion
// Get tree-ish from local repository state.
auto maybe_git_tree_map = paths.git_get_port_treeish_map(paths.current_registry_ports);
auto git_tree_map = maybe_git_tree_map.value_or_exit(VCPKG_LINE_INFO);
std::vector<fs::path> updated_files;

for (auto&& port_name : port_names)
{
Expand Down Expand Up @@ -394,10 +427,26 @@ namespace vcpkg::Commands::AddVersion

auto port_versions_path = paths.current_registry_versions / fs::u8path({port_name[0], '-'}) /
fs::u8path(Strings::concat(port_name, ".json"));
updated_files.push_back(port_versions_path);
update_version_db_file(
paths, port_name, schemed_version, git_tree, port_versions_path, overwrite_version, verbose, add_all);
update_baseline_version(paths, port_name, schemed_version.versiont, baseline_path, baseline_map, verbose);
}
if (!updated_files.empty()) updated_files.push_back(baseline_path);
if (commit)
{
const auto result = paths.git_commit(paths.current_registry_dot_git_dir,
std::move(updated_files),
commit_message.value_or(amend ? "" : "Add version files"),
amend);
if (result.exit_code != 0)
{
System::printf(System::Color::error,
"Error: Failed to commit the changes. The git output is: %s\n",
result.output);
Checks::exit_fail(VCPKG_LINE_INFO);
}
}
Checks::exit_success(VCPKG_LINE_INFO);
}

Expand Down
37 changes: 36 additions & 1 deletion src/vcpkg/vcpkgpaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <vcpkg/base/hash.h>
#include <vcpkg/base/jsonreader.h>
#include <vcpkg/base/system.debug.h>
#include <vcpkg/base/system.process.h>
#include <vcpkg/base/util.h>

#include <vcpkg/binarycaching.h>
Expand Down Expand Up @@ -749,6 +748,42 @@ namespace vcpkg
}
}

System::ExitCodeAndOutput VcpkgPaths::git_commit(const fs::path& dot_git_dir,
std::vector<fs::path>&& files,
const std::string& message,
bool amend) const
{
for (auto& path : files)
path = get_filesystem().relative(VCPKG_LINE_INFO, path, dot_git_dir.parent_path());
System::Command add_cmd = git_cmd_builder(dot_git_dir, dot_git_dir.parent_path())
.string_arg("add")
.string_arg("--force")
.string_arg("--");
for (const auto& path : files)
add_cmd.path_arg(path);
const auto result = System::cmd_execute_and_capture_output(add_cmd);
if (result.exit_code != 0) return result;

System::Command commit_cmd = git_cmd_builder(dot_git_dir, dot_git_dir.parent_path()).string_arg("commit");
if (amend)
{
commit_cmd.string_arg("--amend");
}
if (!message.empty())
{
commit_cmd.string_arg("-m").string_arg(message);
}
else if (amend)
{
commit_cmd.string_arg("--no-edit");
}
commit_cmd.string_arg("--");
for (const auto& path : files)
commit_cmd.path_arg(path);

return System::cmd_execute_and_capture_output(commit_cmd);
}

ExpectedS<std::map<std::string, std::string, std::less<>>> VcpkgPaths::git_get_port_treeish_map(
const fs::path& ports_dir) const
{
Expand Down

0 comments on commit d25433c

Please sign in to comment.