navid/veins / commit
4179151a4b9c
Navid M <navidmomtahen@gmail.com> · 2026-11-28
parents: 9b2d51ea7f41
Browse files at this commit · Download source
--- a/xveins/src/cfg.cpp+++ b/xveins/src/cfg.cpp@@ -57,6 +57,7 @@ std::cout << "usage: vn cfg [<key>] [<value>]\n"; std::cout << " vn cfg [<get|set|unset|list>] [<key>] [<value>]\n"; std::cout << " vn cfg -list\n";+ std::cout << " vn cfg -d [-i]\n"; std::cout << " vn cfg -unset <key>\n"; std::cout << "\n"; std::cout << "read and write local repository configuration\n";@@ -64,10 +65,49 @@ std::cout << " vn cfg list all keys\n"; std::cout << " vn cfg <key> print a value\n"; std::cout << " vn cfg <key> <value> set a value\n";+ std::cout << " vn cfg -d show defaults and global values\n";+ std::cout << " vn cfg -i -d also show internal keys\n"; std::cout << " vn cfg -unset <key> remove a key\n"; std::cout << " vn cfg -global ... use the user config instead\n"; std::cout << "\nthe global editor command is read from 'editor' or 'core.editor'\n"; std::cout << " vn cfg -global editor \"code --wait\"\n";+}++int cfg_defaults(bool internal) {+#if defined(_WIN32)+ const std::string editor_default = "notepad";+ const std::string pager_default = "more";+#else+ const std::string editor_default = "vi";+ const std::string pager_default = "less -R";+#endif+ std::map<std::string, std::string> defaults = {+ {"core.editor", "(unset)"},+ {"core.pager", "(unset)"},+ {"editor", editor_default},+ {"log.default_amount", "20"},+ {"merge.adjacent", "strict"},+ {"pager", pager_default},+ {"rename.autodetect", "similar"},+ {"user.email", "(unset)"},+ {"user.name", "(unset)"},+ };+ if (internal) {+ defaults["bookmark.current"] = "(unset)";+ defaults["promisor"] = "(unset)";+ defaults["remote.<name>.key"] = "(unset)";+ defaults["remote.<name>.tls-pin"] = "(unset)";+ defaults["remote.<name>.url"] = "(unset)";+ defaults["remote.current"] = "(unset)";+ defaults["remote.default"] = "(unset)";+ }+ for (const auto& entry : defaults) {+ std::cout << entry.first << " = " << entry.second;+ if (auto current = user_config_value(entry.first))+ std::cout << " (current: " << *current << ")";+ std::cout << "\n";+ }+ return 0; } int cfg_list(const fs::path* root, bool global) {@@ -128,6 +168,8 @@ bool global = false; bool list = false; bool unset = false;+ bool show_defaults = false;+ bool internal = false; bool end_flags = false; std::vector<std::string> positional; for (std::size_t i = 2; i < args.size(); ++i) {@@ -140,6 +182,10 @@ list = true; } else if (!end_flags && (arg == "-unset" || arg == "--unset")) { unset = true;+ } else if (!end_flags && (arg == "-d" || arg == "--defaults")) {+ show_defaults = true;+ } else if (!end_flags && (arg == "-i" || arg == "--internal")) {+ internal = true; } else if (!end_flags && cfg_is_flag(arg)) { error_prefix(); std::cout << "unknown flag: " << arg << "\n";@@ -146,7 +192,21 @@ return 1; } else { positional.push_back(arg);+ }+ }++ if (show_defaults) {+ if (list || unset || !positional.empty()) {+ error_prefix();+ std::cout << "cfg -d takes no operands\n";+ return 1; }+ return cfg_defaults(internal);+ }+ if (internal) {+ error_prefix();+ std::cout << "cfg -i requires -d\n";+ return 1; } if (positional.size() == 1 && (positional[0] == "list" || positional[0] == "ls")) {--- a/xveins/tests/integration_tests.cpp+++ b/xveins/tests/integration_tests.cpp@@ -856,6 +856,48 @@ f.failed({"cfg", "key", "value", "extra"}, "accepts a key and one value"); f.failed({"cfg", "-unset"}, "requires one key"); f.failed({"cfg", "unknown"}, "unknown config key");+}++void config_defaults(test::Context& t) {+ VnFixture f;+ const fs::path outside = f.root / "outside";+ fs::create_directories(outside);+#if defined(_WIN32)+ const std::string editor_default = "notepad";+ const std::string pager_default = "more";+#else+ const std::string editor_default = "vi";+ const std::string pager_default = "less -R";+#endif+ const std::string defaults = f.vn({"cfg", "-d"}, outside).out;+ t.expect_contains(defaults, "log.default_amount = 20");+ t.expect_contains(defaults, "rename.autodetect = similar");+ t.expect_contains(defaults, "merge.adjacent = strict");+ t.expect_contains(defaults, "user.name = (unset)");+ t.expect_contains(defaults, "editor = " + editor_default);+ t.expect_contains(defaults, "pager = " + pager_default);+ t.expect_not_contains(defaults, "bookmark.current");++ f.vn({"cfg", "-global", "log.default_amount", "50"}, outside);+ f.vn({"cfg", "-global", "editor", "code --wait"}, outside);+ const std::string with_current = f.vn({"cfg", "-d", "-global"}, outside).out;+ t.expect_contains(with_current, "log.default_amount = 20 (current: 50)");+ t.expect_contains(with_current, "editor = " + editor_default + " (current: code --wait)");++ f.init();+ f.vn({"cfg", "log.default_amount", "1"});+ const std::string local_ignored = f.vn({"cfg", "-d"}).out;+ t.expect_contains(local_ignored, "log.default_amount = 20 (current: 50)");+ t.expect_not_contains(local_ignored, "current: 1");++ const std::string internal = f.vn({"cfg", "-i", "-d"}).out;+ t.expect_contains(internal, "bookmark.current = (unset)");+ t.expect_contains(internal, "remote.<name>.url = (unset)");+ t.expect_contains(internal, "promisor = (unset)");++ f.failed({"cfg", "-d", "extra"}, "takes no operands");+ f.failed({"cfg", "-d", "-list"}, "takes no operands");+ f.failed({"cfg", "-i"}, "requires -d"); } void config_pager(test::Context& t) {@@ -1233,6 +1275,7 @@ {"local", config_local}, {"global_identity", config_global_identity}, {"errors", config_errors},+ {"defaults", config_defaults}, {"pager", config_pager}, }}, {"merge",