2011-06-21 20:08:09 +00:00
|
|
|
{- management of the git-annex branch
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
-
|
|
|
|
- Copyright 2011 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-06-21 20:08:09 +00:00
|
|
|
module Branch (
|
2011-06-22 19:58:30 +00:00
|
|
|
create,
|
2011-06-21 20:08:09 +00:00
|
|
|
update,
|
2011-06-21 23:11:55 +00:00
|
|
|
get,
|
|
|
|
change,
|
2011-06-22 21:47:06 +00:00
|
|
|
commit,
|
2011-06-24 15:59:34 +00:00
|
|
|
files,
|
|
|
|
refExists,
|
|
|
|
hasOrigin,
|
|
|
|
name
|
2011-06-21 20:08:09 +00:00
|
|
|
) where
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
|
2011-06-30 02:19:40 +00:00
|
|
|
import Control.Monad (when, unless, liftM)
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
import Control.Monad.State (liftIO)
|
2011-06-21 21:39:45 +00:00
|
|
|
import System.FilePath
|
|
|
|
import System.Directory
|
|
|
|
import Data.String.Utils
|
|
|
|
import System.Cmd.Utils
|
2011-06-21 23:52:40 +00:00
|
|
|
import Data.Maybe
|
2011-06-22 21:47:06 +00:00
|
|
|
import Data.List
|
2011-06-22 23:48:04 +00:00
|
|
|
import System.IO
|
2011-06-30 04:35:51 +00:00
|
|
|
import System.IO.Binary
|
2011-07-05 17:26:59 +00:00
|
|
|
import System.Posix.Process
|
|
|
|
import System.Exit
|
2011-06-30 03:56:47 +00:00
|
|
|
import qualified Data.ByteString.Char8 as B
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
|
2011-06-22 19:58:30 +00:00
|
|
|
import Types.BranchState
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-06-30 17:32:47 +00:00
|
|
|
import qualified Git.UnionMerge
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
import qualified Annex
|
|
|
|
import Utility
|
|
|
|
import Types
|
|
|
|
import Messages
|
2011-06-23 15:37:26 +00:00
|
|
|
import Locations
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
|
2011-06-28 18:14:49 +00:00
|
|
|
type GitRef = String
|
|
|
|
|
2011-06-21 21:39:45 +00:00
|
|
|
{- Name of the branch that is used to store git-annex's information. -}
|
2011-06-28 18:14:49 +00:00
|
|
|
name :: GitRef
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
name = "git-annex"
|
|
|
|
|
2011-06-21 21:39:45 +00:00
|
|
|
{- Fully qualified name of the branch. -}
|
2011-06-28 18:14:49 +00:00
|
|
|
fullname :: GitRef
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
fullname = "refs/heads/" ++ name
|
|
|
|
|
2011-06-24 15:59:34 +00:00
|
|
|
{- Branch's name in origin. -}
|
2011-06-28 18:14:49 +00:00
|
|
|
originname :: GitRef
|
2011-06-24 15:59:34 +00:00
|
|
|
originname = "origin/" ++ name
|
|
|
|
|
2011-06-23 15:37:26 +00:00
|
|
|
{- Converts a fully qualified git ref into a short version for human
|
|
|
|
- consumptiom. -}
|
2011-06-28 18:14:49 +00:00
|
|
|
shortref :: GitRef -> String
|
2011-06-22 21:47:06 +00:00
|
|
|
shortref = remove "refs/heads/" . remove "refs/remotes/"
|
|
|
|
where
|
|
|
|
remove prefix s
|
|
|
|
| prefix `isPrefixOf` s = drop (length prefix) s
|
|
|
|
| otherwise = s
|
|
|
|
|
2011-06-21 21:39:45 +00:00
|
|
|
{- A separate index file for the branch. -}
|
|
|
|
index :: Git.Repo -> FilePath
|
2011-06-23 16:11:09 +00:00
|
|
|
index g = gitAnnexDir g </> "index"
|
2011-06-21 21:39:45 +00:00
|
|
|
|
|
|
|
{- Populates the branch's index file with the current branch contents.
|
|
|
|
-
|
|
|
|
- Usually, this is only done when the index doesn't yet exist, and
|
2011-06-23 15:37:26 +00:00
|
|
|
- the index is used to build up changes to be commited to the branch,
|
|
|
|
- and merge in changes from other branches.
|
2011-06-21 21:39:45 +00:00
|
|
|
-}
|
2011-06-22 19:58:30 +00:00
|
|
|
genIndex :: Git.Repo -> IO ()
|
2011-06-30 17:32:47 +00:00
|
|
|
genIndex g = Git.UnionMerge.ls_tree g fullname >>= Git.UnionMerge.update_index g
|
2011-06-21 21:39:45 +00:00
|
|
|
|
|
|
|
{- Runs an action using the branch's index file. -}
|
|
|
|
withIndex :: Annex a -> Annex a
|
2011-06-23 02:56:27 +00:00
|
|
|
withIndex = withIndex' False
|
|
|
|
withIndex' :: Bool -> Annex a -> Annex a
|
|
|
|
withIndex' bootstrapping a = do
|
2011-06-21 21:39:45 +00:00
|
|
|
g <- Annex.gitRepo
|
|
|
|
let f = index g
|
2011-06-23 02:56:27 +00:00
|
|
|
reset <- liftIO $ Git.useIndex f
|
2011-06-21 21:39:45 +00:00
|
|
|
|
2011-06-23 19:38:52 +00:00
|
|
|
e <- liftIO $ doesFileExist f
|
2011-06-27 01:01:19 +00:00
|
|
|
unless e $ do
|
|
|
|
unless bootstrapping $ create
|
|
|
|
liftIO $ createDirectoryIfMissing True $ takeDirectory f
|
|
|
|
liftIO $ unless bootstrapping $ genIndex g
|
2011-06-21 21:39:45 +00:00
|
|
|
|
|
|
|
r <- a
|
2011-06-23 02:56:27 +00:00
|
|
|
liftIO reset
|
2011-06-21 21:39:45 +00:00
|
|
|
return r
|
|
|
|
|
2011-06-22 19:58:30 +00:00
|
|
|
withIndexUpdate :: Annex a -> Annex a
|
|
|
|
withIndexUpdate a = update >> withIndex a
|
|
|
|
|
|
|
|
getState :: Annex BranchState
|
|
|
|
getState = Annex.getState Annex.branchstate
|
|
|
|
|
|
|
|
setState :: BranchState -> Annex ()
|
|
|
|
setState state = Annex.changeState $ \s -> s { Annex.branchstate = state }
|
|
|
|
|
2011-06-22 18:18:49 +00:00
|
|
|
setCache :: FilePath -> String -> Annex ()
|
2011-06-22 19:58:30 +00:00
|
|
|
setCache file content = do
|
|
|
|
state <- getState
|
|
|
|
setState state { cachedFile = Just file, cachedContent = content }
|
|
|
|
|
2011-06-22 18:18:49 +00:00
|
|
|
invalidateCache :: Annex ()
|
2011-06-22 19:58:30 +00:00
|
|
|
invalidateCache = do
|
|
|
|
state <- getState
|
|
|
|
setState state { cachedFile = Nothing, cachedContent = "" }
|
|
|
|
|
|
|
|
getCache :: FilePath -> Annex (Maybe String)
|
|
|
|
getCache file = getState >>= handle
|
|
|
|
where
|
|
|
|
handle state
|
|
|
|
| cachedFile state == Just file =
|
|
|
|
return $ Just $ cachedContent state
|
|
|
|
| otherwise = return Nothing
|
|
|
|
|
|
|
|
{- Creates the branch, if it does not already exist. -}
|
|
|
|
create :: Annex ()
|
2011-06-28 18:14:49 +00:00
|
|
|
create = unlessM (refExists fullname) $ do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
e <- hasOrigin
|
|
|
|
if e
|
|
|
|
then liftIO $ Git.run g "branch" [Param name, Param originname]
|
|
|
|
else withIndex' True $
|
2011-06-30 17:32:47 +00:00
|
|
|
liftIO $ Git.commit g "branch created" fullname []
|
2011-06-22 18:18:49 +00:00
|
|
|
|
2011-06-23 15:37:26 +00:00
|
|
|
{- Stages the journal, and commits staged changes to the branch. -}
|
2011-06-22 23:48:04 +00:00
|
|
|
commit :: String -> Annex ()
|
2011-06-28 18:14:49 +00:00
|
|
|
commit message = whenM stageJournalFiles $ do
|
|
|
|
g <- Annex.gitRepo
|
2011-06-30 17:32:47 +00:00
|
|
|
withIndex $ liftIO $ Git.commit g message fullname [fullname]
|
2011-06-22 23:48:04 +00:00
|
|
|
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
{- Ensures that the branch is up-to-date; should be called before
|
|
|
|
- data is read from it. Runs only once per git-annex run. -}
|
|
|
|
update :: Annex ()
|
|
|
|
update = do
|
2011-06-30 01:23:40 +00:00
|
|
|
state <- getState
|
2011-06-22 19:58:30 +00:00
|
|
|
unless (branchUpdated state) $ withIndex $ do
|
fix consistency, and partially close a race during merge
Only "partially" because the journal is not locked during the merge, so
there's a small window where a different git-annex process could write info
to the journal that overwrites info taken from the merge.
That could be dealt with by locking, but the lock would really need to be
around the whole git-annex, to only let one run at a time. Otherwise, even
with the journal locked during the merge, another git-annex could already
be running, generate an overwriting change, and only store it in the journal
after the merge was complete. And similarly, two git-annex processes could
fight and overwrite each other's information independant of any merging.
So, a toplevel lock for git-annex may get added; it's something I've
considered before, as these potential, unlikely problems are not new.
(OTOH, fsck will deal with such problems.)
2011-06-23 19:51:07 +00:00
|
|
|
{- Since branches get merged into the index, it's important to
|
|
|
|
- first stage the journal into the index. Otherwise, any
|
|
|
|
- changes in the journal would later get staged, and might
|
|
|
|
- overwrite changes made during the merge.
|
|
|
|
-
|
|
|
|
- It would be cleaner to handle the merge by updating the
|
|
|
|
- journal, not the index, with changes from the branches.
|
|
|
|
-}
|
2011-06-23 20:44:26 +00:00
|
|
|
staged <- stageJournalFiles
|
fix consistency, and partially close a race during merge
Only "partially" because the journal is not locked during the merge, so
there's a small window where a different git-annex process could write info
to the journal that overwrites info taken from the merge.
That could be dealt with by locking, but the lock would really need to be
around the whole git-annex, to only let one run at a time. Otherwise, even
with the journal locked during the merge, another git-annex could already
be running, generate an overwriting change, and only store it in the journal
after the merge was complete. And similarly, two git-annex processes could
fight and overwrite each other's information independant of any merging.
So, a toplevel lock for git-annex may get added; it's something I've
considered before, as these potential, unlikely problems are not new.
(OTOH, fsck will deal with such problems.)
2011-06-23 19:51:07 +00:00
|
|
|
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
g <- Annex.gitRepo
|
2011-06-21 23:52:40 +00:00
|
|
|
r <- liftIO $ Git.pipeRead g [Param "show-ref", Param name]
|
|
|
|
let refs = map (last . words) (lines r)
|
|
|
|
updated <- catMaybes `liftM` mapM updateRef refs
|
2011-06-23 20:44:26 +00:00
|
|
|
unless (null updated && not staged) $ liftIO $
|
2011-06-30 17:32:47 +00:00
|
|
|
Git.commit g "update" fullname (fullname:updated)
|
2011-06-22 19:58:30 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.branchstate = state { branchUpdated = True } }
|
2011-06-22 18:18:49 +00:00
|
|
|
invalidateCache
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
|
2011-06-24 15:59:34 +00:00
|
|
|
{- Does origin/git-annex exist? -}
|
|
|
|
hasOrigin :: Annex Bool
|
|
|
|
hasOrigin = refExists originname
|
|
|
|
|
|
|
|
{- Checks if a git ref exists. -}
|
2011-06-28 18:14:49 +00:00
|
|
|
refExists :: GitRef -> Annex Bool
|
2011-06-24 15:59:34 +00:00
|
|
|
refExists ref = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ Git.runBool g "show-ref"
|
|
|
|
[Param "--verify", Param "-q", Param ref]
|
|
|
|
|
2011-06-21 23:52:40 +00:00
|
|
|
{- Ensures that a given ref has been merged into the index. -}
|
2011-06-28 18:14:49 +00:00
|
|
|
updateRef :: GitRef -> Annex (Maybe String)
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
updateRef ref
|
2011-06-21 23:52:40 +00:00
|
|
|
| ref == fullname = return Nothing
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
| otherwise = do
|
|
|
|
g <- Annex.gitRepo
|
2011-06-23 13:56:04 +00:00
|
|
|
-- checking with log to see if there have been changes
|
|
|
|
-- is less expensive than always merging
|
code to update a git-annex branch
There is no suitable git hook to run code when pulling changes that
might need to be merged into the git-annex branch. The post-merge hook
is only run when changes are merged into HEAD, and it's possible,
and indeed likely that many pulls will only have changes in git-annex,
but not in HEAD, and not trigger it.
So, git-annex will have to take care to update the branch before reading
from it, to make sure it has merged in current info from remotes. Happily,
this can be done quite inexpensively, just a git-show-ref to list
branches, and a minimalized git-log to see if there are unmerged changes
on the branches. To further speed up, it will be done only once per
git-annex run, max.
2011-06-21 18:29:09 +00:00
|
|
|
diffs <- liftIO $ Git.pipeRead g [
|
|
|
|
Param "log",
|
|
|
|
Param (name++".."++ref),
|
|
|
|
Params "--oneline -n1"
|
|
|
|
]
|
2011-06-21 23:52:40 +00:00
|
|
|
if (null diffs)
|
|
|
|
then return Nothing
|
|
|
|
else do
|
2011-06-22 21:47:06 +00:00
|
|
|
showSideAction $ "merging " ++ shortref ref ++ " into " ++ name ++ "..."
|
2011-06-21 23:52:40 +00:00
|
|
|
-- By passing only one ref, it is actually
|
|
|
|
-- merged into the index, preserving any
|
|
|
|
-- changes that may already be staged.
|
2011-06-23 13:56:04 +00:00
|
|
|
--
|
|
|
|
-- However, any changes in the git-annex
|
|
|
|
-- branch that are *not* reflected in the
|
|
|
|
-- index will be removed. So, documentation
|
|
|
|
-- advises users not to directly modify the
|
|
|
|
-- branch.
|
2011-06-30 17:32:47 +00:00
|
|
|
liftIO $ Git.UnionMerge.merge g [ref]
|
2011-06-21 23:52:40 +00:00
|
|
|
return $ Just ref
|
2011-06-21 20:08:09 +00:00
|
|
|
|
2011-06-23 15:37:26 +00:00
|
|
|
{- Records changed content of a file into the journal. -}
|
2011-06-21 20:08:09 +00:00
|
|
|
change :: FilePath -> String -> Annex ()
|
2011-06-21 23:11:55 +00:00
|
|
|
change file content = do
|
2011-06-23 15:37:26 +00:00
|
|
|
setJournalFile file content
|
|
|
|
setCache file content
|
|
|
|
|
|
|
|
{- Gets the content of a file on the branch, or content from the journal, or
|
|
|
|
- staged in the index.
|
|
|
|
-
|
|
|
|
- Returns an empty string if the file doesn't exist yet. -}
|
2011-06-21 21:39:45 +00:00
|
|
|
get :: FilePath -> Annex String
|
2011-06-22 19:58:30 +00:00
|
|
|
get file = do
|
|
|
|
cached <- getCache file
|
|
|
|
case cached of
|
|
|
|
Just content -> return content
|
2011-06-23 15:37:26 +00:00
|
|
|
Nothing -> do
|
|
|
|
j <- getJournalFile file
|
|
|
|
case j of
|
|
|
|
Just content -> do
|
|
|
|
setCache file content
|
|
|
|
return content
|
|
|
|
Nothing -> withIndexUpdate $ do
|
2011-06-30 01:23:40 +00:00
|
|
|
content <- catFile file
|
2011-06-23 15:37:26 +00:00
|
|
|
setCache file content
|
|
|
|
return content
|
2011-06-30 01:23:40 +00:00
|
|
|
|
|
|
|
{- Uses git cat-file in batch mode to read the content of a file.
|
|
|
|
-
|
|
|
|
- Only one process is run, and it persists and is used for all accesses. -}
|
|
|
|
catFile :: FilePath -> Annex String
|
|
|
|
catFile file = do
|
|
|
|
state <- getState
|
|
|
|
maybe (startup state) ask (catFileHandles state)
|
2011-06-21 21:39:45 +00:00
|
|
|
where
|
2011-06-30 01:23:40 +00:00
|
|
|
startup state = do
|
|
|
|
g <- Annex.gitRepo
|
2011-07-02 23:22:11 +00:00
|
|
|
(_, from, to) <- liftIO $ hPipeBoth "git" $
|
|
|
|
toCommand $ Git.gitCommandLine g
|
|
|
|
[Param "cat-file", Param "--batch"]
|
2011-06-30 01:23:40 +00:00
|
|
|
setState state { catFileHandles = Just (from, to) }
|
|
|
|
ask (from, to)
|
2011-06-30 02:19:40 +00:00
|
|
|
ask (from, to) = liftIO $ do
|
|
|
|
let want = fullname ++ ":" ++ file
|
|
|
|
hPutStrLn to want
|
|
|
|
hFlush to
|
|
|
|
header <- hGetLine from
|
2011-06-30 17:12:51 +00:00
|
|
|
case words header of
|
|
|
|
[sha, blob, size]
|
|
|
|
| length sha == Git.shaSize &&
|
|
|
|
blob == "blob" -> handle from size
|
|
|
|
| otherwise -> empty
|
2011-07-02 23:16:28 +00:00
|
|
|
_
|
|
|
|
| header == want ++ " missing" -> empty
|
|
|
|
| otherwise -> error $ "unknown response from git cat-file " ++ header
|
2011-06-30 17:12:51 +00:00
|
|
|
handle from size = case reads size of
|
|
|
|
[(bytes, "")] -> readcontent from bytes
|
|
|
|
_ -> empty
|
|
|
|
readcontent from bytes = do
|
|
|
|
content <- B.hGet from bytes
|
|
|
|
c <- hGetChar from
|
|
|
|
when (c /= '\n') $
|
|
|
|
error "missing newline from git cat-file"
|
|
|
|
return $ B.unpack content
|
|
|
|
empty = return ""
|
2011-06-23 03:24:14 +00:00
|
|
|
|
2011-06-23 15:37:26 +00:00
|
|
|
{- Lists all files on the branch. There may be duplicates in the list. -}
|
2011-06-23 03:24:14 +00:00
|
|
|
files :: Annex [FilePath]
|
|
|
|
files = withIndexUpdate $ do
|
|
|
|
g <- Annex.gitRepo
|
2011-06-23 15:37:26 +00:00
|
|
|
bfiles <- liftIO $ Git.pipeNullSplit g
|
2011-06-23 03:24:14 +00:00
|
|
|
[Params "ls-tree --name-only -r -z", Param fullname]
|
2011-06-23 15:37:26 +00:00
|
|
|
jfiles <- getJournalFiles
|
|
|
|
return $ jfiles ++ bfiles
|
|
|
|
|
|
|
|
{- Records content for a file in the branch to the journal.
|
|
|
|
-
|
|
|
|
- Using the journal, rather than immediatly staging content to the index
|
|
|
|
- avoids git needing to rewrite the index after every change. -}
|
|
|
|
setJournalFile :: FilePath -> String -> Annex ()
|
|
|
|
setJournalFile file content = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ catch (write g) $ const $ do
|
|
|
|
createDirectoryIfMissing True $ gitAnnexJournalDir g
|
|
|
|
createDirectoryIfMissing True $ gitAnnexTmpDir g
|
|
|
|
write g
|
|
|
|
where
|
|
|
|
-- journal file is written atomically
|
|
|
|
write g = do
|
|
|
|
let jfile = journalFile g file
|
|
|
|
let tmpfile = gitAnnexTmpDir g </> takeFileName jfile
|
2011-06-30 04:35:51 +00:00
|
|
|
writeBinaryFile tmpfile content
|
2011-06-23 15:37:26 +00:00
|
|
|
renameFile tmpfile jfile
|
|
|
|
|
|
|
|
{- Gets journalled content for a file in the branch. -}
|
|
|
|
getJournalFile :: FilePath -> Annex (Maybe String)
|
|
|
|
getJournalFile file = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
liftIO $ catch (liftM Just . readFileStrict $ journalFile g file)
|
|
|
|
(const $ return Nothing)
|
|
|
|
|
|
|
|
{- List of journal files. -}
|
|
|
|
getJournalFiles :: Annex [FilePath]
|
|
|
|
getJournalFiles = getJournalFilesRaw >>= return . map fileJournal
|
|
|
|
|
|
|
|
getJournalFilesRaw :: Annex [FilePath]
|
|
|
|
getJournalFilesRaw = do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
fs <- liftIO $ catch (getDirectoryContents $ gitAnnexJournalDir g)
|
|
|
|
(const $ return [])
|
|
|
|
return $ filter (\f -> f /= "." && f /= "..") fs
|
|
|
|
|
|
|
|
{- Stages all journal files into the index, and returns True if the index
|
|
|
|
- was modified. -}
|
|
|
|
stageJournalFiles :: Annex Bool
|
|
|
|
stageJournalFiles = do
|
|
|
|
l <- getJournalFilesRaw
|
|
|
|
if null l
|
|
|
|
then return False
|
|
|
|
else do
|
|
|
|
g <- Annex.gitRepo
|
|
|
|
withIndex $ liftIO $ stage g l
|
|
|
|
return True
|
|
|
|
where
|
|
|
|
stage g fs = do
|
|
|
|
let dir = gitAnnexJournalDir g
|
|
|
|
let paths = map (dir </>) fs
|
|
|
|
-- inject all the journal files directly into git
|
|
|
|
-- in one quick command
|
2011-07-05 17:26:59 +00:00
|
|
|
(pid, fromh, toh) <- hPipeBoth "git" $ toCommand $
|
|
|
|
Git.gitCommandLine g [Param "hash-object", Param "-w", Param "--stdin-paths"]
|
|
|
|
_ <- forkProcess $ do
|
|
|
|
hPutStr toh $ unlines paths
|
|
|
|
hClose toh
|
|
|
|
exitSuccess
|
|
|
|
hClose toh
|
|
|
|
s <- hGetContents fromh
|
2011-06-23 15:37:26 +00:00
|
|
|
-- update the index, also in just one command
|
2011-06-30 17:32:47 +00:00
|
|
|
Git.UnionMerge.update_index g $
|
2011-06-23 15:37:26 +00:00
|
|
|
index_lines (lines s) $ map fileJournal fs
|
2011-07-05 17:26:59 +00:00
|
|
|
hClose fromh
|
|
|
|
forceSuccess pid
|
2011-06-23 15:37:26 +00:00
|
|
|
mapM_ removeFile paths
|
|
|
|
index_lines shas fs = map genline $ zip shas fs
|
2011-06-30 17:32:47 +00:00
|
|
|
genline (sha, file) = Git.UnionMerge.update_index_line sha file
|
2011-06-23 15:37:26 +00:00
|
|
|
|
|
|
|
{- Produces a filename to use in the journal for a file on the branch.
|
|
|
|
-
|
|
|
|
- The journal typically won't have a lot of files in it, so the hashing
|
|
|
|
- used in the branch is not necessary, and all the files are put directly
|
|
|
|
- in the journal directory.
|
|
|
|
-}
|
|
|
|
journalFile :: Git.Repo -> FilePath -> FilePath
|
|
|
|
journalFile repo file = gitAnnexJournalDir repo </> concatMap mangle file
|
|
|
|
where
|
|
|
|
mangle '/' = "_"
|
|
|
|
mangle '_' = "__"
|
|
|
|
mangle c = [c]
|
|
|
|
|
|
|
|
{- Converts a journal file (relative to the journal dir) back to the
|
|
|
|
- filename on the branch. -}
|
|
|
|
fileJournal :: FilePath -> FilePath
|
|
|
|
fileJournal = replace "//" "_" . replace "_" "/"
|