summaryrefslogtreecommitdiff
path: root/Command/Sync.hs
blob: c0d7f37ad0a47159aae568a9ade9532814192397 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{- git-annex command
 -
 - Copyright 2011 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Command.Sync where

import Common.Annex
import Command
import qualified Annex.Branch
import qualified Git

import qualified Data.ByteString.Lazy.Char8 as L

def :: [Command]
def = [command "sync" paramPaths seek "synchronize local repository with remote"]

seek :: [CommandSeek]
seek = [withNothing start]

start :: CommandStart
start = do
        showStart "sync" "."
	showOutput
        next perform

perform :: CommandPerform
perform = do
	remote <- defaultRemote =<< currentBranch
	checkRemote remote
	commit
	inRepo $ Git.run "pull" [Param remote]
	Annex.Branch.update
	inRepo $ Git.run "push" [Param remote, matchingbranches]
	next $ return True
	where
		-- git push may be configured to not push matching
		-- branches; this should ensure it always does.
		matchingbranches = Param ":"

commit :: Annex ()
commit = do
	-- Commit will fail when the tree is clean (or when in a confliced
	-- merge, etc). Ignore failure.
	_ <- inRepo $ Git.runBool "commit" [Param "-a", Param "-m", Param "sync"]
	return ()

-- the remote defaults to origin when not configured
defaultRemote :: String -> Annex String
defaultRemote branch =
	fromRepo $ Git.configGet ("branch." ++ branch ++ ".remote") "origin"

currentBranch :: Annex String
currentBranch = last . split "/" . L.unpack . head . L.lines <$>
	inRepo (Git.pipeRead [Param "symbolic-ref", Param "HEAD"])

checkRemote :: String -> Annex ()
checkRemote remote = do
	remoteurl <- fromRepo $
		Git.configGet ("remote." ++ remote ++ ".url") ""
	when (null remoteurl) $ do
		error $ "No url is configured for the remote: " ++ remote