Skip to content

Commit

Permalink
Merge pull request #5100 from unisonweb/24-06-12-switch-to-most-recen…
Browse files Browse the repository at this point in the history
…t-branch
  • Loading branch information
aryairani authored Jun 20, 2024
2 parents e354912 + 3ec5a5f commit 177f683
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 42 deletions.
4 changes: 2 additions & 2 deletions unison-cli/src/Unison/Cli/ProjectUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ justTheIds x =
ProjectAndBranch x.project.projectId x.branch.branchId

justTheIds' :: Sqlite.ProjectBranch -> ProjectAndBranch ProjectId ProjectBranchId
justTheIds' x =
ProjectAndBranch x.projectId x.branchId
justTheIds' branch =
ProjectAndBranch branch.projectId branch.branchId

justTheNames :: ProjectAndBranch Sqlite.Project Sqlite.ProjectBranch -> ProjectAndBranch ProjectName ProjectBranchName
justTheNames x =
Expand Down
20 changes: 9 additions & 11 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput/CommitMerge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ module Unison.Codebase.Editor.HandleInput.CommitMerge
where

import U.Codebase.Sqlite.Project qualified
import U.Codebase.Sqlite.ProjectBranch (ProjectBranch (..))
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.ProjectUtils qualified as ProjectUtils
import Unison.Codebase.Editor.HandleInput.DeleteBranch qualified as DeleteBranch
import Unison.Codebase.Editor.HandleInput.Merge2 qualified as Merge
import Unison.Codebase.Editor.HandleInput.ProjectSwitch qualified as ProjectSwitch
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Merge.TwoWay (TwoWay (..))
import Unison.Prelude
Expand All @@ -23,27 +23,25 @@ handleCommitMerge :: Cli ()
handleCommitMerge = do
(mergeProjectAndBranch, _path) <- ProjectUtils.expectCurrentProjectBranch

-- Assert that this is a "merge" branch and get its parent, which is the branch we were on when we ran `merge`.
-- Assert that this is a "merge" branch, get its parent (which is the branch we were on when we ran `merge`),
-- and switch to the parent.

parentBranchId <-
ProjectUtils.getMergeBranchParent mergeProjectAndBranch.branch
& onNothing (Cli.returnEarly Output.NoMergeInProgress)

parentBranch <-
Cli.runTransaction do
Queries.expectProjectBranch mergeProjectAndBranch.project.projectId parentBranchId

let parentProjectAndBranch =
ProjectAndBranch mergeProjectAndBranch.project parentBranch

-- Switch to the parent

ProjectSwitch.switchToProjectBranch (ProjectUtils.justTheIds parentProjectAndBranch)
parentBranch <- Queries.expectProjectBranch mergeProjectAndBranch.project.projectId parentBranchId
Queries.setMostRecentBranch parentBranch.projectId parentBranch.branchId
pure parentBranch
Cli.cd (ProjectUtils.projectBranchPath (ProjectAndBranch parentBranch.projectId parentBranch.branchId))

-- Merge the merge branch into the parent

