git-annex/Assistant/Threads/Merger.hs

97 lines
2.7 KiB
Haskell
Raw Normal View History

{- 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-08-26 18:14:12 +00:00
module Assistant.Threads.Merger where
import Assistant.Common
import Assistant.TransferQueue
import Assistant.BranchChange
import Utility.DirWatcher
import Utility.Types.DirWatcher
import qualified Annex.Branch
import qualified Git
import qualified Git.Branch
import qualified Command.Sync
{- This thread watches for changes to .git/refs/, and handles incoming
- pushes. -}
2012-10-29 15:40:22 +00:00
mergeThread :: NamedThread
mergeThread = namedThread "Merger" $ do
2012-10-29 15:40:22 +00:00
g <- liftAnnex gitRepo
let dir = Git.localGitDir g </> "refs"
2012-10-29 15:40:22 +00:00
liftIO $ createDirectoryIfMissing True dir
let hook a = Just <$> asIO2 (runHandler a)
addhook <- hook onAdd
errhook <- hook onErr
let hooks = mkWatchHooks
2012-10-29 15:40:22 +00:00
{ addHook = addhook
, errHook = errhook
}
2012-10-29 15:40:22 +00:00
void $ liftIO $ watchDir dir (const False) hooks id
debug ["watching", dir]
2012-10-29 15:40:22 +00:00
type Handler = FilePath -> Assistant ()
{- Runs an action handler.
-
- Exceptions are ignored, otherwise a whole thread could be crashed.
-}
2012-10-29 15:40:22 +00:00
runHandler :: Handler -> FilePath -> Maybe FileStatus -> Assistant ()
runHandler handler file _filestatus =
either (liftIO . print) (const noop) =<< tryIO <~> handler file
{- Called when there's an error with inotify. -}
onErr :: Handler
2012-10-29 15:40:22 +00:00
onErr msg = error msg
{- 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-10-29 15:40:22 +00:00
onAdd file
| ".lock" `isSuffixOf` file = noop
| isAnnexBranch file = do
2012-10-29 23:20:54 +00:00
branchChanged
whenM (liftAnnex Annex.Branch.forceUpdate) $
queueDeferredDownloads "retrying deferred download" Later
2012-10-29 15:40:22 +00:00
| "/synced/" `isInfixOf` file = do
mergecurrent =<< liftAnnex (inRepo Git.Branch.current)
| otherwise = noop
2012-10-29 15:40:22 +00:00
where
changedbranch = fileToBranch file
mergecurrent (Just current)
| equivBranches changedbranch current = do
debug
[ "merging", show changedbranch
, "into", show current
]
void $ liftAnnex $ Command.Sync.mergeFrom changedbranch
2012-10-29 15:40:22 +00:00
mergecurrent _ = noop
equivBranches :: Git.Ref -> Git.Ref -> Bool
equivBranches x y = base x == base y
2012-10-31 06:34:03 +00:00
where
base = takeFileName . show
isAnnexBranch :: FilePath -> Bool
isAnnexBranch f = n `isSuffixOf` f
2012-10-31 06:34:03 +00:00
where
n = "/" ++ show Annex.Branch.name
fileToBranch :: FilePath -> Git.Ref
fileToBranch f = Git.Ref $ "refs" </> base
2012-10-31 06:34:03 +00:00
where
base = Prelude.last $ split "/refs/" f