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:
		
					parent
					
						
							
								d9b6be7782
							
						
					
				
			
			
				commit
				
					
						c5b017e55b
					
				
			
		
					 2 changed files with 21 additions and 2 deletions
				
			
		| 
						 | 
					@ -11,7 +11,7 @@
 | 
				
			||||||
module Git.Filename where
 | 
					module Git.Filename where
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import Common
 | 
					import Common
 | 
				
			||||||
import Utility.Format (decode_c, encode_c, isUtf8Byte)
 | 
					import Utility.Format (decode_c, encode_c, encode_c', isUtf8Byte)
 | 
				
			||||||
import Utility.QuickCheck
 | 
					import Utility.QuickCheck
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import Data.Char
 | 
					import Data.Char
 | 
				
			||||||
| 
						 | 
					@ -39,6 +39,20 @@ encodeAlways s = "\"" <> encode_c needencode s <> "\""
 | 
				
			||||||
  where
 | 
					  where
 | 
				
			||||||
	needencode c = isUtf8Byte c || c == fromIntegral (ord '"')
 | 
						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
 | 
					-- Encoding and then decoding roundtrips only when the string does not
 | 
				
			||||||
-- contain high unicode, because eg, both "\12345" and "\227\128\185"
 | 
					-- contain high unicode, because eg, both "\12345" and "\227\128\185"
 | 
				
			||||||
-- are encoded to "\343\200\271".
 | 
					-- are encoded to "\343\200\271".
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,3 +35,8 @@ useful defense in depth against terminal security holes, and also good to
 | 
				
			||||||
behave more like git.
 | 
					behave more like git.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
--[[Joey]]
 | 
					--[[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]]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue