2014-02-18 21:38:23 +00:00
|
|
|
{- git-annex command
|
|
|
|
-
|
2023-02-27 18:39:33 +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
|
|
|
-}
|
|
|
|
|
filter out control characters in warning messages
Converted warning and similar to use StringContainingQuotedPath. Most
warnings are static strings, some do refer to filepaths that need to be
quoted, and others don't need quoting.
Note that, since quote filters out control characters of even
UnquotedString, this makes all warnings safe, even when an attacker
sneaks in a control character in some other way.
When json is being output, no quoting is done, since json gets its own
quoting.
This does, as a side effect, make warning messages in json output not
be indented. The indentation is only needed to offset warning messages
underneath the display of the file they apply to, so that's ok.
Sponsored-by: Brett Eisenberg on Patreon
2023-04-10 18:47:32 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2014-02-18 21:38:23 +00:00
|
|
|
module Command.View where
|
|
|
|
|
|
|
|
import Command
|
2023-02-07 20:28:46 +00:00
|
|
|
import qualified Annex
|
2014-02-18 21:38:23 +00:00
|
|
|
import qualified Git
|
|
|
|
import qualified Git.Command
|
|
|
|
import qualified Git.Ref
|
|
|
|
import qualified Git.Branch
|
2018-05-14 18:58:13 +00:00
|
|
|
import qualified Git.LsFiles as LsFiles
|
|
|
|
import Git.FilePath
|
Don't allow entering a view with staged or unstaged changes.
In some cases, unstaged changes are safe, eg dotfiles in the top which
are not affected by a view. Or non-annexed files in general which would
prevent view branch checkout from proceeding. But in other cases,
particularly unstaged changes to annexed files, entering a view would wipe
out those changes! And so don't allow entering a view with any unstaged
changes.
Staged changes are not safe when entering a view, because the changes get
committed to the view branch, and so the user is unlikely to remember them
when they exit the view, and so will effectively lose them, even if they're
still present in the view branch.
Also, improved the git status parser, although the improvement turned out
to not really be needed.
This commit was sponsored by Eric Drechsel on Patreon.
2018-05-14 20:51:06 +00:00
|
|
|
import Git.Status
|
2014-02-18 21:38:23 +00:00
|
|
|
import Types.View
|
|
|
|
import Annex.View
|
|
|
|
import Logs.View
|
2023-02-27 18:39:33 +00:00
|
|
|
import Types.AdjustedBranch
|
2023-02-27 19:48:58 +00:00
|
|
|
import Annex.AdjustedBranch.Name
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2020-10-30 19:55:59 +00:00
|
|
|
import qualified System.FilePath.ByteString as P
|
|
|
|
|
2015-07-08 16:33:27 +00:00
|
|
|
cmd :: Command
|
2019-08-26 19:52:19 +00:00
|
|
|
cmd = notBareRepo $
|
2015-07-08 19:08:02 +00:00
|
|
|
command "view" SectionMetaData "enter a view branch"
|
|
|
|
paramView (withParams seek)
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2015-07-08 19:08:02 +00:00
|
|
|
seek :: CmdParams -> CommandSeek
|
2018-10-01 18:12:06 +00:00
|
|
|
seek = withWords (commandAction . start)
|
2014-02-18 21:38:23 +00:00
|
|
|
|
|
|
|
start :: [String] -> CommandStart
|
2016-11-16 01:29:54 +00:00
|
|
|
start [] = giveup "Specify metadata to include in view"
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
start ps = ifM safeToEnterView
|
|
|
|
( do
|
2023-02-27 19:48:58 +00:00
|
|
|
(view, madj) <- mkView ps
|
|
|
|
go view madj =<< currentView
|
make CommandStart return a StartMessage
The goal is to be able to run CommandStart in the main thread when -J is
used, rather than unncessarily passing it off to a worker thread, which
incurs overhead that is signficant when the CommandStart is going to
quickly decide to stop.
To do that, the message it displays needs to be displayed in the worker
thread, after the CommandStart has run.
Also, the change will mean that CommandStart will no longer necessarily
run with the same Annex state as CommandPerform. While its docs already
said it should avoid modifying Annex state, I audited all the
CommandStart code as part of the conversion. (Note that CommandSeek
already sometimes runs with a different Annex state, and that has not been
a source of any problems, so I am not too worried that this change will
lead to breakage going forward.)
The only modification of Annex state I found was it calling
allowMessages in some Commands that default to noMessages. Dealt with
that by adding a startCustomOutput and a startingUsualMessages.
This lets a command start with noMessages and then select the output it
wants for each CommandStart.
One bit of breakage: onlyActionOn has been removed from commands that used it.
The plan is that, since a StartMessage contains an ActionItem,
when a Key can be extracted from that, the parallel job runner can
run onlyActionOn' automatically. Then commands won't need to worry about
this detail. Future work.
Otherwise, this was a fairly straightforward process of making each
CommandStart compile again. Hopefully other behavior changes were mostly
avoided.
In a few cases, a command had a CommandStart that called a CommandPerform
that then called showStart multiple times. I have collapsed those
down to a single start action. The main command to perhaps suffer from it
is Command.Direct, which used to show a start for each file, and no
longer does.
Another minor behavior change is that some commands used showStart
before, but had an associated file and a Key available, so were changed
to ShowStart with an ActionItemAssociatedFile. That will not change the
normal output or behavior, but --json output will now include the key.
This should not break it for anyone using a real json parser.
2019-06-06 19:42:30 +00:00
|
|
|
, giveup "Not safe to enter view."
|
|
|
|
)
|
2014-02-18 21:38:23 +00:00
|
|
|
where
|
2020-09-14 20:49:33 +00:00
|
|
|
ai = ActionItemOther Nothing
|
|
|
|
si = SeekInput ps
|
2023-02-27 19:48:58 +00:00
|
|
|
go view madj Nothing = starting "view" ai si $
|
|
|
|
perform view madj
|
|
|
|
go view _ (Just (v, _madj))
|
2014-02-18 21:38:23 +00:00
|
|
|
| v == view = stop
|
2016-11-16 01:29:54 +00:00
|
|
|
| otherwise = giveup "Already in a view. Use the vfilter and vadd commands to further refine this view."
|
2014-02-18 21:38:23 +00:00
|
|
|
|
Don't allow entering a view with staged or unstaged changes.
In some cases, unstaged changes are safe, eg dotfiles in the top which
are not affected by a view. Or non-annexed files in general which would
prevent view branch checkout from proceeding. But in other cases,
particularly unstaged changes to annexed files, entering a view would wipe
out those changes! And so don't allow entering a view with any unstaged
changes.
Staged changes are not safe when entering a view, because the changes get
committed to the view branch, and so the user is unlikely to remember them
when they exit the view, and so will effectively lose them, even if they're
still present in the view branch.
Also, improved the git status parser, although the improvement turned out
to not really be needed.
This commit was sponsored by Eric Drechsel on Patreon.
2018-05-14 20:51:06 +00:00
|
|
|
safeToEnterView :: Annex Bool
|
|
|
|
safeToEnterView = do
|
|
|
|
(l, cleanup) <- inRepo $ getStatus [] []
|
|
|
|
case filter dangerous l of
|
|
|
|
[] -> liftIO cleanup
|
|
|
|
_ -> do
|
|
|
|
warning "Your uncommitted changes would be lost when entering a view."
|
|
|
|
void $ liftIO cleanup
|
|
|
|
return False
|
|
|
|
where
|
|
|
|
dangerous (StagedUnstaged { staged = Nothing, unstaged = Nothing }) = False
|
|
|
|
-- Untracked files will not be affected by entering a view,
|
|
|
|
-- so are not dangerous.
|
|
|
|
dangerous (StagedUnstaged { staged = Just (Untracked _), unstaged = Nothing }) = False
|
|
|
|
dangerous (StagedUnstaged { unstaged = Just (Untracked _), staged = Nothing }) = False
|
|
|
|
dangerous (StagedUnstaged { unstaged = Just (Untracked _), staged = Just (Untracked _) }) = False
|
|
|
|
-- Staged changes would have their modifications either be
|
|
|
|
-- lost when entering a view, or committed as part of the view.
|
|
|
|
-- Either is dangerous because upon leaving the view; the staged
|
|
|
|
-- changes would be lost.
|
|
|
|
dangerous (StagedUnstaged { staged = Just _ }) = True
|
|
|
|
-- Unstaged changes to annexed files would get lost when entering a
|
|
|
|
-- view.
|
|
|
|
dangerous (StagedUnstaged { unstaged = Just _ }) = True
|
|
|
|
|
2023-02-27 19:48:58 +00:00
|
|
|
perform :: View -> Maybe Adjustment -> CommandPerform
|
|
|
|
perform view madj = do
|
2015-10-11 17:29:44 +00:00
|
|
|
showAction "searching"
|
2023-02-27 19:48:58 +00:00
|
|
|
next $ checkoutViewBranch view madj applyView
|
2014-02-18 21:38:23 +00:00
|
|
|
|
|
|
|
paramView :: String
|
2023-02-07 22:09:10 +00:00
|
|
|
paramView = paramRepeating "TAG FIELD=GLOB ?TAG FIELD?=GLOB FIELD!=VALUE"
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2023-02-27 19:48:58 +00:00
|
|
|
mkView :: [String] -> Annex (View, Maybe Adjustment)
|
2015-07-08 19:08:02 +00:00
|
|
|
mkView ps = go =<< inRepo Git.Branch.current
|
2014-02-18 21:38:23 +00:00
|
|
|
where
|
2016-11-16 01:29:54 +00:00
|
|
|
go Nothing = giveup "not on any branch!"
|
2023-02-27 19:48:58 +00:00
|
|
|
go (Just b) = case adjustedToOriginal b of
|
|
|
|
Nothing -> go' b Nothing
|
|
|
|
Just (adj, b') -> go' b' (Just adj)
|
|
|
|
go' b madj = do
|
2023-02-07 20:28:46 +00:00
|
|
|
vu <- annexViewUnsetDirectory <$> Annex.getGitConfig
|
2023-02-27 19:48:58 +00:00
|
|
|
let v = fst $ refineView (View b []) $
|
2023-02-07 20:28:46 +00:00
|
|
|
map (parseViewParam vu) (reverse ps)
|
2023-02-27 19:48:58 +00:00
|
|
|
return (v, madj)
|
2014-02-18 21:38:23 +00:00
|
|
|
|
2023-02-27 18:39:33 +00:00
|
|
|
checkoutViewBranch :: View -> Maybe Adjustment -> (View -> Maybe Adjustment -> Annex Git.Branch) -> CommandCleanup
|
|
|
|
checkoutViewBranch view madj mkbranch = do
|
2015-01-07 01:01:05 +00:00
|
|
|
here <- liftIO getCurrentDirectory
|
2014-02-19 00:57:14 +00:00
|
|
|
|
2023-02-27 18:39:33 +00:00
|
|
|
branch <- mkbranch view madj
|
2014-02-19 00:57:14 +00:00
|
|
|
|
2014-02-19 01:57:21 +00:00
|
|
|
showOutput
|
2014-02-18 21:38:23 +00:00
|
|
|
ok <- inRepo $ Git.Command.runBool
|
|
|
|
[ Param "checkout"
|
2014-02-19 05:09:17 +00:00
|
|
|
, Param (Git.fromRef $ Git.Ref.base branch)
|
2014-02-18 21:38:23 +00:00
|
|
|
]
|
|
|
|
when ok $ do
|
|
|
|
setView view
|
|
|
|
{- A git repo can easily have empty directories in it,
|
2018-05-14 18:58:13 +00:00
|
|
|
- and this pollutes the view, so remove them.
|
2023-03-14 02:39:16 +00:00
|
|
|
- (However, empty directories used by submodules are not
|
2018-05-14 18:58:13 +00:00
|
|
|
- removed.) -}
|
2020-10-30 19:55:59 +00:00
|
|
|
top <- liftIO . absPath =<< fromRepo Git.repoPath
|
2018-05-14 18:58:13 +00:00
|
|
|
(l, cleanup) <- inRepo $
|
2020-10-30 19:55:59 +00:00
|
|
|
LsFiles.notInRepoIncludingEmptyDirectories [] False [top]
|
2018-05-14 18:58:13 +00:00
|
|
|
forM_ l (removeemptydir top)
|
|
|
|
liftIO $ void cleanup
|
2015-01-07 01:01:05 +00:00
|
|
|
unlessM (liftIO $ doesDirectoryExist here) $ do
|
2023-04-10 21:03:41 +00:00
|
|
|
showLongNote $ UnquotedString $ cwdmissing (fromRawFilePath top)
|
2014-02-18 21:38:23 +00:00
|
|
|
return ok
|
|
|
|
where
|
2018-05-14 18:58:13 +00:00
|
|
|
removeemptydir top d = do
|
2019-12-09 17:49:05 +00:00
|
|
|
p <- inRepo $ toTopFilePath d
|
2020-10-30 19:55:59 +00:00
|
|
|
liftIO $ tryIO $ removeDirectory $
|
|
|
|
fromRawFilePath $ (top P.</> getTopFilePath p)
|
2014-02-18 21:38:23 +00:00
|
|
|
cwdmissing top = unlines
|
|
|
|
[ "This view does not include the subdirectory you are currently in."
|
|
|
|
, "Perhaps you should: cd " ++ top
|
|
|
|
]
|