2014-11-12 19:41:15 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2014 Joey Hess <id@joeyh.name>
|
2014-11-12 19:41:15 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Command.Proxy where
|
|
|
|
|
|
|
|
import Command
|
|
|
|
import Config
|
2017-12-31 20:08:31 +00:00
|
|
|
import Utility.Tmp.Dir
|
2014-11-12 19:41:15 +00:00
|
|
|
import Utility.Env
|
|
|
|
import Annex.Direct
|
2015-08-04 18:01:59 +00:00
|
|
|
import qualified Git
|
2014-11-12 19:41:15 +00:00
|
|
|
import qualified Git.Sha
|
2015-03-04 19:25:13 +00:00
|
|
|
import qualified Git.Ref
|
2015-04-09 16:24:42 +00:00
|
|
|
import qualified Git.Branch
|
2015-08-04 18:01:59 +00:00
|
|
|
import qualified Git.LsFiles
|
|
|
|
import Git.FilePath
|
|
|
|
import Utility.CopyFile
|
2014-11-12 19:41:15 +00:00
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
|
|
|
cmd = notBareRepo $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "proxy" SectionPlumbing
|
|
|
|
"safely bypass direct mode guard"
|
|
|
|
("-- git command") (withParams seek)
|
2014-11-12 19:41:15 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2014-11-13 17:56:29 +00:00
|
|
|
seek = withWords start
|
2014-11-12 19:41:15 +00:00
|
|
|
|
|
|
|
start :: [String] -> CommandStart
|
2016-11-16 01:29:54 +00:00
|
|
|
start [] = giveup "Did not specify command to run."
|
2014-11-12 19:41:15 +00:00
|
|
|
start (c:ps) = liftIO . exitWith =<< ifM isDirect
|
|
|
|
( do
|
2015-04-09 16:24:42 +00:00
|
|
|
tmp <- gitAnnexTmpMiscDir <$> gitRepo
|
|
|
|
withTmpDirIn tmp "proxy" go
|
2014-11-12 19:41:15 +00:00
|
|
|
, liftIO $ safeSystem c (map Param ps)
|
|
|
|
)
|
|
|
|
where
|
|
|
|
go tmp = do
|
|
|
|
oldref <- fromMaybe Git.Sha.emptyTree
|
2015-08-04 18:43:42 +00:00
|
|
|
<$> (inRepo . maybe Git.Ref.headSha Git.Ref.sha
|
|
|
|
=<< inRepo Git.Branch.currentUnsafe)
|
2015-08-04 18:01:59 +00:00
|
|
|
|
2015-04-09 16:24:42 +00:00
|
|
|
exitcode <- proxy tmp
|
2015-08-04 18:45:16 +00:00
|
|
|
|
2015-08-04 18:01:59 +00:00
|
|
|
cleanupproxy tmp oldref
|
|
|
|
|
2014-11-12 19:41:15 +00:00
|
|
|
return exitcode
|
2015-08-04 18:01:59 +00:00
|
|
|
|
2014-11-12 19:41:15 +00:00
|
|
|
proxy tmp = do
|
2015-04-09 16:24:42 +00:00
|
|
|
usetmp <- liftIO $ Just . addEntry "GIT_WORK_TREE" tmp <$> getEnvironment
|
2015-08-04 18:45:16 +00:00
|
|
|
|
|
|
|
-- Set up the tmp work tree, to contain both a checkout of all
|
|
|
|
-- staged files as well as hard links (or copies) of any
|
|
|
|
-- unstaged files.
|
2015-04-09 16:24:42 +00:00
|
|
|
unlessM (isNothing <$> inRepo Git.Branch.current) $
|
|
|
|
unlessM (liftIO $ boolSystemEnv "git" [Param "checkout", Param "--", Param "."] usetmp) $
|
|
|
|
error "Failed to set up proxy work tree."
|
2015-08-04 18:01:59 +00:00
|
|
|
top <- fromRepo Git.repoPath
|
|
|
|
(fs, cleanup) <- inRepo $ Git.LsFiles.notInRepo True [top]
|
|
|
|
forM_ fs $ \f -> do
|
|
|
|
tf <- inRepo $ toTopFilePath f
|
|
|
|
let tmpf = tmp </> getTopFilePath tf
|
|
|
|
liftIO $ do
|
|
|
|
createDirectoryIfMissing True (takeDirectory tmpf)
|
|
|
|
createLinkOrCopy f tmpf
|
|
|
|
liftIO $ void cleanup
|
2015-08-04 18:45:16 +00:00
|
|
|
|
|
|
|
liftIO $ safeSystemEnv c (map Param ps) usetmp
|
2015-08-04 18:01:59 +00:00
|
|
|
|
|
|
|
-- To merge the changes made by the proxied command into
|
|
|
|
-- the work tree is similar to cleaning up after a
|
|
|
|
-- direct mode merge. But, here we force updates of any
|
|
|
|
-- non-annxed files that were changed by the proxied
|
|
|
|
-- command.
|
|
|
|
cleanupproxy tmp oldref = do
|
|
|
|
updateWorkTree tmp oldref True
|
|
|
|
liftIO $ removeDirectoryRecursive tmp
|