From 8a2197adfab9d8425f92000621a802dce37e124c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 18 Aug 2011 12:20:47 -0400 Subject: [PATCH] Added annex-cost-command configuration, which can be used to vary the cost of a remote based on the output of a shell command. Also avoided crashing if the user specified cost value cannot be parsed. --- Config.hs | 21 ++++++++++++++++----- debian/changelog | 2 ++ doc/git-annex.mdwn | 6 ++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Config.hs b/Config.hs index 9cbf2d52fd..6f2014483b 100644 --- a/Config.hs +++ b/Config.hs @@ -9,6 +9,8 @@ module Config where import Data.Maybe import Control.Monad.State (liftIO) +import Control.Monad (liftM) +import System.Cmd.Utils import qualified Git import qualified Annex @@ -40,14 +42,23 @@ remoteConfig r key = "remote." ++ fromMaybe "" (Git.repoRemoteName r) ++ ".annex {- Calculates cost for a remote. - - The default cost is 100 for local repositories, and 200 for remote - - repositories; it can also be configured by remote..annex-cost + - repositories; it can also be configured by remote..annex-cost, + - or if remote..annex-cost-command is set and prints a number, that + - is used. -} remoteCost :: Git.Repo -> Int -> Annex Int remoteCost r def = do - c <- getConfig r "cost" "" - if not $ null c - then return $ read c - else return def + cmd <- getConfig r "cost-command" "" + return . safeparse =<< if not $ null cmd + then liftM snd $ liftIO $ pipeFrom "sh" ["-c", cmd] + else getConfig r "cost" "" + where + safeparse v + | null ws || null ps = def + | otherwise = (fst . head) ps + where + ws = words v + ps = reads $ head ws cheapRemoteCost :: Int cheapRemoteCost = 100 diff --git a/debian/changelog b/debian/changelog index 9230c00218..1ace861a46 100644 --- a/debian/changelog +++ b/debian/changelog @@ -4,6 +4,8 @@ git-annex (3.20110818) UNRELEASED; urgency=low is first being created. Clones will automatically notice that git-annex is in use and automatically perform a basic initalization. It's still recommended to run "git annex init" in any clones, to describe them. + * Added annex-cost-command configuration, which can be used to vary the + cost of a remote based on the output of a shell command. -- Joey Hess Wed, 17 Aug 2011 13:44:44 -0400 diff --git a/doc/git-annex.mdwn b/doc/git-annex.mdwn index 9d63ca156d..a262d465fd 100644 --- a/doc/git-annex.mdwn +++ b/doc/git-annex.mdwn @@ -424,6 +424,12 @@ Here are all the supported configuration settings. The default cost is 100 for local repositories, and 200 for remote repositories. +* `remote..annex-cost-command` + + If set, the command is run, and the number it outputs is used as the cost. + This allows varying the cost based on eg, the current network. The + cost-command can be any shell command line. + * `remote..annex-ignore` If set to `true`, prevents git-annex