summaryrefslogtreecommitdiff
path: root/Command
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2011-12-09 20:27:22 -0400
committerGravatar Joey Hess <joey@kitenet.net>2011-12-09 20:27:22 -0400
commitfb8231f3a1eb67e61a7df0381dc82be2f0ca0417 (patch)
treea082bdf63554745c1fd31c9a4b5db9cbb99f483e /Command
parent4b3c4c0c2b88aa17a9b7822470e3e5dd2a3e7c2b (diff)
sync: New command that synchronises the local repository and default remote, by running git commit, pull, and push for you.
Diffstat (limited to 'Command')
-rw-r--r--Command/Sync.hs64
1 files changed, 64 insertions, 0 deletions
diff --git a/Command/Sync.hs b/Command/Sync.hs
new file mode 100644
index 000000000..c0d7f37ad
--- /dev/null
+++ b/Command/Sync.hs
@@ -0,0 +1,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