2014-02-18 21:38:23 +00:00
|
|
|
{- git-annex recent views log
|
|
|
|
-
|
|
|
|
- The most recently accessed view comes first.
|
|
|
|
-
|
|
|
|
- This file is stored locally in .git/annex/, not in the git-annex branch.
|
|
|
|
-
|
2023-02-08 17:55:55 +00:00
|
|
|
- Copyright 2014-2023 Joey Hess <id@joeyh.name>
|
2014-02-18 21:38:23 +00:00
|
|
|
-
|
2019-03-13 19:48:14 +00:00
|
|
|
- Licensed under the GNU AGPL version 3 or higher.
|
2014-02-18 21:38:23 +00:00
|
|
|
-}
|
|
|
|
|
2020-04-07 17:27:11 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2014-02-18 21:38:23 +00:00
|
|
|
module Logs.View (
|
|
|
|
currentView,
|
|
|
|
setView,
|
2014-02-19 01:02:27 +00:00
|
|
|
removeView,
|
2014-02-18 21:38:23 +00:00
|
|
|
recentViews,
|
|
|
|
branchView,
|
2023-02-08 17:55:55 +00:00
|
|
|
fromViewBranch,
|
2014-06-04 18:03:41 +00:00
|
|
|
is_branchView,
|
2023-02-27 18:39:33 +00:00
|
|
|
branchViewPrefix,
|
2014-02-18 21:38:23 +00:00
|
|
|
prop_branchView_legal,
|
|
|
|
) where
|
|
|
|
|
2016-01-20 20:36:33 +00:00
|
|
|
import Annex.Common
|
2014-02-18 21:38:23 +00:00
|
|
|
import Types.View
|
|
|
|
import Types.MetaData
|
2023-02-27 18:39:33 +00:00
|
|
|
import Types.AdjustedBranch
|
|
|
|
import Annex.AdjustedBranch.Name
|
2014-02-18 21:38:23 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Branch
|
|
|
|
import qualified Git.Ref
|
2014-02-19 05:09:17 +00:00
|
|
|
import Git.Types
|
2018-01-02 21:17:10 +00:00
|
|
|
import Logs.File
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2019-01-07 19:51:05 +00:00
|
|
|
import qualified Data.Text as T
|
2014-02-18 21:38:23 +00:00
|
|
|
import qualified Data.Set as S
|
|
|
|
import Data.Char
|
2020-04-07 17:27:11 +00:00
|
|
|
import qualified Data.ByteString as B
|
2014-02-18 21:38:23 +00:00
|
|
|
|
|
|
|
setView :: View -> Annex ()
|
|
|
|
setView v = do
|
2014-02-19 01:02:27 +00:00
|
|
|
old <- take 99 . filter (/= v) <$> recentViews
|
|
|
|
writeViews (v : old)
|
|
|
|
|
|
|
|
writeViews :: [View] -> Annex ()
|
|
|
|
writeViews l = do
|
2014-02-18 21:38:23 +00:00
|
|
|
f <- fromRepo gitAnnexViewLog
|
2018-01-02 21:17:10 +00:00
|
|
|
writeLogFile f $ unlines $ map show l
|
2014-02-19 01:02:27 +00:00
|
|
|
|
|
|
|
removeView :: View -> Annex ()
|
|
|
|
removeView v = writeViews =<< filter (/= v) <$> recentViews
|
2014-02-18 21:38:23 +00:00
|
|
|
|
|
|
|
recentViews :: Annex [View]
|
|
|
|
recentViews = do
|
2020-10-29 16:02:46 +00:00
|
|
|
f <- fromRawFilePath <$> fromRepo gitAnnexViewLog
|
2014-02-19 05:09:17 +00:00
|
|
|
liftIO $ mapMaybe readish . lines <$> catchDefaultIO [] (readFile f)
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2023-02-27 18:39:33 +00:00
|
|
|
{- Gets the currently checked out view, if there is one.
|
|
|
|
-
|
|
|
|
- The view may also have an adjustment applied to it.
|
|
|
|
-}
|
|
|
|
currentView :: Annex (Maybe (View, Maybe Adjustment))
|
2014-02-19 16:52:47 +00:00
|
|
|
currentView = go =<< inRepo Git.Branch.current
|
2014-02-18 21:38:23 +00:00
|
|
|
where
|
2023-02-27 18:39:33 +00:00
|
|
|
go (Just b) = case adjustedToOriginal b of
|
|
|
|
Nothing -> getvb b Nothing
|
|
|
|
Just (adj, b') -> getvb b' (Just adj)
|
|
|
|
go Nothing = return Nothing
|
|
|
|
|
|
|
|
getvb b madj
|
|
|
|
| branchViewPrefix `B.isPrefixOf` fromRef' b = do
|
|
|
|
vb <- headMaybe
|
|
|
|
. filter (\v -> branchView v Nothing == b || branchViewOld v == b)
|
|
|
|
<$> recentViews
|
|
|
|
case vb of
|
|
|
|
Just vb' -> return (Just (vb', madj))
|
|
|
|
Nothing -> return Nothing
|
|
|
|
| otherwise = return Nothing
|
|
|
|
|
|
|
|
{- Note that this is not the prefix used when an adjustment is applied to a
|
|
|
|
- view branch. -}
|
2020-04-07 17:27:11 +00:00
|
|
|
branchViewPrefix :: B.ByteString
|
2014-02-19 16:52:47 +00:00
|
|
|
branchViewPrefix = "refs/heads/views"
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2023-02-27 18:39:33 +00:00
|
|
|
{- Generates a git branch name for a View, which may also have an
|
|
|
|
- adjustment applied to it.
|
2014-02-18 21:38:23 +00:00
|
|
|
-
|
|
|
|
- There is no guarantee that each view gets a unique branch name,
|
2023-02-08 17:55:55 +00:00
|
|
|
- but the branch name is used to express the view as well as possible
|
|
|
|
- given the constraints on git branch names. It includes the name of the
|
|
|
|
- parent branch, and what metadata is used.
|
2014-02-18 21:38:23 +00:00
|
|
|
-}
|
2023-02-27 18:39:33 +00:00
|
|
|
branchView :: View -> Maybe Adjustment -> Git.Branch
|
|
|
|
branchView view madj = case madj of
|
|
|
|
Nothing -> vb
|
|
|
|
Just adj -> adjBranch $ originalToAdjusted vb adj
|
2023-02-08 17:55:55 +00:00
|
|
|
where
|
|
|
|
basebranch = fromRef' (Git.Ref.base (viewParentBranch view))
|
2023-02-27 18:39:33 +00:00
|
|
|
vb = Git.Ref $ branchViewPrefix <> "/" <> basebranch
|
|
|
|
<> "(" <> branchViewDesc view False <> ")"
|
2023-02-08 17:55:55 +00:00
|
|
|
|
|
|
|
{- Old name used for a view did not include the name of the parent branch. -}
|
|
|
|
branchViewOld :: View -> Git.Branch
|
|
|
|
branchViewOld view = Git.Ref $
|
|
|
|
branchViewPrefix <> "/" <> branchViewDesc view True
|
|
|
|
|
|
|
|
branchViewDesc :: View -> Bool -> B.ByteString
|
|
|
|
branchViewDesc view pareninvisibles = encodeBS $
|
|
|
|
intercalate ";" $ map branchcomp (viewComponents view)
|
2014-02-18 21:38:23 +00:00
|
|
|
where
|
|
|
|
branchcomp c
|
2023-02-08 17:55:55 +00:00
|
|
|
| viewVisible c || not pareninvisibles = branchcomp' c
|
2014-02-18 21:38:23 +00:00
|
|
|
| otherwise = "(" ++ branchcomp' c ++ ")"
|
2019-01-07 19:51:05 +00:00
|
|
|
branchcomp' (ViewComponent metafield viewfilter _) = concat
|
|
|
|
[ forcelegal (T.unpack (fromMetaField metafield))
|
2014-02-19 21:29:04 +00:00
|
|
|
, branchvals viewfilter
|
|
|
|
]
|
2014-03-02 18:53:19 +00:00
|
|
|
branchvals (FilterValues set) = '=' : branchset set
|
|
|
|
branchvals (FilterGlob glob) = '=' : forcelegal glob
|
|
|
|
branchvals (ExcludeValues set) = "!=" ++ branchset set
|
2023-02-07 20:28:46 +00:00
|
|
|
branchvals (FilterValuesOrUnset set _) = '=' : branchset set
|
|
|
|
branchvals (FilterGlobOrUnset glob _) = '=' : forcelegal glob
|
2014-03-02 18:53:19 +00:00
|
|
|
branchset = intercalate ","
|
2019-01-07 19:51:05 +00:00
|
|
|
. map (forcelegal . decodeBS . fromMetaValue)
|
2014-03-02 18:53:19 +00:00
|
|
|
. S.toList
|
2014-02-18 21:38:23 +00:00
|
|
|
forcelegal s
|
|
|
|
| Git.Ref.legal True s = s
|
|
|
|
| otherwise = map (\c -> if isAlphaNum c then c else '_') s
|
|
|
|
|
2014-06-04 18:03:41 +00:00
|
|
|
is_branchView :: Git.Branch -> Bool
|
2023-02-27 18:39:33 +00:00
|
|
|
is_branchView b = case adjustedToOriginal b of
|
|
|
|
Nothing -> hasprefix b
|
|
|
|
Just (_adj, b') -> hasprefix b'
|
|
|
|
where
|
|
|
|
hasprefix (Ref b') = (branchViewPrefix <> "/") `B.isPrefixOf` b'
|
2023-02-08 17:55:55 +00:00
|
|
|
|
|
|
|
{- Converts a view branch as generated by branchView (but not by
|
|
|
|
- branchViewOld) back to the parent branch.
|
|
|
|
- Has no effect on other branches. -}
|
|
|
|
fromViewBranch :: Git.Branch -> Git.Branch
|
2023-02-27 18:39:33 +00:00
|
|
|
fromViewBranch b = case adjustedToOriginal b of
|
|
|
|
Nothing -> go b
|
|
|
|
Just (_adj, b') -> go b'
|
2023-02-08 17:55:55 +00:00
|
|
|
where
|
2023-02-27 18:39:33 +00:00
|
|
|
go b' =
|
|
|
|
let bs = fromRef' b'
|
|
|
|
in if (branchViewPrefix <> "/") `B.isPrefixOf` bs
|
|
|
|
then
|
|
|
|
let (branch, _desc) = separate' (== openparen) (B.drop prefixlen bs)
|
|
|
|
in Ref branch
|
|
|
|
else b'
|
|
|
|
|
2023-02-08 17:55:55 +00:00
|
|
|
prefixlen = B.length branchViewPrefix + 1
|
|
|
|
openparen = fromIntegral (ord '(')
|
2014-06-04 18:03:41 +00:00
|
|
|
|
2014-02-18 21:38:23 +00:00
|
|
|
prop_branchView_legal :: View -> Bool
|
2023-02-27 18:39:33 +00:00
|
|
|
prop_branchView_legal = Git.Ref.legal False
|
|
|
|
. fromRef . (\v -> branchView v Nothing)
|