2011-12-10 00:27:22 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2011-12-30 20:24:30 +00:00
|
|
|
- Copyright 2011 Joachim Breitner <mail@joachim-breitner.de>
|
2012-12-25 18:10:07 +00:00
|
|
|
- Copyright 2011,2012 Joey Hess <joey@kitenet.net>
|
2011-12-10 00:27:22 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
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
|
2012-06-27 17:08:32 +00:00
|
|
|
import qualified Annex.Queue
|
|
|
|
import Annex.Content
|
2012-12-12 23:20:38 +00:00
|
|
|
import Annex.Direct
|
2012-06-27 17:08:32 +00:00
|
|
|
import Annex.CatFile
|
2011-12-14 19:56:11 +00:00
|
|
|
import qualified Git.Command
|
2012-06-27 17:08:32 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
2012-06-23 14:22:56 +00:00
|
|
|
import qualified Git.Merge
|
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
|
2012-06-27 17:08:32 +00:00
|
|
|
import Git.Types (BlobType(..))
|
2011-12-31 01:17:36 +00:00
|
|
|
import qualified Types.Remote
|
2011-12-31 07:27:37 +00:00
|
|
|
import qualified Remote.Git
|
2012-08-08 20:06:01 +00:00
|
|
|
import Types.Key
|
2012-10-11 22:39:21 +00:00
|
|
|
import Config
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2012-06-27 20:03:42 +00:00
|
|
|
import Data.Hash.MD5
|
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-08-05 19:45:47 +00:00
|
|
|
branch <- fromMaybe nobranch <$> inRepo Git.Branch.current
|
2011-12-31 01:17:36 +00:00
|
|
|
remotes <- syncRemotes rs
|
2012-02-16 04:41:30 +00:00
|
|
|
return $ concat
|
2011-12-30 21:54:09 +00:00
|
|
|
[ [ 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
|
|
|
]
|
2012-11-12 05:05:04 +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]
|
2012-03-14 21:43:34 +00:00
|
|
|
syncRemotes rs = ifM (Annex.getState Annex.fast) ( nub <$> pickfast , wanted )
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
pickfast = (++) <$> listed <*> (good =<< fastest <$> available)
|
|
|
|
wanted
|
|
|
|
| null rs = good =<< concat . Remote.byCost <$> available
|
|
|
|
| otherwise = listed
|
|
|
|
listed = do
|
|
|
|
l <- catMaybes <$> mapM (Remote.byName . Just) rs
|
|
|
|
let s = filter Remote.specialRemote l
|
|
|
|
unless (null s) $
|
|
|
|
error $ "cannot sync special remotes: " ++
|
|
|
|
unwords (map Types.Remote.name s)
|
|
|
|
return l
|
|
|
|
available = filter (not . Remote.specialRemote)
|
2013-01-01 17:52:47 +00:00
|
|
|
. filter (remoteAnnexSync . Types.Remote.gitconfig)
|
|
|
|
<$> Remote.enabledRemoteList
|
2012-11-12 05:05:04 +00:00
|
|
|
good = filterM $ Remote.Git.repoAvail . Types.Remote.repo
|
|
|
|
fastest = fromMaybe [] . headMaybe . Remote.byCost
|
2011-12-10 00:27:22 +00:00
|
|
|
|
2011-12-10 16:21:22 +00:00
|
|
|
commit :: CommandStart
|
2012-12-12 23:20:38 +00:00
|
|
|
commit = next $ next $ do
|
|
|
|
ifM isDirect
|
|
|
|
( ifM stageDirect
|
|
|
|
( runcommit [] , return True )
|
|
|
|
, runcommit [Param "-a"]
|
|
|
|
)
|
|
|
|
where
|
|
|
|
runcommit ps = do
|
|
|
|
showStart "commit" ""
|
2011-12-10 16:21:22 +00:00
|
|
|
showOutput
|
2012-12-13 19:58:38 +00:00
|
|
|
Annex.Branch.commit "update"
|
2011-12-10 16:21:22 +00:00
|
|
|
-- Commit will fail when the tree is clean, so ignore failure.
|
2012-12-12 23:20:38 +00:00
|
|
|
_ <- inRepo $ Git.Command.runBool "commit" $ ps ++
|
|
|
|
[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
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
syncbranch = syncBranch branch
|
|
|
|
needmerge = do
|
|
|
|
unlessM (inRepo $ Git.Ref.exists syncbranch) $
|
|
|
|
inRepo $ updateBranch syncbranch
|
|
|
|
inRepo $ Git.Branch.changed branch syncbranch
|
|
|
|
go False = stop
|
|
|
|
go True = do
|
|
|
|
showStart "merge" $ Git.Ref.describe syncbranch
|
|
|
|
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
|
2012-06-22 19:46:21 +00:00
|
|
|
inRepo $ updateBranch $ syncBranch branch
|
2011-12-31 07:08:41 +00:00
|
|
|
stop
|
2011-12-30 22:52:24 +00:00
|
|
|
|
2012-06-22 19:46:21 +00:00
|
|
|
updateBranch :: Git.Ref -> Git.Repo -> IO ()
|
|
|
|
updateBranch syncbranch g =
|
2011-12-30 22:52:24 +00:00
|
|
|
unlessM go $ error $ "failed to update " ++ show syncbranch
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
go = Git.Command.runBool "branch"
|
|
|
|
[ Param "-f"
|
|
|
|
, Param $ show $ Git.Ref.base syncbranch
|
|
|
|
] g
|
2011-12-30 21:38:38 +00:00
|
|
|
|
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
|
2012-03-14 21:43:34 +00:00
|
|
|
stopUnless fetch $
|
2012-08-05 20:35:30 +00:00
|
|
|
next $ mergeRemote remote (Just branch)
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
fetch = inRepo $ Git.Command.runBool "fetch"
|
|
|
|
[Param $ Remote.name remote]
|
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
|
2012-12-12 17:06:03 +00:00
|
|
|
- other remote synced to this remote. So, merge them both. -}
|
2012-08-05 20:35:30 +00:00
|
|
|
mergeRemote :: Remote -> (Maybe Git.Ref) -> CommandCleanup
|
|
|
|
mergeRemote remote b = case b of
|
|
|
|
Nothing -> do
|
|
|
|
branch <- inRepo Git.Branch.currentUnsafe
|
2012-12-12 17:06:03 +00:00
|
|
|
all id <$> (mapM merge $ branchlist branch)
|
2012-12-12 23:20:38 +00:00
|
|
|
Just _ -> all id <$> (mapM merge =<< tomerge (branchlist b))
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
merge = mergeFrom . remoteBranch remote
|
|
|
|
tomerge branches = filterM (changed remote) branches
|
|
|
|
branchlist Nothing = []
|
|
|
|
branchlist (Just branch) = [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
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
needpush = anyM (newer remote) [syncBranch branch, Annex.Branch.name]
|
|
|
|
go False = stop
|
|
|
|
go True = do
|
|
|
|
showStart "push" (Remote.name remote)
|
|
|
|
next $ next $ do
|
|
|
|
showOutput
|
|
|
|
inRepo $ pushBranch remote branch
|
2012-06-22 19:46:21 +00:00
|
|
|
|
|
|
|
pushBranch :: Remote -> Git.Ref -> Git.Repo -> IO Bool
|
|
|
|
pushBranch remote branch g =
|
|
|
|
Git.Command.runBool "push"
|
2012-09-16 21:54:12 +00:00
|
|
|
[ Param $ Remote.name remote
|
|
|
|
, Param $ refspec Annex.Branch.name
|
|
|
|
, Param $ refspec branch
|
2012-06-22 19:46:21 +00:00
|
|
|
] g
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
refspec b = concat
|
|
|
|
[ show $ Git.Ref.base b
|
|
|
|
, ":"
|
|
|
|
, show $ Git.Ref.base $ syncBranch b
|
|
|
|
]
|
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
|
avoid unnecessary transfer scans when syncing a disconnected remote
Found a very cheap way to determine when a disconnected remote has
diverged, and has new content that needs to be transferred: Piggyback on
the git-annex branch update, which already checks for divergence.
However, this does not check if new content has appeared locally while
disconnected, that should be transferred to the remote.
Also, this does not handle cases where the two git repos are in sync,
but their content syncing has not caught up yet.
This code could have its efficiency improved:
* When multiple remotes are synced, if any one has diverged, they're
all queued for transfer scans.
* The transfer scanner could be told whether the remote has new content,
the local repo has new content, or both, and could optimise its scan
accordingly.
2012-08-22 18:51:11 +00:00
|
|
|
void $ Annex.Branch.forceUpdate
|
2011-12-30 21:38:38 +00:00
|
|
|
stop
|
2011-12-31 06:18:16 +00:00
|
|
|
|
2012-12-18 19:04:44 +00:00
|
|
|
{- Merges from a branch into the current branch. -}
|
2012-06-27 16:09:01 +00:00
|
|
|
mergeFrom :: Git.Ref -> Annex Bool
|
2012-12-18 19:04:44 +00:00
|
|
|
mergeFrom branch = do
|
|
|
|
showOutput
|
|
|
|
ifM isDirect
|
|
|
|
( maybe go godirect =<< inRepo Git.Branch.current
|
|
|
|
, go
|
|
|
|
)
|
2012-12-12 17:06:03 +00:00
|
|
|
where
|
2012-12-18 19:04:44 +00:00
|
|
|
go = runmerge $ inRepo $ Git.Merge.mergeNonInteractive branch
|
2012-12-12 17:06:03 +00:00
|
|
|
godirect currbranch = do
|
|
|
|
old <- inRepo $ Git.Ref.sha currbranch
|
2012-12-18 19:04:44 +00:00
|
|
|
d <- fromRepo gitAnnexMergeDir
|
|
|
|
r <- runmerge $ inRepo $ mergeDirect d branch
|
2012-12-12 17:06:03 +00:00
|
|
|
new <- inRepo $ Git.Ref.sha currbranch
|
|
|
|
case (old, new) of
|
2012-12-18 19:04:44 +00:00
|
|
|
(Just oldsha, Just newsha) ->
|
|
|
|
mergeDirectCleanup d oldsha newsha
|
2012-12-12 17:06:03 +00:00
|
|
|
_ -> noop
|
|
|
|
return r
|
2012-12-18 19:04:44 +00:00
|
|
|
runmerge a = ifM (a)
|
|
|
|
( return True
|
|
|
|
, resolveMerge
|
|
|
|
)
|
2012-06-27 16:09:01 +00:00
|
|
|
|
|
|
|
{- Resolves a conflicted merge. It's important that any conflicts be
|
|
|
|
- resolved in a way that itself avoids later merge conflicts, since
|
|
|
|
- multiple repositories may be doing this concurrently.
|
|
|
|
-
|
|
|
|
- Only annexed files are resolved; other files are left for the user to
|
|
|
|
- handle.
|
|
|
|
-
|
|
|
|
- This uses the Keys pointed to by the files to construct new
|
2012-06-27 17:08:32 +00:00
|
|
|
- filenames. So when both sides modified file foo,
|
|
|
|
- it will be deleted, and replaced with files foo.KEYA and foo.KEYB.
|
2012-06-27 16:09:01 +00:00
|
|
|
-
|
2012-06-27 17:08:32 +00:00
|
|
|
- On the other hand, when one side deleted foo, and the other modified it,
|
|
|
|
- it will be deleted, and the modified version stored as file
|
|
|
|
- foo.KEYA (or KEYB).
|
2012-06-27 16:09:01 +00:00
|
|
|
-}
|
|
|
|
resolveMerge :: Annex Bool
|
|
|
|
resolveMerge = do
|
2012-06-27 17:08:32 +00:00
|
|
|
top <- fromRepo Git.repoPath
|
2012-10-04 23:56:32 +00:00
|
|
|
(fs, cleanup) <- inRepo (LsFiles.unmerged [top])
|
|
|
|
merged <- all id <$> mapM resolveMerge' fs
|
2013-01-18 01:19:00 +00:00
|
|
|
void $ liftIO cleanup
|
|
|
|
|
|
|
|
(deleted, cleanup2) <- inRepo (LsFiles.deleted [top])
|
2013-02-05 19:11:05 +00:00
|
|
|
unless (null deleted) $
|
|
|
|
Annex.Queue.addCommand "rm" [Params "--quiet -f --"] deleted
|
2013-01-18 01:19:00 +00:00
|
|
|
void $ liftIO cleanup2
|
|
|
|
|
2012-06-27 19:03:13 +00:00
|
|
|
when merged $ do
|
|
|
|
Annex.Queue.flush
|
|
|
|
void $ inRepo $ Git.Command.runBool "commit"
|
2012-06-27 20:03:42 +00:00
|
|
|
[Param "-m", Param "git-annex automatic merge conflict fix"]
|
2012-06-27 19:03:13 +00:00
|
|
|
return merged
|
2012-06-27 17:08:32 +00:00
|
|
|
|
|
|
|
resolveMerge' :: LsFiles.Unmerged -> Annex Bool
|
|
|
|
resolveMerge' u
|
2012-06-27 17:35:02 +00:00
|
|
|
| issymlink LsFiles.valUs && issymlink LsFiles.valThem =
|
|
|
|
withKey LsFiles.valUs $ \keyUs ->
|
2012-12-18 21:15:16 +00:00
|
|
|
withKey LsFiles.valThem $ \keyThem -> do
|
|
|
|
go keyUs keyThem
|
2012-06-27 17:35:02 +00:00
|
|
|
| otherwise = return False
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
go keyUs keyThem
|
|
|
|
| keyUs == keyThem = do
|
|
|
|
makelink keyUs
|
|
|
|
return True
|
|
|
|
| otherwise = do
|
2012-12-18 21:15:16 +00:00
|
|
|
ifM isDirect
|
|
|
|
( maybe noop (\k -> removeDirect k file) keyUs
|
|
|
|
, liftIO $ nukeFile file
|
|
|
|
)
|
2012-11-12 05:05:04 +00:00
|
|
|
Annex.Queue.addCommand "rm" [Params "--quiet -f --"] [file]
|
|
|
|
makelink keyUs
|
|
|
|
makelink keyThem
|
|
|
|
return True
|
|
|
|
file = LsFiles.unmergedFile u
|
|
|
|
issymlink select = any (select (LsFiles.unmergedBlobType u) ==)
|
|
|
|
[Just SymlinkBlob, Nothing]
|
|
|
|
makelink (Just key) = do
|
|
|
|
let dest = mergeFile file key
|
|
|
|
l <- calcGitLink dest key
|
|
|
|
liftIO $ do
|
|
|
|
nukeFile dest
|
|
|
|
createSymbolicLink l dest
|
|
|
|
Annex.Queue.addCommand "add" [Param "--force", Param "--"] [dest]
|
2012-12-18 21:15:16 +00:00
|
|
|
whenM (isDirect) $
|
|
|
|
toDirect key dest
|
2012-11-12 05:05:04 +00:00
|
|
|
makelink _ = noop
|
|
|
|
withKey select a = do
|
|
|
|
let msha = select $ LsFiles.unmergedSha u
|
|
|
|
case msha of
|
|
|
|
Nothing -> a Nothing
|
|
|
|
Just sha -> do
|
2012-12-12 23:20:38 +00:00
|
|
|
key <- catKey sha
|
2012-11-12 05:05:04 +00:00
|
|
|
maybe (return False) (a . Just) key
|
2012-06-27 20:03:42 +00:00
|
|
|
|
|
|
|
{- The filename to use when resolving a conflicted merge of a file,
|
|
|
|
- that points to a key.
|
|
|
|
-
|
|
|
|
- Something derived from the key needs to be included in the filename,
|
|
|
|
- but rather than exposing the whole key to the user, a very weak hash
|
|
|
|
- is used. There is a very real, although still unlikely, chance of
|
|
|
|
- conflicts using this hash.
|
|
|
|
-
|
|
|
|
- In the event that there is a conflict with the filename generated
|
|
|
|
- for some other key, that conflict will itself be handled by the
|
|
|
|
- conflicted merge resolution code. That case is detected, and the full
|
|
|
|
- key is used in the filename.
|
|
|
|
-}
|
|
|
|
mergeFile :: FilePath -> Key -> FilePath
|
|
|
|
mergeFile file key
|
2012-08-08 20:06:01 +00:00
|
|
|
| doubleconflict = go $ key2file key
|
|
|
|
| otherwise = go $ shortHash $ key2file key
|
2012-11-12 05:05:04 +00:00
|
|
|
where
|
|
|
|
varmarker = ".variant-"
|
|
|
|
doubleconflict = varmarker `isSuffixOf` (dropExtension file)
|
|
|
|
go v = takeDirectory file
|
|
|
|
</> dropExtension (takeFileName file)
|
|
|
|
++ varmarker ++ v
|
|
|
|
++ takeExtension file
|
2012-06-27 20:03:42 +00:00
|
|
|
|
|
|
|
shortHash :: String -> String
|
2012-09-13 23:14:00 +00:00
|
|
|
shortHash = take 4 . md5s . md5FilePath
|
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
|
2012-03-14 21:43:34 +00:00
|
|
|
ifM (inRepo $ Git.Ref.exists r)
|
|
|
|
( inRepo $ Git.Branch.changed b r
|
|
|
|
, return False
|
|
|
|
)
|
2011-12-31 07:01:18 +00:00
|
|
|
|
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
|
2012-03-14 21:43:34 +00:00
|
|
|
ifM (inRepo $ Git.Ref.exists r)
|
|
|
|
( inRepo $ Git.Branch.changed r b
|
|
|
|
, return True
|
|
|
|
)
|