From a88b90c9dbb0db665ed62bb47296f4f3524cd267 Mon Sep 17 00:00:00 2001 From: Robert Helgesson Date: Sat, 19 Aug 2023 18:49:03 +0200 Subject: [PATCH 1/2] lib.generators.toGitINI: escape string values in configuration This should handle the special characters that typically occur. Upstreaming of https://github.com/nix-community/home-manager/commit/642d9ffe24eec1290e94d70e8129ba74922fddf2 --- lib/generators.nix | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/generators.nix b/lib/generators.nix index c37be1942d82..a4671db2eaae 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -189,10 +189,10 @@ rec { * } * *> [url "ssh://git@github.com/"] - *> insteadOf = https://github.com/ + *> insteadOf = "https://github.com" *> *> [user] - *> name = edolstra + *> name = "edolstra" */ toGitINI = attrs: with builtins; @@ -209,9 +209,17 @@ rec { else ''${section} "${subsection}"''; + mkValueString = v: + let + escapedV = '' + "${ + replaceStrings [ "\n" " " ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ] v + }"''; + in mkValueStringDefault { } (if isString v then escapedV else v); + # generation for multiple ini values mkKeyValue = k: v: - let mkKeyValue = mkKeyValueDefault { } " = " k; + let mkKeyValue = mkKeyValueDefault { inherit mkValueString; } " = " k; in concatStringsSep "\n" (map (kv: "\t" + mkKeyValue kv) (lib.toList v)); # converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI From 147560180c5b39266d10c6a6b8f54110d9667b99 Mon Sep 17 00:00:00 2001 From: VwCSXg Date: Mon, 21 Aug 2023 13:12:40 +0200 Subject: [PATCH 2/2] lib.generators.toGitINI: added test Added basic generators.toGitINI test. Mostly taken from https://github.com/nix-community/home-manager/blob/958c06303f43cf0625694326b7f7e5475b1a2d5c/tests/modules/programs/git/git.nix. The ${"\t} escape is used so that the lines aren't recognized as "Wrong indent style". --- lib/tests/misc.nix | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index 6d55ae684771..80223dccb261 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -948,6 +948,51 @@ runTests { ''; }; + testToGitINI = { + expr = generators.toGitINI { + user = { + email = "user@example.org"; + name = "John Doe"; + signingKey = "00112233445566778899AABBCCDDEEFF"; + }; + gpg.program = "path-to-gpg"; + tag.gpgSign = true; + include.path = "~/path/to/config.inc"; + includeIf."gitdif:~/src/dir".path = "~/path/to/conditional.inc"; + extra = { + boolean = true; + integer = 38; + name = "value"; + subsection.value = "test"; + };}; + expected = '' + [extra] + ${"\t"}boolean = true + ${"\t"}integer = 38 + ${"\t"}name = "value" + + [extra "subsection"] + ${"\t"}value = "test" + + [gpg] + ${"\t"}program = "path-to-gpg" + + [include] + ${"\t"}path = "~/path/to/config.inc" + + [includeIf "gitdif:~/src/dir"] + ${"\t"}path = "~/path/to/conditional.inc" + + [tag] + ${"\t"}gpgSign = true + + [user] + ${"\t"}email = "user@example.org" + ${"\t"}name = "John Doe" + ${"\t"}signingKey = "00112233445566778899AABBCCDDEEFF" + ''; + }; + /* right now only invocation check */ testToJSONSimple = let val = {