webapp: Generate better git remote names.

Wrote a better git remote name sanitizer. Git blows up on lots of weird
stuff, especially if it starts the remote name, but I managed to get
some common punctuation working.
This commit is contained in:
Joey Hess 2012-10-31 15:17:00 -04:00
parent c03d9b2310
commit 39e82b1af8
4 changed files with 40 additions and 10 deletions

View file

@ -19,10 +19,10 @@ import qualified Git.Command
import qualified Command.InitRemote import qualified Command.InitRemote
import Logs.UUID import Logs.UUID
import Logs.Remote import Logs.Remote
import Git.Remote
import qualified Data.Text as T import qualified Data.Text as T
import qualified Data.Map as M import qualified Data.Map as M
import Data.Char
{- Sets up and begins syncing with a new ssh or rsync remote. -} {- Sets up and begins syncing with a new ssh or rsync remote. -}
makeSshRemote :: Bool -> SshData -> Assistant Remote makeSshRemote :: Bool -> SshData -> Assistant Remote
@ -112,6 +112,4 @@ uniqueRemoteName basename n r
name name
| n == 0 = legalbasename | n == 0 = legalbasename
| otherwise = legalbasename ++ show n | otherwise = legalbasename ++ show n
legalbasename = filter legal basename legalbasename = makeLegalName basename
legal '_' = True
legal c = isAlphaNum c

View file

@ -10,6 +10,7 @@ module Assistant.Ssh where
import Common.Annex import Common.Annex
import Utility.TempFile import Utility.TempFile
import Utility.UserInfo import Utility.UserInfo
import Git.Remote
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
@ -51,14 +52,11 @@ sshDir = do
genSshHost :: Text -> Maybe Text -> String genSshHost :: Text -> Maybe Text -> String
genSshHost host user = maybe "" (\v -> T.unpack v ++ "@") user ++ T.unpack host genSshHost host user = maybe "" (\v -> T.unpack v ++ "@") user ++ T.unpack host
{- host_dir, with all / in dir replaced by _, and bad characters removed -} {- Generates a git remote name, like host_dir or host -}
genSshRepoName :: String -> FilePath -> String genSshRepoName :: String -> FilePath -> String
genSshRepoName host dir genSshRepoName host dir
| null dir = filter legal host | null dir = makeLegalName host
| otherwise = filter legal $ host ++ "_" ++ replace "/" "_" dir | otherwise = makeLegalName $ host ++ "_" ++ dir
where
legal '_' = True
legal c = isAlphaNum c
{- The output of ssh, including both stdout and stderr. -} {- The output of ssh, including both stdout and stderr. -}
sshTranscript :: [String] -> String -> IO (String, Bool) sshTranscript :: [String] -> String -> IO (String, Bool)

33
Git/Remote.hs Normal file
View file

@ -0,0 +1,33 @@
{- git remote stuff
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Git.Remote where
import Common
import Data.Char
{- Construct a legal git remote name out of an arbitrary input string.
-
- There seems to be no formal definition of this in the git source,
- just some ad-hoc checks, and some other things that fail with certian
- types of names (like ones starting with '-').
-}
makeLegalName :: String -> String
makeLegalName s = case filter legal $ replace "/" "_" s of
-- it can't be empty
[] -> "unnamed"
-- it can't start with / or - or .
'.':s' -> makeLegalName s'
'/':s' -> makeLegalName s'
'-':s' -> makeLegalName s'
s' -> s'
where
{- Only alphanumerics, and a few common bits of punctuation common
- in hostnames. -}
legal '_' = True
legal '.' = True
legal c = isAlphaNum c

1
debian/changelog vendored
View file

@ -37,6 +37,7 @@ git-annex (3.20121018) UNRELEASED; urgency=low
* assistant: Fix syncing local drives. * assistant: Fix syncing local drives.
* webapp: Fix creation of rsync.net repositories. * webapp: Fix creation of rsync.net repositories.
* webapp: Fix renaming of special remotes. * webapp: Fix renaming of special remotes.
* webapp: Generate better git remote names.
-- Joey Hess <joeyh@debian.org> Wed, 17 Oct 2012 14:24:10 -0400 -- Joey Hess <joeyh@debian.org> Wed, 17 Oct 2012 14:24:10 -0400