-
Notifications
You must be signed in to change notification settings - Fork 273
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 #4615 from unisonweb/travis/4423/branch-relative-path
Allow forking between projects/branches
- Loading branch information
Showing
8 changed files
with
272 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
module Unison.CommandLine.BranchRelativePath | ||
( BranchRelativePath (..), | ||
parseBranchRelativePath, | ||
branchRelativePathParser, | ||
) | ||
where | ||
|
||
import Data.Char (isSpace) | ||
import Data.Set qualified as Set | ||
import Data.Text qualified as Text | ||
import Data.These (These (..)) | ||
import Text.Megaparsec qualified as Megaparsec | ||
import Text.Megaparsec.Char qualified as Megaparsec | ||
import Unison.Codebase.Path qualified as Path | ||
import Unison.Codebase.Path.Parse qualified as Path | ||
import Unison.Prelude | ||
import Unison.Project (ProjectBranchName, ProjectBranchSpecifier (..), ProjectName) | ||
import Unison.Project qualified as Project | ||
import Unison.Util.ColorText qualified as CT | ||
import Unison.Util.Pretty qualified as P | ||
|
||
data BranchRelativePath | ||
= BranchRelative (These (Either ProjectBranchName (ProjectName, ProjectBranchName)) Path.Relative) | ||
| LoosePath Path.Path' | ||
deriving stock (Eq, Show) | ||
|
||
parseBranchRelativePath :: String -> Either (P.Pretty CT.ColorText) BranchRelativePath | ||
parseBranchRelativePath str = | ||
case Megaparsec.parse branchRelativePathParser "<none>" (Text.pack str) of | ||
Left e -> Left (P.string (Megaparsec.errorBundlePretty e)) | ||
Right x -> Right x | ||
|
||
branchRelativePathParser :: Megaparsec.Parsec Void Text BranchRelativePath | ||
branchRelativePathParser = | ||
asum | ||
[ LoosePath <$> path', | ||
BranchRelative <$> branchRelative | ||
] | ||
where | ||
branchRelative :: Megaparsec.Parsec Void Text (These (Either ProjectBranchName (ProjectName, ProjectBranchName)) Path.Relative) | ||
branchRelative = asum [fullPath, currentBranchRootPath] | ||
|
||
path' = Megaparsec.try do | ||
offset <- Megaparsec.getOffset | ||
pathStr <- Megaparsec.takeWhile1P (Just "path char") (not . isSpace) | ||
case Path.parsePath' (Text.unpack pathStr) of | ||
Left err -> failureAt offset err | ||
Right x -> pure x | ||
|
||
relPath = do | ||
offset <- Megaparsec.getOffset | ||
path' >>= \(Path.Path' inner) -> case inner of | ||
Left _ -> failureAt offset "Expected a relative path but found an absolute path" | ||
Right x -> pure x | ||
|
||
fullPath = do | ||
projectAndBranchNames <- do | ||
projectBranch <- Project.projectAndBranchNamesParser ProjectBranchSpecifier'Name | ||
offset <- Megaparsec.getOffset | ||
_ <- Megaparsec.char ':' | ||
case projectBranch of | ||
This _ -> failureAt offset "Expected a project and branch before the colon (e.g. project/branch:a.path)" | ||
That pbn -> pure (Left pbn) | ||
These pn pbn -> pure (Right (pn, pbn)) | ||
optional relPath <&> \case | ||
Nothing -> This projectAndBranchNames | ||
Just rp -> These projectAndBranchNames rp | ||
|
||
currentBranchRootPath = do | ||
_ <- Megaparsec.char ':' | ||
That <$> relPath | ||
|
||
failureAt :: forall a. Int -> String -> Megaparsec.Parsec Void Text a | ||
failureAt offset str = Megaparsec.parseError (Megaparsec.FancyError offset (Set.singleton (Megaparsec.ErrorFail str))) |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ import Network.URI qualified as URI | |
import System.Console.Haskeline.Completion (Completion (Completion)) | ||
import System.Console.Haskeline.Completion qualified as Haskeline | ||
import System.Console.Haskeline.Completion qualified as Line | ||
import Text.Megaparsec qualified as P | ||
import Text.Megaparsec qualified as Megaparsec | ||
import U.Codebase.Sqlite.DbId (ProjectBranchId, ProjectId) | ||
import U.Codebase.Sqlite.Project qualified as Sqlite | ||
import U.Codebase.Sqlite.Queries qualified as Queries | ||
|
@@ -45,6 +45,7 @@ import Unison.Codebase.SyncMode qualified as SyncMode | |
import Unison.Codebase.Verbosity (Verbosity) | ||
import Unison.Codebase.Verbosity qualified as Verbosity | ||
import Unison.CommandLine | ||
import Unison.CommandLine.BranchRelativePath (parseBranchRelativePath) | ||
import Unison.CommandLine.Completion | ||
import Unison.CommandLine.FZFResolvers qualified as Resolvers | ||
import Unison.CommandLine.InputPattern (ArgumentType (..), InputPattern (InputPattern), IsOptional (..), unionSuggestions) | ||
|
@@ -1175,9 +1176,9 @@ forkLocal = | |
] | ||
(makeExample forkLocal ["src", "dest"] <> "creates the namespace `dest` as a copy of `src`.") | ||
( \case | ||
[src, dest] -> first fromString $ do | ||
src <- Input.parseBranchId src | ||
dest <- Path.parsePath' dest | ||
[src, dest] -> do | ||
src <- Input.parseBranchId2 src | ||
dest <- parseBranchRelativePath dest | ||
pure $ Input.ForkLocalBranchI src dest | ||
_ -> Left (I.help forkLocal) | ||
) | ||
|
@@ -3512,7 +3513,7 @@ projectNameArg = | |
|
||
parsePullSource :: Text -> Maybe (ReadRemoteNamespace (These ProjectName ProjectBranchNameOrLatestRelease)) | ||
parsePullSource = | ||
P.parseMaybe (readRemoteNamespaceParser ProjectBranchSpecifier'NameOrLatestRelease) | ||
Megaparsec.parseMaybe (readRemoteNamespaceParser ProjectBranchSpecifier'NameOrLatestRelease) | ||
|
||
-- | Parse a 'Input.PushSource'. | ||
parsePushSource :: String -> Either (P.Pretty CT.ColorText) Input.PushSource | ||
|
@@ -3527,7 +3528,7 @@ parsePushSource sourceStr = | |
-- | Parse a push target. | ||
parsePushTarget :: String -> Either (P.Pretty CT.ColorText) (WriteRemoteNamespace (These ProjectName ProjectBranchName)) | ||
parsePushTarget target = | ||
case P.parseMaybe UriParser.writeRemoteNamespace (Text.pack target) of | ||
case Megaparsec.parseMaybe UriParser.writeRemoteNamespace (Text.pack target) of | ||
Nothing -> Left (I.help push) | ||
Just path -> Right path | ||
|
||
|
@@ -3549,7 +3550,7 @@ parseWriteGitRepo :: String -> String -> Either (P.Pretty P.ColorText) WriteGitR | |
parseWriteGitRepo label input = do | ||
first | ||
(fromString . show) -- turn any parsing errors into a Pretty. | ||
(P.parse (UriParser.writeGitRepo <* P.eof) label (Text.pack input)) | ||
(Megaparsec.parse (UriParser.writeGitRepo <* Megaparsec.eof) label (Text.pack input)) | ||
|
||
collectNothings :: (a -> Maybe b) -> [a] -> [a] | ||
collectNothings f as = [a | (Nothing, a) <- map f as `zip` as] | ||
|
@@ -3570,23 +3571,23 @@ explainRemote pushPull = | |
where | ||
gitRepo = PushPull.fold @(P.Pretty P.ColorText) "[email protected]:" "https://github.com/" pushPull | ||
|
||
showErrorFancy :: (P.ShowErrorComponent e) => P.ErrorFancy e -> String | ||
showErrorFancy (P.ErrorFail msg) = msg | ||
showErrorFancy (P.ErrorIndentation ord ref actual) = | ||
showErrorFancy :: (Megaparsec.ShowErrorComponent e) => Megaparsec.ErrorFancy e -> String | ||
showErrorFancy (Megaparsec.ErrorFail msg) = msg | ||
showErrorFancy (Megaparsec.ErrorIndentation ord ref actual) = | ||
"incorrect indentation (got " | ||
<> show (P.unPos actual) | ||
<> show (Megaparsec.unPos actual) | ||
<> ", should be " | ||
<> p | ||
<> show (P.unPos ref) | ||
<> show (Megaparsec.unPos ref) | ||
<> ")" | ||
where | ||
p = case ord of | ||
LT -> "less than " | ||
EQ -> "equal to " | ||
GT -> "greater than " | ||
showErrorFancy (P.ErrorCustom a) = P.showErrorComponent a | ||
showErrorFancy (Megaparsec.ErrorCustom a) = Megaparsec.showErrorComponent a | ||
|
||
showErrorItem :: P.ErrorItem (P.Token Text) -> String | ||
showErrorItem (P.Tokens ts) = P.showTokens (Proxy @Text) ts | ||
showErrorItem (P.Label label) = NE.toList label | ||
showErrorItem P.EndOfInput = "end of input" | ||
showErrorItem :: Megaparsec.ErrorItem (Megaparsec.Token Text) -> String | ||
showErrorItem (Megaparsec.Tokens ts) = Megaparsec.showTokens (Proxy @Text) ts | ||
showErrorItem (Megaparsec.Label label) = NE.toList label | ||
showErrorItem Megaparsec.EndOfInput = "end of input" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
```ucm:hide | ||
.> builtins.merge | ||
.> project.create-empty p0 | ||
.> project.create-empty p1 | ||
``` | ||
|
||
```unison | ||
foo = 5 | ||
foo.bar = 1 | ||
``` | ||
|
||
```ucm | ||
p0/main> add | ||
``` | ||
|
||
```unison | ||
bonk = 5 | ||
donk.bonk = 1 | ||
``` | ||
|
||
```ucm | ||
p1/main> add | ||
p1/main> fork p0/main: zzz | ||
p1/main> find zzz | ||
p1/main> fork p0/main:foo yyy | ||
p1/main> find yyy | ||
p0/main> fork p1/main: p0/main:p1 | ||
p0/main> ls p1 | ||
p0/main> ls p1.zzz | ||
p0/main> ls p1.yyy | ||
``` |
Oops, something went wrong.