2014-02-16 21:39:54 +00:00
|
|
|
|
{- metadata based branch views
|
|
|
|
|
-
|
|
|
|
|
- Copyright 2014 Joey Hess <joey@kitenet.net>
|
|
|
|
|
-
|
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Annex.View where
|
|
|
|
|
|
|
|
|
|
import Common.Annex
|
2014-02-22 17:35:50 +00:00
|
|
|
|
import Annex.View.ViewedFile
|
2014-02-17 04:18:57 +00:00
|
|
|
|
import Types.View
|
2014-02-16 21:39:54 +00:00
|
|
|
|
import Types.MetaData
|
2014-02-18 21:38:23 +00:00
|
|
|
|
import qualified Git
|
2014-02-19 18:14:44 +00:00
|
|
|
|
import qualified Git.DiffTree as DiffTree
|
2014-02-16 21:39:54 +00:00
|
|
|
|
import qualified Git.Branch
|
2014-02-18 21:38:23 +00:00
|
|
|
|
import qualified Git.LsFiles
|
2014-02-19 18:14:44 +00:00
|
|
|
|
import qualified Git.Ref
|
2014-02-18 21:38:23 +00:00
|
|
|
|
import Git.UpdateIndex
|
|
|
|
|
import Git.Sha
|
|
|
|
|
import Git.HashObject
|
2014-02-19 00:32:00 +00:00
|
|
|
|
import Git.Types
|
2014-02-19 18:14:44 +00:00
|
|
|
|
import Git.FilePath
|
2014-02-18 21:38:23 +00:00
|
|
|
|
import qualified Backend
|
|
|
|
|
import Annex.Index
|
|
|
|
|
import Annex.Link
|
2014-02-19 18:14:44 +00:00
|
|
|
|
import Annex.CatFile
|
2014-02-18 21:38:23 +00:00
|
|
|
|
import Logs.MetaData
|
|
|
|
|
import Logs.View
|
2014-02-21 22:34:34 +00:00
|
|
|
|
import Utility.Glob
|
2014-02-19 00:32:00 +00:00
|
|
|
|
import Utility.FileMode
|
2014-02-19 18:14:44 +00:00
|
|
|
|
import Types.Command
|
|
|
|
|
import Config
|
|
|
|
|
import CmdLine.Action
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
|
|
|
|
import qualified Data.Set as S
|
2014-02-17 02:44:28 +00:00
|
|
|
|
import "mtl" Control.Monad.Writer
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
{- Each visible ViewFilter in a view results in another level of
|
2014-02-19 05:28:48 +00:00
|
|
|
|
- subdirectory nesting. When a file matches multiple ways, it will appear
|
|
|
|
|
- in multiple subdirectories. This means there is a bit of an exponential
|
|
|
|
|
- blowup with a single file appearing in a crazy number of places!
|
|
|
|
|
-
|
|
|
|
|
- Capping the view size to 5 is reasonable; why wants to dig
|
|
|
|
|
- through 5+ levels of subdirectories to find anything?
|
|
|
|
|
-}
|
|
|
|
|
viewTooLarge :: View -> Bool
|
|
|
|
|
viewTooLarge view = visibleViewSize view > 5
|
|
|
|
|
|
|
|
|
|
visibleViewSize :: View -> Int
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
visibleViewSize = length . filter viewVisible . viewComponents
|
2014-02-19 05:28:48 +00:00
|
|
|
|
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
data ViewChange = Unchanged | Narrowing | Widening
|
|
|
|
|
deriving (Ord, Eq, Show)
|
|
|
|
|
|
|
|
|
|
{- Updates a view, adding new fields to filter on (Narrowing),
|
|
|
|
|
- or allowing new values in an existing field (Widening). -}
|
|
|
|
|
refineView :: View -> [(MetaField, String)] -> (View, ViewChange)
|
|
|
|
|
refineView = go Unchanged
|
|
|
|
|
where
|
|
|
|
|
go c v [] = (v, c)
|
|
|
|
|
go c v ((f, s):rest) =
|
|
|
|
|
let (v', c') = refineView' v f s
|
|
|
|
|
in go (max c c') v' rest
|
|
|
|
|
|
|
|
|
|
{- Adds an additional filter to a view. This can only result in narrowing
|
|
|
|
|
- the view. Multivalued filters are added in non-visible form. -}
|
|
|
|
|
filterView :: View -> [(MetaField, String)] -> View
|
|
|
|
|
filterView v vs = v { viewComponents = viewComponents f' ++ viewComponents v}
|
|
|
|
|
where
|
|
|
|
|
f = fst $ refineView (v {viewComponents = []}) vs
|
|
|
|
|
f' = f { viewComponents = map toinvisible (viewComponents f) }
|
|
|
|
|
toinvisible c = c { viewVisible = False }
|
|
|
|
|
|
|
|
|
|
refineView' :: View -> MetaField -> String -> (View, ViewChange)
|
|
|
|
|
refineView' view field wanted
|
2014-02-18 21:38:23 +00:00
|
|
|
|
| field `elem` (map viewField components) =
|
|
|
|
|
let (components', viewchanges) = runWriter $ mapM updatefield components
|
|
|
|
|
in (view { viewComponents = components' }, maximum viewchanges)
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
| otherwise =
|
|
|
|
|
let component = ViewComponent field viewfilter (multiValue viewfilter)
|
|
|
|
|
view' = view { viewComponents = component : components }
|
2014-02-19 05:28:48 +00:00
|
|
|
|
in if viewTooLarge view'
|
|
|
|
|
then error $ "View is too large (" ++ show (visibleViewSize view') ++ " levels of subdirectories)"
|
|
|
|
|
else (view', Narrowing)
|
2014-02-17 02:44:28 +00:00
|
|
|
|
where
|
2014-02-18 21:38:23 +00:00
|
|
|
|
components = viewComponents view
|
2014-02-17 02:44:28 +00:00
|
|
|
|
viewfilter
|
2014-02-18 21:38:23 +00:00
|
|
|
|
| any (`elem` wanted) "*?" = FilterGlob wanted
|
2014-02-17 02:44:28 +00:00
|
|
|
|
| otherwise = FilterValues $ S.singleton $ toMetaValue wanted
|
2014-02-17 04:38:33 +00:00
|
|
|
|
updatefield :: ViewComponent -> Writer [ViewChange] ViewComponent
|
|
|
|
|
updatefield v
|
|
|
|
|
| viewField v == field = do
|
|
|
|
|
let (newvf, viewchange) = combineViewFilter (viewFilter v) viewfilter
|
2014-02-17 02:44:28 +00:00
|
|
|
|
tell [viewchange]
|
2014-02-17 04:38:33 +00:00
|
|
|
|
return $ v { viewFilter = newvf }
|
2014-02-17 02:44:28 +00:00
|
|
|
|
| otherwise = return v
|
|
|
|
|
|
2014-02-19 06:27:58 +00:00
|
|
|
|
{- Combine old and new ViewFilters, yielding a result that matches
|
2014-02-17 02:44:28 +00:00
|
|
|
|
- either old+new, or only new.
|
|
|
|
|
-
|
|
|
|
|
- If we have FilterValues and change to a FilterGlob,
|
|
|
|
|
- it's always a widening change, because the glob could match other
|
|
|
|
|
- values. OTOH, going the other way, it's a Narrowing change if the old
|
|
|
|
|
- glob matches all the new FilterValues.
|
|
|
|
|
-
|
|
|
|
|
- With two globs, the old one is discarded, and the new one is used.
|
|
|
|
|
- We can tell if that's a narrowing change by checking if the old
|
|
|
|
|
- glob matches the new glob. For example, "*" matches "foo*",
|
|
|
|
|
- so that's narrowing. While "f?o" does not match "f??", so that's
|
|
|
|
|
- widening.
|
|
|
|
|
-}
|
|
|
|
|
combineViewFilter :: ViewFilter -> ViewFilter -> (ViewFilter, ViewChange)
|
|
|
|
|
combineViewFilter old@(FilterValues olds) (FilterValues news)
|
|
|
|
|
| combined == old = (combined, Unchanged)
|
|
|
|
|
| otherwise = (combined, Widening)
|
|
|
|
|
where
|
|
|
|
|
combined = FilterValues (S.union olds news)
|
2014-02-17 04:38:33 +00:00
|
|
|
|
combineViewFilter (FilterValues _) newglob@(FilterGlob _) =
|
2014-02-17 02:44:28 +00:00
|
|
|
|
(newglob, Widening)
|
|
|
|
|
combineViewFilter (FilterGlob oldglob) new@(FilterValues s)
|
2014-02-21 22:34:34 +00:00
|
|
|
|
| all (matchGlob (compileGlob oldglob CaseInsensative) . fromMetaValue) (S.toList s) = (new, Narrowing)
|
2014-02-17 02:44:28 +00:00
|
|
|
|
| otherwise = (new, Widening)
|
|
|
|
|
combineViewFilter (FilterGlob old) newglob@(FilterGlob new)
|
|
|
|
|
| old == new = (newglob, Unchanged)
|
2014-02-21 22:34:34 +00:00
|
|
|
|
| matchGlob (compileGlob old CaseInsensative) new = (newglob, Narrowing)
|
2014-02-17 02:44:28 +00:00
|
|
|
|
| otherwise = (newglob, Widening)
|
|
|
|
|
|
2014-02-16 21:39:54 +00:00
|
|
|
|
{- Generates views for a file from a branch, based on its metadata
|
|
|
|
|
- and the filename used in the branch.
|
|
|
|
|
-
|
|
|
|
|
- Note that a file may appear multiple times in a view, when it
|
|
|
|
|
- has multiple matching values for a MetaField used in the View.
|
2014-02-16 21:46:52 +00:00
|
|
|
|
-
|
|
|
|
|
- Of course if its MetaData does not match the View, it won't appear at
|
|
|
|
|
- all.
|
2014-02-19 06:27:58 +00:00
|
|
|
|
-
|
|
|
|
|
- Note that for efficiency, it's useful to partially
|
|
|
|
|
- evaluate this function with the view parameter and reuse
|
|
|
|
|
- the result. The globs in the view will then be compiled and memoized.
|
2014-02-16 21:39:54 +00:00
|
|
|
|
-}
|
2014-02-22 17:35:50 +00:00
|
|
|
|
viewedFiles :: View -> MkViewedFile -> FilePath -> MetaData -> [ViewedFile]
|
|
|
|
|
viewedFiles view =
|
2014-02-19 06:27:58 +00:00
|
|
|
|
let matchers = map viewComponentMatcher (viewComponents view)
|
2014-02-22 17:35:50 +00:00
|
|
|
|
in \mkviewedfile file metadata ->
|
2014-02-19 06:27:58 +00:00
|
|
|
|
let matches = map (\m -> m metadata) matchers
|
|
|
|
|
in if any isNothing matches
|
|
|
|
|
then []
|
|
|
|
|
else
|
|
|
|
|
let paths = pathProduct $
|
|
|
|
|
map (map toViewPath) (visible matches)
|
|
|
|
|
in if null paths
|
2014-02-22 17:35:50 +00:00
|
|
|
|
then [mkviewedfile file]
|
|
|
|
|
else map (</> mkviewedfile file) paths
|
2014-02-16 21:39:54 +00:00
|
|
|
|
where
|
|
|
|
|
visible = map (fromJust . snd) .
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
filter (viewVisible . fst) .
|
|
|
|
|
zip (viewComponents view)
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
2014-02-19 06:27:58 +00:00
|
|
|
|
{- Checks if metadata matches a ViewComponent filter, and if so
|
|
|
|
|
- returns the value, or values that match. Self-memoizing on ViewComponent. -}
|
|
|
|
|
viewComponentMatcher :: ViewComponent -> (MetaData -> Maybe [MetaValue])
|
|
|
|
|
viewComponentMatcher viewcomponent = \metadata ->
|
|
|
|
|
let s = matcher (currentMetaDataValues metafield metadata)
|
|
|
|
|
in if S.null s then Nothing else Just (S.toList s)
|
|
|
|
|
where
|
|
|
|
|
metafield = viewField viewcomponent
|
|
|
|
|
matcher = case viewFilter viewcomponent of
|
|
|
|
|
FilterValues s -> \values -> S.intersection s values
|
|
|
|
|
FilterGlob glob ->
|
2014-02-21 22:34:34 +00:00
|
|
|
|
let cglob = compileGlob glob CaseInsensative
|
2014-02-19 06:27:58 +00:00
|
|
|
|
in \values ->
|
2014-02-21 22:34:34 +00:00
|
|
|
|
S.filter (matchGlob cglob . fromMetaValue) values
|
2014-02-19 06:27:58 +00:00
|
|
|
|
|
2014-02-17 01:00:12 +00:00
|
|
|
|
toViewPath :: MetaValue -> FilePath
|
|
|
|
|
toViewPath = concatMap escapeslash . fromMetaValue
|
|
|
|
|
where
|
|
|
|
|
escapeslash c
|
|
|
|
|
| c == '/' = [pseudoSlash]
|
|
|
|
|
| c == '\\' = [pseudoBackslash]
|
|
|
|
|
| c == pseudoSlash = [pseudoSlash, pseudoSlash]
|
|
|
|
|
| c == pseudoBackslash = [pseudoBackslash, pseudoBackslash]
|
|
|
|
|
| otherwise = [c]
|
|
|
|
|
|
|
|
|
|
fromViewPath :: FilePath -> MetaValue
|
|
|
|
|
fromViewPath = toMetaValue . deescapeslash []
|
|
|
|
|
where
|
|
|
|
|
deescapeslash s [] = reverse s
|
|
|
|
|
deescapeslash s (c:cs)
|
|
|
|
|
| c == pseudoSlash = case cs of
|
|
|
|
|
(c':cs')
|
|
|
|
|
| c' == pseudoSlash -> deescapeslash (pseudoSlash:s) cs'
|
|
|
|
|
_ -> deescapeslash ('/':s) cs
|
|
|
|
|
| c == pseudoBackslash = case cs of
|
|
|
|
|
(c':cs')
|
|
|
|
|
| c' == pseudoBackslash -> deescapeslash (pseudoBackslash:s) cs'
|
|
|
|
|
_ -> deescapeslash ('/':s) cs
|
|
|
|
|
| otherwise = deescapeslash (c:s) cs
|
|
|
|
|
|
|
|
|
|
pseudoSlash :: Char
|
|
|
|
|
pseudoSlash = '\8725' -- '∕' /= '/'
|
|
|
|
|
|
|
|
|
|
pseudoBackslash :: Char
|
|
|
|
|
pseudoBackslash = '\9586' -- '╲' /= '\'
|
|
|
|
|
|
2014-02-16 21:39:54 +00:00
|
|
|
|
pathProduct :: [[FilePath]] -> [FilePath]
|
|
|
|
|
pathProduct [] = []
|
|
|
|
|
pathProduct (l:ls) = foldl combinel l ls
|
|
|
|
|
where
|
|
|
|
|
combinel xs ys = [combine x y | x <- xs, y <- ys]
|
|
|
|
|
|
2014-02-22 17:35:50 +00:00
|
|
|
|
{- Extracts the metadata from a ViewedFile, based on the view that was used
|
2014-02-16 21:39:54 +00:00
|
|
|
|
- to construct it. -}
|
2014-02-22 17:35:50 +00:00
|
|
|
|
fromView :: View -> ViewedFile -> MetaData
|
2014-02-16 21:39:54 +00:00
|
|
|
|
fromView view f = foldr (uncurry updateMetaData) newMetaData (zip fields values)
|
|
|
|
|
where
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
visible = filter viewVisible (viewComponents view)
|
2014-02-17 04:38:33 +00:00
|
|
|
|
fields = map viewField visible
|
2014-02-16 21:39:54 +00:00
|
|
|
|
paths = splitDirectories $ dropFileName f
|
2014-02-17 01:00:12 +00:00
|
|
|
|
values = map fromViewPath paths
|
|
|
|
|
|
|
|
|
|
{- Constructing a view that will match arbitrary metadata, and applying
|
2014-02-22 17:35:50 +00:00
|
|
|
|
- it to a file yields a set of ViewedFile which all contain the same
|
2014-02-17 01:00:12 +00:00
|
|
|
|
- MetaFields that were present in the input metadata
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
- (excluding fields that are not visible). -}
|
|
|
|
|
prop_view_roundtrips :: FilePath -> MetaData -> Bool -> Bool
|
|
|
|
|
prop_view_roundtrips f metadata visible = null f || viewTooLarge view ||
|
2014-02-22 17:35:50 +00:00
|
|
|
|
all hasfields (viewedFiles view viewedFileFromReference f metadata)
|
2014-02-17 01:00:12 +00:00
|
|
|
|
where
|
2014-02-18 21:38:23 +00:00
|
|
|
|
view = View (Git.Ref "master") $
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
map (\(mf, mv) -> ViewComponent mf (FilterValues $ S.filter (not . null . fromMetaValue) mv) visible)
|
2014-02-18 21:38:23 +00:00
|
|
|
|
(fromMetaData metadata)
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
visiblefields = sort (map viewField $ filter viewVisible (viewComponents view))
|
2014-02-17 01:00:12 +00:00
|
|
|
|
hasfields fv = sort (map fst (fromMetaData (fromView view fv))) == visiblefields
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
|
|
|
|
{- Applies a view to the currently checked out branch, generating a new
|
|
|
|
|
- branch for the view.
|
|
|
|
|
-}
|
|
|
|
|
applyView :: View -> Annex Git.Branch
|
2014-02-22 17:35:50 +00:00
|
|
|
|
applyView view = applyView' viewedFileFromReference view
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
2014-02-17 02:44:28 +00:00
|
|
|
|
{- Generates a new branch for a View, which must be a more narrow
|
2014-02-16 21:39:54 +00:00
|
|
|
|
- version of the View originally used to generate the currently
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
- checked out branch. That is, it must match a subset of the files
|
|
|
|
|
- in view, not any others.
|
2014-02-16 21:39:54 +00:00
|
|
|
|
-}
|
2014-02-17 02:44:28 +00:00
|
|
|
|
narrowView :: View -> Annex Git.Branch
|
2014-02-22 17:35:50 +00:00
|
|
|
|
narrowView = applyView' viewedFileReuse
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
|
|
|
|
{- Go through each file in the currently checked out branch.
|
|
|
|
|
- If the file is not annexed, skip it, unless it's a dotfile in the top.
|
2014-02-22 17:35:50 +00:00
|
|
|
|
- Look up the metadata of annexed files, and generate any ViewedFiles,
|
2014-02-18 21:38:23 +00:00
|
|
|
|
- and stage them.
|
|
|
|
|
-
|
2014-02-19 00:57:14 +00:00
|
|
|
|
- Currently only works in indirect mode. Must be run from top of
|
|
|
|
|
- repository.
|
2014-02-16 21:39:54 +00:00
|
|
|
|
-}
|
2014-02-22 17:35:50 +00:00
|
|
|
|
applyView' :: MkViewedFile -> View -> Annex Git.Branch
|
|
|
|
|
applyView' mkviewedfile view = do
|
2014-02-18 21:38:23 +00:00
|
|
|
|
top <- fromRepo Git.repoPath
|
|
|
|
|
(l, clean) <- inRepo $ Git.LsFiles.inRepo [top]
|
2014-02-19 00:01:44 +00:00
|
|
|
|
liftIO . nukeFile =<< fromRepo gitAnnexViewIndex
|
2014-02-18 21:38:23 +00:00
|
|
|
|
genViewBranch view $ do
|
|
|
|
|
uh <- inRepo Git.UpdateIndex.startUpdateIndex
|
|
|
|
|
hasher <- inRepo hashObjectStart
|
|
|
|
|
forM_ l $ \f ->
|
|
|
|
|
go uh hasher f =<< Backend.lookupFile f
|
|
|
|
|
liftIO $ do
|
|
|
|
|
hashObjectStop hasher
|
|
|
|
|
void $ stopUpdateIndex uh
|
|
|
|
|
void clean
|
|
|
|
|
where
|
2014-02-22 17:35:50 +00:00
|
|
|
|
genviewedfiles = viewedFiles view mkviewedfile -- enables memoization
|
2014-02-18 21:38:23 +00:00
|
|
|
|
go uh hasher f (Just (k, _)) = do
|
|
|
|
|
metadata <- getCurrentMetaData k
|
2014-02-22 17:35:50 +00:00
|
|
|
|
forM_ (genviewedfiles f metadata) $ \fv -> do
|
2014-02-19 00:32:00 +00:00
|
|
|
|
stagesymlink uh hasher fv =<< inRepo (gitAnnexLink fv k)
|
|
|
|
|
go uh hasher f Nothing
|
|
|
|
|
| "." `isPrefixOf` f = do
|
|
|
|
|
s <- liftIO $ getSymbolicLinkStatus f
|
|
|
|
|
if isSymbolicLink s
|
|
|
|
|
then stagesymlink uh hasher f =<< liftIO (readSymbolicLink f)
|
|
|
|
|
else do
|
|
|
|
|
sha <- liftIO $ Git.HashObject.hashFile hasher f
|
|
|
|
|
let blobtype = if isExecutable (fileMode s)
|
|
|
|
|
then ExecutableBlob
|
|
|
|
|
else FileBlob
|
|
|
|
|
liftIO . Git.UpdateIndex.streamUpdateIndex' uh
|
|
|
|
|
=<< inRepo (Git.UpdateIndex.stageFile sha blobtype f)
|
|
|
|
|
| otherwise = noop
|
|
|
|
|
stagesymlink uh hasher f linktarget = do
|
|
|
|
|
sha <- hashSymlink' hasher linktarget
|
|
|
|
|
liftIO . Git.UpdateIndex.streamUpdateIndex' uh
|
|
|
|
|
=<< inRepo (Git.UpdateIndex.stageSymlink f sha)
|
2014-02-16 21:39:54 +00:00
|
|
|
|
|
|
|
|
|
{- Applies a view to the reference branch, generating a new branch
|
|
|
|
|
- for the View.
|
|
|
|
|
-
|
|
|
|
|
- This needs to work incrementally, to quickly update the view branch
|
|
|
|
|
- when the reference branch is changed. So, it works based on an
|
|
|
|
|
- old version of the reference branch, uses diffTree to find the
|
|
|
|
|
- changes, and applies those changes to the view branch.
|
|
|
|
|
-}
|
|
|
|
|
updateView :: View -> Git.Ref -> Git.Ref -> Annex Git.Branch
|
|
|
|
|
updateView view ref oldref = genViewBranch view $ do
|
2014-02-19 18:14:44 +00:00
|
|
|
|
(diffs, cleanup) <- inRepo $ DiffTree.diffTree oldref ref
|
2014-02-16 21:39:54 +00:00
|
|
|
|
forM_ diffs go
|
|
|
|
|
void $ liftIO cleanup
|
|
|
|
|
where
|
|
|
|
|
go diff
|
2014-02-19 18:14:44 +00:00
|
|
|
|
| DiffTree.dstsha diff == nullSha = error "TODO delete file"
|
2014-02-16 21:39:54 +00:00
|
|
|
|
| otherwise = error "TODO add file"
|
|
|
|
|
|
2014-02-19 18:14:44 +00:00
|
|
|
|
{- Diff between currently checked out branch and staged changes, and
|
|
|
|
|
- update metadata to reflect the changes that are being committed to the
|
|
|
|
|
- view.
|
|
|
|
|
-
|
|
|
|
|
- Adding a file to a directory adds the metadata represented by
|
|
|
|
|
- that directory to the file, and removing a file from a directory
|
|
|
|
|
- removes the metadata.
|
|
|
|
|
-
|
|
|
|
|
- Note that removes must be handled before adds. This is so
|
|
|
|
|
- that moving a file from x/foo/ to x/bar/ adds back the metadata for x.
|
|
|
|
|
-}
|
2014-02-22 17:35:50 +00:00
|
|
|
|
withViewChanges :: (ViewedFile -> Key -> CommandStart) -> (ViewedFile -> Key -> CommandStart) -> Annex ()
|
2014-02-19 18:14:44 +00:00
|
|
|
|
withViewChanges addmeta removemeta = do
|
|
|
|
|
makeabs <- flip fromTopFilePath <$> gitRepo
|
|
|
|
|
(diffs, cleanup) <- inRepo $ DiffTree.diffIndex Git.Ref.headRef
|
|
|
|
|
forM_ diffs handleremovals
|
|
|
|
|
forM_ diffs (handleadds makeabs)
|
|
|
|
|
void $ liftIO cleanup
|
|
|
|
|
where
|
|
|
|
|
handleremovals item
|
|
|
|
|
| DiffTree.srcsha item /= nullSha =
|
|
|
|
|
handle item removemeta
|
|
|
|
|
=<< catKey (DiffTree.srcsha item) (DiffTree.srcmode item)
|
|
|
|
|
| otherwise = noop
|
|
|
|
|
handleadds makeabs item
|
|
|
|
|
| DiffTree.dstsha item /= nullSha =
|
|
|
|
|
handle item addmeta
|
|
|
|
|
=<< ifM isDirect
|
|
|
|
|
( catKey (DiffTree.dstsha item) (DiffTree.dstmode item)
|
|
|
|
|
-- optimisation
|
|
|
|
|
, isAnnexLink $ makeabs $ DiffTree.file item
|
|
|
|
|
)
|
|
|
|
|
| otherwise = noop
|
|
|
|
|
handle item a = maybe noop
|
|
|
|
|
(void . commandAction . a (getTopFilePath $ DiffTree.file item))
|
|
|
|
|
|
2014-02-18 21:38:23 +00:00
|
|
|
|
{- Generates a branch for a view. This is done using a different index
|
|
|
|
|
- file. An action is run to stage the files that will be in the branch.
|
|
|
|
|
- Then a commit is made, to the view branch. The view branch is not
|
|
|
|
|
- checked out, but entering it will display the view. -}
|
2014-02-16 21:39:54 +00:00
|
|
|
|
genViewBranch :: View -> Annex () -> Annex Git.Branch
|
2014-02-18 21:38:23 +00:00
|
|
|
|
genViewBranch view a = withIndex $ do
|
2014-02-16 21:39:54 +00:00
|
|
|
|
a
|
|
|
|
|
let branch = branchView view
|
2014-02-19 05:09:17 +00:00
|
|
|
|
void $ inRepo $ Git.Branch.commit True (fromRef branch) branch []
|
2014-02-16 21:39:54 +00:00
|
|
|
|
return branch
|
|
|
|
|
|
2014-02-18 21:38:23 +00:00
|
|
|
|
{- Runs an action using the view index file.
|
|
|
|
|
- Note that the file does not necessarily exist, or can contain
|
|
|
|
|
- info staged for an old view. -}
|
|
|
|
|
withIndex :: Annex a -> Annex a
|
|
|
|
|
withIndex a = do
|
|
|
|
|
f <- fromRepo gitAnnexViewIndex
|
|
|
|
|
withIndexFile f a
|
add tip about metadata driven views (and more flexible view filtering)
While writing this documentation, I realized that there needed to be a way
to stay in a view like tag=* while adding a filter like tag=work that
applies to the same field.
So, there are really two ways a view can be refined. It can have a new
"field=explicitvalue" filter added to it, which does not change the
"shape" of the view, but narrows the files it shows.
Or, it can have a new view added, which adds another level of
subdirectories.
So, added a vfilter command, which takes explicit values to add to the
filter, and rejects changes that would change the shape of the view.
And, made vadd only accept changes that change the shape of the view.
And, changed the View data type slightly; now components that can match
multiple metadata values can be visible, or not visible.
This commit was sponsored by Stelian Iancu.
2014-02-19 19:10:18 +00:00
|
|
|
|
|
|
|
|
|
withCurrentView :: (View -> Annex a) -> Annex a
|
|
|
|
|
withCurrentView a = maybe (error "Not in a view.") a =<< currentView
|