From ef013506cb8c82d547160195b66572b5774db14f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 8 Feb 2012 15:35:18 -0400 Subject: [PATCH] addurl: Added a --file option Can be used to specify what file the url is added to. This can be used to override the default filename that is used when adding an url, which is based on the url. Or, when the file already exists, the url is recorded as another location of the file. --- Command/AddUrl.hs | 37 +++++++++++++++++++++++-------------- Usage.hs | 2 ++ debian/changelog | 5 +++++ doc/git-annex.mdwn | 9 +++++++-- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/Command/AddUrl.hs b/Command/AddUrl.hs index 46584f0d81..600a6169d8 100644 --- a/Command/AddUrl.hs +++ b/Command/AddUrl.hs @@ -17,26 +17,39 @@ import qualified Annex import qualified Backend.URL import Annex.Content import Logs.Web +import qualified Option def :: [Command] -def = [command "addurl" (paramRepeating paramUrl) seek "add urls to annex"] +def = [withOptions [fileOption] $ + command "addurl" (paramRepeating paramUrl) seek "add urls to annex"] + +fileOption :: Option +fileOption = Option.field [] "file" paramFile "specify what file the url is added to" seek :: [CommandSeek] -seek = [withStrings start] +seek = [withField fileOption return $ \f -> + withStrings $ start f] -start :: String -> CommandStart -start s = notBareRepo $ go $ parseURI s +start :: Maybe FilePath -> String -> CommandStart +start optfile s = notBareRepo $ go $ parseURI s where go Nothing = error $ "bad url " ++ s go (Just url) = do - file <- liftIO $ url2file url + let file = fromMaybe (url2file url) optfile showStart "addurl" file next $ perform s file perform :: String -> FilePath -> CommandPerform -perform url file = do - fast <- Annex.getState Annex.fast - if fast then nodownload url file else download url file +perform url file = ifAnnexed file addurl geturl + where + geturl = do + whenM (liftIO $ doesFileExist file) $ + error $ "already have this url in " ++ file + fast <- Annex.getState Annex.fast + if fast then nodownload url file else download url file + addurl (key, _backend) = do + setUrlPresent key url + next $ return True download :: String -> FilePath -> CommandPerform download url file = do @@ -60,12 +73,8 @@ nodownload url file = do setUrlPresent key url next $ Command.Add.cleanup file key False -url2file :: URI -> IO FilePath -url2file url = do - whenM (doesFileExist file) $ - error $ "already have this url in " ++ file - return file +url2file :: URI -> FilePath +url2file url = escape $ uriRegName auth ++ uriPath url ++ uriQuery url where - file = escape $ uriRegName auth ++ uriPath url ++ uriQuery url escape = replace "/" "_" . replace "?" "_" auth = fromMaybe (error $ "bad url " ++ show url) $ uriAuthority url diff --git a/Usage.hs b/Usage.hs index 34c344b183..a33f6f311b 100644 --- a/Usage.hs +++ b/Usage.hs @@ -76,6 +76,8 @@ paramDate :: String paramDate = "DATE" paramFormat :: String paramFormat = "FORMAT" +paramFile :: String +paramFile = "FILE" paramKeyValue :: String paramKeyValue = "K=V" paramNothing :: String diff --git a/debian/changelog b/debian/changelog index ad7121da24..2f9d79939d 100644 --- a/debian/changelog +++ b/debian/changelog @@ -11,6 +11,11 @@ git-annex (3.20120124) UNRELEASED; urgency=low This version can only be built with ghc 7.4 or newer. * S3: Fix irrefutable pattern failure when accessing encrypted S3 credentials. + * addurl: Added a --file option, which can be used to specify what + file the url is added to. This can be used to override the default + filename that is used when adding an url, which is based on the url. + Or, when the file already exists, the url is recorded as another + location of the file. -- Joey Hess Tue, 24 Jan 2012 16:21:55 -0400 diff --git a/doc/git-annex.mdwn b/doc/git-annex.mdwn index 148b6336de..9232bf0208 100644 --- a/doc/git-annex.mdwn +++ b/doc/git-annex.mdwn @@ -145,9 +145,14 @@ subdirectories). * addurl [url ...] - Downloads each url to a file, which is added to the annex. + Downloads each url to its own file, which is added to the annex. - To avoid immediately downloading the url, specify --fast + To avoid immediately downloading the url, specify --fast. + + To specify what file the url is added to, specify --file. This changes + the behavior; now all the specified urls are recorded as alternate + locations from which the file can be downloaded. In this mode, addurl + can be used both to add new files, or to add urls to existing files. # REPOSITORY SETUP COMMANDS