-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4477 from unisonweb/travis/move-all
- Loading branch information
Showing
14 changed files
with
476 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
unison-cli/src/Unison/Codebase/Editor/HandleInput/MoveAll.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Unison.Codebase.Editor.HandleInput.MoveAll (handleMoveAll) where | ||
|
||
import Unison.Cli.Monad (Cli) | ||
import Unison.Cli.Monad qualified as Cli | ||
import Unison.Cli.MonadUtils qualified as Cli | ||
import Unison.Codebase.Editor.Output qualified as Output | ||
import Unison.Codebase.Path qualified as Path | ||
import Unison.HashQualified' qualified as HQ' | ||
import Unison.Prelude | ||
import Unison.Codebase.Editor.HandleInput.MoveBranch (moveBranchFunc) | ||
import Unison.Codebase.Editor.HandleInput.MoveTerm (moveTermSteps) | ||
import Unison.Codebase.Editor.HandleInput.MoveType (moveTypeSteps) | ||
|
||
handleMoveAll :: Bool -> Path.Path' -> Path.Path' -> Text -> Cli () | ||
handleMoveAll hasConfirmed src' dest' description = do | ||
moveBranchFunc <- moveBranchFunc hasConfirmed src' dest' | ||
moveTermTypeSteps <- case (,) <$> Path.toSplit' src' <*> Path.toSplit' dest' of | ||
Nothing -> pure [] | ||
Just (fmap HQ'.NameOnly -> src, dest) -> do | ||
termSteps <- moveTermSteps src dest | ||
typeSteps <- moveTypeSteps src dest | ||
pure (termSteps ++ typeSteps) | ||
case (moveBranchFunc, moveTermTypeSteps) of | ||
(Nothing, []) -> Cli.respond (Output.MoveNothingFound src') | ||
(mupdates, steps) -> do | ||
Cli.updateAndStepAt description (maybeToList mupdates) steps | ||
Cli.respond Output.Success |
43 changes: 26 additions & 17 deletions
43
unison-cli/src/Unison/Codebase/Editor/HandleInput/MoveBranch.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,42 @@ | ||
module Unison.Codebase.Editor.HandleInput.MoveBranch where | ||
module Unison.Codebase.Editor.HandleInput.MoveBranch (doMoveBranch, moveBranchFunc) where | ||
|
||
import Unison.Cli.Monad (Cli) | ||
import Unison.Cli.Monad qualified as Cli | ||
import Unison.Cli.MonadUtils qualified as Cli | ||
import Unison.Codebase.Branch (Branch) | ||
import Unison.Codebase.Branch qualified as Branch | ||
import Unison.Codebase.Editor.Output (Output (..)) | ||
import Unison.Codebase.Path qualified as Path | ||
import Unison.Prelude | ||
|
||
-- | Moves a branch and its history from one location to another, and saves the new root | ||
-- branch. | ||
doMoveBranch :: Text -> Bool -> Path.Path' -> Path.Path' -> Cli () | ||
doMoveBranch actionDescription hasConfirmed src' dest' = do | ||
moveBranchFunc :: Bool -> Path.Path' -> Path.Path' -> Cli (Maybe (Path.Absolute, Branch IO -> Branch IO)) | ||
moveBranchFunc hasConfirmed src' dest' = do | ||
srcAbs <- Cli.resolvePath' src' | ||
destAbs <- Cli.resolvePath' dest' | ||
destBranchExists <- Cli.branchExistsAtPath' dest' | ||
let isRootMove = (Path.isRoot srcAbs || Path.isRoot destAbs) | ||
when (isRootMove && not hasConfirmed) do | ||
Cli.returnEarly MoveRootBranchConfirmation | ||
srcBranch <- Cli.expectBranchAtPath' src' | ||
Cli.getMaybeBranchAt srcAbs >>= traverse \srcBranch -> do | ||
-- We want the move to appear as a single step in the root namespace, but we need to make | ||
-- surgical changes in both the root and the destination, so we make our modifications at the shared parent of | ||
-- those changes such that they appear as a single change in the root. | ||
let (changeRootPath, srcLoc, destLoc) = Path.longestPathPrefix (Path.unabsolute srcAbs) (Path.unabsolute destAbs) | ||
let doMove changeRoot = | ||
changeRoot | ||
& Branch.modifyAt srcLoc (const Branch.empty) | ||
& Branch.modifyAt destLoc (const srcBranch) | ||
if (destBranchExists && not isRootMove) | ||
then Cli.respond (MovedOverExistingBranch dest') | ||
else pure () | ||
pure (Path.Absolute changeRootPath, doMove) | ||
|
||
-- We want the move to appear as a single step in the root namespace, but we need to make | ||
-- surgical changes in both the root and the destination, so we make our modifications at the shared parent of | ||
-- those changes such that they appear as a single change in the root. | ||
let (changeRootPath, srcLoc, destLoc) = Path.longestPathPrefix (Path.unabsolute srcAbs) (Path.unabsolute destAbs) | ||
Cli.updateAt actionDescription (Path.Absolute changeRootPath) \changeRoot -> | ||
changeRoot | ||
& Branch.modifyAt srcLoc (const Branch.empty) | ||
& Branch.modifyAt destLoc (const srcBranch) | ||
if (destBranchExists && not isRootMove) | ||
then Cli.respond (MovedOverExistingBranch dest') | ||
else Cli.respond Success | ||
-- | Moves a branch and its history from one location to another, and saves the new root | ||
-- branch. | ||
doMoveBranch :: Text -> Bool -> Path.Path' -> Path.Path' -> Cli () | ||
doMoveBranch actionDescription hasConfirmed src' dest' = do | ||
moveBranchFunc hasConfirmed src' dest' >>= \case | ||
Nothing -> Cli.respond (BranchNotFound src') | ||
Just (path, func) -> do | ||
_ <- Cli.updateAt actionDescription path func | ||
Cli.respond Success |
48 changes: 48 additions & 0 deletions
48
unison-cli/src/Unison/Codebase/Editor/HandleInput/MoveTerm.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Unison.Codebase.Editor.HandleInput.MoveTerm (doMoveTerm, moveTermSteps) where | ||
|
||
import Control.Lens (over, _2) | ||
import Data.Set qualified as Set | ||
import Unison.Cli.Monad (Cli) | ||
import Unison.Cli.Monad qualified as Cli | ||
import Unison.Cli.MonadUtils qualified as Cli | ||
import Unison.Codebase qualified as Codebase | ||
import Unison.Codebase.Branch (Branch0) | ||
import Unison.Codebase.BranchUtil qualified as BranchUtil | ||
import Unison.Codebase.Editor.Output qualified as Output | ||
import Unison.Codebase.Path (Path, Path') | ||
import Unison.Codebase.Path qualified as Path | ||
import Unison.HashQualified' qualified as HQ' | ||
import Unison.NameSegment (NameSegment) | ||
import Unison.Prelude | ||
|
||
moveTermSteps :: (Path', HQ'.HQSegment) -> (Path', NameSegment) -> Cli [(Path, Branch0 m -> Branch0 m)] | ||
moveTermSteps src' dest' = do | ||
src <- Cli.resolveSplit' src' | ||
srcTerms <- Cli.getTermsAt src | ||
case Set.toList srcTerms of | ||
[] -> pure [] | ||
_:_:_ -> do | ||
hqLength <- Cli.runTransaction Codebase.hashLength | ||
Cli.returnEarly (Output.DeleteNameAmbiguous hqLength src' srcTerms Set.empty) | ||
[srcTerm] -> do | ||
dest <- Cli.resolveSplit' dest' | ||
destTerms <- Cli.getTermsAt (Path.convert dest) | ||
when (not (Set.null destTerms)) do | ||
Cli.returnEarly (Output.TermAlreadyExists dest' destTerms) | ||
let p = Path.convert src | ||
srcMetadata <- do | ||
root0 <- Cli.getRootBranch0 | ||
pure (BranchUtil.getTermMetadataAt p srcTerm root0) | ||
pure | ||
[ -- Mitchell: throwing away any hash-qualification here seems wrong! | ||
BranchUtil.makeDeleteTermName (over _2 HQ'.toName p) srcTerm, | ||
BranchUtil.makeAddTermName (Path.convert dest) srcTerm srcMetadata | ||
] | ||
|
||
doMoveTerm :: (Path', HQ'.HQSegment) -> (Path', NameSegment) -> Text -> Cli () | ||
doMoveTerm src' dest' description = do | ||
steps <- moveTermSteps src' dest' | ||
when (null steps) do | ||
Cli.returnEarly (Output.TermNotFound src') | ||
Cli.stepManyAt description steps | ||
Cli.respond Output.Success |
48 changes: 48 additions & 0 deletions
48
unison-cli/src/Unison/Codebase/Editor/HandleInput/MoveType.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Unison.Codebase.Editor.HandleInput.MoveType (doMoveType, moveTypeSteps) where | ||
|
||
import Control.Lens (over, _2) | ||
import Data.Set qualified as Set | ||
import Unison.Cli.Monad (Cli) | ||
import Unison.Cli.Monad qualified as Cli | ||
import Unison.Cli.MonadUtils qualified as Cli | ||
import Unison.Codebase qualified as Codebase | ||
import Unison.Codebase.Branch (Branch0) | ||
import Unison.Codebase.BranchUtil qualified as BranchUtil | ||
import Unison.Codebase.Editor.Output qualified as Output | ||
import Unison.Codebase.Path (Path, Path') | ||
import Unison.Codebase.Path qualified as Path | ||
import Unison.HashQualified' qualified as HQ' | ||
import Unison.NameSegment (NameSegment) | ||
import Unison.Prelude | ||
|
||
moveTypeSteps :: (Path', HQ'.HQSegment) -> (Path', NameSegment) -> Cli [(Path, Branch0 m -> Branch0 m)] | ||
moveTypeSteps src' dest' = do | ||
src <- Cli.resolveSplit' src' | ||
srcTypes <- Cli.getTypesAt src | ||
case Set.toList srcTypes of | ||
[] -> pure [] | ||
_:_:_ -> do | ||
hqLength <- Cli.runTransaction Codebase.hashLength | ||
Cli.returnEarly (Output.DeleteNameAmbiguous hqLength src' Set.empty srcTypes) | ||
[srcType] -> do | ||
dest <- Cli.resolveSplit' dest' | ||
destTypes <- Cli.getTypesAt (Path.convert dest) | ||
when (not (Set.null destTypes)) do | ||
Cli.returnEarly (Output.TypeAlreadyExists dest' destTypes) | ||
let p = Path.convert src | ||
srcMetadata <- do | ||
root0 <- Cli.getRootBranch0 | ||
pure (BranchUtil.getTypeMetadataAt p srcType root0) | ||
pure | ||
[ -- Mitchell: throwing away any hash-qualification here seems wrong! | ||
BranchUtil.makeDeleteTypeName (over _2 HQ'.toName p) srcType, | ||
BranchUtil.makeAddTypeName (Path.convert dest) srcType srcMetadata | ||
] | ||
|
||
doMoveType :: (Path', HQ'.HQSegment) -> (Path', NameSegment) -> Text -> Cli () | ||
doMoveType src' dest' description = do | ||
steps <- moveTypeSteps src' dest' | ||
when (null steps) do | ||
Cli.returnEarly (Output.TypeNotFound src') | ||
Cli.stepManyAt description steps | ||
Cli.respond Output.Success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.