Unverified Commit 54bc9b45 authored by Robert Helgesson's avatar Robert Helgesson Committed by GitHub
Browse files

Merge pull request #250220

lib.generators.toGitINI: escape string values in configuration
parents 804d0108 14756018
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -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
+45 −0
Original line number Diff line number Diff line
@@ -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 = {