Skip to content

Commit e376745

Browse files
committed
Use last-known remote branch instead of causal negotiation
1 parent 590505a commit e376745

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

unison-cli/src/Unison/Cli/DownloadUtils.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ where
1111
import Control.Concurrent.STM (atomically)
1212
import Control.Concurrent.STM.TVar (modifyTVar', newTVarIO, readTVar, readTVarIO)
1313
import Data.List.NonEmpty (pattern (:|))
14+
import Data.Set qualified as Set
1415
import System.Console.Regions qualified as Console.Regions
1516
import System.IO.Unsafe (unsafePerformIO)
1617
import U.Codebase.HashTags (CausalHash)
18+
import U.Codebase.Sqlite.Queries qualified as Q
1719
import U.Codebase.Sqlite.Queries qualified as Queries
20+
import U.Codebase.Sqlite.RemoteProjectBranch qualified as SqliteRPB
1821
import Unison.Cli.Monad (Cli)
1922
import Unison.Cli.Monad qualified as Cli
2023
import Unison.Cli.Share.Projects qualified as Share
@@ -77,9 +80,15 @@ downloadProjectBranchFromShare useSquashed branch =
7780
Cli.respond (Output.DownloadedEntities numDownloaded)
7881
SyncV2 -> do
7982
let branchRef = SyncV2.BranchRef (into @Text (ProjectAndBranch branch.projectName remoteProjectBranchName))
80-
let downloadedCallback = \_ -> pure ()
8183
let shouldValidate = not $ Codeserver.isCustomCodeserver Codeserver.defaultCodeserver
82-
result <- SyncV2.syncFromCodeserver shouldValidate Share.hardCodedBaseUrl branchRef causalHashJwt downloadedCallback
84+
knownRemoteHash <- fmap (fromMaybe Set.empty) . Cli.runTransaction $ runMaybeT do
85+
lastKnownCausalHashId <- SqliteRPB.lastKnownCausalHash <$> MaybeT (Q.loadRemoteBranch branch.projectId Share.hardCodedUri branch.branchId)
86+
lastKnownCausalHash <- lift $ Q.expectCausalHash lastKnownCausalHashId
87+
-- Check that we actually have this causal saved.
88+
lift (Q.checkBranchExistsForCausalHash lastKnownCausalHash) >>= \case
89+
True -> pure (Set.singleton lastKnownCausalHash)
90+
False -> pure mempty
91+
result <- SyncV2.syncFromCodeserver shouldValidate Share.hardCodedBaseUrl branchRef causalHashJwt knownRemoteHash
8392
result & onLeft \err0 -> do
8493
done case err0 of
8594
Share.SyncError pullErr ->

unison-cli/src/Unison/Share/SyncV2.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,20 @@ syncFromCodeserver ::
155155
SyncV2.BranchRef ->
156156
-- | The hash to download.
157157
Share.HashJWT ->
158-
-- | Callback that's given a number of entities we just downloaded.
159-
(Int -> IO ()) ->
158+
-- | Set of known hashes to avoid downloading.
159+
-- If provided we'll skip the negotiation stage.
160+
Set CausalHash ->
160161
Cli (Either (SyncError SyncV2.PullError) ())
161-
syncFromCodeserver shouldValidate unisonShareUrl branchRef hashJwt _downloadedCallback = do
162+
syncFromCodeserver shouldValidate unisonShareUrl branchRef hashJwt providedKnownHashes = do
162163
Cli.Env {authHTTPClient, codebase} <- ask
163164
-- Every insert into SQLite checks the temp entity tables, but syncv2 doesn't actually use them, so it's faster
164165
-- if we clear them out before starting a sync.
165166
Cli.runTransaction Q.clearTempEntityTables
166167
runExceptT do
167-
knownHashes <- ExceptT $ negotiateKnownCausals unisonShareUrl branchRef hashJwt
168+
knownHashes <-
169+
if Set.null providedKnownHashes
170+
then ExceptT $ negotiateKnownCausals unisonShareUrl branchRef hashJwt
171+
else pure (Set.map Sync.causalHashToHash32 providedKnownHashes)
168172
let hash = Share.hashJWTHash hashJwt
169173
ExceptT $ do
170174
(Cli.runTransaction (Q.entityLocation hash)) >>= \case

0 commit comments

Comments
 (0)