Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Project Roots #5041

Draft
wants to merge 164 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 157 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
73145db
Add Location pt 2
ChrisPenner May 20, 2024
fe751e1
Remove direct access to root branch
ChrisPenner May 20, 2024
12b3107
Add migration to move project branches to sqlite
ChrisPenner May 20, 2024
39f065e
Propagate root branch accessors
ChrisPenner May 20, 2024
2c64c6a
Checkpoint
ChrisPenner May 21, 2024
1a15c3f
Set currentProjectPath in SQLite
ChrisPenner May 21, 2024
dca8431
Add getShallowProjectRootByNames
ChrisPenner May 21, 2024
2b50419
Rewrite BranchRelativePaths machinery
ChrisPenner May 22, 2024
5a11d23
Resolve merge conflicts
ChrisPenner May 23, 2024
29fd307
Checkpoint
ChrisPenner May 23, 2024
2c98ad1
Checkpoint
ChrisPenner May 23, 2024
467dee1
Checkpoint
ChrisPenner May 24, 2024
457ca14
Allow updating branch heads
ChrisPenner May 24, 2024
9de134f
Merge deletion of git commands into project-root
ChrisPenner May 28, 2024
9756ac8
WIP
ChrisPenner May 28, 2024
8377f68
merge trunk
ChrisPenner Jun 3, 2024
965f36f
Merge trunk
ChrisPenner Jun 3, 2024
33c42db
Fix weird added file
ChrisPenner Jun 3, 2024
0d80992
Remove root branch accessors from codebase
ChrisPenner Jun 3, 2024
216967f
Use onCreate hook to initialize projects
ChrisPenner Jun 3, 2024
1d906b3
Clean up uses of root in Codebase, kill the root branch cache
ChrisPenner Jun 3, 2024
3361010
Fix up uses of root branch in Backend
ChrisPenner Jun 3, 2024
2e754a8
Propagate lack of root branch combinators to local Codebase Server
ChrisPenner Jun 3, 2024
2773153
Fix argparse for running 'main' within project
ChrisPenner Jun 3, 2024
33458a8
Fix LSP to work with projects
ChrisPenner Jun 3, 2024
77af994
Merge trunk
ChrisPenner Jun 3, 2024
4e2ccf2
Fix up some more root branch gets
ChrisPenner Jun 3, 2024
138c6e9
Fix up input patterns for project branch args
ChrisPenner Jun 3, 2024
3267a3e
Fix up Merge2
ChrisPenner Jun 3, 2024
9bf5d6f
Fix up MonadUtils
ChrisPenner Jun 3, 2024
c3a2dfb
Don't include causalHashId in ProjectBranch
ChrisPenner Jun 4, 2024
ecba936
Fix up project branch deletes
ChrisPenner Jun 4, 2024
f42d57f
Fix up MonadUtils again
ChrisPenner Jun 4, 2024
5ad808c
Update MoveTerm, MoveType, MoveBranch
ChrisPenner Jun 4, 2024
36c63e4
Merge trunk
ChrisPenner Jun 5, 2024
ce1c221
Fixup MoveAll
ChrisPenner Jun 5, 2024
a46321f
Fix up Branch.hs module
ChrisPenner Jun 5, 2024
0016706
Pull cleanup
ChrisPenner Jun 5, 2024
0799264
Remove NoSync primitive usages in Update/Propagate
ChrisPenner Jun 5, 2024
07f8774
Remove nosync usages from AddRun
ChrisPenner Jun 5, 2024
14bd801
Remove NoSync primitive usages
ChrisPenner Jun 5, 2024
8b5859c
Remove NoSync primitives
ChrisPenner Jun 5, 2024
87bd969
Inline nosync versions into their regular forms so people aren't temp…
ChrisPenner Jun 5, 2024
9f7b825
Tweaks
ChrisPenner Jun 5, 2024
1e67634
Merge removal of nosync with project-root
ChrisPenner Jun 6, 2024
6518e3f
Deleting a bunch of stuff to do with push/pull loose code
ChrisPenner Jun 6, 2024
3f44257
Better project-branch centric utils
ChrisPenner Jun 7, 2024
276758f
More command cleanups
ChrisPenner Jun 7, 2024
3aa8fcc
Fix up Project Clone
ChrisPenner Jun 7, 2024
c42d128
Fix up ProjectCreate
ChrisPenner Jun 7, 2024
b663d44
Finish cleaning up create branch
ChrisPenner Jun 7, 2024
16dea7a
Fix up Merge
ChrisPenner Jun 7, 2024
e24a5ee
More command fixups
ChrisPenner Jun 7, 2024
e65f6e1
Remove ability to push loose code paths (local or on share)
ChrisPenner Jun 7, 2024
9c17d14
Remove ability to push to loose code or pull into loose code.
ChrisPenner Jun 7, 2024
5b0045d
Fix ReleaseDraft
ChrisPenner Jun 7, 2024
9936a02
Fix Upgrade
ChrisPenner Jun 7, 2024
1e4627b
Fix update.old
ChrisPenner Jun 7, 2024
7b5845f
Fix AddRun
ChrisPenner Jun 7, 2024
b1ad159
Fix up Pull
ChrisPenner Jun 7, 2024
0b16a7c
Fixup BranchId
ChrisPenner Jun 7, 2024
bcc39d8
Remerge trunk
ChrisPenner Jun 7, 2024
7a4ae28
Remerge trunk again
ChrisPenner Jun 10, 2024
7274361
Fix up Pull and output messages
ChrisPenner Jun 10, 2024
a7d455c
Fix CommitUpgrade
ChrisPenner Jun 10, 2024
87c9d5b
automatically run ormolu
ChrisPenner Jun 10, 2024
1faba84
Don't expose dangerous primitives for setting project root
ChrisPenner Jun 10, 2024
6d78dab
Fix up most imports in HandleInput
ChrisPenner Jun 10, 2024
b76e559
Fix projectbranch resolve rename
ChrisPenner Jun 10, 2024
591d72c
Resolve imports in HandleInput
ChrisPenner Jun 10, 2024
18cde10
WIP
ChrisPenner Jun 10, 2024
8138e61
toText and output munging
ChrisPenner Jun 10, 2024
48371c0
Propagate ProjectPath into CLI Main
ChrisPenner Jun 10, 2024
b876ab1
Solve branch conflict
ChrisPenner Jun 11, 2024
91527b6
Merge trunk
ChrisPenner Jun 11, 2024
2863b66
Convert transcript parser to be project based
ChrisPenner Jun 12, 2024
23fd0a0
Allow passing project and branch as starting path
ChrisPenner Jun 12, 2024
7298bbe
Revive causal hash signal in LSP
ChrisPenner Jun 12, 2024
7aabcf5
Fix up Execute
ChrisPenner Jun 12, 2024
95fd37a
Bootstrap scratch project in migration
ChrisPenner Jun 12, 2024
203f2ce
Add better callstacks to sqlite exceptions.
ChrisPenner Jun 12, 2024
d53f267
Create scratch project during codebase creation
ChrisPenner Jun 12, 2024
6ba3e87
Add to project reflog
ChrisPenner Jun 12, 2024
cdf10c9
Migration to port project branches to have causal ids.
ChrisPenner Jun 12, 2024
6d8605e
Merge trunk
ChrisPenner Jun 13, 2024
6c11576
Split migrations into separate transactions
ChrisPenner Jun 14, 2024
ff2c270
Fix up a buncha sql
ChrisPenner Jun 14, 2024
f192edb
Fix up migration quirks
ChrisPenner Jun 14, 2024
cc441e9
Fix projectpath updating in memory branch
ChrisPenner Jun 15, 2024
1c186d4
Merge trunk into cp/project-root
ChrisPenner Jun 24, 2024
60f99c2
Insert scratch branch after adding the causal hash table
ChrisPenner Jun 24, 2024
4261902
Fix order of operations on codebase creation
ChrisPenner Jun 24, 2024
4fb0077
Don't create new projects on each transcript, just use the current pr…
ChrisPenner Jun 24, 2024
5a6fe20
Migrate loose code into legacy project
ChrisPenner Jun 24, 2024
8c2b6cf
Remove now unused namespaceRoot combinators
ChrisPenner Jun 24, 2024
1cbac28
Fix unique type guid generation
ChrisPenner Jun 24, 2024
1f34de9
Swap stepAt combinators to use project paths
ChrisPenner Jun 25, 2024
dadc4e4
Merge trunk and resolve basic conflicts
ChrisPenner Jun 27, 2024
0b8548f
Delete some out of date bits of transcripts
ChrisPenner Jun 27, 2024
38d60e7
Switch project before deleting it
ChrisPenner Jun 27, 2024
fce12cb
Work on fixing some transcripts for projects
ChrisPenner Jun 27, 2024
5f78557
Solve conflicts
ChrisPenner Jul 1, 2024
2002a56
Remove loose code support from api
ChrisPenner Jul 1, 2024
724dea4
Update api transcripts
ChrisPenner Jul 1, 2024
d93b5de
Update paths in branch-relative-path.md
ChrisPenner Jul 1, 2024
fbd7bb9
Update 'bug-strange-closure.md'
ChrisPenner Jul 2, 2024
081f344
Fix delete.md
ChrisPenner Jul 2, 2024
99bad76
Allow hashes or branches in diff.namespace
ChrisPenner Jul 2, 2024
b636068
Partially update diff-namespace
ChrisPenner Jul 2, 2024
6f19a87
Update merge.md and fix5129
ChrisPenner Jul 2, 2024
983fb05
Port move-namespace to projects
ChrisPenner Jul 2, 2024
55ad236
Update name-selection transcript
ChrisPenner Jul 2, 2024
e66b315
Partially translate names.md
ChrisPenner Jul 2, 2024
498e898
Update namespace-dependencies
ChrisPenner Jul 2, 2024
9af023b
Fixup resolution-failures.md
ChrisPenner Jul 2, 2024
c095003
Fix up tab-completion transcripts for project roots
ChrisPenner Jul 2, 2024
478545e
Rewrite view.md, still failing
ChrisPenner Jul 2, 2024
7dbb365
Convert diff-namespaces.md to project-root
ChrisPenner Jul 2, 2024
5c675df
Revive ability to delete root namespace
ChrisPenner Jul 2, 2024
6ae9f8b
Fix behaviour of branch command
ChrisPenner Jul 2, 2024
eaf233c
automatically run ormolu
ChrisPenner Jul 2, 2024
e9f2aa3
Un-ignore transcript-parser-commands.md and fix it.
ChrisPenner Jul 2, 2024
2331c16
Mostly Fix up deleting the branch you're on
ChrisPenner Jul 2, 2024
cc07b63
Merge trunk
ChrisPenner Jul 2, 2024
1dab376
Fix behaviour of deleting last project or branch
ChrisPenner Jul 3, 2024
a7820fe
Deprecate root reflog behaviour in favour of project/branch reflogs
ChrisPenner Jul 3, 2024
7148685
Implement DB combinators for project and branch reflogs
ChrisPenner Jul 3, 2024
7f57612
More WIP on reflog commands
ChrisPenner Jul 3, 2024
2db50ad
Specialize 'fromHash' to CausalHash
ChrisPenner Jul 3, 2024
31874bd
Add reflog.global command
ChrisPenner Jul 3, 2024
8a405e0
Update reflog command WIP
ChrisPenner Jul 3, 2024
49258bc
Merge trunk
ChrisPenner Jul 5, 2024
dd81f0a
disable names.global transcript for now
ChrisPenner Jul 5, 2024
4c89423
Rerun transcripts
ChrisPenner Jul 5, 2024
d0002b4
Disable view.global transcript for now
ChrisPenner Jul 5, 2024
424b43b
Fix up reset transcripts for projects
ChrisPenner Jul 5, 2024
6021a3a
automatically run ormolu
ChrisPenner Jul 6, 2024
927b76b
Get tests building again.
ChrisPenner Jul 6, 2024
c431d35
Fix integration-tests
ChrisPenner Jul 6, 2024
69edcef
Fix up round-trip tests
ChrisPenner Jul 6, 2024
0cea9d6
Merge trunk, clean up random extra files
ChrisPenner Jul 8, 2024
5e775cc
Improve docs
ChrisPenner Jul 8, 2024
c625e47
Add undo.md transcript
ChrisPenner Jul 8, 2024
66d9b76
Ignore more scratchfiles
ChrisPenner Jul 9, 2024
e506b00
No in-memory branch in loop-state
ChrisPenner Jul 9, 2024
d8e34c2
Write new Transactional Signal type
ChrisPenner Jul 9, 2024
4254a51
Update LSP to listen for changes
ChrisPenner Jul 9, 2024
c156ba7
Add isTranscriptTest to Cli.Env
ChrisPenner Jul 9, 2024
6fe6d67
Omit times from project reflogs
ChrisPenner Jul 9, 2024
736ccf1
Merge trunk
ChrisPenner Jul 9, 2024
951f318
preload branches into the branch cache when switching projects
ChrisPenner Jul 9, 2024
09ecc74
Fix test build
ChrisPenner Jul 9, 2024
1543160
Revive move-branch confirmations
ChrisPenner Jul 10, 2024
67a41cd
Docs and transcripts
ChrisPenner Jul 10, 2024
1cd3f3c
Fix move-namespace transcript
ChrisPenner Jul 10, 2024
fa6c59e
Fix merge transcript
ChrisPenner Jul 10, 2024
f725bf2
Fix names transcript
ChrisPenner Jul 10, 2024
bf20459
Typo
ChrisPenner Jul 10, 2024
2f4e57d
Fix Upgrade
ChrisPenner Jul 10, 2024
f382ef1
Attempt to fix up update.old
ChrisPenner Jul 10, 2024
a455180
No leading dot on brps
ChrisPenner Jul 10, 2024
0cd3cd1
Merge branch 'trunk' into cp/project-root
aryairani Jul 11, 2024
7113005
Remerge remote
ChrisPenner Jul 11, 2024
4045805
Merge trunk
ChrisPenner Jul 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Unison
.unison*
test-output
transcript-*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arya and I believe this was for some ancient transcript stuff that doesn't exist anymore, but it was also preventing changes to one of our transcripts that started with transcript- so I removed it.

