Skip to content

Commit d25433c

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

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-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: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,20 +266,29 @@ namespace vcpkg::Commands::AddVersion
266266
static constexpr StringLiteral OPTION_ALL = "all";
267267
static constexpr StringLiteral OPTION_OVERWRITE_VERSION = "overwrite-version";
268268
static constexpr StringLiteral OPTION_SKIP_FORMATTING_CHECK = "skip-formatting-check";
269+
static constexpr StringLiteral OPTION_COMMIT = "commit";
270+
static constexpr StringLiteral OPTION_COMMIT_AMEND = "amend";
271+
static constexpr StringLiteral OPTION_COMMIT_MESSAGE = "commit-message";
269272
static constexpr StringLiteral OPTION_VERBOSE = "verbose";
270273

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

283+
const CommandSetting COMMAND_SETTINGS[] = {
284+
{OPTION_COMMIT_MESSAGE, "The commit message when creating a new commit."},
285+
};
286+
278287
const CommandStructure COMMAND_STRUCTURE{
279288
create_example_string(R"###(x-add-version <port name>)###"),
280289
0,
281290
1,
282-
{{COMMAND_SWITCHES}, {}, {}},
291+
{{COMMAND_SWITCHES}, {COMMAND_SETTINGS}, {}},
283292
nullptr,
284293
};
285294

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

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

345378
for (auto&& port_name : port_names)
346379
{
@@ -394,10 +427,26 @@ namespace vcpkg::Commands::AddVersion
394427

395428
auto port_versions_path = paths.current_registry_versions / fs::u8path({port_name[0], '-'}) /
396429
fs::u8path(Strings::concat(port_name, ".json"));
430+
updated_files.push_back(port_versions_path);
397431
update_version_db_file(
398432
paths, port_name, schemed_version, git_tree, port_versions_path, overwrite_version, verbose, add_all);
399433
update_baseline_version(paths, port_name, schemed_version.versiont, baseline_path, baseline_map, verbose);
400434
}
435+
if (!updated_files.empty()) updated_files.push_back(baseline_path);
436+
if (commit)
437+
{
438+
const auto result = paths.git_commit(paths.current_registry_dot_git_dir,
439+
std::move(updated_files),
440+
commit_message.value_or(amend ? "" : "Add version files"),
441+
amend);
442+
if (result.exit_code != 0)
443+
{
444+
System::printf(System::Color::error,
445+
"Error: Failed to commit the changes. The git output is: %s\n",
446+
result.output);
447+
Checks::exit_fail(VCPKG_LINE_INFO);
448+
}
449+
}
401450
Checks::exit_success(VCPKG_LINE_INFO);
402451
}
403452

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)