2012-06-22 21:01:08 +00:00
|
|
|
{- git-annex assistant git merge thread
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
2012-06-23 05:20:40 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2012-06-22 21:01:08 +00:00
|
|
|
-}
|
|
|
|
|
2012-06-25 20:10:10 +00:00
|
|
|
module Assistant.Threads.Merger where
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Assistant.ThreadedMonad
|
|
|
|
import Utility.DirWatcher
|
|
|
|
import Utility.Types.DirWatcher
|
2012-06-28 00:50:50 +00:00
|
|
|
import qualified Annex.Branch
|
2012-06-22 21:01:08 +00:00
|
|
|
import qualified Git
|
2012-06-28 00:30:04 +00:00
|
|
|
import qualified Git.Command
|
2012-06-23 14:29:46 +00:00
|
|
|
import qualified Git.Merge
|
2012-06-22 21:01:08 +00:00
|
|
|
import qualified Git.Branch
|
|
|
|
import qualified Command.Sync
|
2012-06-28 00:30:04 +00:00
|
|
|
import qualified Remote
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
{- This thread watches for changes to .git/refs/heads/synced/*,
|
|
|
|
- which indicate incoming pushes. It merges those pushes into the
|
|
|
|
- currently checked out branch. -}
|
|
|
|
mergeThread :: ThreadState -> IO ()
|
|
|
|
mergeThread st = do
|
|
|
|
g <- runThreadState st $ fromRepo id
|
|
|
|
let dir = Git.localGitDir g </> "refs" </> "heads" </> "synced"
|
|
|
|
createDirectoryIfMissing True dir
|
2012-06-28 00:50:50 +00:00
|
|
|
let hook a = Just $ runHandler g a
|
|
|
|
-- XXX: For reasons currently unknown, using the ThreadState
|
|
|
|
-- inside the watch hooks leads to a MVar deadlock.
|
|
|
|
-- Luckily, we don't currently need to do that.
|
2012-06-22 21:01:08 +00:00
|
|
|
let hooks = mkWatchHooks
|
|
|
|
{ addHook = hook onAdd
|
|
|
|
, errHook = hook onErr
|
|
|
|
}
|
|
|
|
watchDir dir (const False) hooks id
|
|
|
|
|
2012-06-28 00:50:50 +00:00
|
|
|
type Handler = Git.Repo -> FilePath -> Maybe FileStatus -> IO ()
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
{- Runs an action handler.
|
|
|
|
-
|
|
|
|
- Exceptions are ignored, otherwise a whole thread could be crashed.
|
|
|
|
-}
|
2012-06-28 00:50:50 +00:00
|
|
|
runHandler :: Git.Repo -> Handler -> FilePath -> Maybe FileStatus -> IO ()
|
|
|
|
runHandler g handler file filestatus = void $ do
|
2012-06-22 21:01:08 +00:00
|
|
|
either print (const noop) =<< tryIO go
|
|
|
|
where
|
2012-06-28 00:50:50 +00:00
|
|
|
go = handler g file filestatus
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
{- Called when there's an error with inotify. -}
|
|
|
|
onErr :: Handler
|
2012-06-28 00:50:50 +00:00
|
|
|
onErr _ msg _ = error msg
|
2012-06-22 21:01:08 +00:00
|
|
|
|
|
|
|
{- Called when a new branch ref is written.
|
|
|
|
-
|
|
|
|
- This relies on git's atomic method of updating branch ref files,
|
|
|
|
- which is to first write the new file to .lock, and then rename it
|
|
|
|
- over the old file. So, ignore .lock files, and the rename ensures
|
|
|
|
- the watcher sees a new file being added on each update.
|
|
|
|
-
|
|
|
|
- At startup, synthetic add events fire, causing this to run, but that's
|
|
|
|
- ok; it ensures that any changes pushed since the last time the assistant
|
|
|
|
- ran are merged in.
|
|
|
|
-}
|
|
|
|
onAdd :: Handler
|
2012-06-28 00:50:50 +00:00
|
|
|
onAdd g file _
|
2012-06-22 21:01:08 +00:00
|
|
|
| ".lock" `isSuffixOf` file = noop
|
|
|
|
| otherwise = do
|
2012-06-28 00:06:21 +00:00
|
|
|
let changedbranch = Git.Ref $
|
|
|
|
"refs" </> "heads" </> takeFileName file
|
2012-06-22 21:01:08 +00:00
|
|
|
current <- Git.Branch.current g
|
2012-06-28 00:06:21 +00:00
|
|
|
when (Just changedbranch == current) $
|
2012-06-28 00:50:50 +00:00
|
|
|
void $ mergeBranch changedbranch g
|
2012-06-22 21:01:08 +00:00
|
|
|
|
2012-06-28 00:50:50 +00:00
|
|
|
mergeBranch :: Git.Ref -> Git.Repo -> IO Bool
|
|
|
|
mergeBranch = Git.Merge.mergeNonInteractive . Command.Sync.syncBranch
|
2012-06-26 00:16:30 +00:00
|
|
|
|
|
|
|
{- Manually pull from remotes and merge their branches. Called by the pusher
|
|
|
|
- when a push fails, which can happen due to a remote not having pushed
|
|
|
|
- changes to us. That could be because it doesn't have us as a remote, or
|
|
|
|
- because the assistant is not running there, or other reasons. -}
|
|
|
|
manualPull :: Git.Ref -> [Remote] -> Annex ()
|
2012-06-28 00:50:50 +00:00
|
|
|
manualPull currentbranch remotes = do
|
|
|
|
forM_ remotes $ \r ->
|
|
|
|
inRepo $ Git.Command.runBool "fetch" [Param $ Remote.name r]
|
|
|
|
Annex.Branch.forceUpdate
|
|
|
|
forM_ remotes $ \r ->
|
|
|
|
Command.Sync.mergeRemote r currentbranch
|