From e4cab414aee77b03140d0198ce8c0756c46b5c0a Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Sun, 6 Nov 2016 20:12:54 -0800 Subject: Add new command: portsdiff --- toolsrc/src/commands_portsdiff.cpp | 155 +++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 toolsrc/src/commands_portsdiff.cpp (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp new file mode 100644 index 000000000..e15db3567 --- /dev/null +++ b/toolsrc/src/commands_portsdiff.cpp @@ -0,0 +1,155 @@ +#include "vcpkg_Commands.h" +#include "vcpkg_System.h" +#include "vcpkg.h" +#include +#include +#include "vcpkg_Maps.h" +#include +#include +#include + +namespace vcpkg +{ + static void do_print_name_and_version(const std::vector& ports_to_print, const std::map& names_and_versions) + { + for (const std::string& name : ports_to_print) + { + const std::string& version = names_and_versions.at(name); + std::cout << std::left + << std::setw(20) << name << ' ' + << std::setw(16) << version << ' ' + << '\n'; + } + } + + static void do_print_name_and_previous_version_and_current_version(const std::vector& ports_to_print, + const std::map& previous_names_and_versions, + const std::map& current_names_and_versions) + { + for (const std::string& name : ports_to_print) + { + if (name == "") + { + continue; + } + + const std::string& previous_version = previous_names_and_versions.at(name); + const std::string& current_version = current_names_and_versions.at(name); + std::cout << std::left + << std::setw(20) << name << ' ' + << std::setw(16) << previous_version << " -> " << current_version + << '\n'; + } + } + + static std::map read_all_ports(const fs::path& ports_folder_path) + { + std::map names_and_versions; + + for (auto it = fs::directory_iterator(ports_folder_path); it != fs::directory_iterator(); ++it) + { + const fs::path& path = it->path(); + + try + { + auto pghs = get_paragraphs(path / "CONTROL"); + if (pghs.empty()) + continue; + auto srcpgh = SourceParagraph(pghs[0]); + names_and_versions.emplace(srcpgh.name, srcpgh.version); + } + catch (std::runtime_error const&) + { + } + } + + return names_and_versions; + } + + static std::map read_ports_from_commit(const vcpkg_paths& paths, const std::wstring& git_commit_id) + { + const fs::path dot_git_dir = paths.root / ".git"; + const std::wstring ports_dir_name_as_string = paths.ports.filename().native(); + const fs::path temp_checkout_path = paths.root / Strings::wformat(L"%s-%s", ports_dir_name_as_string, git_commit_id); + fs::create_directory(temp_checkout_path); + const std::wstring checkout_this_dir = Strings::wformat(LR"(.\%s)", ports_dir_name_as_string); // Must be relative to the root of the repository + + const std::wstring cmd = Strings::wformat(LR"(git --git-dir="%s" --work-tree="%s" checkout %s -f -q -- %s %s & git reset >NUL)", + dot_git_dir.native(), + temp_checkout_path.native(), + git_commit_id, + checkout_this_dir, + L".vcpkg-root"); + System::cmd_execute(cmd); + std::map names_and_versions = read_all_ports(temp_checkout_path / ports_dir_name_as_string); + fs::remove_all(temp_checkout_path); + return names_and_versions; + } + + void portsdiff_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths) + { + static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", create_example_string("portsdiff mybranchname")); + args.check_min_arg_count(1, example.c_str()); + args.check_max_arg_count(2, example.c_str()); + const std::wstring git_commit_id_for_previous_snapshot = Strings::utf8_to_utf16(args.command_arguments.at(0)); + const std::wstring git_commit_id_for_current_snapshot = args.command_arguments.size() < 2 ? L"HEAD" : Strings::utf8_to_utf16(args.command_arguments.at(1)); + + const std::map current_names_and_versions = read_ports_from_commit(paths, git_commit_id_for_current_snapshot); + const std::map previous_names_and_versions = read_ports_from_commit(paths, git_commit_id_for_previous_snapshot); + + // Already sorted, so set_difference can work on std::vector too + std::vector current_ports = Maps::extract_keys(current_names_and_versions); + std::vector previous_ports = Maps::extract_keys(previous_names_and_versions); + + std::vector added_ports; + std::set_difference( + current_ports.cbegin(), current_ports.cend(), + previous_ports.cbegin(), previous_ports.cend(), + std::back_inserter(added_ports)); + + if (!added_ports.empty()) + { + System::println("\nThe following %d ports were added:\n", added_ports.size()); + do_print_name_and_version(added_ports, current_names_and_versions); + } + + std::vector removed_ports; + std::set_difference( + previous_ports.cbegin(), previous_ports.cend(), + current_ports.cbegin(), current_ports.cend(), + std::back_inserter(removed_ports)); + + if (!removed_ports.empty()) + { + System::println("\nThe following %d ports were removed:\n", removed_ports.size()); + do_print_name_and_version(removed_ports, previous_names_and_versions); + } + + std::vector potentially_updated_ports; + std::set_intersection( + current_ports.cbegin(), current_ports.cend(), + previous_ports.cbegin(), previous_ports.cend(), + std::back_inserter(potentially_updated_ports)); + + std::vector updated_ports; + std::copy_if(potentially_updated_ports.cbegin(), potentially_updated_ports.cend(), std::back_inserter(updated_ports), + [&](const std::string& port) -> bool + { + return current_names_and_versions.at(port) != previous_names_and_versions.at(port); + } + ); + + if (!updated_ports.empty()) + { + System::println("\nThe following %d ports were updated:\n", updated_ports.size()); + do_print_name_and_previous_version_and_current_version(updated_ports, previous_names_and_versions, current_names_and_versions); + } + + if (added_ports.empty() && removed_ports.empty() && updated_ports.empty()) + { + System::println("There were no changes in the ports between the two commits."); + } + + exit(EXIT_SUCCESS); + } +} -- cgit v1.2.3 From a721db2c1fbfc1b87065b0b43e32249117e53242 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 7 Nov 2016 16:06:36 -0800 Subject: Refactor: create new Paragraphs.h/cpp --- toolsrc/src/commands_portsdiff.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index e15db3567..fdbaec0ac 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "Paragraphs.h" namespace vcpkg { @@ -52,7 +53,7 @@ namespace vcpkg try { - auto pghs = get_paragraphs(path / "CONTROL"); + auto pghs = Paragraphs::get_paragraphs(path / "CONTROL"); if (pghs.empty()) continue; auto srcpgh = SourceParagraph(pghs[0]); -- cgit v1.2.3 From f13b9cd24af72f4865ccfa0a64a1a43770da4833 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 7 Nov 2016 16:20:32 -0800 Subject: Don't #include vcpkg.h in portsdiff.cpp --- toolsrc/src/commands_portsdiff.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index fdbaec0ac..3789e3b42 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -1,6 +1,5 @@ #include "vcpkg_Commands.h" #include "vcpkg_System.h" -#include "vcpkg.h" #include #include #include "vcpkg_Maps.h" @@ -8,6 +7,7 @@ #include #include #include "Paragraphs.h" +#include "SourceParagraph.h" namespace vcpkg { -- cgit v1.2.3 From 0f5a833b81529362b290b2b62fe81ce07b98e766 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 28 Nov 2016 18:07:42 -0800 Subject: [vcpkg portsdiff] Add check that commit id exists --- toolsrc/src/commands_portsdiff.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index 3789e3b42..b6b604e54 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -8,6 +8,7 @@ #include #include "Paragraphs.h" #include "SourceParagraph.h" +#include "vcpkg_Environment.h" namespace vcpkg { @@ -87,14 +88,28 @@ namespace vcpkg return names_and_versions; } + static void check_commit_exists(const std::wstring& git_commit_id) + { + static const std::string VALID_COMMIT_OUTPUT = "commit\n"; + + const std::wstring cmd = Strings::wformat(LR"(git cat-file -t %s 2>NUL)", git_commit_id); + const System::exit_code_and_output output = System::cmd_execute_and_capture_output(cmd); + Checks::check_exit(output.output == VALID_COMMIT_OUTPUT, "Invalid commit id %s", Strings::utf16_to_utf8(git_commit_id)); + } + void portsdiff_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths) { static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", create_example_string("portsdiff mybranchname")); args.check_min_arg_count(1, example.c_str()); args.check_max_arg_count(2, example.c_str()); + + Environment::ensure_git_on_path(paths); const std::wstring git_commit_id_for_previous_snapshot = Strings::utf8_to_utf16(args.command_arguments.at(0)); const std::wstring git_commit_id_for_current_snapshot = args.command_arguments.size() < 2 ? L"HEAD" : Strings::utf8_to_utf16(args.command_arguments.at(1)); + check_commit_exists(git_commit_id_for_current_snapshot); + check_commit_exists(git_commit_id_for_previous_snapshot); + const std::map current_names_and_versions = read_ports_from_commit(paths, git_commit_id_for_current_snapshot); const std::map previous_names_and_versions = read_ports_from_commit(paths, git_commit_id_for_previous_snapshot); -- cgit v1.2.3 From b629cd904440c640b7e5b4c3fdf17df5aac90bad Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 12 Dec 2016 15:03:36 -0800 Subject: [vcpkg_cmd_arguments] Use std::string instead of char* --- toolsrc/src/commands_portsdiff.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index b6b604e54..46c6c90c7 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -100,8 +100,8 @@ namespace vcpkg void portsdiff_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths) { static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", create_example_string("portsdiff mybranchname")); - args.check_min_arg_count(1, example.c_str()); - args.check_max_arg_count(2, example.c_str()); + args.check_min_arg_count(1, example); + args.check_max_arg_count(2, example); Environment::ensure_git_on_path(paths); const std::wstring git_commit_id_for_previous_snapshot = Strings::utf8_to_utf16(args.command_arguments.at(0)); -- cgit v1.2.3 From df2a05e8546281135c6ea12430734d74371bcf46 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 12 Jan 2017 17:35:33 -0800 Subject: Introduce Command namespace. Refactoring --- toolsrc/src/commands_portsdiff.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index 46c6c90c7..4794dee2b 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -10,7 +10,7 @@ #include "SourceParagraph.h" #include "vcpkg_Environment.h" -namespace vcpkg +namespace vcpkg::Commands { static void do_print_name_and_version(const std::vector& ports_to_print, const std::map& names_and_versions) { @@ -99,7 +99,7 @@ namespace vcpkg void portsdiff_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths) { - static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", create_example_string("portsdiff mybranchname")); + static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", Commands::Helpers::create_example_string("portsdiff mybranchname")); args.check_min_arg_count(1, example); args.check_max_arg_count(2, example); -- cgit v1.2.3 From cc8851144a871931b93d84bd962364159ad1c424 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Thu, 12 Jan 2017 22:03:57 -0800 Subject: Reorganize commands, each in its own namespace Additionally, functions related to a command can now live in the same namespace --- toolsrc/src/commands_portsdiff.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index 4794dee2b..1665d7c47 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -10,7 +10,7 @@ #include "SourceParagraph.h" #include "vcpkg_Environment.h" -namespace vcpkg::Commands +namespace vcpkg::Commands::PortsDiff { static void do_print_name_and_version(const std::vector& ports_to_print, const std::map& names_and_versions) { @@ -97,9 +97,9 @@ namespace vcpkg::Commands Checks::check_exit(output.output == VALID_COMMIT_OUTPUT, "Invalid commit id %s", Strings::utf16_to_utf8(git_commit_id)); } - void portsdiff_command(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths) + void perform_and_exit(const vcpkg_cmd_arguments& args, const vcpkg_paths& paths) { - static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", Commands::Helpers::create_example_string("portsdiff mybranchname")); + static const std::string example = Strings::format("The argument should be a branch/tag/hash to checkout.\n%s", Commands::Help::create_example_string("portsdiff mybranchname")); args.check_min_arg_count(1, example); args.check_max_arg_count(2, example); -- cgit v1.2.3 From 050e4a0f7a8156bd862f95c06ea298ab6697e147 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Fri, 27 Jan 2017 12:49:09 -0800 Subject: Introduce precompiled headers --- toolsrc/src/commands_portsdiff.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'toolsrc/src/commands_portsdiff.cpp') diff --git a/toolsrc/src/commands_portsdiff.cpp b/toolsrc/src/commands_portsdiff.cpp index 1665d7c47..e75633b3c 100644 --- a/toolsrc/src/commands_portsdiff.cpp +++ b/toolsrc/src/commands_portsdiff.cpp @@ -1,11 +1,7 @@ +#include "pch.h" #include "vcpkg_Commands.h" #include "vcpkg_System.h" -#include -#include #include "vcpkg_Maps.h" -#include -#include -#include #include "Paragraphs.h" #include "SourceParagraph.h" #include "vcpkg_Environment.h" -- cgit v1.2.3