git-annex/Command/Sync.hs

73 lines
1.9 KiB
Haskell
Raw Normal View History

{- git-annex command
-
- Copyright 2011 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.Sync where
import Common.Annex
import Command
import qualified Annex.Branch
2011-12-14 19:56:11 +00:00
import qualified Git.Command
import qualified Git.Config
import qualified Data.ByteString.Lazy.Char8 as L
def :: [Command]
def = [command "sync" paramPaths seek "synchronize local repository with remote"]
2011-12-10 16:21:22 +00:00
-- syncing involves several operations, any of which can independantly fail
seek :: [CommandSeek]
2011-12-10 16:21:22 +00:00
seek = map withNothing [commit, pull, push]
2011-12-10 16:21:22 +00:00
commit :: CommandStart
commit = do
showStart "commit" ""
next $ next $ do
showOutput
-- Commit will fail when the tree is clean, so ignore failure.
2011-12-14 19:56:11 +00:00
_ <- inRepo $ Git.Command.runBool "commit"
[Param "-a", Param "-m", Param "sync"]
2011-12-10 16:21:22 +00:00
return True
pull :: CommandStart
pull = do
remote <- defaultRemote
showStart "pull" remote
next $ next $ do
showOutput
checkRemote remote
2011-12-14 19:56:11 +00:00
inRepo $ Git.Command.runBool "pull" [Param remote]
2011-12-10 16:21:22 +00:00
push :: CommandStart
push = do
remote <- defaultRemote
showStart "push" remote
next $ next $ do
Annex.Branch.update
showOutput
2011-12-14 19:56:11 +00:00
inRepo $ Git.Command.runBool "push" [Param remote, matchingbranches]
where
-- git push may be configured to not push matching
-- branches; this should ensure it always does.
matchingbranches = Param ":"
-- the remote defaults to origin when not configured
2011-12-10 16:21:22 +00:00
defaultRemote :: Annex String
defaultRemote = do
branch <- currentBranch
fromRepo $ Git.Config.get ("branch." ++ branch ++ ".remote") "origin"
currentBranch :: Annex String
currentBranch = last . split "/" . L.unpack . head . L.lines <$>
2011-12-14 19:56:11 +00:00
inRepo (Git.Command.pipeRead [Param "symbolic-ref", Param "HEAD"])
checkRemote :: String -> Annex ()
checkRemote remote = do
remoteurl <- fromRepo $
Git.Config.get ("remote." ++ remote ++ ".url") ""
when (null remoteurl) $ do
error $ "No url is configured for the remote: " ++ remote