full emulation of git filename escaping

Not yet used, but the plan is to make git-annex use this when displaying
filenames similar to how git does.

Sponsored-by: Lawrence Brogan on Patreon
This commit is contained in:
Joey Hess 2023-04-07 17:12:55 -04:00
parent d9b6be7782
commit c5b017e55b
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
2 changed files with 21 additions and 2 deletions

View file

@ -11,7 +11,7 @@
module Git.Filename where
import Common
import Utility.Format (decode_c, encode_c, isUtf8Byte)
import Utility.Format (decode_c, encode_c, encode_c', isUtf8Byte)
import Utility.QuickCheck
import Data.Char
@ -39,6 +39,20 @@ encodeAlways s = "\"" <> encode_c needencode s <> "\""
where
needencode c = isUtf8Byte c || c == fromIntegral (ord '"')
-- git config core.quotePath controls whether to quote unicode characters
newtype QuotePath = QuotePath Bool
-- encodes and double quotes when git would
encode :: QuotePath -> RawFilePath -> S.ByteString
encode (QuotePath qp) s = case encode_c' needencode s of
Nothing -> s
Just s' -> "\"" <> s' <> "\""
where
needencode c
| c == fromIntegral (ord '"') = True
| qp = isUtf8Byte c
| otherwise = False
-- Encoding and then decoding roundtrips only when the string does not
-- contain high unicode, because eg, both "\12345" and "\227\128\185"
-- are encoded to "\343\200\271".

View file

@ -35,3 +35,8 @@ useful defense in depth against terminal security holes, and also good to
behave more like git.
--[[Joey]]
> Git.Filename.encode is implemented, and only needs to be used.
> Note that core.quotePath controls whether git quotes unicode characters
> (by default it does), so once this gets implemented, some users may want
> to set that config to false. --[[Joey]]