Skip to content

Commit

Permalink
git: escape string values in configuration
Browse files Browse the repository at this point in the history
This should handle the special characters that typically occur.

Fixes #1206
  • Loading branch information
rycee committed Apr 30, 2020
1 parent 8b82f52 commit 642d9ff
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
12 changes: 11 additions & 1 deletion modules/programs/git.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ let
else
''${section} "${subsection}"'';

mkValueString = v:
let
escapedV = ''
"${
replaceStrings [ "\n" " " ''"'' "\\" ] [ "\\n" "\\t" ''\"'' "\\\\" ] v
}"'';
in generators.mkValueStringDefault { } (if isString v then escapedV else v);

# generation for multiple ini values
mkKeyValue = k: v:
let mkKeyValue = generators.mkKeyValueDefault { } " = " k;
let
mkKeyValue =
generators.mkKeyValueDefault { inherit mkValueString; } " = " k;
in concatStringsSep "\n" (map (kv: " " + mkKeyValue kv) (toList v));

# converts { a.b.c = 5; } to { "a.b".c = 5; } for toINI
Expand Down
4 changes: 2 additions & 2 deletions tests/modules/programs/git/git-expected-include.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[user]
email = user@example.org
name = John Doe
email = "user@example.org"
name = "John Doe"
31 changes: 16 additions & 15 deletions tests/modules/programs/git/git-expected.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[alias]
a1 = foo
a2 = baz
a1 = "foo"
a2 = "baz"
escapes = "\"\\n\t"

[commit]
gpgSign = true
Expand All @@ -10,33 +11,33 @@
integer = 38
multiple = 1
multiple = 2
name = value
name = "value"

[extra "backcompat.with.dots"]
previously = worked
previously = "worked"

[extra "subsection"]
value = test
value = "test"

[filter "lfs"]
clean = git-lfs clean -- %f
process = git-lfs filter-process
clean = "git-lfs clean -- %f"
process = "git-lfs filter-process"
required = true
smudge = git-lfs smudge -- %f
smudge = "git-lfs smudge -- %f"

[gpg]
program = path-to-gpg
program = "path-to-gpg"

[user]
email = user@example.org
name = John Doe
signingKey = 00112233445566778899AABBCCDDEEFF
email = "user@example.org"
name = "John Doe"
signingKey = "00112233445566778899AABBCCDDEEFF"

[include]
path = ~/path/to/config.inc
path = "~/path/to/config.inc"

[includeIf "gitdir:~/src/dir"]
path = ~/path/to/conditional.inc
path = "~/path/to/conditional.inc"

[includeIf "gitdir:~/src/dir"]
path = @git_include_path@
path = "@git_include_path@"
20 changes: 10 additions & 10 deletions tests/modules/programs/git/git-with-email-expected.conf
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[sendemail "hm-account"]
from = hm@example.org
smtpEncryption = tls
smtpServer = smtp.example.org
smtpUser = home.manager.jr
from = "hm@example.org"
smtpEncryption = "tls"
smtpServer = "smtp.example.org"
smtpUser = "home.manager.jr"

[sendemail "hm@example.com"]
from = hm@example.com
smtpEncryption = tls
smtpServer = smtp.example.com
smtpUser = home.manager
from = "hm@example.com"
smtpEncryption = "tls"
smtpServer = "smtp.example.com"
smtpUser = "home.manager"

[user]
email = hm@example.com
name = H. M. Test
email = "hm@example.com"
name = "H. M. Test"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This can be anything.

[user]
email = user@example.org
name = John Doe
email = "user@example.org"
name = "John Doe"
1 change: 1 addition & 0 deletions tests/modules/programs/git/git.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ in {
aliases = {
a1 = "foo";
a2 = "bar";
escapes = ''"\n '';
};
extraConfig = {
extra = {
Expand Down

2 comments on commit 642d9ff

@kalbasit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rycee This change broke my escaping and had to fix it by removing those escapes, maybe worth a news item?

kalbasit/shabka@8c082f7
kalbasit/dotshabka@b947baf

@rycee
Copy link
Member Author

@rycee rycee commented on 642d9ff Jun 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I've added a news entry in 1f174f6.

Please sign in to comment.