summaryrefslogtreecommitdiff
path: root/Git/Filename.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2020-01-02 12:34:10 -0400
committerJoey Hess <joeyh@joeyh.name>2020-01-02 12:42:57 -0400
commit9df8a6eb9405dde4464d27133c04f5ee539a85de (patch)
tree8a7ac5f52be8679f8a2525515a0b2c1b715c99ad /Git/Filename.hs
parent16022a8b98f4bc134542e78a42538364d2f97d92 (diff)
downloadgit-repair-9df8a6eb9405dde4464d27133c04f5ee539a85de.tar.gz
merge from git-annex and relicense accordingly
Merge git library and utility from git-annex. The former is now relicensed AGPL, so git-repair as a whole becomes AGPL. For simplicity, I am relicensing the remainder of the code in git-repair AGPL as well, per the header changes in this commit. While that code is also technically available under the GPL license, as it's been released under that license before, changes going forward will be only released by me under the AGPL.
Diffstat (limited to 'Git/Filename.hs')
-rw-r--r--Git/Filename.hs53
1 files changed, 37 insertions, 16 deletions
diff --git a/Git/Filename.hs b/Git/Filename.hs
index 355e75f..010e5ba 100644
--- a/Git/Filename.hs
+++ b/Git/Filename.hs
@@ -3,7 +3,7 @@
-
- Copyright 2010, 2011 Joey Hess <id@joeyh.name>
-
- - Licensed under the GNU GPL version 3 or higher.
+ - Licensed under the GNU AGPL version 3 or higher.
-}
module Git.Filename where
@@ -12,23 +12,44 @@ import Common
import Utility.Format (decode_c, encode_c)
import Data.Char
+import Data.Word
+import qualified Data.ByteString as S
-decode :: String -> FilePath
-decode [] = []
-decode f@(c:s)
- -- encoded strings will be inside double quotes
- | c == '"' && end s == ['"'] = decode_c $ beginning s
- | otherwise = f
+-- encoded filenames will be inside double quotes
+decode :: S.ByteString -> RawFilePath
+decode b = case S.uncons b of
+ Nothing -> b
+ Just (h, t)
+ | h /= q -> b
+ | otherwise -> case S.unsnoc t of
+ Nothing -> b
+ Just (i, l)
+ | l /= q -> b
+ | otherwise ->
+ encodeBS $ decode_c $ decodeBS i
+ where
+ q :: Word8
+ q = fromIntegral (ord '"')
{- Should not need to use this, except for testing decode. -}
-encode :: FilePath -> String
-encode s = "\"" ++ encode_c s ++ "\""
+encode :: RawFilePath -> S.ByteString
+encode s = encodeBS $ "\"" ++ encode_c (decodeBS s) ++ "\""
-{- For quickcheck.
- -
- - See comment on Utility.Format.prop_encode_c_decode_c_roundtrip for
- - why this only tests chars < 256 -}
-prop_encode_decode_roundtrip :: String -> Bool
-prop_encode_decode_roundtrip s = s' == decode (encode s')
+prop_encode_decode_roundtrip :: FilePath -> Bool
+prop_encode_decode_roundtrip s = s' ==
+ fromRawFilePath (decode (encode (toRawFilePath s')))
where
- s' = filter (\c -> ord c < 256) s
+ s' = nonul (nohigh s)
+ -- 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".
+ --
+ -- This property papers over the problem, by only
+ -- testing ascii
+ nohigh = filter isAscii
+ -- A String can contain a NUL, but toRawFilePath
+ -- truncates on the NUL, which is generally fine
+ -- because unix filenames cannot contain NUL.
+ -- So the encoding only roundtrips when there is no nul.
+ nonul = filter (/= '\NUL')