summaryrefslogtreecommitdiff
path: root/Command/AddUrl.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-09-07 19:04:51 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-09-07 19:04:51 -0400
commit03d6209e1ccee4a8df7d1b0336c1d5587a2b3ff6 (patch)
treeb65ce23e1f72242a13ce5b942cf8648ff2677c49 /Command/AddUrl.hs
parent7c768c09841d7346444d65721b132d144835fc99 (diff)
addurl: Always use whole url as destination filename, rather than only its file component.
First, this ensures that git annex addurl, when run repeatedly with the same url, doesn't create duplicate files, which it did before when it fell back to the longer filename. Secondly, the file part of an url is frequently not very descriptive on its own. The uri scheme, auth, and port is intentionally left out, as clutter.
Diffstat (limited to 'Command/AddUrl.hs')
-rw-r--r--Command/AddUrl.hs26
1 files changed, 9 insertions, 17 deletions
diff --git a/Command/AddUrl.hs b/Command/AddUrl.hs
index 55e51100c..9fc68ca03 100644
--- a/Command/AddUrl.hs
+++ b/Command/AddUrl.hs
@@ -7,9 +7,10 @@
module Command.AddUrl where
-import Control.Monad.State (liftIO, when)
+import Control.Monad.State
import Network.URI
import Data.String.Utils
+import Data.Maybe
import System.Directory
import Command
@@ -24,6 +25,7 @@ import Content
import PresenceLog
import Locations
import Utility.Path
+import Utility.Conditional
command :: [Command]
command = [repoCommand "addurl" paramPath seek "add urls to annex"]
@@ -75,20 +77,10 @@ nodownload url file = do
url2file :: URI -> IO FilePath
url2file url = do
- let parts = filter safe $ split "/" $ uriPath url
- if null parts
- then fallback
- else do
- let file = last parts
- e <- doesFileExist file
- if e then fallback else return file
+ whenM (doesFileExist file) $
+ error $ "already have this url in " ++ file
+ return file
where
- fallback = do
- let file = replace "/" "_" $ show url
- e <- doesFileExist file
- when e $ error "already have this url"
- return file
- safe "" = False
- safe "." = False
- safe ".." = False
- safe _ = True
+ file = escape $ uriRegName auth ++ uriPath url ++ uriQuery url
+ escape = replace "/?" $ repeat '_'
+ auth = fromMaybe (error $ "bad url " ++ show url) $ uriAuthority url