12
12
#include < vcpkg/vcpkgpaths.h>
13
13
#include < vcpkg/versions.h>
14
14
15
+ #include < optional>
16
+
15
17
using namespace vcpkg ;
16
18
17
19
namespace
@@ -266,20 +268,29 @@ namespace vcpkg::Commands::AddVersion
266
268
static constexpr StringLiteral OPTION_ALL = " all" ;
267
269
static constexpr StringLiteral OPTION_OVERWRITE_VERSION = " overwrite-version" ;
268
270
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" ;
269
274
static constexpr StringLiteral OPTION_VERBOSE = " verbose" ;
270
275
271
276
const CommandSwitch COMMAND_SWITCHES[] = {
272
277
{OPTION_ALL, " Process versions for all ports." },
273
278
{OPTION_OVERWRITE_VERSION, " Overwrite `git-tree` of an existing version." },
274
279
{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." },
275
282
{OPTION_VERBOSE, " Print success messages instead of just errors." },
276
283
};
277
284
285
+ const CommandSetting COMMAND_SETTINGS[] = {
286
+ {OPTION_COMMIT_MESSAGE, " The commit message when creating a new commit." },
287
+ };
288
+
278
289
const CommandStructure COMMAND_STRUCTURE{
279
290
create_example_string (R"###( x-add-version <port name>)###" ),
280
291
0 ,
281
292
1 ,
282
- {{COMMAND_SWITCHES}, {}, {}},
293
+ {{COMMAND_SWITCHES}, {COMMAND_SETTINGS }, {}},
283
294
nullptr ,
284
295
};
285
296
@@ -290,6 +301,29 @@ namespace vcpkg::Commands::AddVersion
290
301
const bool overwrite_version = Util::Sets::contains (parsed_args.switches , OPTION_OVERWRITE_VERSION);
291
302
const bool skip_formatting_check = Util::Sets::contains (parsed_args.switches , OPTION_SKIP_FORMATTING_CHECK);
292
303
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
+ }
293
327
294
328
auto & fs = paths.get_filesystem ();
295
329
auto baseline_path = paths.current_registry_versions / fs::u8path (" baseline.json" );
@@ -341,6 +375,7 @@ namespace vcpkg::Commands::AddVersion
341
375
// Get tree-ish from local repository state.
342
376
auto maybe_git_tree_map = paths.git_get_port_treeish_map (paths.current_registry_ports );
343
377
auto git_tree_map = maybe_git_tree_map.value_or_exit (VCPKG_LINE_INFO);
378
+ std::vector<fs::path> updated_files;
344
379
345
380
for (auto && port_name : port_names)
346
381
{
@@ -394,10 +429,26 @@ namespace vcpkg::Commands::AddVersion
394
429
395
430
auto port_versions_path = paths.current_registry_versions / fs::u8path ({port_name[0 ], ' -' }) /
396
431
fs::u8path (Strings::concat (port_name, " .json" ));
432
+ updated_files.push_back (port_versions_path);
397
433
update_version_db_file (
398
434
paths, port_name, schemed_version, git_tree, port_versions_path, overwrite_version, verbose, add_all);
399
435
update_baseline_version (paths, port_name, schemed_version.versiont , baseline_path, baseline_map, verbose);
400
436
}
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
+ }
401
452
Checks::exit_success (VCPKG_LINE_INFO);
402
453
}
403
454
0 commit comments