2011-09-28 18:03:59 +00:00
|
|
|
{- git ls-tree interface
|
|
|
|
-
|
2019-02-21 21:32:59 +00:00
|
|
|
- Copyright 2011-2019 Joey Hess <id@joeyh.name>
|
2011-09-28 18:03:59 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2011-09-28 18:03:59 +00:00
|
|
|
-}
|
|
|
|
|
2016-02-24 03:08:41 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
|
2011-09-28 18:03:59 +00:00
|
|
|
module Git.LsTree (
|
2011-09-28 20:43:10 +00:00
|
|
|
TreeItem(..),
|
2019-02-21 21:32:59 +00:00
|
|
|
LsTreeMode(..),
|
2011-09-29 23:04:24 +00:00
|
|
|
lsTree,
|
2016-02-23 20:36:08 +00:00
|
|
|
lsTree',
|
2013-10-21 19:28:06 +00:00
|
|
|
lsTreeParams,
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
lsTreeFiles,
|
2016-02-23 20:36:08 +00:00
|
|
|
parseLsTree,
|
2019-05-20 20:37:04 +00:00
|
|
|
formatLsTree,
|
2011-09-28 18:03:59 +00:00
|
|
|
) where
|
|
|
|
|
2011-12-20 18:37:53 +00:00
|
|
|
import Common
|
2011-09-30 03:43:42 +00:00
|
|
|
import Git
|
2011-12-14 19:56:11 +00:00
|
|
|
import Git.Command
|
2012-12-10 18:36:57 +00:00
|
|
|
import Git.Sha
|
2013-10-17 18:51:19 +00:00
|
|
|
import Git.FilePath
|
2011-12-13 19:22:43 +00:00
|
|
|
import qualified Git.Filename
|
2011-09-28 18:03:59 +00:00
|
|
|
|
2015-05-10 20:50:46 +00:00
|
|
|
import Numeric
|
2017-02-20 17:44:55 +00:00
|
|
|
import Data.Char
|
2015-05-10 20:50:46 +00:00
|
|
|
import System.Posix.Types
|
|
|
|
|
2011-09-28 18:03:59 +00:00
|
|
|
data TreeItem = TreeItem
|
2011-09-28 18:14:52 +00:00
|
|
|
{ mode :: FileMode
|
2011-09-29 23:04:24 +00:00
|
|
|
, typeobj :: String
|
2016-01-01 19:56:24 +00:00
|
|
|
, sha :: Ref
|
2013-10-17 18:51:19 +00:00
|
|
|
, file :: TopFilePath
|
2011-09-28 18:03:59 +00:00
|
|
|
} deriving Show
|
|
|
|
|
2019-02-21 21:32:59 +00:00
|
|
|
data LsTreeMode = LsTreeRecursive | LsTreeNonRecursive
|
|
|
|
|
|
|
|
{- Lists the contents of a tree, with lazy output. -}
|
|
|
|
lsTree :: LsTreeMode -> Ref -> Repo -> IO ([TreeItem], IO Bool)
|
2016-02-23 20:36:08 +00:00
|
|
|
lsTree = lsTree' []
|
|
|
|
|
2019-02-21 21:32:59 +00:00
|
|
|
lsTree' :: [CommandParam] -> LsTreeMode -> Ref -> Repo -> IO ([TreeItem], IO Bool)
|
2019-02-22 16:41:17 +00:00
|
|
|
lsTree' ps lsmode t repo = do
|
|
|
|
(l, cleanup) <- pipeNullSplit (lsTreeParams lsmode t ps) repo
|
2016-01-01 19:50:59 +00:00
|
|
|
return (map parseLsTree l, cleanup)
|
2013-10-21 19:28:06 +00:00
|
|
|
|
2019-02-21 21:32:59 +00:00
|
|
|
lsTreeParams :: LsTreeMode -> Ref -> [CommandParam] -> [CommandParam]
|
2019-02-22 16:41:17 +00:00
|
|
|
lsTreeParams lsmode r ps =
|
2015-06-01 17:52:23 +00:00
|
|
|
[ Param "ls-tree"
|
|
|
|
, Param "--full-tree"
|
|
|
|
, Param "-z"
|
2019-02-21 21:32:59 +00:00
|
|
|
] ++ recursiveparams ++ ps ++
|
2015-07-06 18:21:43 +00:00
|
|
|
[ Param "--"
|
|
|
|
, File $ fromRef r
|
2015-06-01 17:52:23 +00:00
|
|
|
]
|
2019-02-21 21:32:59 +00:00
|
|
|
where
|
2019-02-22 16:41:17 +00:00
|
|
|
recursiveparams = case lsmode of
|
2019-02-21 21:32:59 +00:00
|
|
|
LsTreeRecursive -> [ Param "-r" ]
|
|
|
|
LsTreeNonRecursive -> []
|
2011-09-28 18:03:59 +00:00
|
|
|
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
{- Lists specified files in a tree. -}
|
|
|
|
lsTreeFiles :: Ref -> [FilePath] -> Repo -> IO [TreeItem]
|
2013-03-19 02:09:51 +00:00
|
|
|
lsTreeFiles t fs repo = map parseLsTree <$> pipeNullSplitStrict ps repo
|
|
|
|
where
|
2015-06-01 17:52:23 +00:00
|
|
|
ps =
|
|
|
|
[ Param "ls-tree"
|
|
|
|
, Param "--full-tree"
|
|
|
|
, Param "-z"
|
|
|
|
, Param "--"
|
|
|
|
, File $ fromRef t
|
|
|
|
] ++ map File fs
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
|
2017-02-20 17:44:55 +00:00
|
|
|
{- Parses a line of ls-tree output, in format:
|
|
|
|
- mode SP type SP sha TAB file
|
|
|
|
-
|
2011-09-28 18:03:59 +00:00
|
|
|
- (The --long format is not currently supported.) -}
|
support all filename encodings with ghc 7.4
Under ghc 7.4, this seems to be able to handle all filename encodings
again. Including filename encodings that do not match the LANG setting.
I think this will not work with earlier versions of ghc, it uses some ghc
internals.
Turns out that ghc 7.4 has a special filesystem encoding that it uses when
reading/writing filenames (as FilePaths). This encoding is documented
to allow "arbitrary undecodable bytes to be round-tripped through it".
So, to get FilePaths from eg, git ls-files, set the Handle that is reading
from git to use this encoding. Then things basically just work.
However, I have not found a way to make Text read using this encoding.
Text really does assume unicode. So I had to switch back to using String
when reading/writing data to git. Which is a pity, because it's some
percent slower, but at least it works.
Note that stdout and stderr also have to be set to this encoding, or
printing out filenames that contain undecodable bytes causes a crash.
IMHO this is a misfeature in ghc, that the user can pass you a filename,
which you can readFile, etc, but that default, putStr of filename may
cause a crash!
Git.CheckAttr gave me special trouble, because the filenames I got back
from git, after feeding them in, had further encoding breakage.
Rather than try to deal with that, I just zip up the input filenames
with the attributes. Which must be returned in the same order queried
for this to work.
Also of note is an apparent GHC bug I worked around in Git.CheckAttr. It
used to forkProcess and feed git from the child process. Unfortunatly,
after this forkProcess, accessing the `files` variable from the parent
returns []. Not the value that was passed into the function. This screams
of a bad bug, that's clobbering a variable, but for now I just avoid
forkProcess there to work around it. That forkProcess was itself only added
because of a ghc bug, #624389. I've confirmed that the test case for that
bug doesn't reproduce it with ghc 7.4. So that's ok, except for the new ghc
bug I have not isolated and reported. Why does this simple bit of code
magnet the ghc bugs? :)
Also, the symlink touching code is currently broken, when used on utf-8
filenames in a non-utf-8 locale, or probably on any filename containing
undecodable bytes, and I temporarily commented it out.
2012-02-03 19:12:41 +00:00
|
|
|
parseLsTree :: String -> TreeItem
|
2011-09-30 02:31:20 +00:00
|
|
|
parseLsTree l = TreeItem
|
2016-02-24 03:08:41 +00:00
|
|
|
{ mode = smode
|
support all filename encodings with ghc 7.4
Under ghc 7.4, this seems to be able to handle all filename encodings
again. Including filename encodings that do not match the LANG setting.
I think this will not work with earlier versions of ghc, it uses some ghc
internals.
Turns out that ghc 7.4 has a special filesystem encoding that it uses when
reading/writing filenames (as FilePaths). This encoding is documented
to allow "arbitrary undecodable bytes to be round-tripped through it".
So, to get FilePaths from eg, git ls-files, set the Handle that is reading
from git to use this encoding. Then things basically just work.
However, I have not found a way to make Text read using this encoding.
Text really does assume unicode. So I had to switch back to using String
when reading/writing data to git. Which is a pity, because it's some
percent slower, but at least it works.
Note that stdout and stderr also have to be set to this encoding, or
printing out filenames that contain undecodable bytes causes a crash.
IMHO this is a misfeature in ghc, that the user can pass you a filename,
which you can readFile, etc, but that default, putStr of filename may
cause a crash!
Git.CheckAttr gave me special trouble, because the filenames I got back
from git, after feeding them in, had further encoding breakage.
Rather than try to deal with that, I just zip up the input filenames
with the attributes. Which must be returned in the same order queried
for this to work.
Also of note is an apparent GHC bug I worked around in Git.CheckAttr. It
used to forkProcess and feed git from the child process. Unfortunatly,
after this forkProcess, accessing the `files` variable from the parent
returns []. Not the value that was passed into the function. This screams
of a bad bug, that's clobbering a variable, but for now I just avoid
forkProcess there to work around it. That forkProcess was itself only added
because of a ghc bug, #624389. I've confirmed that the test case for that
bug doesn't reproduce it with ghc 7.4. So that's ok, except for the new ghc
bug I have not isolated and reported. Why does this simple bit of code
magnet the ghc bugs? :)
Also, the symlink touching code is currently broken, when used on utf-8
filenames in a non-utf-8 locale, or probably on any filename containing
undecodable bytes, and I temporarily commented it out.
2012-02-03 19:12:41 +00:00
|
|
|
, typeobj = t
|
2016-01-01 19:56:24 +00:00
|
|
|
, sha = Ref s
|
2016-02-26 20:12:40 +00:00
|
|
|
, file = sfile
|
2011-09-30 02:31:20 +00:00
|
|
|
}
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2017-02-20 17:44:55 +00:00
|
|
|
(m, past_m) = splitAt 7 l -- mode is 6 bytes
|
|
|
|
(!t, past_t) = separate isSpace past_m
|
|
|
|
(!s, past_s) = splitAt shaSize past_t
|
|
|
|
!f = drop 1 past_s
|
2016-02-24 03:08:41 +00:00
|
|
|
!smode = fst $ Prelude.head $ readOct m
|
|
|
|
!sfile = asTopFilePath $ Git.Filename.decode f
|
2019-05-20 20:37:04 +00:00
|
|
|
|
|
|
|
{- Inverse of parseLsTree -}
|
|
|
|
formatLsTree :: TreeItem -> String
|
|
|
|
formatLsTree ti = unwords
|
|
|
|
[ showOct (mode ti) ""
|
|
|
|
, typeobj ti
|
|
|
|
, fromRef (sha ti)
|
|
|
|
, getTopFilePath (file ti)
|
|
|
|
]
|