2011-07-06 00:36:43 +00:00
|
|
|
{- git-annex remote access with ssh
|
2011-03-05 19:47:00 +00:00
|
|
|
-
|
2012-07-02 14:57:51 +00:00
|
|
|
- Copyright 2011.2012 Joey Hess <joey@kitenet.net>
|
2011-03-05 19:47:00 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-01-10 19:29:10 +00:00
|
|
|
module Remote.Helper.Ssh where
|
2011-03-05 19:47:00 +00:00
|
|
|
|
2012-01-20 19:34:52 +00:00
|
|
|
import Common.Annex
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-12-14 19:30:14 +00:00
|
|
|
import qualified Git.Url
|
2011-03-28 01:43:25 +00:00
|
|
|
import Config
|
2011-10-15 21:47:03 +00:00
|
|
|
import Annex.UUID
|
2012-01-20 19:34:52 +00:00
|
|
|
import Annex.Ssh
|
2012-07-02 14:57:51 +00:00
|
|
|
import Fields
|
2011-03-05 19:47:00 +00:00
|
|
|
|
|
|
|
{- Generates parameters to ssh to a repository's host and run a command.
|
|
|
|
- Caller is responsible for doing any neccessary shellEscaping of the
|
|
|
|
- passed command. -}
|
|
|
|
sshToRepo :: Git.Repo -> [CommandParam] -> Annex [CommandParam]
|
|
|
|
sshToRepo repo sshcmd = do
|
2012-03-22 03:41:01 +00:00
|
|
|
opts <- map Param . words <$> getRemoteConfig repo "ssh-options" ""
|
2012-01-20 21:32:32 +00:00
|
|
|
params <- sshParams (Git.Url.hostuser repo, Git.Url.port repo) opts
|
|
|
|
return $ params ++ sshcmd
|
2011-04-09 18:26:32 +00:00
|
|
|
|
|
|
|
{- Generates parameters to run a git-annex-shell command on a remote
|
|
|
|
- repository. -}
|
2012-07-02 14:57:51 +00:00
|
|
|
git_annex_shell :: Git.Repo -> String -> [CommandParam] -> [(Field, String)] -> Annex (Maybe (FilePath, [CommandParam]))
|
|
|
|
git_annex_shell r command params fields
|
|
|
|
| not $ Git.repoIsUrl r = return $ Just (shellcmd, shellopts ++ fieldopts)
|
2011-04-09 18:26:32 +00:00
|
|
|
| Git.repoIsSsh r = do
|
2011-10-11 18:43:45 +00:00
|
|
|
uuid <- getRepoUUID r
|
2011-10-06 23:11:30 +00:00
|
|
|
sshparams <- sshToRepo r [Param $ sshcmd uuid ]
|
2011-04-09 18:26:32 +00:00
|
|
|
return $ Just ("ssh", sshparams)
|
|
|
|
| otherwise = return Nothing
|
|
|
|
where
|
Clean up handling of git directory and git worktree.
Baked into the code was an assumption that a repository's git directory
could be determined by adding ".git" to its work tree (or nothing for bare
repos). That fails when core.worktree, or GIT_DIR and GIT_WORK_TREE are
used to separate the two.
This was attacked at the type level, by storing the gitdir and worktree
separately, so Nothing for the worktree means a bare repo.
A complication arose because we don't learn where a repository is bare
until its configuration is read. So another Location type handles
repositories that have not had their config read yet. I am not entirely
happy with this being a Location type, rather than representing them
entirely separate from the Git type. The new code is not worse than the
old, but better types could enforce more safety.
Added support for core.worktree. Overriding it with -c isn't supported
because it's not really clear what to do if a git repo's config is read, is
not bare, and is then overridden to bare. What is the right git directory
in this case? I will worry about this if/when someone has a use case for
overriding core.worktree with -c. (See Git.Config.updateLocation)
Also removed and renamed some functions like gitDir and workTree that
misused git's terminology.
One minor regression is known: git annex add in a bare repository does not
print a nice error message, but runs git ls-files in a way that fails
earlier with a less nice error message. This is because before --work-tree
was always passed to git commands, even in a bare repo, while now it's not.
2012-05-18 20:38:26 +00:00
|
|
|
dir = Git.repoPath r
|
2011-04-09 18:26:32 +00:00
|
|
|
shellcmd = "git-annex-shell"
|
2011-07-15 16:47:14 +00:00
|
|
|
shellopts = Param command : File dir : params
|
2011-10-06 23:11:30 +00:00
|
|
|
sshcmd uuid = unwords $
|
2011-12-09 05:57:13 +00:00
|
|
|
shellcmd : map shellEscape (toCommand shellopts) ++
|
2012-07-02 14:57:51 +00:00
|
|
|
uuidcheck uuid ++
|
|
|
|
map shellEscape (toCommand fieldopts)
|
2011-11-07 18:46:01 +00:00
|
|
|
uuidcheck NoUUID = []
|
|
|
|
uuidcheck (UUID u) = ["--uuid", u]
|
2012-07-02 14:57:51 +00:00
|
|
|
fieldopts
|
|
|
|
| null fields = []
|
|
|
|
| otherwise = fieldsep : map fieldopt fields ++ [fieldsep]
|
|
|
|
fieldsep = Param "--"
|
|
|
|
fieldopt (field, value) = Param $
|
|
|
|
fieldName field ++ "=" ++ value
|
2011-04-09 18:26:32 +00:00
|
|
|
|
|
|
|
{- Uses a supplied function (such as boolSystem) to run a git-annex-shell
|
|
|
|
- command on a remote.
|
|
|
|
-
|
|
|
|
- Or, if the remote does not support running remote commands, returns
|
|
|
|
- a specified error value. -}
|
|
|
|
onRemote
|
|
|
|
:: Git.Repo
|
|
|
|
-> (FilePath -> [CommandParam] -> IO a, a)
|
|
|
|
-> String
|
|
|
|
-> [CommandParam]
|
2012-07-02 14:57:51 +00:00
|
|
|
-> [(Field, String)]
|
2011-04-09 18:26:32 +00:00
|
|
|
-> Annex a
|
2012-07-02 14:57:51 +00:00
|
|
|
onRemote r (with, errorval) command params fields = do
|
|
|
|
s <- git_annex_shell r command params fields
|
2011-04-09 18:26:32 +00:00
|
|
|
case s of
|
|
|
|
Just (c, ps) -> liftIO $ with c ps
|
|
|
|
Nothing -> return errorval
|