Skip to content

Commit 24aac98

Browse files
committed
x-add-version: Add option to automatically commit result
1 parent 295e3e7 commit 24aac98

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

include/vcpkg/vcpkgpaths.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <vcpkg/base/lazy.h>
1414
#include <vcpkg/base/optional.h>
1515
#include <vcpkg/base/system.h>
16+
#include <vcpkg/base/system.process.h>
1617
#include <vcpkg/base/util.h>
1718

1819
namespace vcpkg
@@ -151,6 +152,10 @@ namespace vcpkg
151152
StringView git_tree,
152153
const fs::path& dot_git_dir) const;
153154
ExpectedS<std::string> git_show(const std::string& treeish, const fs::path& dot_git_dir) const;
155+
System::ExitCodeAndOutput git_commit(const fs::path& dot_git_dir,
156+
std::vector<fs::path>&& files,
157+
const std::string& message,
158+
bool amend) const;
154159

155160
const Downloads::DownloadManager& get_download_manager() const;
156161

src/vcpkg/commands.add-version.cpp

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <vcpkg/vcpkgpaths.h>
1313
#include <vcpkg/versions.h>
1414

15+
#include <optional>
16+
1517
using namespace vcpkg;
1618

1719
namespace
@@ -266,20 +268,29 @@ namespace vcpkg::Commands::AddVersion
266268
static constexpr StringLiteral OPTION_ALL = "all";
267269
static constexpr StringLiteral OPTION_OVERWRITE_VERSION = "overwrite-version";
268270
static constexpr StringLiteral OPTION_SKIP_FORMATTING_CHECK = "skip-formatting-check";
271+
static constexpr StringLiteral OPTION_COMMIT = "commit";
272+
static constexpr StringLiteral OPTION_COMMIT_AMEND = "amend";
273+
static constexpr StringLiteral OPTION_COMMIT_MESSAGE = "commit-message";
269274
static constexpr StringLiteral OPTION_VERBOSE = "verbose";
270275

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

285+
const CommandSetting COMMAND_SETTINGS[] = {
286+
{OPTION_COMMIT_MESSAGE, "The commit message when creating a new commit."},
287+
};
288+
278289
const CommandStructure COMMAND_STRUCTURE{
279290
create_example_string(R"###(x-add-version <port name>)###"),
280291
0,
281292
1,
282-
{{COMMAND_SWITCHES}, {}, {}},
293+
{{COMMAND_SWITCHES}, {COMMAND_SETTINGS}, {}},
283294
nullptr,
284295
};
285296

@@ -290,6 +301,29 @@ namespace vcpkg::Commands::AddVersion
290301
const bool overwrite_version = Util::Sets::contains(parsed_args.switches, OPTION_OVERWRITE_VERSION);
291302
const bool skip_formatting_check = Util::Sets::contains(parsed_args.switches, OPTION_SKIP_FORMATTING_CHECK);
292303
const bool verbose = Util::Sets::contains(parsed_args.switches, OPTION_VERBOSE);
304+
const bool commit = Util::Sets::contains(parsed_args.switches, OPTION_COMMIT);
305+
const bool amend = Util::Sets::contains(parsed_args.switches, OPTION_COMMIT_AMEND);
306+
307+
std::optional<std::string> commit_message;
308+
const auto iter_commit_message = parsed_args.settings.find(OPTION_COMMIT_MESSAGE);
309+
if (iter_commit_message != parsed_args.settings.end())
310+
{
311+
commit_message.emplace(iter_commit_message->second);
312+
if (commit_message.value().empty())
313+
{
314+
System::print2(System::Color::error, "Error: The specified commit message must be not empty.\n.");
315+
Checks::exit_fail(VCPKG_LINE_INFO);
316+
}
317+
}
318+
319+
if ((amend || commit_message) && !commit)
320+
{
321+
System::printf(System::Color::warning,
322+
"Warning: `--%s` or `--%s` was specified, assuming `--%s`\n",
323+
OPTION_COMMIT_AMEND,
324+
OPTION_COMMIT_MESSAGE,
325+
OPTION_COMMIT);
326+
}
293327

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

345380
for (auto&& port_name : port_names)
346381
{
@@ -394,10 +429,26 @@ namespace vcpkg::Commands::AddVersion
394429

395430
auto port_versions_path = paths.current_registry_versions / fs::u8path({port_name[0], '-'}) /
396431
fs::u8path(Strings::concat(port_name, ".json"));
432+
updated_files.push_back(port_versions_path);
397433
update_version_db_file(
398434
paths, port_name, schemed_version, git_tree, port_versions_path, overwrite_version, verbose, add_all);
399435
update_baseline_version(paths, port_name, schemed_version.versiont, baseline_path, baseline_map, verbose);
400436
}
437+
if (!updated_files.empty()) updated_files.push_back(baseline_path);
438+
if (commit)
439+
{
440+
const auto result = paths.git_commit(paths.current_registry_dot_git_dir,
441+
std::move(updated_files),
442+
commit_message.value_or(amend ? "" : "Add version files"),
443+
amend);
444+
if (result.exit_code != 0)
445+
{
446+
System::printf(System::Color::error,
447+
"Error: Failed to commit the changes. The git output is: %s\n",
448+
result.output);
449+
Checks::exit_fail(VCPKG_LINE_INFO);
450+
}
451+
}
401452
Checks::exit_success(VCPKG_LINE_INFO);
402453
}
403454

src/vcpkg/vcpkgpaths.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <vcpkg/base/hash.h>
55
#include <vcpkg/base/jsonreader.h>
66
#include <vcpkg/base/system.debug.h>
7-
#include <vcpkg/base/system.process.h>
87
#include <vcpkg/base/util.h>
98

109
#include <vcpkg/binarycaching.h>
@@ -749,6 +748,42 @@ namespace vcpkg
749748
}
750749
}
751750

751+
System::ExitCodeAndOutput VcpkgPaths::git_commit(const fs::path& dot_git_dir,
752+
std::vector<fs::path>&& files,
753+
const std::string& message,
754+
bool amend) const
755+
{
756+
for (auto& path : files)
757+
path = get_filesystem().relative(VCPKG_LINE_INFO, path, dot_git_dir.parent_path());
758+
System::Command add_cmd = git_cmd_builder(dot_git_dir, dot_git_dir.parent_path())
759+
.string_arg("add")
760+
.string_arg("--force")
761+
.string_arg("--");
762+
for (const auto& path : files)
763+
add_cmd.path_arg(path);
764+
const auto result = System::cmd_execute_and_capture_output(add_cmd);
765+
if (result.exit_code != 0) return result;
766+
767+
System::Command commit_cmd = git_cmd_builder(dot_git_dir, dot_git_dir.parent_path()).string_arg("commit");
768+
if (amend)
769+
{
770+
commit_cmd.string_arg("--amend");
771+
}
772+
if (!message.empty())
773+
{
774+
commit_cmd.string_arg("-m").string_arg(message);
775+
}
776+
else if (amend)
777+
{
778+
commit_cmd.string_arg("--no-edit");
779+
}
780+
commit_cmd.string_arg("--");
781+
for (const auto& path : files)
782+
commit_cmd.path_arg(path);
783+
784+
return System::cmd_execute_and_capture_output(commit_cmd);
785+
}
786+
752787
ExpectedS<std::map<std::string, std::string, std::less<>>> VcpkgPaths::git_get_port_treeish_map(
753788
const fs::path& ports_dir) const
754789
{

0 commit comments

Comments
 (0)