2011-12-10 00:27:22 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
2011-12-30 20:24:30 +00:00
|
|
|
- Copyright 2011 Joachim Breitner <mail@joachim-breitner.de>
|
2011-12-10 00:27:22 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-12-30 20:24:30 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2011-12-10 00:27:22 +00:00
|
|
|
module Command.Sync where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Command
|
2011-12-29 17:37:30 +00:00
|
|
|
import qualified Remote
|
2011-12-10 00:27:22 +00:00
|
|
|
import qualified Annex.Branch
|
2011-12-14 19:56:11 +00:00
|
|
|
import qualified Git.Command
|
2011-12-13 19:05:07 +00:00
|
|
|
import qualified Git.Config
|
2011-12-15 22:11:42 +00:00
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git
|
2011-12-10 00:27:22 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as L
|
|
|
|
|
|
|
|
def :: [Command]
|
2011-12-29 17:37:30 +00:00
|
|
|
def = [command "sync" (paramOptional (paramRepeating paramRemote))
|
2011-12-30 20:24:30 +00:00
|
|
|
[seek] "synchronize local repository with remote repositories"]
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-10 16:21:22 +00:00
|
|
|
-- syncing involves several operations, any of which can independantly fail
|
2011-12-29 17:37:30 +00:00
|
|
|
seek :: CommandSeek
|
|
|
|
seek args = do
|
2011-12-30 20:24:30 +00:00
|
|
|
!branch <- currentBranch
|
2011-12-30 21:38:38 +00:00
|
|
|
let syncbranch = Git.Ref.under "refs/heads/synced/" branch
|
|
|
|
remotes <- syncRemotes syncbranch args
|
2011-12-30 21:54:09 +00:00
|
|
|
return $ concat $
|
|
|
|
[ [ commit ]
|
|
|
|
, [ mergeLocal branch ]
|
|
|
|
, [ update remote branch | remote <- remotes ]
|
|
|
|
, [ mergeAnnex ]
|
|
|
|
, [ pushLocal syncbranch ]
|
|
|
|
, [ pushRemote remote branch syncbranch | remote <- remotes ]
|
|
|
|
]
|
2011-12-30 20:24:30 +00:00
|
|
|
|
|
|
|
syncRemotes :: Git.Ref -> [String] -> Annex [Remote.Remote Annex]
|
|
|
|
syncRemotes branch [] = defaultSyncRemotes branch
|
|
|
|
syncRemotes _ rs = mapM Remote.byName rs
|
2011-12-29 17:37:30 +00:00
|
|
|
|
2011-12-29 19:50:57 +00:00
|
|
|
defaultSyncRemotes :: Git.Ref -> Annex [Remote.Remote Annex]
|
2011-12-30 21:38:38 +00:00
|
|
|
defaultSyncRemotes syncbranch = mapM Remote.byName
|
2011-12-30 20:24:30 +00:00
|
|
|
=<< process . L.unpack <$> inRepo showref
|
2011-12-30 20:50:05 +00:00
|
|
|
where
|
2011-12-30 20:24:30 +00:00
|
|
|
showref = Git.Command.pipeRead
|
2011-12-30 21:38:38 +00:00
|
|
|
[ Param "show-ref"
|
|
|
|
, Param $ show $ Git.Ref.base syncbranch
|
|
|
|
]
|
2011-12-30 20:24:30 +00:00
|
|
|
process = map getRemoteName . filter isRemote .
|
|
|
|
map getBranchName . lines
|
|
|
|
isRemote r = "refs/remotes/" `isPrefixOf` r
|
|
|
|
getBranchName = snd . separate (== ' ')
|
|
|
|
getRemoteName = fst . separate (== '/') . snd . separate (== '/') . snd . separate (== '/')
|
2011-12-10 00:27:22 +00:00
|
|
|
|
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"
|
2011-12-29 17:37:30 +00:00
|
|
|
[Param "-a", Param "-m", Param "git-annex automatic sync"]
|
2011-12-10 16:21:22 +00:00
|
|
|
return True
|
|
|
|
|
2011-12-29 17:37:30 +00:00
|
|
|
mergeLocal :: Git.Ref -> CommandStart
|
2011-12-30 21:38:38 +00:00
|
|
|
mergeLocal branch = do
|
|
|
|
let mergebranch = Git.Ref.under "refs/heads/synced" branch
|
|
|
|
showStart "merge" $ Git.Ref.describe mergebranch
|
|
|
|
next $ next $ mergeFromIfExists mergebranch
|
2011-12-29 17:37:30 +00:00
|
|
|
|
|
|
|
pushLocal :: Git.Ref -> CommandStart
|
2011-12-30 21:38:38 +00:00
|
|
|
pushLocal syncbranch = go =<< inRepo (Git.Ref.exists syncbranch)
|
|
|
|
where
|
|
|
|
go False = stop
|
|
|
|
go True = do
|
|
|
|
unlessM (updatebranch) $
|
|
|
|
error $ "failed to update " ++ show syncbranch
|
|
|
|
stop
|
|
|
|
updatebranch = inRepo $ Git.Command.runBool "branch"
|
|
|
|
[ Param "-f"
|
|
|
|
, Param $ show $ Git.Ref.base syncbranch
|
|
|
|
]
|
|
|
|
|
|
|
|
mergeFromIfExists :: Git.Ref -> CommandCleanup
|
|
|
|
mergeFromIfExists branch = go =<< inRepo (Git.Ref.exists branch)
|
|
|
|
where
|
|
|
|
go True = do
|
|
|
|
showOutput
|
|
|
|
inRepo $ Git.Command.runBool "merge"
|
|
|
|
[Param (show branch)]
|
|
|
|
go False = do
|
|
|
|
showNote $ Git.Ref.describe branch ++
|
|
|
|
" does not exist, not merging"
|
|
|
|
return False
|
|
|
|
|
|
|
|
update :: Remote.Remote Annex -> Git.Ref -> CommandStart
|
|
|
|
update remote branch = do
|
|
|
|
showStart "update" (Remote.name remote)
|
|
|
|
next $ do
|
2011-12-10 16:21:22 +00:00
|
|
|
checkRemote remote
|
2011-12-30 21:38:38 +00:00
|
|
|
showOutput
|
|
|
|
fetched <- inRepo $ Git.Command.runBool "fetch" [Param (Remote.name remote)]
|
|
|
|
if fetched
|
|
|
|
then next $ mergeRemote remote branch
|
|
|
|
else stop
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-30 21:38:38 +00:00
|
|
|
mergeRemote :: Remote.Remote Annex -> Git.Ref -> CommandCleanup
|
2011-12-30 20:50:05 +00:00
|
|
|
mergeRemote remote = mergeFromIfExists .
|
|
|
|
Git.Ref.under ("refs/remotes/" ++ Remote.name remote ++ "/synced")
|
2011-12-29 17:37:30 +00:00
|
|
|
|
2011-12-30 21:38:38 +00:00
|
|
|
pushRemote :: Remote.Remote Annex -> Git.Ref -> Git.Ref -> CommandStart
|
|
|
|
pushRemote remote branch syncbranch = do
|
|
|
|
showStart "push" (Remote.name remote)
|
2011-12-30 20:50:05 +00:00
|
|
|
let syncbranchRemote = Git.Ref.under
|
|
|
|
("refs/remotes/" ++ Remote.name remote) syncbranch
|
|
|
|
let refspec = show (Git.Ref.base branch) ++ ":" ++ show (Git.Ref.base syncbranch)
|
2011-12-30 20:24:30 +00:00
|
|
|
ex <- inRepo $ Git.Ref.exists syncbranchRemote
|
|
|
|
next $ next $ do
|
|
|
|
showOutput
|
|
|
|
inRepo $ Git.Command.runBool "push" $
|
|
|
|
[ Param (Remote.name remote)
|
2011-12-30 20:50:05 +00:00
|
|
|
, Param (show $ Annex.Branch.name) ] ++
|
2011-12-30 20:24:30 +00:00
|
|
|
[ Param refspec | ex ]
|
2011-12-29 17:37:30 +00:00
|
|
|
|
|
|
|
currentBranch :: Annex Git.Ref
|
|
|
|
currentBranch = Git.Ref . firstLine . L.unpack <$>
|
2011-12-14 19:56:11 +00:00
|
|
|
inRepo (Git.Command.pipeRead [Param "symbolic-ref", Param "HEAD"])
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-29 17:37:30 +00:00
|
|
|
checkRemote :: Remote.Remote Annex -> Annex ()
|
2011-12-10 00:27:22 +00:00
|
|
|
checkRemote remote = do
|
|
|
|
remoteurl <- fromRepo $
|
2011-12-29 17:37:30 +00:00
|
|
|
Git.Config.get ("remote." ++ Remote.name remote ++ ".url") ""
|
|
|
|
when (null remoteurl) $
|
|
|
|
error $ "No url is configured for the remote: " ++ Remote.name remote
|
2011-12-30 20:03:41 +00:00
|
|
|
|
|
|
|
mergeAnnex :: CommandStart
|
2011-12-30 21:38:38 +00:00
|
|
|
mergeAnnex = do
|
2011-12-30 20:03:41 +00:00
|
|
|
Annex.Branch.forceUpdate
|
2011-12-30 21:38:38 +00:00
|
|
|
stop
|