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-31 01:17:36 +00:00
|
|
|
import qualified Annex
|
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-30 22:04:01 +00:00
|
|
|
import qualified Git.Branch
|
2011-12-15 22:11:42 +00:00
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git
|
2011-12-31 01:17:36 +00:00
|
|
|
import qualified Types.Remote
|
2011-12-31 07:27:37 +00:00
|
|
|
import qualified Remote.Git
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-31 01:17:36 +00:00
|
|
|
import qualified Data.Map as M
|
2011-12-10 00:27:22 +00:00
|
|
|
|
|
|
|
def :: [Command]
|
2011-12-29 17:37:30 +00:00
|
|
|
def = [command "sync" (paramOptional (paramRepeating paramRemote))
|
2011-12-31 06:18:16 +00:00
|
|
|
[seek] "synchronize local repository with remotes"]
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-31 06:18:16 +00:00
|
|
|
-- syncing involves several operations, any of which can independently fail
|
2011-12-29 17:37:30 +00:00
|
|
|
seek :: CommandSeek
|
2011-12-31 01:17:36 +00:00
|
|
|
seek rs = do
|
2012-01-02 15:57:02 +00:00
|
|
|
!branch <- fromMaybe nobranch <$> inRepo Git.Branch.current
|
2011-12-31 01:17:36 +00:00
|
|
|
remotes <- syncRemotes rs
|
2011-12-30 21:54:09 +00:00
|
|
|
return $ concat $
|
|
|
|
[ [ commit ]
|
2011-12-30 23:11:22 +00:00
|
|
|
, [ mergeLocal branch ]
|
2011-12-30 22:04:01 +00:00
|
|
|
, [ pullRemote remote branch | remote <- remotes ]
|
2011-12-30 21:54:09 +00:00
|
|
|
, [ mergeAnnex ]
|
2011-12-30 23:11:22 +00:00
|
|
|
, [ pushLocal branch ]
|
|
|
|
, [ pushRemote remote branch | remote <- remotes ]
|
2011-12-30 21:54:09 +00:00
|
|
|
]
|
2011-12-31 07:38:58 +00:00
|
|
|
where
|
|
|
|
nobranch = error "no branch is checked out"
|
2011-12-30 20:24:30 +00:00
|
|
|
|
2011-12-30 23:11:22 +00:00
|
|
|
syncBranch :: Git.Ref -> Git.Ref
|
|
|
|
syncBranch = Git.Ref.under "refs/heads/synced/"
|
2011-12-29 17:37:30 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
remoteBranch :: Remote -> Git.Ref -> Git.Ref
|
2011-12-31 07:01:18 +00:00
|
|
|
remoteBranch remote = Git.Ref.under $ "refs/remotes/" ++ Remote.name remote
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
syncRemotes :: [String] -> Annex [Remote]
|
2011-12-31 01:17:36 +00:00
|
|
|
syncRemotes rs = do
|
|
|
|
fast <- Annex.getState Annex.fast
|
|
|
|
if fast
|
|
|
|
then nub <$> pickfast
|
|
|
|
else wanted
|
2011-12-30 20:50:05 +00:00
|
|
|
where
|
2011-12-31 08:50:39 +00:00
|
|
|
pickfast = (++) <$> listed <*> (good =<< fastest <$> available)
|
2011-12-31 01:17:36 +00:00
|
|
|
wanted
|
2011-12-31 08:50:39 +00:00
|
|
|
| null rs = good =<< available
|
2011-12-31 01:17:36 +00:00
|
|
|
| otherwise = listed
|
|
|
|
listed = mapM Remote.byName rs
|
2011-12-31 07:27:37 +00:00
|
|
|
available = filter nonspecial <$> Remote.enabledRemoteList
|
2011-12-31 08:50:39 +00:00
|
|
|
good = filterM $ Remote.Git.repoAvail . Types.Remote.repo
|
2011-12-31 07:27:37 +00:00
|
|
|
nonspecial r = Types.Remote.remotetype r == Remote.Git.remote
|
2011-12-31 01:17:36 +00:00
|
|
|
fastest = fromMaybe [] . headMaybe .
|
|
|
|
map snd . sort . M.toList . costmap
|
|
|
|
costmap = M.fromListWith (++) . map costpair
|
|
|
|
costpair r = (Types.Remote.cost r, [r])
|
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-30 23:11:22 +00:00
|
|
|
mergeLocal :: Git.Ref -> CommandStart
|
|
|
|
mergeLocal branch = go =<< needmerge
|
2011-12-30 22:04:01 +00:00
|
|
|
where
|
2011-12-30 23:11:22 +00:00
|
|
|
syncbranch = syncBranch branch
|
2011-12-30 22:19:45 +00:00
|
|
|
needmerge = do
|
2011-12-30 22:52:24 +00:00
|
|
|
unlessM (inRepo $ Git.Ref.exists syncbranch) $
|
|
|
|
updateBranch syncbranch
|
|
|
|
inRepo $ Git.Branch.changed branch syncbranch
|
2011-12-30 22:04:01 +00:00
|
|
|
go False = stop
|
|
|
|
go True = do
|
2011-12-30 22:52:24 +00:00
|
|
|
showStart "merge" $ Git.Ref.describe syncbranch
|
2011-12-31 05:51:39 +00:00
|
|
|
next $ next $ mergeFrom syncbranch
|
2011-12-29 17:37:30 +00:00
|
|
|
|
|
|
|
pushLocal :: Git.Ref -> CommandStart
|
2011-12-31 07:08:41 +00:00
|
|
|
pushLocal branch = do
|
|
|
|
updateBranch $ syncBranch branch
|
|
|
|
stop
|
2011-12-30 22:52:24 +00:00
|
|
|
|
|
|
|
updateBranch :: Git.Ref -> Annex ()
|
|
|
|
updateBranch syncbranch =
|
|
|
|
unlessM go $ error $ "failed to update " ++ show syncbranch
|
|
|
|
where
|
|
|
|
go = inRepo $ Git.Command.runBool "branch"
|
2011-12-30 21:38:38 +00:00
|
|
|
[ Param "-f"
|
|
|
|
, Param $ show $ Git.Ref.base syncbranch
|
|
|
|
]
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
pullRemote :: Remote -> Git.Ref -> CommandStart
|
2011-12-30 22:04:01 +00:00
|
|
|
pullRemote remote branch = do
|
|
|
|
showStart "pull" (Remote.name remote)
|
2011-12-30 21:38:38 +00:00
|
|
|
next $ do
|
|
|
|
showOutput
|
2011-12-30 22:19:45 +00:00
|
|
|
fetched <- inRepo $ Git.Command.runBool "fetch"
|
|
|
|
[Param $ Remote.name remote]
|
2011-12-30 21:38:38 +00:00
|
|
|
if fetched
|
|
|
|
then next $ mergeRemote remote branch
|
|
|
|
else stop
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-30 23:11:22 +00:00
|
|
|
{- The remote probably has both a master and a synced/master branch.
|
|
|
|
- Which to merge from? Well, the master has whatever latest changes
|
|
|
|
- were committed, while the synced/master may have changes that some
|
|
|
|
- other remote synced to this remote. So, merge them both. -}
|
2011-12-31 08:11:39 +00:00
|
|
|
mergeRemote :: Remote -> Git.Ref -> CommandCleanup
|
2011-12-31 06:45:12 +00:00
|
|
|
mergeRemote remote branch = all id <$> (mapM merge =<< tomerge)
|
2011-12-30 23:11:22 +00:00
|
|
|
where
|
2011-12-31 07:01:18 +00:00
|
|
|
merge = mergeFrom . remoteBranch remote
|
|
|
|
tomerge = filterM (changed remote) [branch, syncBranch branch]
|
2011-12-30 23:11:22 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
pushRemote :: Remote -> Git.Ref -> CommandStart
|
2011-12-30 23:38:46 +00:00
|
|
|
pushRemote remote branch = go =<< needpush
|
2011-12-30 22:04:01 +00:00
|
|
|
where
|
2011-12-31 07:01:18 +00:00
|
|
|
needpush = anyM (newer remote) [syncbranch, Annex.Branch.name]
|
2011-12-30 22:04:01 +00:00
|
|
|
go False = stop
|
2011-12-30 22:19:45 +00:00
|
|
|
go True = do
|
2011-12-30 22:04:01 +00:00
|
|
|
showStart "push" (Remote.name remote)
|
|
|
|
next $ next $ do
|
|
|
|
showOutput
|
|
|
|
inRepo $ Git.Command.runBool "push" $
|
|
|
|
[ Param (Remote.name remote)
|
2011-12-30 23:11:22 +00:00
|
|
|
, Param (show $ Annex.Branch.name)
|
|
|
|
, Param refspec
|
|
|
|
]
|
2011-12-30 22:04:01 +00:00
|
|
|
refspec = show (Git.Ref.base branch) ++ ":" ++ show (Git.Ref.base syncbranch)
|
2011-12-30 23:11:22 +00:00
|
|
|
syncbranch = syncBranch branch
|
2011-12-29 17:37:30 +00:00
|
|
|
|
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
|
2011-12-31 06:18:16 +00:00
|
|
|
|
|
|
|
mergeFrom :: Git.Ref -> CommandCleanup
|
|
|
|
mergeFrom branch = do
|
|
|
|
showOutput
|
|
|
|
inRepo $ Git.Command.runBool "merge" [Param $ show branch]
|
2011-12-31 07:01:18 +00:00
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
changed :: Remote -> Git.Ref -> Annex Bool
|
2011-12-31 07:01:18 +00:00
|
|
|
changed remote b = do
|
|
|
|
let r = remoteBranch remote b
|
|
|
|
e <- inRepo $ Git.Ref.exists r
|
|
|
|
if e
|
|
|
|
then inRepo $ Git.Branch.changed b r
|
|
|
|
else return False
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
newer :: Remote -> Git.Ref -> Annex Bool
|
2011-12-31 07:01:18 +00:00
|
|
|
newer remote b = do
|
|
|
|
let r = remoteBranch remote b
|
|
|
|
e <- inRepo $ Git.Ref.exists r
|
|
|
|
if e
|
|
|
|
then inRepo $ Git.Branch.changed r b
|
|
|
|
else return True
|