scratch.u
unisonLocal.zip
*.uc
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICT there's no reason we'd want to keep uc files around.

# Ignore all scratch files...
*.u
# Except those in unison-src
!unison-src/**/*.u
# And integration tests
!unison-cli-integration/integration-tests/IntegrationTests/**/*.u

# Auto-generated
jit-tests.md
Expand All @@ -19,6 +24,7 @@ dist-newstyle
# GHC
*.hie
*.prof
*.prof.html
/.direnv/
/.envrc

Expand Down
128 changes: 72 additions & 56 deletions codebase2/codebase-sqlite/U/Codebase/Sqlite/Operations.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module U.Codebase.Sqlite.Operations
( -- * branches
saveRootBranch,
loadRootCausalHash,
expectRootCausalHash,
expectRootCausal,
expectRootBranchHash,
Comment on lines -3 to -7
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes the ability to access to root namespace entirely.

loadCausalHashAtPath,
expectCausalHashAtPath,
loadCausalBranchAtPath,
Expand All @@ -13,6 +8,7 @@ module U.Codebase.Sqlite.Operations
saveBranchV3,
loadCausalBranchByCausalHash,
expectCausalBranchByCausalHash,
expectBranchByCausalHashId,
expectBranchByBranchHash,
expectBranchByBranchHashId,
expectNamespaceStatsByHash,
Expand Down Expand Up @@ -100,9 +96,16 @@ module U.Codebase.Sqlite.Operations
fuzzySearchDefinitions,
namesPerspectiveForRootAndPath,

-- * Projects
expectProjectAndBranchNames,
expectProjectBranchHead,

-- * reflog
getReflog,
appendReflog,
getDeprecatedRootReflog,
getProjectReflog,
getProjectBranchReflog,
getGlobalReflog,
appendProjectReflog,

-- * low-level stuff
expectDbBranch,
Expand Down Expand Up @@ -183,6 +186,9 @@ import U.Codebase.Sqlite.Patch.TermEdit qualified as S
import U.Codebase.Sqlite.Patch.TermEdit qualified as S.TermEdit
import U.Codebase.Sqlite.Patch.TypeEdit qualified as S
import U.Codebase.Sqlite.Patch.TypeEdit qualified as S.TypeEdit
import U.Codebase.Sqlite.Project (Project (..))
import U.Codebase.Sqlite.ProjectBranch (ProjectBranch (..))
import U.Codebase.Sqlite.ProjectReflog qualified as ProjectReflog
import U.Codebase.Sqlite.Queries qualified as Q
import U.Codebase.Sqlite.Reference qualified as S
import U.Codebase.Sqlite.Reference qualified as S.Reference
Expand All @@ -200,6 +206,7 @@ import U.Codebase.TypeEdit qualified as C.TypeEdit
import U.Codebase.WatchKind (WatchKind)
import U.Util.Base32Hex qualified as Base32Hex
import U.Util.Serialization qualified as S
import Unison.Core.Project (ProjectBranchName, ProjectName)
import Unison.Hash qualified as H
import Unison.Hash32 qualified as Hash32
import Unison.NameSegment (NameSegment)
Expand Down Expand Up @@ -232,23 +239,10 @@ expectValueHashByCausalHashId = loadValueHashById <=< Q.expectCausalValueHashId
loadValueHashById :: Db.BranchHashId -> Transaction BranchHash
loadValueHashById = fmap BranchHash . Q.expectHash . Db.unBranchHashId

expectRootCausalHash :: Transaction CausalHash
expectRootCausalHash = Q.expectCausalHash =<< Q.expectNamespaceRoot

expectRootBranchHash :: Transaction BranchHash
expectRootBranchHash = do
rootCausalHashId <- Q.expectNamespaceRoot
expectValueHashByCausalHashId rootCausalHashId

loadRootCausalHash :: Transaction (Maybe CausalHash)
loadRootCausalHash =
runMaybeT $
lift . Q.expectCausalHash =<< MaybeT Q.loadNamespaceRoot

-- | Load the causal hash at the given path from the provided root, if Nothing, use the
-- codebase root.
loadCausalHashAtPath :: Maybe CausalHash -> [NameSegment] -> Transaction (Maybe CausalHash)
loadCausalHashAtPath mayRootCausalHash =
loadCausalHashAtPath :: CausalHash -> [NameSegment] -> Transaction (Maybe CausalHash)
loadCausalHashAtPath rootCausalHash =
let go :: Db.CausalHashId -> [NameSegment] -> MaybeT Transaction CausalHash
go hashId = \case
[] -> lift (Q.expectCausalHash hashId)
Expand All @@ -258,15 +252,13 @@ loadCausalHashAtPath mayRootCausalHash =
(_, hashId') <- MaybeT (pure (Map.lookup tid children))
go hashId' ts
in \path -> do
hashId <- case mayRootCausalHash of
Nothing -> Q.expectNamespaceRoot
Just rootCH -> Q.expectCausalHashIdByCausalHash rootCH
hashId <- Q.expectCausalHashIdByCausalHash rootCausalHash
runMaybeT (go hashId path)

-- | Expect the causal hash at the given path from the provided root, if Nothing, use the
-- codebase root.
expectCausalHashAtPath :: Maybe CausalHash -> [NameSegment] -> Transaction CausalHash
expectCausalHashAtPath mayRootCausalHash =
expectCausalHashAtPath :: CausalHash -> [NameSegment] -> Transaction CausalHash
expectCausalHashAtPath rootCausalHash =
let go :: Db.CausalHashId -> [NameSegment] -> Transaction CausalHash
go hashId = \case
[] -> Q.expectCausalHash hashId
Expand All @@ -276,23 +268,21 @@ expectCausalHashAtPath mayRootCausalHash =
let (_, hashId') = children Map.! tid
go hashId' ts
in \path -> do
hashId <- case mayRootCausalHash of
Nothing -> Q.expectNamespaceRoot
Just rootCH -> Q.expectCausalHashIdByCausalHash rootCH
hashId <- Q.expectCausalHashIdByCausalHash rootCausalHash
go hashId path

loadCausalBranchAtPath ::
Maybe CausalHash ->
CausalHash ->
[NameSegment] ->
Transaction (Maybe (C.Branch.CausalBranch Transaction))
loadCausalBranchAtPath maybeRootCausalHash path =
loadCausalHashAtPath maybeRootCausalHash path >>= \case
loadCausalBranchAtPath rootCausalHash path =
loadCausalHashAtPath rootCausalHash path >>= \case
Nothing -> pure Nothing
Just causalHash -> Just <$> expectCausalBranchByCausalHash causalHash

loadBranchAtPath :: Maybe CausalHash -> [NameSegment] -> Transaction (Maybe (C.Branch.Branch Transaction))
loadBranchAtPath maybeRootCausalHash path =
loadCausalBranchAtPath maybeRootCausalHash path >>= \case
loadBranchAtPath :: CausalHash -> [NameSegment] -> Transaction (Maybe (C.Branch.Branch Transaction))
loadBranchAtPath rootCausalHash path =
loadCausalBranchAtPath rootCausalHash path >>= \case
Nothing -> pure Nothing
Just causal -> Just <$> C.Causal.value causal

Expand Down Expand Up @@ -613,16 +603,6 @@ s2cBranch (S.Branch.Full.Branch tms tps patches children) =
boId <- Q.expectBranchObjectIdByCausalHashId chId
expectBranch boId

saveRootBranch ::
HashHandle ->
C.Branch.CausalBranch Transaction ->
Transaction (Db.BranchObjectId, Db.CausalHashId)
saveRootBranch hh c = do
when debug $ traceM $ "Operations.saveRootBranch " ++ show (C.causalHash c)
(boId, chId) <- saveBranch hh c
Q.setNamespaceRoot chId
pure (boId, chId)

-- saveBranch is kind of a "deep save causal"

-- we want a "shallow save causal" that could take a
Expand Down Expand Up @@ -749,9 +729,6 @@ saveCausalObject hh (C.Causal.Causal hc he parents _) = do
Q.saveCausal hh chId bhId parentCausalHashIds
pure (chId, bhId)

expectRootCausal :: Transaction (C.Branch.CausalBranch Transaction)
expectRootCausal = Q.expectNamespaceRoot >>= expectCausalBranchByCausalHashId

loadCausalBranchByCausalHash :: CausalHash -> Transaction (Maybe (C.Branch.CausalBranch Transaction))
loadCausalBranchByCausalHash hc = do
Q.loadCausalHashIdByCausalHash hc >>= \case
Expand Down Expand Up @@ -1510,15 +1487,43 @@ namespaceStatsForDbBranch = \case
expectNamespaceStatsByHashId bhId

-- | Gets the specified number of reflog entries in chronological order, most recent first.
getReflog :: Int -> Transaction [Reflog.Entry CausalHash Text]
getReflog numEntries = do
entries <- Q.getReflog numEntries
getDeprecatedRootReflog :: Int -> Transaction [Reflog.Entry CausalHash Text]
getDeprecatedRootReflog numEntries = do
entries <- Q.getDeprecatedRootReflog numEntries
traverse (bitraverse Q.expectCausalHash pure) entries

appendReflog :: Reflog.Entry CausalHash Text -> Transaction ()
appendReflog entry = do
dbEntry <- (bitraverse Q.saveCausalHash pure) entry
Q.appendReflog dbEntry
-- | Gets the specified number of reflog entries for the given project in chronological order, most recent first.
getProjectReflog :: Int -> Db.ProjectId -> Transaction [ProjectReflog.Entry Project ProjectBranch CausalHash]
getProjectReflog numEntries projectId = do
entries <- Q.getProjectReflog numEntries projectId
traverse hydrateProjectReflogEntry entries

-- | Gets the specified number of reflog entries for the specified ProjectBranch in chronological order, most recent first.
getProjectBranchReflog :: Int -> Db.ProjectBranchId -> Transaction [ProjectReflog.Entry Project ProjectBranch CausalHash]
getProjectBranchReflog numEntries projectBranchId = do
entries <- Q.getProjectBranchReflog numEntries projectBranchId
traverse hydrateProjectReflogEntry entries

-- | Gets the specified number of reflog entries in chronological order, most recent first.
getGlobalReflog :: Int -> Transaction [ProjectReflog.Entry Project ProjectBranch CausalHash]
getGlobalReflog numEntries = do
entries <- Q.getGlobalReflog numEntries
traverse hydrateProjectReflogEntry entries

hydrateProjectReflogEntry :: ProjectReflog.Entry Db.ProjectId Db.ProjectBranchId Db.CausalHashId -> Transaction (ProjectReflog.Entry Project ProjectBranch CausalHash)
hydrateProjectReflogEntry entry = do
traverse Q.expectCausalHash entry
>>= ProjectReflog.projectAndBranch_
%%~ ( \(projId, branchId) -> do
proj <- Q.expectProject projId
branch <- Q.expectProjectBranch projId branchId
pure (proj, branch)
)

appendProjectReflog :: ProjectReflog.Entry Db.ProjectId Db.ProjectBranchId CausalHash -> Transaction ()
appendProjectReflog entry = do
dbEntry <- traverse Q.saveCausalHash entry
Q.appendProjectBranchReflog dbEntry

-- | Delete any name lookup that's not in the provided list.
--
Expand Down Expand Up @@ -1584,3 +1589,14 @@ stripPrefixFromNamedRef (PathSegments prefix) namedRef =
Nothing -> reversedName
Just strippedReversedPath -> S.ReversedName (name NonEmpty.:| strippedReversedPath)
in namedRef {S.reversedSegments = newReversedName}

expectProjectAndBranchNames :: Db.ProjectId -> Db.ProjectBranchId -> Transaction (ProjectName, ProjectBranchName)
expectProjectAndBranchNames projectId projectBranchId = do
Project {name = pName} <- Q.expectProject projectId
ProjectBranch {name = bName} <- Q.expectProjectBranch projectId projectBranchId
pure (pName, bName)

expectProjectBranchHead :: Db.ProjectId -> Db.ProjectBranchId -> Transaction CausalHash
expectProjectBranchHead projId projectBranchId = do
chId <- Q.expectProjectBranchHead projId projectBranchId
Q.expectCausalHash chId
2 changes: 1 addition & 1 deletion codebase2/codebase-sqlite/U/Codebase/Sqlite/Project.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ data Project = Project
{ projectId :: !ProjectId,
name :: !ProjectName
}
deriving stock (Generic, Show)
deriving stock (Generic, Show, Eq)
deriving anyclass (ToRow, FromRow)
50 changes: 50 additions & 0 deletions codebase2/codebase-sqlite/U/Codebase/Sqlite/ProjectReflog.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ViewPatterns #-}

module U.Codebase.Sqlite.ProjectReflog
( Entry (..),
project_,
branch_,
projectAndBranch_,
)
where

import Control.Lens
import Data.Text (Text)
import Data.Time (UTCTime)
import U.Codebase.Sqlite.DbId (CausalHashId, ProjectBranchId, ProjectId)
import Unison.Sqlite (FromRow (..), ToRow (..), field)

data Entry project branch causal = Entry
{ project :: project,
branch :: branch,
time :: UTCTime,
fromRootCausalHash :: Maybe causal,
toRootCausalHash :: causal,
reason :: Text
}
deriving stock (Eq, Show, Functor, Foldable, Traversable)

project_ :: Lens (Entry project branch causal) (Entry project' branch causal) project project'
project_ = lens project (\e p -> e {project = p})

branch_ :: Lens (Entry project branch causal) (Entry project branch' causal) branch branch'
branch_ = lens branch (\e b -> e {branch = b})

-- | Both Project and Branch Ids are required to load a branch, so this is often more useful.
projectAndBranch_ :: Lens (Entry project branch causal) (Entry project' branch' causal) (project, branch) (project', branch')
projectAndBranch_ = lens (\Entry {..} -> (project, branch)) (\e (project, branch) -> e {project = project, branch = branch})

instance ToRow (Entry ProjectId ProjectBranchId CausalHashId) where
toRow (Entry proj branch time fromRootCausalHash toRootCausalHash reason) =
toRow (proj, branch, time, fromRootCausalHash, toRootCausalHash, reason)

instance FromRow (Entry ProjectId ProjectBranchId CausalHashId) where
fromRow = do
project <- field
branch <- field
time <- field
fromRootCausalHash <- field
toRootCausalHash <- field
reason <- field
pure $ Entry {..}
Loading
Loading