git style quoting for ActionItemOther
Added StringContainingQuotedPath, which is used for ActionItemOther. In the process, checked every ActionItemOther for those containing filenames, and made them use quoting. Sponsored-by: Graham Spencer on Patreon
This commit is contained in:
parent
d689a5b338
commit
2ba1559a8e
41 changed files with 158 additions and 89 deletions
|
@ -133,6 +133,6 @@ parserDiffRaw f = DiffTreeItem
|
|||
<*> (maybe (fail "bad dstsha") return . extractSha =<< nextword)
|
||||
<* A8.char ' '
|
||||
<*> A.takeByteString
|
||||
<*> pure (asTopFilePath $ fromInternalGitPath $ Git.Filename.decode f)
|
||||
<*> pure (asTopFilePath $ fromInternalGitPath $ Git.Filename.unquote f)
|
||||
where
|
||||
nextword = A8.takeTill (== ' ')
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
- top of the repository even when run in a subdirectory. Adding some
|
||||
- types helps keep that straight.
|
||||
-
|
||||
- Copyright 2012-2019 Joey Hess <id@joeyh.name>
|
||||
- Copyright 2012-2023 Joey Hess <id@joeyh.name>
|
||||
-
|
||||
- Licensed under the GNU AGPL version 3 or higher.
|
||||
-}
|
||||
|
@ -51,7 +51,7 @@ data BranchFilePath = BranchFilePath Ref TopFilePath
|
|||
{- Git uses the branch:file form to refer to a BranchFilePath -}
|
||||
descBranchFilePath :: Filename.QuotePath -> BranchFilePath -> S.ByteString
|
||||
descBranchFilePath qp (BranchFilePath b f) =
|
||||
fromRef' b <> ":" <> Filename.encode qp (getTopFilePath f)
|
||||
fromRef' b <> ":" <> Filename.quote qp (getTopFilePath f)
|
||||
|
||||
{- Path to a TopFilePath, within the provided git repo. -}
|
||||
fromTopFilePath :: TopFilePath -> Git.Repo -> RawFilePath
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{- Some git commands output encoded filenames, in a rather annoyingly complex
|
||||
{- Some git commands output quoted filenames, in a rather annoyingly complex
|
||||
- C-style encoding.
|
||||
-
|
||||
- Copyright 2010-2023 Joey Hess <id@joeyh.name>
|
||||
|
@ -6,9 +6,15 @@
|
|||
- Licensed under the GNU AGPL version 3 or higher.
|
||||
-}
|
||||
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE OverloadedStrings, TypeSynonymInstances #-}
|
||||
|
||||
module Git.Filename where
|
||||
module Git.Filename (
|
||||
unquote,
|
||||
quote,
|
||||
QuotePath(..),
|
||||
StringContainingQuotedPath(..),
|
||||
prop_quote_unquote_roundtrip,
|
||||
) where
|
||||
|
||||
import Common
|
||||
import Utility.Format (decode_c, encode_c, encode_c', isUtf8Byte)
|
||||
|
@ -16,11 +22,13 @@ import Utility.QuickCheck
|
|||
|
||||
import Data.Char
|
||||
import Data.Word
|
||||
import Data.String
|
||||
import qualified Data.ByteString as S
|
||||
import qualified Data.Semigroup as Sem
|
||||
import Prelude
|
||||
|
||||
-- encoded filenames will be inside double quotes
|
||||
decode :: S.ByteString -> RawFilePath
|
||||
decode b = case S.uncons b of
|
||||
unquote :: S.ByteString -> RawFilePath
|
||||
unquote b = case S.uncons b of
|
||||
Nothing -> b
|
||||
Just (h, t)
|
||||
| h /= q -> b
|
||||
|
@ -34,24 +42,51 @@ decode b = case S.uncons b of
|
|||
q = fromIntegral (ord '"')
|
||||
|
||||
-- always encodes and double quotes, even in cases that git does not
|
||||
encodeAlways :: RawFilePath -> S.ByteString
|
||||
encodeAlways s = "\"" <> encode_c needencode s <> "\""
|
||||
quoteAlways :: RawFilePath -> S.ByteString
|
||||
quoteAlways 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
|
||||
class Quoteable t where
|
||||
-- double quotes and encodes when git would
|
||||
quote :: QuotePath -> t -> S.ByteString
|
||||
|
||||
instance Quoteable RawFilePath where
|
||||
quote (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
|
||||
|
||||
-- Allows building up a string that contains paths, which will get quoted.
|
||||
-- With OverloadedStrings, strings are passed through without quoting.
|
||||
-- Eg: QuotedPath f <> ": not found"
|
||||
data StringContainingQuotedPath
|
||||
= UnquotedString String
|
||||
| QuotedPath RawFilePath
|
||||
| StringContainingQuotedPathMulti [StringContainingQuotedPath]
|
||||
deriving (Show, Eq)
|
||||
|
||||
instance Quoteable StringContainingQuotedPath where
|
||||
quote _ (UnquotedString s) = encodeBS s
|
||||
quote qp (QuotedPath p) = quote qp p
|
||||
quote qp (StringContainingQuotedPathMulti l) = S.concat (map (quote qp) l)
|
||||
|
||||
instance IsString StringContainingQuotedPath where
|
||||
fromString = UnquotedString
|
||||
|
||||
instance Sem.Semigroup StringContainingQuotedPath where
|
||||
UnquotedString a <> UnquotedString b = UnquotedString (a <> b)
|
||||
a <> b = StringContainingQuotedPathMulti [a, b]
|
||||
|
||||
instance Monoid StringContainingQuotedPath where
|
||||
mempty = UnquotedString mempty
|
||||
|
||||
-- Encoding and then decoding roundtrips only when the string does not
|
||||
-- contain high unicode, because eg, both "\12345" and "\227\128\185"
|
||||
|
@ -59,8 +94,8 @@ encode (QuotePath qp) s = case encode_c' needencode s of
|
|||
--
|
||||
-- That is not a real-world problem, and using TestableFilePath
|
||||
-- limits what's tested to ascii, so avoids running into it.
|
||||
prop_encode_decode_roundtrip :: TestableFilePath -> Bool
|
||||
prop_encode_decode_roundtrip ts =
|
||||
s == fromRawFilePath (decode (encodeAlways (toRawFilePath s)))
|
||||
prop_quote_unquote_roundtrip :: TestableFilePath -> Bool
|
||||
prop_quote_unquote_roundtrip ts =
|
||||
s == fromRawFilePath (unquote (quoteAlways (toRawFilePath s)))
|
||||
where
|
||||
s = fromTestableFilePath ts
|
||||
|
|
|
@ -137,7 +137,7 @@ parserLsTree long = case long of
|
|||
-- sha
|
||||
<*> (Ref <$> A8.takeTill A8.isSpace)
|
||||
|
||||
fileparser = asTopFilePath . Git.Filename.decode <$> A.takeByteString
|
||||
fileparser = asTopFilePath . Git.Filename.unquote <$> A.takeByteString
|
||||
|
||||
sizeparser = fmap Just A8.decimal
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue