@myrialabs/clopen 0.4.47 → 0.4.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -11
- package/backend/artifacts/project/sources.ts +1 -1
- package/backend/auth/auth-service.ts +52 -358
- package/backend/auth/onboarding.test.ts +2 -3
- package/backend/auth/permissions.test.ts +10 -20
- package/backend/auth/permissions.ts +4 -290
- package/backend/auth/rate-limiter.test.ts +8 -9
- package/backend/auth/rate-limiter.ts +0 -2
- package/backend/auth/tokens.ts +1 -8
- package/backend/bootstrap.ts +7 -0
- package/backend/chat/attachments.test.ts +88 -0
- package/backend/chat/attachments.ts +159 -0
- package/backend/chat/engine-handoff.test.ts +0 -2
- package/backend/chat/message-pages.test.ts +163 -0
- package/backend/chat/message-pages.ts +233 -0
- package/backend/chat/stream-manager.ts +91 -67
- package/backend/chat/turn-answer.test.ts +64 -0
- package/backend/chat/turn-answer.ts +125 -0
- package/backend/chat-apps/awaiting.test.ts +39 -0
- package/backend/chat-apps/awaiting.ts +49 -0
- package/backend/chat-apps/conversation.ts +746 -0
- package/backend/chat-apps/inbound.ts +324 -0
- package/backend/chat-apps/index.ts +309 -0
- package/backend/chat-apps/link-events.ts +20 -0
- package/backend/chat-apps/media.test.ts +62 -0
- package/backend/chat-apps/media.ts +208 -0
- package/backend/chat-apps/messages.test.ts +229 -0
- package/backend/chat-apps/messages.ts +365 -0
- package/backend/chat-apps/outbound.ts +243 -0
- package/backend/chat-apps/pairing.test.ts +45 -0
- package/backend/chat-apps/pairing.ts +74 -0
- package/backend/chat-apps/platforms/index.ts +20 -0
- package/backend/chat-apps/platforms/telegram.test.ts +35 -0
- package/backend/chat-apps/platforms/telegram.ts +191 -0
- package/backend/chat-apps/platforms/types.ts +114 -0
- package/backend/chat-apps/questions.ts +133 -0
- package/backend/chat-apps/runtime.ts +572 -0
- package/backend/chat-apps/storage.test.ts +201 -0
- package/backend/chat-apps/turns.test.ts +45 -0
- package/backend/chat-apps/turns.ts +208 -0
- package/backend/containers/pty.ts +3 -11
- package/backend/control/actions/accounts.ts +89 -0
- package/backend/control/actions/files.ts +116 -0
- package/backend/control/actions/models.ts +188 -0
- package/backend/control/actions/projects.ts +272 -0
- package/backend/control/actions/sessions.ts +280 -0
- package/backend/control/actions/skills.ts +111 -0
- package/backend/control/control.test.ts +169 -0
- package/backend/control/define.ts +20 -0
- package/backend/control/folders.test.ts +67 -0
- package/backend/control/index.ts +213 -0
- package/backend/control/invoke.ts +44 -0
- package/backend/control/links.ts +35 -0
- package/backend/control/paging.ts +26 -0
- package/backend/control/types.ts +117 -0
- package/backend/control/workspace.ts +127 -0
- package/backend/database/migrations/084_create_channel_links.ts +71 -0
- package/backend/database/migrations/085_add_home_projects.ts +36 -0
- package/backend/database/migrations/086_single_owner.ts +418 -0
- package/backend/database/migrations/087_create_blob_line_stats.ts +37 -0
- package/backend/database/migrations/index.ts +28 -0
- package/backend/database/queries/auth-queries.ts +7 -85
- package/backend/database/queries/blob-line-stats-queries.ts +48 -0
- package/backend/database/queries/channel-queries.ts +270 -0
- package/backend/database/queries/db-client-connection-queries.ts +18 -56
- package/backend/database/queries/file-share-queries.ts +5 -23
- package/backend/database/queries/git-identity-queries.ts +31 -42
- package/backend/database/queries/graph-queries.ts +1 -1
- package/backend/database/queries/index.ts +15 -1
- package/backend/database/queries/message-queries.test.ts +146 -0
- package/backend/database/queries/message-queries.ts +42 -1
- package/backend/database/queries/permission-set-queries.ts +1 -1
- package/backend/database/queries/profile-queries.ts +1 -1
- package/backend/database/queries/project-mcp-trust-queries.ts +1 -1
- package/backend/database/queries/project-queries.ts +50 -63
- package/backend/database/queries/session-queries.ts +28 -56
- package/backend/database/queries/ssh-connection-queries.ts +18 -41
- package/backend/db-client/env/diff.ts +9 -33
- package/backend/db-client/env/env.test.ts +8 -8
- package/backend/db-client/env/service.ts +1 -1
- package/backend/db-client/integrations/projector.ts +0 -5
- package/backend/engine/adapters/claude/message-converter.test.ts +47 -0
- package/backend/engine/adapters/claude/message-converter.ts +25 -2
- package/backend/engine/adapters/cline/message-converter.ts +0 -1
- package/backend/engine/adapters/cline/stream.ts +11 -15
- package/backend/engine/adapters/codex/message-converter.ts +0 -2
- package/backend/engine/adapters/copilot/message-converter.ts +1 -2
- package/backend/engine/adapters/cursor/message-converter.ts +0 -1
- package/backend/engine/adapters/opencode/message-converter.ts +2 -4
- package/backend/engine/adapters/opencode/stream.ts +11 -0
- package/backend/engine/adapters/parent-scope.test.ts +38 -0
- package/backend/engine/adapters/parent-scope.ts +44 -0
- package/backend/engine/adapters/pi/message-converter.ts +0 -1
- package/backend/engine/adapters/pi/stream.ts +4 -5
- package/backend/engine/adapters/qwen/message-converter.ts +0 -2
- package/backend/engine/bootstrap-default-engine.ts +2 -2
- package/backend/engine/docs/adapter-contract.md +8 -1
- package/backend/engine/install-runner.ts +8 -17
- package/backend/files/file-shares.ts +20 -44
- package/backend/files/file-size-limit.ts +3 -3
- package/backend/files/file-watcher.ts +19 -0
- package/backend/files/image-processing.ts +1 -1
- package/backend/files/os-clipboard.ts +2 -2
- package/backend/git/contributors.test.ts +35 -0
- package/backend/git/contributors.ts +46 -0
- package/backend/git/git-executor.ts +25 -5
- package/backend/git/git-service.ts +420 -19
- package/backend/git/identity/credential-helper.test.ts +2 -7
- package/backend/git/identity/import-local.test.ts +11 -16
- package/backend/git/identity/import-local.ts +12 -12
- package/backend/git/identity/index.ts +10 -24
- package/backend/git/identity/resolve.ts +13 -14
- package/backend/git/identity/scope-config.ts +33 -28
- package/backend/git/identity/service.ts +44 -47
- package/backend/git/op-lock.test.ts +111 -0
- package/backend/git/op-lock.ts +180 -0
- package/backend/git/remote-sync.test.ts +249 -0
- package/backend/host/metrics.ts +1 -1
- package/backend/http/bearer-auth.ts +1 -2
- package/backend/http/files-download.test.ts +8 -9
- package/backend/http/files-download.ts +2 -2
- package/backend/http/files-shared.test.ts +62 -89
- package/backend/http/files-shared.ts +2 -2
- package/backend/http/files-upload.test.ts +3 -7
- package/backend/http/files-upload.ts +3 -3
- package/backend/http/notes-images.test.ts +15 -38
- package/backend/http/notes-images.ts +6 -8
- package/backend/http/ssh-sftp.ts +5 -10
- package/backend/http/static-assets.test.ts +78 -0
- package/backend/http/static-assets.ts +104 -0
- package/backend/index.ts +32 -16
- package/backend/integrations/accounts.ts +61 -3
- package/backend/integrations/index.ts +2 -2
- package/backend/integrations/registry/define.ts +15 -0
- package/backend/integrations/registry/index.ts +2 -0
- package/backend/integrations/registry/providers/telegram.ts +54 -0
- package/backend/integrations/registry/types.ts +21 -1
- package/backend/integrations/webhooks/gateway.ts +7 -6
- package/backend/integrations/webhooks/verify.test.ts +30 -0
- package/backend/integrations/webhooks/verify.ts +4 -0
- package/backend/mcp/external/project.ts +1 -1
- package/backend/mcp/internal/remote-server.test.ts +1 -3
- package/backend/mcp/internal/remote-server.ts +4 -4
- package/backend/mcp/internal/servers/browser-automation/actions/dialogs.ts +67 -0
- package/backend/mcp/internal/servers/browser-automation/actions/index.ts +3 -1
- package/backend/mcp/internal/servers/browser-automation/actions/input.ts +5 -5
- package/backend/mcp/internal/servers/browser-automation/actions/tabs.ts +157 -28
- package/backend/mcp/internal/servers/browser-automation/context.ts +1 -0
- package/backend/mcp/internal/servers/browser-automation/description.ts +5 -2
- package/backend/mcp/internal/servers/browser-automation/format.ts +24 -1
- package/backend/mcp/internal/servers/browser-automation/runner.ts +68 -3
- package/backend/memory/redact.ts +1 -1
- package/backend/notes/access.test.ts +28 -29
- package/backend/notes/access.ts +6 -11
- package/backend/permissions/service.ts +1 -1
- package/backend/preview/browser/browser-clipboard.test.ts +46 -0
- package/backend/preview/browser/browser-clipboard.ts +67 -0
- package/backend/preview/browser/browser-dialog-handler.ts +95 -6
- package/backend/preview/browser/browser-host-bridge.ts +9 -0
- package/backend/preview/browser/browser-interaction-handler.ts +316 -86
- package/backend/preview/browser/browser-mcp-control.ts +34 -0
- package/backend/preview/browser/browser-pool.ts +67 -6
- package/backend/preview/browser/browser-preview-service.ts +383 -29
- package/backend/preview/browser/browser-tab-manager.ts +66 -23
- package/backend/preview/browser/browser-video-capture.ts +92 -5
- package/backend/preview/browser/element-locator.ts +190 -17
- package/backend/preview/browser/keyboard-chords.test.ts +68 -0
- package/backend/preview/browser/keyboard-chords.ts +132 -0
- package/backend/preview/browser/paste.ts +81 -0
- package/backend/preview/browser/process-usage.ts +142 -0
- package/backend/preview/browser/profile-lock.ts +209 -0
- package/backend/preview/browser/scripts/host-bridge.ts +59 -0
- package/backend/preview/browser/scripts/video-stream.ts +58 -0
- package/backend/preview/browser/types.ts +40 -2
- package/backend/preview/browser/viewport.test.ts +45 -0
- package/backend/preview/browser/viewport.ts +96 -0
- package/backend/profiles/service.ts +2 -2
- package/backend/project/status-manager.ts +43 -172
- package/backend/projects/home.ts +61 -0
- package/backend/projects/project-cleanup.integration.test.ts +2 -2
- package/backend/projects/project-cleanup.ts +0 -8
- package/backend/skills/service.ts +2 -3
- package/backend/snapshot/blob-store.ts +9 -1
- package/backend/snapshot/helpers.ts +3 -11
- package/backend/snapshot/line-stats.test.ts +80 -0
- package/backend/snapshot/line-stats.ts +64 -0
- package/backend/snapshot/restore.ts +252 -0
- package/backend/snapshot/snapshot-service.ts +14 -27
- package/backend/snapshot/turn-changes.test.ts +10 -5
- package/backend/snapshot/turn-changes.ts +78 -40
- package/backend/ssh/ptykit.ts +1 -2
- package/backend/terminal/ptykit.ts +10 -8
- package/backend/tunnel/public-origin.ts +31 -20
- package/backend/utils/user-helpers.ts +0 -55
- package/backend/utils/ws-wire.test.ts +87 -0
- package/backend/utils/ws-wire.ts +189 -0
- package/backend/utils/ws.ts +101 -192
- package/backend/worktrees/branching/engine.ts +3 -16
- package/backend/worktrees/branching/index.ts +1 -1
- package/backend/worktrees/branching/projector.ts +1 -1
- package/backend/worktrees/branching/service.ts +13 -25
- package/backend/ws/access.ts +12 -21
- package/backend/ws/artifacts/generate.ts +2 -2
- package/backend/ws/auth/device.ts +16 -71
- package/backend/ws/auth/index.ts +4 -22
- package/backend/ws/auth/login.test.ts +14 -60
- package/backend/ws/auth/login.ts +24 -124
- package/backend/ws/auth/status.ts +0 -1
- package/backend/ws/chat/composer.ts +118 -118
- package/backend/ws/chat/stream.ts +105 -57
- package/backend/ws/chat-apps/index.ts +117 -0
- package/backend/ws/containers/actions.ts +11 -25
- package/backend/ws/containers/index.ts +3 -4
- package/backend/ws/containers/list.ts +10 -36
- package/backend/ws/containers/logs.ts +2 -2
- package/backend/ws/containers/tunnel.ts +1 -2
- package/backend/ws/db-client/access.ts +2 -16
- package/backend/ws/db-client/accounts.ts +5 -15
- package/backend/ws/db-client/connections.ts +17 -25
- package/backend/ws/db-client/context.ts +1 -1
- package/backend/ws/db-client/env.ts +7 -10
- package/backend/ws/db-client/index.ts +2 -2
- package/backend/ws/db-client/io.ts +5 -5
- package/backend/ws/db-client/query-history-retention.test.ts +8 -8
- package/backend/ws/db-client/query.ts +18 -17
- package/backend/ws/db-client/schema.ts +13 -13
- package/backend/ws/db-client/structure.ts +35 -35
- package/backend/ws/db-client/supabase.ts +4 -21
- package/backend/ws/deployments/context.ts +1 -1
- package/backend/ws/deployments/crud.ts +0 -5
- package/backend/ws/engine/access.ts +7 -20
- package/backend/ws/engine/claude/accounts.ts +4 -4
- package/backend/ws/engine/cline/accounts.ts +5 -5
- package/backend/ws/engine/codex/accounts.ts +3 -3
- package/backend/ws/engine/pi/accounts.ts +5 -5
- package/backend/ws/files/clipboard.ts +8 -27
- package/backend/ws/files/image-edit.ts +6 -6
- package/backend/ws/files/index.ts +1 -1
- package/backend/ws/files/path-access.test.ts +32 -131
- package/backend/ws/files/path-access.ts +8 -87
- package/backend/ws/files/read.ts +45 -19
- package/backend/ws/files/reveal.ts +3 -3
- package/backend/ws/files/search.ts +7 -7
- package/backend/ws/files/share.ts +9 -13
- package/backend/ws/files/state.ts +9 -12
- package/backend/ws/files/watch.ts +3 -3
- package/backend/ws/files/write.ts +30 -31
- package/backend/ws/git/branch.ts +80 -59
- package/backend/ws/git/commit.ts +63 -44
- package/backend/ws/git/conflict.ts +16 -18
- package/backend/ws/git/identity-context.ts +8 -17
- package/backend/ws/git/identity.ts +17 -27
- package/backend/ws/git/index.ts +3 -1
- package/backend/ws/git/log.ts +52 -0
- package/backend/ws/git/op-state.ts +139 -0
- package/backend/ws/git/remote.ts +241 -141
- package/backend/ws/git/repo-context.ts +67 -0
- package/backend/ws/git/staging.ts +18 -28
- package/backend/ws/git/status.ts +5 -2
- package/backend/ws/index.ts +5 -1
- package/backend/ws/instructions/crud.ts +1 -1
- package/backend/ws/integrations/crud.ts +1 -3
- package/backend/ws/mcp/crud.ts +3 -5
- package/backend/ws/mcp/tools.ts +1 -1
- package/backend/ws/memory/crud.ts +14 -46
- package/backend/ws/memory/index.ts +2 -2
- package/backend/ws/messages/crud.ts +78 -8
- package/backend/ws/notes/index.ts +33 -39
- package/backend/ws/permissions/crud.ts +3 -4
- package/backend/ws/ports/index.ts +1 -2
- package/backend/ws/ports/kill.ts +5 -10
- package/backend/ws/ports/list.ts +4 -10
- package/backend/ws/preview/access.ts +1 -1
- package/backend/ws/preview/browser/interact.ts +52 -79
- package/backend/ws/preview/browser/native-ui.ts +24 -0
- package/backend/ws/preview/browser/stats.ts +98 -1
- package/backend/ws/preview/browser/tab-info.ts +15 -0
- package/backend/ws/preview/browser/tab.ts +19 -4
- package/backend/ws/preview/index.ts +29 -0
- package/backend/ws/preview/watchers.ts +84 -0
- package/backend/ws/profiles/crud.ts +6 -8
- package/backend/ws/project-artifacts/index.ts +30 -31
- package/backend/ws/projects/crud.ts +33 -35
- package/backend/ws/projects/index.ts +3 -8
- package/backend/ws/projects/info.ts +3 -3
- package/backend/ws/projects/overview.ts +19 -30
- package/backend/ws/projects/status.ts +17 -57
- package/backend/ws/sessions/crud.ts +42 -57
- package/backend/ws/sessions/index.ts +1 -1
- package/backend/ws/settings/crud.ts +1 -1
- package/backend/ws/skills/crud.ts +3 -4
- package/backend/ws/skills/index.ts +1 -1
- package/backend/ws/snapshot/changes.ts +12 -13
- package/backend/ws/snapshot/restore.ts +11 -295
- package/backend/ws/snapshot/timeline.ts +2 -4
- package/backend/ws/ssh/access.ts +6 -21
- package/backend/ws/ssh/connections.ts +21 -31
- package/backend/ws/ssh/forwards.ts +12 -12
- package/backend/ws/ssh/sftp.ts +34 -34
- package/backend/ws/ssh/tunnel.ts +1 -2
- package/backend/ws/stack/install.ts +7 -17
- package/backend/ws/subagents/crud.ts +1 -1
- package/backend/ws/terminal/tunnel.ts +2 -2
- package/backend/ws/tunnel/share.ts +0 -2
- package/backend/ws/user/crud.ts +1 -59
- package/backend/ws/user/index.ts +1 -1
- package/backend/ws/work/context.ts +1 -1
- package/backend/ws/work/crud.ts +0 -5
- package/backend/ws/workspace/state.ts +10 -13
- package/backend/ws/worktrees/branching.ts +7 -36
- package/backend/ws/worktrees/crud.ts +2 -2
- package/bin/clopen.ts +46 -41
- package/dist/assets/addon-clipboard-wHJhZAA4.js.br +0 -0
- package/dist/assets/addon-clipboard-wHJhZAA4.js.gz +0 -0
- package/dist/assets/addon-fit-DX4qG4td.js.br +1 -0
- package/dist/assets/addon-fit-DX4qG4td.js.gz +0 -0
- package/dist/assets/addon-ligatures-DpDY8N_X.js.br +0 -0
- package/dist/assets/addon-ligatures-DpDY8N_X.js.gz +0 -0
- package/dist/assets/addon-unicode11-Bt8F3D7-.js.br +0 -0
- package/dist/assets/addon-unicode11-Bt8F3D7-.js.gz +0 -0
- package/dist/assets/addon-web-links-DIbG5aQx.js.br +0 -0
- package/dist/assets/addon-web-links-DIbG5aQx.js.gz +0 -0
- package/dist/assets/diff.worker-BoHLxmr2.js +7 -0
- package/dist/assets/diff.worker-BoHLxmr2.js.br +0 -0
- package/dist/assets/diff.worker-BoHLxmr2.js.gz +0 -0
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.br +0 -0
- package/dist/assets/graph-layout.worker-CRFqN7ZQ.js.gz +0 -0
- package/dist/assets/{index-CwuKQelV.js → index-CrVWZH_D.js} +1 -1
- package/dist/assets/index-CrVWZH_D.js.br +0 -0
- package/dist/assets/index-CrVWZH_D.js.gz +0 -0
- package/dist/assets/{index-BTcR1byF.js → index-DD0-3EWg.js} +1 -1
- package/dist/assets/index-DD0-3EWg.js.br +0 -0
- package/dist/assets/index-DD0-3EWg.js.gz +0 -0
- package/dist/assets/main-D2_TmcwQ.js +1240 -0
- package/dist/assets/main-D2_TmcwQ.js.br +0 -0
- package/dist/assets/main-D2_TmcwQ.js.gz +0 -0
- package/dist/assets/main-Yucigcmf.css +1 -0
- package/dist/assets/main-Yucigcmf.css.br +0 -0
- package/dist/assets/main-Yucigcmf.css.gz +0 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js.br +0 -0
- package/dist/assets/preview-video.worker-DcpDhCWL.js.gz +0 -0
- package/dist/assets/xterm-B-qIQCd3.js.br +0 -0
- package/dist/assets/xterm-B-qIQCd3.js.gz +0 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.br +0 -0
- package/dist/assets/zipkit-engine-CaR0t_Uy.wasm.gz +0 -0
- package/dist/fonts/dm-sans.css.br +0 -0
- package/dist/fonts/dm-sans.css.gz +0 -0
- package/dist/index.html +2 -2
- package/dist/index.html.br +0 -0
- package/dist/index.html.gz +0 -0
- package/dist/sw.js.br +0 -0
- package/dist/sw.js.gz +0 -0
- package/package.json +3 -3
- package/shared/constants/attachments.ts +14 -0
- package/shared/constants/engine-tools.ts +1 -1
- package/shared/constants/preview.test.ts +259 -0
- package/shared/constants/preview.ts +1018 -57
- package/shared/constants/tool-icons.ts +5 -0
- package/shared/types/containers/container.ts +1 -1
- package/shared/types/database/schema.ts +9 -6
- package/shared/types/git-identity.ts +1 -1
- package/shared/types/git.ts +116 -0
- package/shared/types/integrations.ts +7 -0
- package/shared/types/stores/settings.ts +1 -1
- package/shared/types/unified/index.ts +1 -1
- package/shared/types/unified/message.ts +0 -7
- package/shared/types/unified/stream.ts +13 -5
- package/shared/types/unified/tool.ts +15 -1
- package/shared/utils/ask-user-question.test.ts +319 -0
- package/shared/utils/ask-user-question.ts +154 -0
- package/shared/utils/diff-calculator.test.ts +178 -41
- package/shared/utils/diff-calculator.ts +383 -100
- package/shared/utils/home-project.ts +25 -0
- package/shared/utils/logger.ts +2 -0
- package/shared/utils/process-supervisor.test.ts +172 -0
- package/shared/utils/process-supervisor.ts +251 -0
- package/shared/utils/ws-chunk.test.ts +33 -0
- package/shared/utils/ws-chunk.ts +73 -0
- package/shared/utils/ws-client-chunks.test.ts +45 -0
- package/shared/utils/ws-client.ts +43 -0
- package/shared/utils/ws-server.ts +49 -21
- package/backend/auth/session-invalidation.test.ts +0 -112
- package/backend/auth/session-invalidation.ts +0 -77
- package/backend/project/status-manager.test.ts +0 -32
- package/backend/ws/auth/invites.ts +0 -94
- package/backend/ws/auth/users.ts +0 -112
- package/backend/ws/projects/presence.ts +0 -46
- package/backend/ws/stack/access.ts +0 -31
- package/dist/assets/main-BiDNMWb3.css +0 -1
- package/dist/assets/main-BlhKWz2B.js +0 -1241
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test';
|
|
2
|
+
import { GitBusyError, getScopeOpStates, hasProjectOps, onGitOpChange, withGitOpLock } from './op-lock';
|
|
3
|
+
import type { GitRepoOpState } from '$shared/types/git';
|
|
4
|
+
|
|
5
|
+
let counter = 0;
|
|
6
|
+
/** A fresh repository key per test, so tests never share a lock. */
|
|
7
|
+
function key(scope = 'project-a') {
|
|
8
|
+
return { cwd: `/tmp/op-lock-test-${++counter}`, scope };
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function deferred() {
|
|
12
|
+
let resolve!: () => void;
|
|
13
|
+
const promise = new Promise<void>(r => (resolve = r));
|
|
14
|
+
return { promise, resolve };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('withGitOpLock', () => {
|
|
18
|
+
test('refuses an exclusive operation while another runs, naming it', async () => {
|
|
19
|
+
const repo = key();
|
|
20
|
+
const gate = deferred();
|
|
21
|
+
const pull = withGitOpLock(repo, { category: 'pull', label: 'Pull', target: 'main' }, () => gate.promise);
|
|
22
|
+
|
|
23
|
+
const push = withGitOpLock(repo, { category: 'push', label: 'Push' }, async () => 'pushed');
|
|
24
|
+
await expect(push).rejects.toBeInstanceOf(GitBusyError);
|
|
25
|
+
await expect(push).rejects.toThrow(/Pull \(main\) is still running/);
|
|
26
|
+
|
|
27
|
+
gate.resolve();
|
|
28
|
+
await pull;
|
|
29
|
+
expect(await withGitOpLock(repo, { category: 'push', label: 'Push' }, async () => 'pushed')).toBe('pushed');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('lets staging run while a network operation is in flight', async () => {
|
|
33
|
+
const repo = key();
|
|
34
|
+
const gate = deferred();
|
|
35
|
+
const push = withGitOpLock(repo, { category: 'push', label: 'Push' }, () => gate.promise);
|
|
36
|
+
expect(await withGitOpLock(repo, { category: 'staging', label: 'Staging' }, async () => 'staged')).toBe('staged');
|
|
37
|
+
gate.resolve();
|
|
38
|
+
await push;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('refuses a second network operation', async () => {
|
|
42
|
+
const repo = key();
|
|
43
|
+
const gate = deferred();
|
|
44
|
+
const push = withGitOpLock(repo, { category: 'push', label: 'Push' }, () => gate.promise);
|
|
45
|
+
await expect(withGitOpLock(repo, { category: 'fetch', label: 'Fetch' }, async () => {})).rejects.toBeInstanceOf(
|
|
46
|
+
GitBusyError
|
|
47
|
+
);
|
|
48
|
+
gate.resolve();
|
|
49
|
+
await push;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('queues index writes behind each other instead of refusing', async () => {
|
|
53
|
+
const repo = key();
|
|
54
|
+
const order: string[] = [];
|
|
55
|
+
const gate = deferred();
|
|
56
|
+
const first = withGitOpLock(repo, { category: 'staging', label: 'Staging', queue: true }, async () => {
|
|
57
|
+
await gate.promise;
|
|
58
|
+
order.push('first');
|
|
59
|
+
});
|
|
60
|
+
const second = withGitOpLock(repo, { category: 'staging', label: 'Staging', queue: true }, async () => {
|
|
61
|
+
order.push('second');
|
|
62
|
+
});
|
|
63
|
+
gate.resolve();
|
|
64
|
+
await Promise.all([first, second]);
|
|
65
|
+
expect(order).toEqual(['first', 'second']);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('a queued index write still refuses an exclusive holder', async () => {
|
|
69
|
+
const repo = key();
|
|
70
|
+
const gate = deferred();
|
|
71
|
+
const commit = withGitOpLock(repo, { category: 'commit', label: 'Commit' }, () => gate.promise);
|
|
72
|
+
await expect(
|
|
73
|
+
withGitOpLock(repo, { category: 'staging', label: 'Staging', queue: true }, async () => {})
|
|
74
|
+
).rejects.toBeInstanceOf(GitBusyError);
|
|
75
|
+
gate.resolve();
|
|
76
|
+
await commit;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('releases the lock when the operation throws', async () => {
|
|
80
|
+
const repo = key();
|
|
81
|
+
await expect(
|
|
82
|
+
withGitOpLock(repo, { category: 'pull', label: 'Pull' }, async () => {
|
|
83
|
+
throw new Error('boom');
|
|
84
|
+
})
|
|
85
|
+
).rejects.toThrow('boom');
|
|
86
|
+
expect(await withGitOpLock(repo, { category: 'pull', label: 'Pull' }, async () => 'again')).toBe('again');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('reports state to listeners and snapshots, per scope and project', async () => {
|
|
90
|
+
const repo = { ...key('project-b~wt1'), repoPath: '/nested' };
|
|
91
|
+
const seen: GitRepoOpState[] = [];
|
|
92
|
+
const stop = onGitOpChange(state => {
|
|
93
|
+
if (state.scope === 'project-b~wt1') seen.push(state);
|
|
94
|
+
});
|
|
95
|
+
const gate = deferred();
|
|
96
|
+
const running = withGitOpLock(repo, { category: 'fetch', label: 'Fetch', target: 'origin' }, () => gate.promise);
|
|
97
|
+
|
|
98
|
+
expect(getScopeOpStates('project-b~wt1')).toHaveLength(1);
|
|
99
|
+
expect(getScopeOpStates('project-b~wt1')[0].repoPath).toBe('/nested');
|
|
100
|
+
expect(getScopeOpStates('project-b')).toHaveLength(0);
|
|
101
|
+
expect(hasProjectOps('project-b')).toBe(true);
|
|
102
|
+
|
|
103
|
+
gate.resolve();
|
|
104
|
+
await running;
|
|
105
|
+
stop();
|
|
106
|
+
|
|
107
|
+
expect(seen.map(s => s.ops.length)).toEqual([1, 0]);
|
|
108
|
+
expect(seen[0].ops[0]).toMatchObject({ category: 'fetch', cls: 'network', target: 'origin' });
|
|
109
|
+
expect(hasProjectOps('project-b')).toBe(false);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-repository git operation lock.
|
|
3
|
+
*
|
|
4
|
+
* The panel used to guard against overlapping operations with flags that lived
|
|
5
|
+
* in each browser tab, and only the flag of the button being clicked was
|
|
6
|
+
* checked. A pull from the Branches tab could start while a push from the
|
|
7
|
+
* Changes tab was still running, and a second device saw no spinner at all —
|
|
8
|
+
* so two git processes raced for `index.lock`, or one moved HEAD under the
|
|
9
|
+
* other.
|
|
10
|
+
*
|
|
11
|
+
* The lock lives here instead, where every client's request passes through.
|
|
12
|
+
* Each mutating handler declares what it is doing; a request whose class
|
|
13
|
+
* conflicts with a running operation is refused with the reason, and every
|
|
14
|
+
* change is reported to listeners so the clients render one shared answer.
|
|
15
|
+
*
|
|
16
|
+
* In-memory on purpose: the git processes it describes die with the server,
|
|
17
|
+
* so a lock that survived a restart would describe nothing.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { scopeProjectId } from '$shared/utils/workspace-scope';
|
|
21
|
+
import type { GitActiveOp, GitOpCategory, GitOpClass, GitRepoOpState } from '$shared/types/git';
|
|
22
|
+
|
|
23
|
+
const DEFAULT_CLASS: Record<GitOpCategory, GitOpClass> = {
|
|
24
|
+
commit: 'exclusive',
|
|
25
|
+
staging: 'index',
|
|
26
|
+
push: 'network',
|
|
27
|
+
pull: 'exclusive',
|
|
28
|
+
fetch: 'network',
|
|
29
|
+
more: 'exclusive',
|
|
30
|
+
stash: 'exclusive',
|
|
31
|
+
tag: 'network',
|
|
32
|
+
branch: 'exclusive',
|
|
33
|
+
resolve: 'exclusive',
|
|
34
|
+
configure: 'exclusive'
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export interface GitOpSpec {
|
|
38
|
+
category: GitOpCategory;
|
|
39
|
+
label: string;
|
|
40
|
+
target?: string;
|
|
41
|
+
/** Override the category's class, e.g. deleting a remote branch is network work. */
|
|
42
|
+
cls?: GitOpClass;
|
|
43
|
+
/**
|
|
44
|
+
* Wait behind running operations of the same category instead of refusing.
|
|
45
|
+
* For per-file index writes: staging three files in quick succession is
|
|
46
|
+
* three requests the user meant, and git itself would refuse the overlap
|
|
47
|
+
* with an `index.lock` error. Anything else still refuses immediately.
|
|
48
|
+
*/
|
|
49
|
+
queue?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface GitOpKey {
|
|
53
|
+
/** Absolute working directory of the repository — the identity of the lock. */
|
|
54
|
+
cwd: string;
|
|
55
|
+
/** Workspace scope key, used to route the state to the right clients. */
|
|
56
|
+
scope: string;
|
|
57
|
+
/** Absolute nested-repo path as the client addresses it; absent for the root. */
|
|
58
|
+
repoPath?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Thrown when an operation is refused because a conflicting one is running. */
|
|
62
|
+
export class GitBusyError extends Error {
|
|
63
|
+
constructor(readonly blocking: GitActiveOp) {
|
|
64
|
+
super(
|
|
65
|
+
`Git is busy: ${blocking.label}${blocking.target ? ` (${blocking.target})` : ''} is still running in this repository. Try again when it finishes.`
|
|
66
|
+
);
|
|
67
|
+
this.name = 'GitBusyError';
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface RepoEntry {
|
|
72
|
+
scope: string;
|
|
73
|
+
repoPath?: string;
|
|
74
|
+
ops: GitActiveOp[];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const repos = new Map<string, RepoEntry>();
|
|
78
|
+
const waiters = new Map<string, Array<() => void>>();
|
|
79
|
+
const listeners = new Set<(state: GitRepoOpState) => void>();
|
|
80
|
+
let nextId = 0;
|
|
81
|
+
|
|
82
|
+
function classesConflict(a: GitOpClass, b: GitOpClass): boolean {
|
|
83
|
+
return a === 'exclusive' || b === 'exclusive' || a === b;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function snapshot(entry: RepoEntry): GitRepoOpState {
|
|
87
|
+
return {
|
|
88
|
+
scope: entry.scope,
|
|
89
|
+
...(entry.repoPath && { repoPath: entry.repoPath }),
|
|
90
|
+
ops: entry.ops.map(op => ({ ...op }))
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function notify(entry: RepoEntry): void {
|
|
95
|
+
const state = snapshot(entry);
|
|
96
|
+
for (const listener of listeners) {
|
|
97
|
+
try {
|
|
98
|
+
listener(state);
|
|
99
|
+
} catch {
|
|
100
|
+
// A broken listener must never wedge the lock it is observing.
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Run `fn` while holding an operation slot on the repository at `key.cwd`.
|
|
107
|
+
* Throws `GitBusyError` without running anything when a conflicting operation
|
|
108
|
+
* already holds the repository.
|
|
109
|
+
*/
|
|
110
|
+
export async function withGitOpLock<T>(key: GitOpKey, spec: GitOpSpec, fn: () => Promise<T>): Promise<T> {
|
|
111
|
+
const cls = spec.cls ?? DEFAULT_CLASS[spec.category];
|
|
112
|
+
let entry = repos.get(key.cwd);
|
|
113
|
+
for (;;) {
|
|
114
|
+
const blockers = entry?.ops.filter(op => classesConflict(op.cls, cls)) ?? [];
|
|
115
|
+
if (blockers.length === 0) break;
|
|
116
|
+
// Queue only behind operations of the same kind: three files resolved in a
|
|
117
|
+
// row wait for each other, but nothing waits behind a pull.
|
|
118
|
+
if (!spec.queue || blockers.some(op => op.category !== spec.category)) throw new GitBusyError(blockers[0]);
|
|
119
|
+
await new Promise<void>(resolve => {
|
|
120
|
+
const list = waiters.get(key.cwd) ?? [];
|
|
121
|
+
list.push(resolve);
|
|
122
|
+
waiters.set(key.cwd, list);
|
|
123
|
+
});
|
|
124
|
+
entry = repos.get(key.cwd);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!entry) {
|
|
128
|
+
entry = { scope: key.scope, repoPath: key.repoPath, ops: [] };
|
|
129
|
+
repos.set(key.cwd, entry);
|
|
130
|
+
}
|
|
131
|
+
const op: GitActiveOp = {
|
|
132
|
+
id: `op-${++nextId}`,
|
|
133
|
+
category: spec.category,
|
|
134
|
+
cls,
|
|
135
|
+
label: spec.label,
|
|
136
|
+
...(spec.target && { target: spec.target }),
|
|
137
|
+
startedAt: Date.now()
|
|
138
|
+
};
|
|
139
|
+
entry.ops.push(op);
|
|
140
|
+
notify(entry);
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
return await fn();
|
|
144
|
+
} finally {
|
|
145
|
+
const current = repos.get(key.cwd);
|
|
146
|
+
if (current) {
|
|
147
|
+
current.ops = current.ops.filter(o => o.id !== op.id);
|
|
148
|
+
notify(current);
|
|
149
|
+
if (current.ops.length === 0) repos.delete(key.cwd);
|
|
150
|
+
}
|
|
151
|
+
const queued = waiters.get(key.cwd);
|
|
152
|
+
if (queued) {
|
|
153
|
+
waiters.delete(key.cwd);
|
|
154
|
+
for (const wake of queued) wake();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Every repository with a running operation in one workspace scope. */
|
|
160
|
+
export function getScopeOpStates(scope: string): GitRepoOpState[] {
|
|
161
|
+
const states: GitRepoOpState[] = [];
|
|
162
|
+
for (const entry of repos.values()) {
|
|
163
|
+
if (entry.scope === scope && entry.ops.length > 0) states.push(snapshot(entry));
|
|
164
|
+
}
|
|
165
|
+
return states;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Whether any workspace of `projectId` has a git operation running. */
|
|
169
|
+
export function hasProjectOps(projectId: string): boolean {
|
|
170
|
+
for (const entry of repos.values()) {
|
|
171
|
+
if (entry.ops.length > 0 && scopeProjectId(entry.scope) === projectId) return true;
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Subscribe to lock changes. Returns the unsubscribe function. */
|
|
177
|
+
export function onGitOpChange(listener: (state: GitRepoOpState) => void): () => void {
|
|
178
|
+
listeners.add(listener);
|
|
179
|
+
return () => listeners.delete(listener);
|
|
180
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test';
|
|
2
|
+
import { mkdtemp, rm, writeFile, readFile } from 'node:fs/promises';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import { tmpdir } from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { execGit } from './git-executor';
|
|
7
|
+
import { gitService } from './git-service';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Pull strategies, force pull, branch pruning and clean previews against a
|
|
11
|
+
* real remote: a bare repository with two clones — `local`, the checkout the
|
|
12
|
+
* panel works in, and `other`, standing in for a teammate who pushes.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
// The machine's own git config (pull.rebase, init.defaultBranch, signing) must
|
|
16
|
+
// not decide what these tests see.
|
|
17
|
+
const savedEnv = { global: process.env.GIT_CONFIG_GLOBAL, system: process.env.GIT_CONFIG_SYSTEM };
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
process.env.GIT_CONFIG_GLOBAL = '/dev/null';
|
|
20
|
+
process.env.GIT_CONFIG_SYSTEM = '/dev/null';
|
|
21
|
+
});
|
|
22
|
+
afterAll(() => {
|
|
23
|
+
if (savedEnv.global === undefined) delete process.env.GIT_CONFIG_GLOBAL;
|
|
24
|
+
else process.env.GIT_CONFIG_GLOBAL = savedEnv.global;
|
|
25
|
+
if (savedEnv.system === undefined) delete process.env.GIT_CONFIG_SYSTEM;
|
|
26
|
+
else process.env.GIT_CONFIG_SYSTEM = savedEnv.system;
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
let root: string;
|
|
30
|
+
let local: string;
|
|
31
|
+
let other: string;
|
|
32
|
+
|
|
33
|
+
async function git(cwd: string, ...args: string[]) {
|
|
34
|
+
const result = await execGit(args, cwd);
|
|
35
|
+
if (result.exitCode !== 0) throw new Error(`git ${args.join(' ')}: ${result.stderr}`);
|
|
36
|
+
return result.stdout.trim();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function configure(cwd: string) {
|
|
40
|
+
await git(cwd, 'config', 'user.email', 'test@example.com');
|
|
41
|
+
await git(cwd, 'config', 'user.name', 'Test');
|
|
42
|
+
await git(cwd, 'config', 'commit.gpgsign', 'false');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function commitFile(cwd: string, file: string, content: string, message: string) {
|
|
46
|
+
await writeFile(path.join(cwd, file), content);
|
|
47
|
+
await git(cwd, 'add', file);
|
|
48
|
+
await git(cwd, 'commit', '-m', message);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A teammate's commit, pushed to the shared remote. */
|
|
52
|
+
async function pushFromOther(file: string, content: string, message: string) {
|
|
53
|
+
await git(other, 'pull', '--no-rebase', 'origin', 'main');
|
|
54
|
+
await commitFile(other, file, content, message);
|
|
55
|
+
await git(other, 'push', 'origin', 'main');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
beforeEach(async () => {
|
|
59
|
+
root = await mkdtemp(path.join(tmpdir(), 'clopen-sync-'));
|
|
60
|
+
const bare = path.join(root, 'remote.git');
|
|
61
|
+
await git(root, 'init', '--bare', '-b', 'main', bare);
|
|
62
|
+
|
|
63
|
+
local = path.join(root, 'local');
|
|
64
|
+
await git(root, 'clone', bare, local);
|
|
65
|
+
await configure(local);
|
|
66
|
+
await commitFile(local, 'shared.txt', 'base\n', 'base');
|
|
67
|
+
await git(local, 'push', '-u', 'origin', 'main');
|
|
68
|
+
|
|
69
|
+
other = path.join(root, 'other');
|
|
70
|
+
await git(root, 'clone', bare, other);
|
|
71
|
+
await configure(other);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
afterEach(async () => {
|
|
75
|
+
await rm(root, { recursive: true, force: true });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('pull', () => {
|
|
79
|
+
test('reports how many commits actually arrived', async () => {
|
|
80
|
+
await pushFromOther('a.txt', 'a\n', 'a');
|
|
81
|
+
await pushFromOther('b.txt', 'b\n', 'b');
|
|
82
|
+
|
|
83
|
+
const result = await gitService.pull(local, 'origin', 'main');
|
|
84
|
+
expect(result.success).toBe(true);
|
|
85
|
+
expect(result.pulledCount).toBe(2);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('reports a divergence with counts instead of git\'s raw fatal', async () => {
|
|
89
|
+
await pushFromOther('theirs.txt', 'theirs\n', 'theirs');
|
|
90
|
+
await commitFile(local, 'mine.txt', 'mine\n', 'mine');
|
|
91
|
+
|
|
92
|
+
const result = await gitService.pull(local, 'origin', 'main', 'ff-only');
|
|
93
|
+
expect(result.success).toBe(false);
|
|
94
|
+
expect(result.reason).toBe('diverged');
|
|
95
|
+
expect(result.ahead).toBe(1);
|
|
96
|
+
expect(result.behind).toBe(1);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('merges a diverged branch when asked to', async () => {
|
|
100
|
+
await pushFromOther('theirs.txt', 'theirs\n', 'theirs');
|
|
101
|
+
await commitFile(local, 'mine.txt', 'mine\n', 'mine');
|
|
102
|
+
|
|
103
|
+
const result = await gitService.pull(local, 'origin', 'main', 'merge');
|
|
104
|
+
expect(result.success).toBe(true);
|
|
105
|
+
expect(existsSync(path.join(local, 'theirs.txt'))).toBe(true);
|
|
106
|
+
expect(existsSync(path.join(local, 'mine.txt'))).toBe(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('rebases a diverged branch on a dirty tree (autostash)', async () => {
|
|
110
|
+
await pushFromOther('theirs.txt', 'theirs\n', 'theirs');
|
|
111
|
+
await commitFile(local, 'mine.txt', 'mine\n', 'mine');
|
|
112
|
+
await writeFile(path.join(local, 'mine.txt'), 'mine, edited\n');
|
|
113
|
+
|
|
114
|
+
const result = await gitService.pull(local, 'origin', 'main', 'rebase');
|
|
115
|
+
expect(result.success).toBe(true);
|
|
116
|
+
expect(await git(local, 'log', '-1', '--format=%s', 'HEAD~1')).toBe('theirs');
|
|
117
|
+
expect(await readFile(path.join(local, 'mine.txt'), 'utf8')).toBe('mine, edited\n');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('flags local changes the pull would overwrite, and autostash gets past them', async () => {
|
|
121
|
+
await commitFile(local, 'lines.txt', '1\n2\n3\n', 'lines');
|
|
122
|
+
await git(local, 'push', 'origin', 'main');
|
|
123
|
+
await pushFromOther('lines.txt', 'one\n2\n3\n', 'edit line 1');
|
|
124
|
+
await writeFile(path.join(local, 'lines.txt'), '1\n2\nthree\n');
|
|
125
|
+
|
|
126
|
+
const blocked = await gitService.pull(local, 'origin', 'main', 'merge');
|
|
127
|
+
expect(blocked.success).toBe(false);
|
|
128
|
+
expect(blocked.reason).toBe('local-changes');
|
|
129
|
+
|
|
130
|
+
const stashed = await gitService.pull(local, 'origin', 'main', 'merge', true);
|
|
131
|
+
expect(stashed.success).toBe(true);
|
|
132
|
+
expect(await readFile(path.join(local, 'lines.txt'), 'utf8')).toBe('one\n2\nthree\n');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test('flags conflicts', async () => {
|
|
136
|
+
await pushFromOther('shared.txt', 'theirs\n', 'theirs');
|
|
137
|
+
await commitFile(local, 'shared.txt', 'mine\n', 'mine');
|
|
138
|
+
|
|
139
|
+
const result = await gitService.pull(local, 'origin', 'main', 'merge');
|
|
140
|
+
expect(result.success).toBe(false);
|
|
141
|
+
expect(result.reason).toBe('conflict');
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('force pull', () => {
|
|
146
|
+
test('previews what would be lost without changing anything', async () => {
|
|
147
|
+
await pushFromOther('theirs.txt', 'theirs\n', 'theirs');
|
|
148
|
+
await commitFile(local, 'mine.txt', 'mine\n', 'mine');
|
|
149
|
+
await writeFile(path.join(local, 'shared.txt'), 'dirty\n');
|
|
150
|
+
await writeFile(path.join(local, 'theirs.txt'), 'untracked collision\n');
|
|
151
|
+
|
|
152
|
+
const headBefore = await git(local, 'rev-parse', 'HEAD');
|
|
153
|
+
const preview = await gitService.forcePullPreview(local, 'origin', 'main');
|
|
154
|
+
expect(preview.target).toBe('origin/main');
|
|
155
|
+
expect(preview.localCommits).toBe(1);
|
|
156
|
+
expect(preview.incomingCommits).toBe(1);
|
|
157
|
+
expect(preview.changedFiles).toBe(1);
|
|
158
|
+
expect(preview.collidingUntracked).toEqual(['theirs.txt']);
|
|
159
|
+
expect(await git(local, 'rev-parse', 'HEAD')).toBe(headBefore);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('resets to the remote, stashing tracked changes and colliding untracked files only', async () => {
|
|
163
|
+
await pushFromOther('theirs.txt', 'theirs\n', 'theirs');
|
|
164
|
+
await commitFile(local, 'mine.txt', 'mine\n', 'mine');
|
|
165
|
+
await writeFile(path.join(local, 'shared.txt'), 'dirty\n');
|
|
166
|
+
await writeFile(path.join(local, 'theirs.txt'), 'untracked collision\n');
|
|
167
|
+
await writeFile(path.join(local, 'scratch.txt'), 'leave me\n');
|
|
168
|
+
|
|
169
|
+
const result = await gitService.forcePull(local, 'origin', 'main');
|
|
170
|
+
|
|
171
|
+
expect(result.discardedCommits).toBe(1);
|
|
172
|
+
expect(result.stashMessage).toMatch(/before force pull of main/);
|
|
173
|
+
expect(await git(local, 'rev-parse', 'HEAD')).toBe(await git(local, 'rev-parse', 'origin/main'));
|
|
174
|
+
expect(await readFile(path.join(local, 'theirs.txt'), 'utf8')).toBe('theirs\n');
|
|
175
|
+
expect(await readFile(path.join(local, 'shared.txt'), 'utf8')).toBe('base\n');
|
|
176
|
+
// Untracked files the remote does not have are none of its business.
|
|
177
|
+
expect(await readFile(path.join(local, 'scratch.txt'), 'utf8')).toBe('leave me\n');
|
|
178
|
+
|
|
179
|
+
const stashed = await git(local, 'stash', 'show', '--include-untracked', '--name-only', 'stash@{0}');
|
|
180
|
+
expect(stashed.split('\n').sort()).toEqual(['shared.txt', 'theirs.txt']);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('creates no stash when the tree is clean', async () => {
|
|
184
|
+
await pushFromOther('theirs.txt', 'theirs\n', 'theirs');
|
|
185
|
+
const result = await gitService.forcePull(local, 'origin', 'main');
|
|
186
|
+
expect(result.stashMessage).toBeUndefined();
|
|
187
|
+
expect(result.discardedCommits).toBe(0);
|
|
188
|
+
expect(await git(local, 'stash', 'list')).toBe('');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('refuses a branch that does not exist on the remote', async () => {
|
|
192
|
+
await git(local, 'checkout', '-b', 'local-only');
|
|
193
|
+
await expect(gitService.forcePull(local, 'origin', 'local-only')).rejects.toThrow(/does not exist on the remote/);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('prune branches', () => {
|
|
198
|
+
test('offers merged branches, never the current or default one', async () => {
|
|
199
|
+
await git(local, 'branch', 'merged-one');
|
|
200
|
+
await git(local, 'checkout', '-b', 'unmerged');
|
|
201
|
+
await commitFile(local, 'u.txt', 'u\n', 'unmerged work');
|
|
202
|
+
await git(local, 'checkout', 'main');
|
|
203
|
+
|
|
204
|
+
const { defaultBranch, candidates } = await gitService.getPruneCandidates(local, 'merged', 'origin');
|
|
205
|
+
expect(defaultBranch).toBe('main');
|
|
206
|
+
expect(candidates).toEqual([{ name: 'merged-one', merged: true }]);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('offers branches whose upstream is gone, flagging unmerged ones', async () => {
|
|
210
|
+
await git(local, 'checkout', '-b', 'feature');
|
|
211
|
+
await commitFile(local, 'f.txt', 'f\n', 'feature');
|
|
212
|
+
await git(local, 'push', '-u', 'origin', 'feature');
|
|
213
|
+
await git(local, 'checkout', 'main');
|
|
214
|
+
await git(local, 'push', 'origin', '--delete', 'feature');
|
|
215
|
+
await git(local, 'fetch', '--prune', 'origin');
|
|
216
|
+
|
|
217
|
+
const { candidates } = await gitService.getPruneCandidates(local, 'gone', 'origin');
|
|
218
|
+
expect(candidates).toEqual([{ name: 'feature', merged: false }]);
|
|
219
|
+
|
|
220
|
+
const result = await gitService.deleteBranches(local, ['feature', 'main']);
|
|
221
|
+
expect(result.deleted).toEqual(['feature']);
|
|
222
|
+
expect(result.failed.map(f => f.name)).toEqual(['main']);
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe('clean preview', () => {
|
|
227
|
+
test('lists untracked files, and ignored ones only when asked', async () => {
|
|
228
|
+
await commitFile(local, '.gitignore', '.env\n', 'ignore env');
|
|
229
|
+
await writeFile(path.join(local, '.env'), 'SECRET=1\n');
|
|
230
|
+
await writeFile(path.join(local, 'stray.txt'), 'x\n');
|
|
231
|
+
|
|
232
|
+
expect(await gitService.cleanPreview(local)).toEqual(['stray.txt']);
|
|
233
|
+
expect((await gitService.cleanPreview(local, true)).sort()).toEqual(['.env', 'stray.txt']);
|
|
234
|
+
// The preview deleted nothing.
|
|
235
|
+
expect(existsSync(path.join(local, '.env'))).toBe(true);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('stash including untracked files', () => {
|
|
240
|
+
test('takes untracked files with it', async () => {
|
|
241
|
+
await writeFile(path.join(local, 'new.txt'), 'new\n');
|
|
242
|
+
await gitService.stashSave(local, 'with untracked', false, {}, true);
|
|
243
|
+
expect(existsSync(path.join(local, 'new.txt'))).toBe(false);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test('refuses to combine with staged-only', async () => {
|
|
247
|
+
await expect(gitService.stashSave(local, undefined, true, {}, true)).rejects.toThrow(/not both/);
|
|
248
|
+
});
|
|
249
|
+
});
|
package/backend/host/metrics.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Host metrics — hardware identity and the live process table.
|
|
3
3
|
*
|
|
4
|
-
* One module because Settings →
|
|
4
|
+
* One module because Settings → Machine and Project Info both ask, and their
|
|
5
5
|
* numbers only compare if measured the same way. `si.processes()` reports CPU
|
|
6
6
|
* against a different basis per platform (total capacity on Linux, one core on
|
|
7
7
|
* macOS/BSD, consumed CPU on Windows), so everything is normalised here to
|
|
@@ -13,7 +13,6 @@ import { authQueries } from '../database/queries';
|
|
|
13
13
|
|
|
14
14
|
export interface AuthIdentity {
|
|
15
15
|
userId: string;
|
|
16
|
-
role: string;
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
/**
|
|
@@ -43,5 +42,5 @@ export function authenticateRequest(request: Request): AuthIdentity {
|
|
|
43
42
|
throw Object.assign(new Error('User not found'), { status: 401 });
|
|
44
43
|
}
|
|
45
44
|
authQueries.updateLastActive(session.id);
|
|
46
|
-
return { userId: user.id
|
|
45
|
+
return { userId: user.id };
|
|
47
46
|
}
|
|
@@ -17,7 +17,7 @@ import { filesDownloadRoute } from './files-download';
|
|
|
17
17
|
import { authQueries } from '../database/queries';
|
|
18
18
|
import { hashToken } from '../auth/tokens';
|
|
19
19
|
import { projectQueries } from '../database/queries/project-queries';
|
|
20
|
-
import { initializeDatabase, closeDatabase } from '../database';
|
|
20
|
+
import { initializeDatabase, closeDatabase, getDatabase } from '../database';
|
|
21
21
|
|
|
22
22
|
const TEST_DIR = join(import.meta.dir, '.test-download-integration');
|
|
23
23
|
const TEST_WORKSPACE = join(TEST_DIR, 'workspace');
|
|
@@ -49,7 +49,6 @@ beforeAll(async () => {
|
|
|
49
49
|
name: 'Test User',
|
|
50
50
|
color: '#000000',
|
|
51
51
|
avatar: 'test',
|
|
52
|
-
role: 'member',
|
|
53
52
|
personal_access_token_hash: null,
|
|
54
53
|
created_at: new Date().toISOString()
|
|
55
54
|
});
|
|
@@ -74,12 +73,11 @@ beforeAll(async () => {
|
|
|
74
73
|
last_opened_at: new Date().toISOString()
|
|
75
74
|
});
|
|
76
75
|
testProjectId = project.id;
|
|
77
|
-
projectQueries.
|
|
76
|
+
projectQueries.list(testProjectId);
|
|
78
77
|
});
|
|
79
78
|
|
|
80
79
|
afterAll(async () => {
|
|
81
|
-
|
|
82
|
-
authQueries.deleteUser(testUserId);
|
|
80
|
+
getDatabase().prepare('DELETE FROM users WHERE id = ?').run(testUserId);
|
|
83
81
|
projectQueries.deleteProject(testProjectId);
|
|
84
82
|
closeDatabase();
|
|
85
83
|
await rm(TEST_DIR, { recursive: true, force: true });
|
|
@@ -114,13 +112,14 @@ describe('File download route', () => {
|
|
|
114
112
|
expect(new Uint8Array(await response.arrayBuffer())).toEqual(bytes);
|
|
115
113
|
});
|
|
116
114
|
|
|
117
|
-
it('
|
|
118
|
-
const filePath = join(OUTSIDE_DIR, '
|
|
119
|
-
await writeFile(filePath, '
|
|
115
|
+
it('serves a path outside every project', async () => {
|
|
116
|
+
const filePath = join(OUTSIDE_DIR, 'loose.txt');
|
|
117
|
+
await writeFile(filePath, 'anywhere on the machine');
|
|
120
118
|
|
|
121
119
|
const response = await filesDownloadRoute.handle(createMockDownloadRequest(filePath, testToken));
|
|
122
120
|
|
|
123
|
-
expect(response.status).toBe(
|
|
121
|
+
expect(response.status).toBe(200);
|
|
122
|
+
expect(await response.text()).toBe('anywhere on the machine');
|
|
124
123
|
});
|
|
125
124
|
|
|
126
125
|
it('rejects an unauthenticated request', async () => {
|
|
@@ -20,7 +20,7 @@ import { basename } from 'node:path';
|
|
|
20
20
|
|
|
21
21
|
import { debug } from '$shared/utils/logger';
|
|
22
22
|
import { fileAuditLogQueries } from '../database/queries';
|
|
23
|
-
import { findContainingProjectId,
|
|
23
|
+
import { findContainingProjectId, resolveFilePath } from '../ws/files/path-access';
|
|
24
24
|
import { clientIpFromRequest } from '../utils/client-ip';
|
|
25
25
|
import { authenticateRequest, type AuthIdentity } from './bearer-auth';
|
|
26
26
|
|
|
@@ -59,7 +59,7 @@ export const filesDownloadRoute = new Elysia().get('/api/files/download', async
|
|
|
59
59
|
|
|
60
60
|
let resolvedPath: string;
|
|
61
61
|
try {
|
|
62
|
-
resolvedPath = await
|
|
62
|
+
resolvedPath = await resolveFilePath(pathParam);
|
|
63
63
|
} catch (error) {
|
|
64
64
|
logFailure('Path access denied');
|
|
65
65
|
return new Response(error instanceof Error ? error.message : 'Access denied', { status: 403 });
|