Merge.doMergeLocalBranch
TwoWay
{ alice = parentProjectAndBranch,
{ alice = ProjectAndBranch mergeProjectAndBranch.project parentBranch,
bob = mergeProjectAndBranch
}

Expand Down
20 changes: 9 additions & 11 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput/CommitUpgrade.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ module Unison.Codebase.Editor.HandleInput.CommitUpgrade
where

import U.Codebase.Sqlite.Project qualified
import U.Codebase.Sqlite.ProjectBranch (ProjectBranch (..))
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.ProjectUtils qualified as ProjectUtils
import Unison.Codebase.Editor.HandleInput.DeleteBranch qualified as DeleteBranch
import Unison.Codebase.Editor.HandleInput.Merge2 qualified as Merge
import Unison.Codebase.Editor.HandleInput.ProjectSwitch qualified as ProjectSwitch
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Merge.TwoWay (TwoWay (..))
import Unison.Prelude
Expand All @@ -23,27 +23,25 @@ handleCommitUpgrade :: Cli ()
handleCommitUpgrade = do
(upgradeProjectAndBranch, _path) <- ProjectUtils.expectCurrentProjectBranch

-- Assert that this is an "upgrade" branch and get its parent, which is the branch we were on when we ran `upgrade`.
-- Assert that this is an "upgrade" branch, get its parent (which is the branch we were on when we ran `upgrade`),
-- and switch to the parent.

parentBranchId <-
ProjectUtils.getUpgradeBranchParent upgradeProjectAndBranch.branch
& onNothing (Cli.returnEarly Output.NoUpgradeInProgress)

parentBranch <-
Cli.runTransaction do
Queries.expectProjectBranch upgradeProjectAndBranch.project.projectId parentBranchId

let parentProjectAndBranch =
ProjectAndBranch upgradeProjectAndBranch.project parentBranch

-- Switch to the parent

ProjectSwitch.switchToProjectBranch (ProjectUtils.justTheIds parentProjectAndBranch)
parentBranch <- Queries.expectProjectBranch upgradeProjectAndBranch.project.projectId parentBranchId
Queries.setMostRecentBranch parentBranch.projectId parentBranch.branchId
pure parentBranch
Cli.cd (ProjectUtils.projectBranchPath (ProjectAndBranch parentBranch.projectId parentBranch.branchId))

-- Merge the upgrade branch into the parent

Merge.doMergeLocalBranch
TwoWay
{ alice = parentProjectAndBranch,
{ alice = ProjectAndBranch upgradeProjectAndBranch.project parentBranch,
bob = upgradeProjectAndBranch
}

Expand Down
35 changes: 17 additions & 18 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput/ProjectSwitch.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
-- | @switch@ input handler
module Unison.Codebase.Editor.HandleInput.ProjectSwitch
( projectSwitch,
switchToProjectBranch,
)
where

import Data.These (These (..))
import U.Codebase.Sqlite.DbId (ProjectBranchId, ProjectId)
import U.Codebase.Sqlite.Project qualified
import U.Codebase.Sqlite.ProjectBranch (ProjectBranch (..))
import U.Codebase.Sqlite.Queries qualified as Queries
import Unison.Cli.Monad (Cli)
import Unison.Cli.Monad qualified as Cli
Expand Down Expand Up @@ -59,21 +58,21 @@ switchToProjectAndBranchByTheseNames projectAndBranchNames0 = do
project <-
Queries.loadProjectByName projectName & onNothingM do
rollback (Output.LocalProjectDoesntExist projectName)
let branchName = unsafeFrom @Text "main"
Queries.loadProjectBranchByName project.projectId branchName & onNothingM do
rollback (Output.LocalProjectBranchDoesntExist (ProjectAndBranch projectName branchName))
Queries.loadMostRecentBranch project.projectId >>= \case
Nothing -> do
let branchName = unsafeFrom @Text "main"
branch <-
Queries.loadProjectBranchByName project.projectId branchName & onNothingM do
rollback (Output.LocalProjectBranchDoesntExist (ProjectAndBranch projectName branchName))
Queries.setMostRecentBranch branch.projectId branch.branchId
pure branch
Just branchId -> Queries.expectProjectBranch project.projectId branchId
_ -> do
projectAndBranchNames@(ProjectAndBranch projectName branchName) <- ProjectUtils.hydrateNames projectAndBranchNames0
projectAndBranchNames <- ProjectUtils.hydrateNames projectAndBranchNames0
Cli.runTransactionWithRollback \rollback -> do
Queries.loadProjectBranchByNames projectName branchName & onNothingM do
rollback (Output.LocalProjectBranchDoesntExist projectAndBranchNames)
switchToProjectBranch (ProjectUtils.justTheIds' branch)

-- | Switch to a branch:
--
-- * Record it as the most-recent branch (so it's restored when ucm starts).
-- * Change the current path in the in-memory loop state.
switchToProjectBranch :: ProjectAndBranch ProjectId ProjectBranchId -> Cli ()
switchToProjectBranch x = do
Cli.runTransaction (Queries.setMostRecentBranch x.project x.branch)
Cli.cd (ProjectUtils.projectBranchPath x)
branch <-
Queries.loadProjectBranchByNames projectAndBranchNames.project projectAndBranchNames.branch & onNothingM do
rollback (Output.LocalProjectBranchDoesntExist projectAndBranchNames)
Queries.setMostRecentBranch branch.projectId branch.branchId
pure branch
Cli.cd (ProjectUtils.projectBranchPath (ProjectAndBranch branch.projectId branch.branchId))

0 comments on commit 177f683

Please sign in to comment.