@playcraft/cli 0.1.27 → 0.1.29
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 +3 -38
- package/dist/agent/local-backend.js +1 -1
- package/dist/cli-root-help.js +0 -2
- package/dist/commands/build.js +4 -9
- package/dist/commands/create.js +33 -26
- package/dist/commands/image.js +6 -2
- package/dist/commands/login.js +42 -84
- package/dist/commands/skills.js +0 -2
- package/dist/commands/tools-3d.js +0 -11
- package/dist/commands/tools-generation.js +109 -68
- package/dist/commands/tools-image-context.js +23 -0
- package/dist/commands/tools.js +2 -2
- package/dist/config.js +4 -59
- package/dist/index.js +3 -34
- package/dist/remix/clone.js +0 -2
- package/dist/remix/init-template.js +0 -2
- package/dist/remix/pull.js +0 -2
- package/dist/remix/push.js +1 -21
- package/dist/utils/agent-api-client.js +30 -185
- package/dist/utils/image-asset-context.js +105 -0
- package/dist/utils/playcraft-http-client.js +4 -6
- package/package.json +5 -14
- package/project-template/.claude/skills/playcraft-image-generation/SKILL.md +81 -23
- package/project-templates/phaser/.claude/skills/playcraft-image-generation/SKILL.md +81 -23
- package/project-templates/threejs/.claude/skills/playcraft-image-generation/SKILL.md +81 -23
- package/dist/auth/account-activation.js +0 -157
- package/dist/auth/messages.js +0 -30
- package/dist/commands/platform-skills.generated.js +0 -19
- package/dist/commands/playable.js +0 -53
- package/dist/commands/tools-batch.js +0 -87
- package/dist/commands/video.js +0 -111
- package/dist/commands/workspace-runtime.js +0 -54
- package/dist/project-skills/builtin-import.js +0 -225
- package/dist/project-skills/commands.js +0 -77
- package/dist/project-skills/lifecycle.js +0 -28
- package/dist/project-skills/local-run.js +0 -112
- package/dist/project-skills/local-store.js +0 -221
- package/dist/project-skills/messages.js +0 -80
- package/dist/project-skills/reconcile.js +0 -62
- package/dist/project-skills/remote-cache.js +0 -51
- package/dist/project-skills/validation.js +0 -19
- package/dist/recording/messages.js +0 -40
- package/dist/recording/project-digest.js +0 -85
- package/dist/recording/recorder.js +0 -293
- package/dist/recording/scenario.js +0 -50
- package/dist/utils/environment-endpoints.js +0 -17
- package/dist/utils/tool-operation-journal.js +0 -124
- package/dist/utils/video-evidence-git.js +0 -81
- package/dist/video-replication/client.js +0 -222
- package/dist/video-replication/comparison-evidence.js +0 -149
- package/dist/video-replication/comparison-files.js +0 -57
- package/dist/video-replication/comparison-log.js +0 -72
- package/dist/video-replication/comparison-messages.js +0 -104
- package/dist/video-replication/comparison-output.js +0 -63
- package/dist/video-replication/comparison-preflight.js +0 -66
- package/dist/video-replication/comparison.js +0 -216
- package/dist/video-replication/evidence.js +0 -165
- package/dist/video-replication/media.js +0 -111
- package/dist/video-replication/messages.js +0 -89
- package/dist/workspace-runtime/codex/app-server.js +0 -636
- package/dist/workspace-runtime/codex/config-toml.js +0 -107
- package/dist/workspace-runtime/codex/jsonrpc-stdio.js +0 -123
- package/dist/workspace-runtime/codex/launcher-client.js +0 -163
- package/dist/workspace-runtime/codex/loopback.js +0 -143
- package/dist/workspace-runtime/codex/model-broker.js +0 -258
- package/dist/workspace-runtime/codex/native-adapter.js +0 -9
- package/dist/workspace-runtime/main.js +0 -10
- package/dist/workspace-runtime/persistence/journal.js +0 -355
- package/dist/workspace-runtime/persistence/native-subagents.js +0 -162
- package/dist/workspace-runtime/persistence/sqlite-client.js +0 -77
- package/dist/workspace-runtime/persistence/sqlite-worker.js +0 -1057
- package/dist/workspace-runtime/previews/publish.js +0 -259
- package/dist/workspace-runtime/previews/single-file.js +0 -43
- package/dist/workspace-runtime/processes/managed-writes.js +0 -69
- package/dist/workspace-runtime/production/contracts.js +0 -17
- package/dist/workspace-runtime/production/controller.js +0 -207
- package/dist/workspace-runtime/production/dependencies.js +0 -60
- package/dist/workspace-runtime/production/native.js +0 -90
- package/dist/workspace-runtime/production/processes.js +0 -86
- package/dist/workspace-runtime/production/save-command.js +0 -24
- package/dist/workspace-runtime/production/source-copy.js +0 -91
- package/dist/workspace-runtime/production/stages.js +0 -202
- package/dist/workspace-runtime/production/storage.js +0 -32
- package/dist/workspace-runtime/serve.js +0 -202
- package/dist/workspace-runtime/server/auth.js +0 -21
- package/dist/workspace-runtime/server/connectivity.js +0 -201
- package/dist/workspace-runtime/server/desktop-host.js +0 -429
- package/dist/workspace-runtime/server/desktop-preview-config.js +0 -81
- package/dist/workspace-runtime/server/desktop-preview-service.js +0 -157
- package/dist/workspace-runtime/server/desktop-preview-static.js +0 -53
- package/dist/workspace-runtime/server/desktop-preview.js +0 -189
- package/dist/workspace-runtime/server/dispatch.js +0 -903
- package/dist/workspace-runtime/server/endpoints.js +0 -19
- package/dist/workspace-runtime/server/execution-group.js +0 -85
- package/dist/workspace-runtime/server/http.js +0 -984
- package/dist/workspace-runtime/server/lock.js +0 -53
- package/dist/workspace-runtime/server/rfb-watch-gate.js +0 -168
- package/dist/workspace-runtime/server/terminal-host.js +0 -294
- package/dist/workspace-runtime/server/types.js +0 -1
- package/dist/workspace-runtime/workspaces/artifact-events.js +0 -55
- package/dist/workspace-runtime/workspaces/artifact-files.js +0 -195
- package/dist/workspace-runtime/workspaces/artifact-receipts.js +0 -89
- package/dist/workspace-runtime/workspaces/artifacts.js +0 -260
- package/dist/workspace-runtime/workspaces/attachment-content.js +0 -111
- package/dist/workspace-runtime/workspaces/attachments.js +0 -175
- package/dist/workspace-runtime/workspaces/batch-commit.js +0 -142
- package/dist/workspace-runtime/workspaces/cli-context.js +0 -23
- package/dist/workspace-runtime/workspaces/cli-credentials.js +0 -111
- package/dist/workspace-runtime/workspaces/context-error.js +0 -2
- package/dist/workspace-runtime/workspaces/dependencies.js +0 -264
- package/dist/workspace-runtime/workspaces/developer-command.js +0 -64
- package/dist/workspace-runtime/workspaces/directories.js +0 -113
- package/dist/workspace-runtime/workspaces/directory-access.js +0 -29
- package/dist/workspace-runtime/workspaces/directory-maintenance.js +0 -111
- package/dist/workspace-runtime/workspaces/directory-stats.js +0 -49
- package/dist/workspace-runtime/workspaces/disk-snapshot.js +0 -31
- package/dist/workspace-runtime/workspaces/empty-template.js +0 -40
- package/dist/workspace-runtime/workspaces/files.js +0 -120
- package/dist/workspace-runtime/workspaces/git-credential-broker.js +0 -312
- package/dist/workspace-runtime/workspaces/git-credential-helper.js +0 -68
- package/dist/workspace-runtime/workspaces/git-direct-store.js +0 -155
- package/dist/workspace-runtime/workspaces/git.js +0 -517
- package/dist/workspace-runtime/workspaces/json5-edit.js +0 -170
- package/dist/workspace-runtime/workspaces/parameters.js +0 -272
- package/dist/workspace-runtime/workspaces/prepare.js +0 -264
- package/dist/workspace-runtime/workspaces/registry.js +0 -226
- package/dist/workspace-runtime/workspaces/revisions.js +0 -31
- package/dist/workspace-runtime/workspaces/skill-artifact-loader.js +0 -91
- package/dist/workspace-runtime/workspaces/skills.js +0 -189
- package/dist/workspace-runtime/workspaces/video-attachments.js +0 -49
- package/project-template/.claude/skills/playcraft-replica-validation/SKILL.md +0 -25
- package/project-template/.claude/skills/playcraft-replica-validation/references/contracts.md +0 -79
- package/project-template/.claude/skills/playcraft-video-understanding/SKILL.md +0 -29
- package/project-template/.claude/skills/playcraft-video-understanding/references/contracts.md +0 -37
- package/project-template-v2/.claude/agents/artist.md +0 -82
- package/project-template-v2/.claude/agents/developer.md +0 -153
- package/project-template-v2/.claude/agents/game-designer.md +0 -264
- package/project-template-v2/.claude/agents/refs/artist-art-style-catalog.md +0 -533
- package/project-template-v2/.claude/agents/refs/artist-color-audio-recipes.md +0 -153
- package/project-template-v2/.claude/agents/refs/artist-dimension-axis.md +0 -27
- package/project-template-v2/.claude/agents/refs/artist-master-composite-recipes.md +0 -208
- package/project-template-v2/.claude/agents/refs/atom-skill-library.md +0 -81
- package/project-template-v2/.claude/agents/refs/developer-impl-cookbook.md +0 -432
- package/project-template-v2/.claude/agents/refs/framework-5-component-filter.md +0 -252
- package/project-template-v2/.claude/agents/refs/framework-game-feel-juice.md +0 -266
- package/project-template-v2/.claude/agents/refs/framework-mda.md +0 -147
- package/project-template-v2/.claude/agents/refs/game-designer-gameplay-sufficiency.md +0 -123
- package/project-template-v2/.claude/agents/refs/ta-3d-flip-recipe.md +0 -88
- package/project-template-v2/.claude/agents/refs/ta-atlas-deliverable-standard.md +0 -67
- package/project-template-v2/.claude/agents/refs/ta-batch-pipeline-recipes.md +0 -120
- package/project-template-v2/.claude/agents/refs/ta-image-generation-detail.md +0 -300
- package/project-template-v2/.claude/agents/refs/ta-image-ops-reference.md +0 -495
- package/project-template-v2/.claude/agents/refs/ta-pipeline-cookbook.md +0 -1141
- package/project-template-v2/.claude/agents/refs/ta-tools-reference.md +0 -111
- package/project-template-v2/.claude/agents/refs/ta-vfx-preset-catalog.md +0 -365
- package/project-template-v2/.claude/agents/refs/threejs-cannon-pitfalls.md +0 -412
- package/project-template-v2/.claude/agents/reviewer.md +0 -75
- package/project-template-v2/.claude/agents/technical-artist.md +0 -86
- package/project-template-v2/.claude/hooks/snapshot-milestone.mjs +0 -243
- package/project-template-v2/.claude/hooks/user-prompt.mjs +0 -133
- package/project-template-v2/.claude/settings.json +0 -33
- package/project-template-v2/.claude/skills/brainstorming/SKILL.md +0 -161
- package/project-template-v2/.claude/skills/brainstorming/scripts/frame-template.html +0 -270
- package/project-template-v2/.claude/skills/brainstorming/scripts/helper.js +0 -177
- package/project-template-v2/.claude/skills/brainstorming/scripts/server.cjs +0 -354
- package/project-template-v2/.claude/skills/brainstorming/scripts/start-server.sh +0 -148
- package/project-template-v2/.claude/skills/brainstorming/scripts/stop-server.sh +0 -56
- package/project-template-v2/.claude/skills/brainstorming/scripts/wait-for-selection.sh +0 -62
- package/project-template-v2/.claude/skills/brainstorming/spec-document-reviewer-prompt.md +0 -49
- package/project-template-v2/.claude/skills/brainstorming/visual-companion.md +0 -309
- package/project-template-v2/.claude/skills/playcraft-ad-psychology/SKILL.md +0 -182
- package/project-template-v2/.claude/skills/playcraft-art-style-guide/SKILL.md +0 -123
- package/project-template-v2/.claude/skills/playcraft-asset-state-sheet/SKILL.md +0 -205
- package/project-template-v2/.claude/skills/playcraft-audio-generation/SKILL.md +0 -280
- package/project-template-v2/.claude/skills/playcraft-batch-pipeline/SKILL.md +0 -184
- package/project-template-v2/.claude/skills/playcraft-build-optimizer/SKILL.md +0 -306
- package/project-template-v2/.claude/skills/playcraft-image-generation/SKILL.md +0 -298
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/build-sprite-sheet.template.mjs +0 -123
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/compare-style.template.mjs +0 -254
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/gen-batch-sprite.template.mjs +0 -324
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/gen-batch.template.mjs +0 -97
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/gen-edit-variants.template.mjs +0 -118
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/process-batch.template.mjs +0 -137
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/prompt-cookbook.md +0 -397
- package/project-template-v2/.claude/skills/playcraft-image-generation/reference/validate-sprite-sheet.template.mjs +0 -296
- package/project-template-v2/.claude/skills/playcraft-image-ops/SKILL.md +0 -122
- package/project-template-v2/.claude/skills/playcraft-image-processing/SKILL.md +0 -219
- package/project-template-v2/.claude/skills/playcraft-masking/SKILL.md +0 -373
- package/project-template-v2/.claude/skills/playcraft-playable-optimization/SKILL.md +0 -161
- package/project-template-v2/.claude/skills/playcraft-research/SKILL.md +0 -215
- package/project-template-v2/.claude/skills/playcraft-skill-recommender/SKILL.md +0 -382
- package/project-template-v2/.claude/skills/playcraft-sprite-generation/SKILL.md +0 -423
- package/project-template-v2/.claude/skills/playcraft-sprite-remix/SKILL.md +0 -158
- package/project-template-v2/.claude/skills/playcraft-sprite-sheet/SKILL.md +0 -100
- package/project-template-v2/.claude/skills/playcraft-storyboard/SKILL.md +0 -167
- package/project-template-v2/.claude/skills/playcraft-style-qa/SKILL.md +0 -270
- package/project-template-v2/.claude/skills/playcraft-text-rendering/SKILL.md +0 -236
- package/project-template-v2/.claude/skills/playcraft-vfx-animation/SKILL.md +0 -130
- package/project-template-v2/.claude/skills/playwright-cli/SKILL.md +0 -390
- package/project-template-v2/.claude/skills/playwright-cli/references/element-attributes.md +0 -23
- package/project-template-v2/.claude/skills/playwright-cli/references/playwright-tests.md +0 -39
- package/project-template-v2/.claude/skills/playwright-cli/references/request-mocking.md +0 -87
- package/project-template-v2/.claude/skills/playwright-cli/references/running-code.md +0 -240
- package/project-template-v2/.claude/skills/playwright-cli/references/session-management.md +0 -226
- package/project-template-v2/.claude/skills/playwright-cli/references/spec-driven-testing.md +0 -308
- package/project-template-v2/.claude/skills/playwright-cli/references/storage-state.md +0 -275
- package/project-template-v2/.claude/skills/playwright-cli/references/test-generation.md +0 -134
- package/project-template-v2/.claude/skills/playwright-cli/references/tracing.md +0 -142
- package/project-template-v2/.claude/skills/playwright-cli/references/video-recording.md +0 -153
- package/project-template-v2/.claude/skills/session-analyzer/SKILL.md +0 -386
- package/project-template-v2/.claude/skills/session-analyzer/scripts/execution-breakdown.mjs +0 -182
- package/project-template-v2/.claude/skills/session-analyzer/scripts/find-turns.mjs +0 -72
- package/project-template-v2/.claude/skills/session-analyzer/scripts/heavy-output.mjs +0 -121
- package/project-template-v2/.claude/skills/session-analyzer/scripts/resolve-session.mjs +0 -102
- package/project-template-v2/.claude/skills/session-analyzer/scripts/subagent-stats.mjs +0 -127
- package/project-template-v2/.claude/skills/session-analyzer/scripts/subagent-tool-timeline.mjs +0 -106
- package/project-template-v2/.claude/skills/session-analyzer/scripts/time-gaps.mjs +0 -128
- package/project-template-v2/.claude/skills/session-analyzer/scripts/turn-timeline.mjs +0 -67
- package/project-template-v2/.claude/snapshot.mjs +0 -263
- package/project-template-v2/.playcraft/skills.lock.json +0 -152
- package/project-template-v2/CLAUDE.md +0 -146
- package/project-template-v2/assets/audio/bgm/.gitkeep +0 -0
- package/project-template-v2/assets/audio/sfx/.gitkeep +0 -0
- package/project-template-v2/assets/bundles/.gitkeep +0 -0
- package/project-template-v2/assets/images/bg/.gitkeep +0 -0
- package/project-template-v2/assets/images/reference/.gitkeep +0 -0
- package/project-template-v2/assets/images/storyboard/.gitkeep +0 -0
- package/project-template-v2/assets/images/tiles/.gitkeep +0 -0
- package/project-template-v2/assets/images/ui/.gitkeep +0 -0
- package/project-template-v2/assets/images/vfx/.gitkeep +0 -0
- package/project-template-v2/assets/models/.gitkeep +0 -0
- package/project-template-v2/docs/harness/iteration-1/context-flow.md +0 -254
- package/project-template-v2/docs/harness/iteration-1/generate-flow.md +0 -91
- package/project-template-v2/docs/harness/iteration-1/ideate-flow.md +0 -214
- package/project-template-v2/docs/harness/iteration-1/optimize-flow.md +0 -75
- package/project-template-v2/docs/harness/iteration-1/wrapup-flow.md +0 -63
- package/project-template-v2/docs/harness/iteration-2/context-flow.md +0 -223
- package/project-template-v2/docs/harness/iteration-2/generate-flow.md +0 -129
- package/project-template-v2/docs/harness/iteration-2/ideate-flow.md +0 -267
- package/project-template-v2/docs/harness/iteration-2/optimize-flow.md +0 -164
- package/project-template-v2/docs/harness/iteration-2/wrapup-flow.md +0 -115
- package/project-template-v2/docs/harness/orchestrator-flow.md +0 -364
- package/project-template-v2/docs/project-state.json +0 -60
- package/project-template-v2/docs/project-state.md +0 -72
- package/project-template-v2/docs/standards/README.md +0 -225
- package/project-template-v2/docs/standards/agent-behavior-standards.md +0 -174
- package/project-template-v2/docs/standards/artifacts/design-brief.md +0 -19
- package/project-template-v2/docs/standards/artifacts/design.md +0 -22
- package/project-template-v2/docs/standards/artifacts/game-code.md +0 -41
- package/project-template-v2/docs/standards/artifacts/todo-list.md +0 -41
- package/project-template-v2/docs/standards/iter1-agent-behavior-standards.md +0 -343
- package/project-template-v2/game/index.ts +0 -18
- package/project-template-v2/globals.d.ts +0 -51
- package/project-template-v2/index.css +0 -34
- package/project-template-v2/index.html +0 -18
- package/project-template-v2/main.ts +0 -9
- package/project-template-v2/package.json +0 -46
- package/project-template-v2/skills/_shared/scripts/dispatch-clear.mjs +0 -31
- package/project-template-v2/skills/_shared/scripts/dispatch-set.mjs +0 -86
- package/project-template-v2/skills/_shared/scripts/dod-check.mjs +0 -153
- package/project-template-v2/skills/_shared/scripts/handoff-append.mjs +0 -70
- package/project-template-v2/skills/_shared/scripts/lib/validator-artifacts.mjs +0 -91
- package/project-template-v2/skills/_shared/scripts/pipeline/dod-config.mjs +0 -131
- package/project-template-v2/skills/_shared/scripts/pipeline/index.mjs +0 -80
- package/project-template-v2/skills/_shared/scripts/pipeline/iteration-1-core.mjs +0 -90
- package/project-template-v2/skills/_shared/scripts/pipeline/iteration-2-wrap.mjs +0 -93
- package/project-template-v2/skills/_shared/scripts/pipeline/iteration-3-visual.mjs +0 -23
- package/project-template-v2/skills/_shared/scripts/render-project-state.mjs +0 -230
- package/project-template-v2/skills/_shared/scripts/state-advance-stage.mjs +0 -58
- package/project-template-v2/skills/_shared/scripts/state-get.mjs +0 -339
- package/project-template-v2/skills/_shared/scripts/state-handoff.mjs +0 -39
- package/project-template-v2/skills/_shared/scripts/state-set.mjs +0 -94
- package/project-template-v2/skills/_shared/scripts/state-store.mjs +0 -783
- package/project-template-v2/skills/_shared/scripts/todo-add.mjs +0 -88
- package/project-template-v2/skills/_shared/scripts/todo-get.mjs +0 -130
- package/project-template-v2/skills/_shared/scripts/todo-remove.mjs +0 -33
- package/project-template-v2/skills/_shared/scripts/todo-set.mjs +0 -47
- package/project-template-v2/skills/_shared/scripts/verify-asset-code-sync.mjs +0 -268
- package/project-template-v2/skills/_shared/scripts/verify-env.mjs +0 -285
- package/project-template-v2/skills/_shared/scripts/verify-placeholders.mjs +0 -161
- package/project-template-v2/skills/playable-autoplay/SKILL.md +0 -176
- package/project-template-v2/skills/playable-autoplay/agents/openai.yaml +0 -4
- package/project-template-v2/skills/playable-debug/SKILL.md +0 -116
- package/project-template-v2/skills/playable-debug/agents/openai.yaml +0 -4
- package/project-template-v2/skills/playable-debug/references/debug-config.md +0 -40
- package/project-template-v2/skills/playable-record/SKILL.md +0 -140
- package/project-template-v2/skills/playable-record/scripts/lib/dev-server.mjs +0 -104
- package/project-template-v2/skills/playable-record/scripts/lib/record-audio-bridge.js +0 -141
- package/project-template-v2/skills/playable-record/scripts/record-playable.mjs +0 -425
- package/project-template-v2/skills/playable-record/scripts/verify-contract.mjs +0 -261
- package/project-template-v2/skills/playable-record/scripts/verify-firstwin.mjs +0 -398
- package/project-template-v2/skills/playable-record/scripts/verify-fusion.mjs +0 -322
- package/project-template-v2/skills/playable-record/scripts/verify-lifecycle.mjs +0 -105
- package/project-template-v2/skills/playable-record/scripts/verify-vlm-video.mjs +0 -230
- package/project-template-v2/skills/playable-validate/SKILL.md +0 -61
- package/project-template-v2/skills/playable-validate/validation-rules.md +0 -14
- package/project-template-v2/skills/playable-verify-ui/SKILL.md +0 -63
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/__init__.py +0 -1
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/config_expr.js +0 -218
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/node_utils.js +0 -51
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/profile_loader.py +0 -59
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/report.py +0 -106
- package/project-template-v2/skills/playable-verify-ui/scripts/lib/runtime_contract.js +0 -116
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_fetch_antipatterns.py +0 -59
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_hardcoded_layout.js +0 -64
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_runtime_contract.js +0 -58
- package/project-template-v2/skills/playable-verify-ui/scripts/verify_text_overlap.py +0 -333
- package/project-template-v2/ta-workspace/scripts/.gitkeep +0 -0
- package/project-template-v2/tsconfig.json +0 -20
- package/project-template-v2/vite.config.ts +0 -27
- package/project-templates/batch-locks/phaser.yaml +0 -922
- package/project-templates/batch-locks/threejs.yaml +0 -978
- package/project-templates/empty/AGENTS.md +0 -25
- package/project-templates/empty/gitignore +0 -33
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { constants } from 'node:fs';
|
|
2
|
-
import { lstat, mkdir, open, realpath, readdir, rename, unlink } from 'node:fs/promises';
|
|
3
|
-
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import { join, resolve, sep } from 'node:path';
|
|
5
|
-
import { isLocalWorkspaceGitEnvironment, workspaceGitDirectAccessSchema, workspaceDirectoryNameSchema, sameWorkspaceRegistration } from '@playcraft/common/cloud-workspace/runtime';
|
|
6
|
-
/** Control-owned files only. Same-user processes are not an operating-system isolation boundary. */
|
|
7
|
-
export class WorkspaceGitDirectStore {
|
|
8
|
-
state;
|
|
9
|
-
mutation = Promise.resolve();
|
|
10
|
-
constructor(state) {
|
|
11
|
-
this.state = state;
|
|
12
|
-
}
|
|
13
|
-
exclusive(action) {
|
|
14
|
-
const result = this.mutation.then(action);
|
|
15
|
-
this.mutation = result.catch(() => undefined);
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
identity() {
|
|
19
|
-
const c = this.state.config;
|
|
20
|
-
return JSON.stringify([c.environment, c.userId, c.runtimeId, c.generation]);
|
|
21
|
-
}
|
|
22
|
-
name(id, directoryName = 'basic') {
|
|
23
|
-
if (!/^[a-f0-9-]{36}$/.test(id))
|
|
24
|
-
throw new Error('git_access_denied');
|
|
25
|
-
workspaceDirectoryNameSchema.parse(directoryName);
|
|
26
|
-
return `${id}${directoryName === 'basic' ? '' : `--${directoryName}`}.json`;
|
|
27
|
-
}
|
|
28
|
-
async directory(create, action) {
|
|
29
|
-
const stateDir = resolve(this.state.config.stateDir);
|
|
30
|
-
const directory = join(stateDir, 'direct-git');
|
|
31
|
-
if (!create && !(await lstat(directory).catch(error => {
|
|
32
|
-
if (error.code === 'ENOENT')
|
|
33
|
-
return undefined;
|
|
34
|
-
throw error;
|
|
35
|
-
})))
|
|
36
|
-
return undefined;
|
|
37
|
-
const root = await realpath(this.state.config.workspaceRoot);
|
|
38
|
-
if (this.state.config.stateDir !== stateDir || await realpath(stateDir) !== stateDir || stateDir === root || stateDir.startsWith(`${root}${sep}`)) {
|
|
39
|
-
throw new Error('git_control_directory_rejected');
|
|
40
|
-
}
|
|
41
|
-
for (const path of [stateDir, directory]) {
|
|
42
|
-
if (path === directory && create)
|
|
43
|
-
await mkdir(path, { mode: 0o700 }).catch(error => {
|
|
44
|
-
if (error.code !== 'EEXIST')
|
|
45
|
-
throw error;
|
|
46
|
-
});
|
|
47
|
-
const stat = await lstat(path);
|
|
48
|
-
if (!stat.isDirectory() || stat.isSymbolicLink() || stat.uid !== process.getuid?.() || (stat.mode & 0o077) !== 0) {
|
|
49
|
-
throw new Error('git_control_directory_rejected');
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
const handle = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
53
|
-
try {
|
|
54
|
-
const pinned = process.platform === 'linux' ? `/proc/self/fd/${handle.fd}` : directory;
|
|
55
|
-
return await action(pinned);
|
|
56
|
-
}
|
|
57
|
-
finally {
|
|
58
|
-
await handle.close();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
async read(path) {
|
|
62
|
-
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW).catch(error => {
|
|
63
|
-
if (error.code === 'ENOENT')
|
|
64
|
-
return undefined;
|
|
65
|
-
throw error;
|
|
66
|
-
});
|
|
67
|
-
if (!handle)
|
|
68
|
-
return undefined;
|
|
69
|
-
try {
|
|
70
|
-
const stat = await handle.stat();
|
|
71
|
-
if (!stat.isFile() || stat.uid !== process.getuid?.() || (stat.mode & 0o777) !== 0o600 || stat.nlink !== 1 || stat.size > 16384) {
|
|
72
|
-
throw new Error('git_credential_file_rejected');
|
|
73
|
-
}
|
|
74
|
-
return JSON.parse(await handle.readFile('utf8'));
|
|
75
|
-
}
|
|
76
|
-
finally {
|
|
77
|
-
await handle.close();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
async get(binding, directoryName = 'basic') {
|
|
81
|
-
return this.directory(false, async (path) => {
|
|
82
|
-
const stored = await this.read(join(path, this.name(binding.workspaceId, directoryName)));
|
|
83
|
-
if (!stored)
|
|
84
|
-
return undefined;
|
|
85
|
-
if (stored.identity !== this.identity() || !sameWorkspaceRegistration(stored.binding, binding)
|
|
86
|
-
|| !isLocalWorkspaceGitEnvironment(this.state.config.environment))
|
|
87
|
-
throw new Error('git_access_denied');
|
|
88
|
-
if (stored.access !== null) {
|
|
89
|
-
const access = workspaceGitDirectAccessSchema.parse(stored.access);
|
|
90
|
-
if (access.repositoryUrl !== binding.repositoryUrl)
|
|
91
|
-
throw new Error('git_access_denied');
|
|
92
|
-
}
|
|
93
|
-
return stored;
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
async put(binding, access, rejectedPassword, directoryName = 'basic') {
|
|
97
|
-
if (!isLocalWorkspaceGitEnvironment(this.state.config.environment) || this.state.journal.destroyed())
|
|
98
|
-
throw new Error('git_access_denied');
|
|
99
|
-
if (access && (workspaceGitDirectAccessSchema.parse(access).repositoryUrl !== binding.repositoryUrl))
|
|
100
|
-
throw new Error('git_access_denied');
|
|
101
|
-
await this.exclusive(() => this.directory(true, async (path) => {
|
|
102
|
-
const target = join(path, this.name(binding.workspaceId, directoryName));
|
|
103
|
-
// Refuse symlink/hardlink/permissive old files even though rename would replace them.
|
|
104
|
-
const old = await this.read(target);
|
|
105
|
-
if (rejectedPassword !== undefined && old?.access?.password !== rejectedPassword)
|
|
106
|
-
return;
|
|
107
|
-
const temporary = join(path, `${randomUUID()}.tmp`);
|
|
108
|
-
const handle = await open(temporary, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
109
|
-
try {
|
|
110
|
-
await handle.writeFile(JSON.stringify({ identity: this.identity(), binding, access }));
|
|
111
|
-
await handle.sync();
|
|
112
|
-
await handle.close();
|
|
113
|
-
if (this.state.journal.destroyed())
|
|
114
|
-
throw new Error('git_access_denied');
|
|
115
|
-
await rename(temporary, target);
|
|
116
|
-
const dir = await open(path, constants.O_RDONLY | constants.O_DIRECTORY);
|
|
117
|
-
try {
|
|
118
|
-
await dir.sync();
|
|
119
|
-
}
|
|
120
|
-
finally {
|
|
121
|
-
await dir.close();
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
finally {
|
|
125
|
-
await handle.close();
|
|
126
|
-
await unlink(temporary).catch(error => { if (error.code !== 'ENOENT')
|
|
127
|
-
throw error; });
|
|
128
|
-
}
|
|
129
|
-
}));
|
|
130
|
-
}
|
|
131
|
-
async remove(workspaceId, directoryName = 'basic') {
|
|
132
|
-
await this.exclusive(() => this.directory(false, async (path) => {
|
|
133
|
-
const target = join(path, this.name(workspaceId, directoryName));
|
|
134
|
-
await this.read(target);
|
|
135
|
-
await unlink(target).catch(error => { if (error.code !== 'ENOENT')
|
|
136
|
-
throw error; });
|
|
137
|
-
}));
|
|
138
|
-
}
|
|
139
|
-
async sweep(destroy = false) {
|
|
140
|
-
await this.exclusive(() => this.directory(false, async (path) => {
|
|
141
|
-
for (const name of await readdir(path)) {
|
|
142
|
-
if (!/^[a-f0-9-]{36}(?:--remix-[a-z0-9][a-z0-9-]{3,63})?\.(json|tmp)$/.test(name))
|
|
143
|
-
throw new Error('git_credential_file_rejected');
|
|
144
|
-
const target = join(path, name);
|
|
145
|
-
if (name.endsWith('.tmp')) {
|
|
146
|
-
await unlink(target);
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
const stored = await this.read(target);
|
|
150
|
-
if (destroy || stored?.identity !== this.identity() || !isLocalWorkspaceGitEnvironment(this.state.config.environment))
|
|
151
|
-
await unlink(target);
|
|
152
|
-
}
|
|
153
|
-
}));
|
|
154
|
-
}
|
|
155
|
-
}
|
|
@@ -1,517 +0,0 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
|
-
import { excludeFromGit } from './dependencies.js';
|
|
3
|
-
import { lstat, readFile, realpath, stat, unlink } from 'node:fs/promises';
|
|
4
|
-
import { tmpdir } from 'node:os';
|
|
5
|
-
import { basename, dirname, join, relative, resolve, sep } from 'node:path';
|
|
6
|
-
import { WORKSPACE_FILE_CONTENT_MAX, gitDiffSchema, gitFileContentSchema, gitGraphSchema, gitStatusSchema, workspaceRelativePathSchema, } from '@playcraft/common/cloud-workspace/runtime';
|
|
7
|
-
const GIT_DIFF_MAX_BYTES = 1024 * 1024;
|
|
8
|
-
const GIT_PREFIX = [
|
|
9
|
-
'-c',
|
|
10
|
-
'core.hooksPath=/dev/null',
|
|
11
|
-
'-c',
|
|
12
|
-
'commit.gpgsign=false',
|
|
13
|
-
'-c',
|
|
14
|
-
'safe.directory=*',
|
|
15
|
-
];
|
|
16
|
-
export class GitWorkspaceError extends Error {
|
|
17
|
-
code;
|
|
18
|
-
constructor(code, message) {
|
|
19
|
-
super(message);
|
|
20
|
-
this.code = code;
|
|
21
|
-
this.name = 'GitWorkspaceError';
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
export async function probeGitBinary() {
|
|
25
|
-
try {
|
|
26
|
-
const result = await runGitProcess(tmpdir(), ['--version'], 4000);
|
|
27
|
-
return result.code === 0;
|
|
28
|
-
}
|
|
29
|
-
catch (error) {
|
|
30
|
-
return error instanceof GitWorkspaceError && error.code === 'git_binary_missing' ? false : false;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export async function readWorkspaceGitStatus(root) {
|
|
34
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
35
|
-
const branchResult = await gitAllowFail(rootReal, ['symbolic-ref', '--short', 'HEAD']);
|
|
36
|
-
const branch = branchResult.code === 0 ? branchResult.stdout.trim() || 'HEAD' : 'HEAD';
|
|
37
|
-
const upstream = await gitAllowFail(rootReal, ['rev-list', '--left-right', '--count', '@{upstream}...HEAD']);
|
|
38
|
-
let ahead = 0;
|
|
39
|
-
let behind = 0;
|
|
40
|
-
if (upstream.code === 0) {
|
|
41
|
-
const [left, right] = upstream.stdout.trim().split(/\s+/);
|
|
42
|
-
behind = Number.parseInt(left ?? '0', 10) || 0;
|
|
43
|
-
ahead = Number.parseInt(right ?? '0', 10) || 0;
|
|
44
|
-
}
|
|
45
|
-
const porcelain = await git(rootReal, ['status', '--porcelain=v1', '--untracked-files=all', '-z']);
|
|
46
|
-
// Line counts are a convenience; a repository without commits still lists its files.
|
|
47
|
-
const [staged, unstaged] = await Promise.all([
|
|
48
|
-
gitAllowFail(rootReal, ['diff', '--cached', '--numstat', '-z']),
|
|
49
|
-
gitAllowFail(rootReal, ['diff', '--numstat', '-z']),
|
|
50
|
-
]);
|
|
51
|
-
const stagedCounts = staged.code === 0 ? parseNumstat(staged.stdout) : new Map();
|
|
52
|
-
const unstagedCounts = unstaged.code === 0 ? parseNumstat(unstaged.stdout) : new Map();
|
|
53
|
-
return gitStatusSchema.parse({
|
|
54
|
-
initialized: true,
|
|
55
|
-
branch,
|
|
56
|
-
ahead,
|
|
57
|
-
behind,
|
|
58
|
-
hasUpstream: upstream.code === 0,
|
|
59
|
-
files: parsePorcelain(porcelain).map((entry) => ({
|
|
60
|
-
...entry,
|
|
61
|
-
...(stagedCounts.has(entry.path) ? { staged: stagedCounts.get(entry.path) } : {}),
|
|
62
|
-
...(unstagedCounts.has(entry.path) ? { unstaged: unstagedCounts.get(entry.path) } : {}),
|
|
63
|
-
})),
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* One file as stored in the last commit or in the index. Reads the blob as is, without filters,
|
|
68
|
-
* so the browser compares exactly what Git holds.
|
|
69
|
-
*/
|
|
70
|
-
export async function readWorkspaceGitFile(root, path, ref) {
|
|
71
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
72
|
-
assertWorkspacePaths([path]);
|
|
73
|
-
const object = ref === 'HEAD' ? `HEAD:${path}` : `:${path}`;
|
|
74
|
-
const absent = gitFileContentSchema.parse({ path, ref, exists: false, binary: false, content: '' });
|
|
75
|
-
const size = await gitAllowFail(rootReal, ['cat-file', '-s', object]);
|
|
76
|
-
if (size.code !== 0)
|
|
77
|
-
return absent;
|
|
78
|
-
if (Number.parseInt(size.stdout.trim(), 10) > WORKSPACE_FILE_CONTENT_MAX) {
|
|
79
|
-
throw new GitWorkspaceError('git_file_too_large', path);
|
|
80
|
-
}
|
|
81
|
-
const blob = await gitAllowFail(rootReal, ['cat-file', 'blob', object]);
|
|
82
|
-
if (blob.code !== 0)
|
|
83
|
-
return absent;
|
|
84
|
-
// Git treats a NUL byte as the mark of binary content; so does the diff view.
|
|
85
|
-
const binary = blob.stdout.includes('\0');
|
|
86
|
-
return gitFileContentSchema.parse({ path, ref, exists: true, binary, content: binary ? '' : blob.stdout });
|
|
87
|
-
}
|
|
88
|
-
/** Adds the paths to the index, including deletions. */
|
|
89
|
-
export async function stageWorkspaceGit(root, payload) {
|
|
90
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
91
|
-
assertWorkspacePaths(payload.paths);
|
|
92
|
-
await git(rootReal, ['--literal-pathspecs', 'add', '-A', '--', ...payload.paths]);
|
|
93
|
-
}
|
|
94
|
-
/** Returns the paths' index entries to the last commit, leaving the work tree alone. */
|
|
95
|
-
export async function unstageWorkspaceGit(root, payload) {
|
|
96
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
97
|
-
assertWorkspacePaths(payload.paths);
|
|
98
|
-
const head = await gitAllowFail(rootReal, ['rev-parse', '--verify', '--quiet', 'HEAD']);
|
|
99
|
-
if (head.code === 0) {
|
|
100
|
-
await git(rootReal, ['--literal-pathspecs', 'restore', '--staged', '--', ...payload.paths]);
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
// Before the first commit there is nothing to restore from; the entries are simply removed.
|
|
104
|
-
await git(rootReal, ['--literal-pathspecs', 'rm', '--cached', '-r', '-q', '--ignore-unmatch', '--', ...payload.paths]);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Throws away work-tree changes: tracked files return to their staged content, untracked files are
|
|
109
|
-
* deleted. Staged content is never touched, so a staged change survives a discard.
|
|
110
|
-
*/
|
|
111
|
-
export async function discardWorkspaceGit(root, payload) {
|
|
112
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
113
|
-
assertWorkspacePaths(payload.paths);
|
|
114
|
-
const others = await git(rootReal, ['--literal-pathspecs', 'ls-files', '--others', '--exclude-standard', '-z', '--', ...payload.paths]);
|
|
115
|
-
const untracked = new Set(others.split('\0').filter((path) => path.length > 0));
|
|
116
|
-
const tracked = payload.paths.filter((path) => !untracked.has(path));
|
|
117
|
-
if (tracked.length > 0) {
|
|
118
|
-
await git(rootReal, ['--literal-pathspecs', 'restore', '--worktree', '--', ...tracked]);
|
|
119
|
-
}
|
|
120
|
-
for (const path of untracked) {
|
|
121
|
-
const target = resolve(rootReal, path);
|
|
122
|
-
// The parent must resolve inside the workspace, so a symlinked directory cannot redirect the delete.
|
|
123
|
-
assertInsideRoot(rootReal, join(await realpath(dirname(target)), basename(target)));
|
|
124
|
-
if ((await lstat(target)).isDirectory())
|
|
125
|
-
throw new GitWorkspaceError('invalid_workspace_path', path);
|
|
126
|
-
await unlink(target);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function assertWorkspacePaths(paths) {
|
|
130
|
-
for (const relativePath of paths) {
|
|
131
|
-
if (!workspaceRelativePathSchema.safeParse(relativePath).success) {
|
|
132
|
-
throw new GitWorkspaceError('invalid_workspace_path', relativePath);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
export async function readWorkspaceGitDiff(root) {
|
|
137
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
138
|
-
const vsHead = await gitAllowFail(rootReal, ['diff', '--no-color', 'HEAD']);
|
|
139
|
-
const patch = vsHead.code === 0
|
|
140
|
-
? vsHead.stdout
|
|
141
|
-
: `${(await gitAllowFail(rootReal, ['diff', '--no-color'])).stdout}${(await gitAllowFail(rootReal, ['diff', '--no-color', '--cached'])).stdout}`;
|
|
142
|
-
if (Buffer.byteLength(patch, 'utf8') > GIT_DIFF_MAX_BYTES) {
|
|
143
|
-
throw new GitWorkspaceError('git_diff_too_large', '1MiB');
|
|
144
|
-
}
|
|
145
|
-
return gitDiffSchema.parse({ patch });
|
|
146
|
-
}
|
|
147
|
-
export async function readWorkspaceGitGraph(root) {
|
|
148
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
149
|
-
const log = await gitAllowFail(rootReal, ['log', '-n', '20', '--format=%H%x09%s%x09%cI']);
|
|
150
|
-
if (log.code !== 0)
|
|
151
|
-
return gitGraphSchema.parse({ commits: [] });
|
|
152
|
-
const commits = log.stdout
|
|
153
|
-
.split('\n')
|
|
154
|
-
.map((line) => line.trimEnd())
|
|
155
|
-
.filter((line) => line.length > 0)
|
|
156
|
-
.map((line) => {
|
|
157
|
-
const [sha, subject, committedAt] = splitLogLine(line);
|
|
158
|
-
return {
|
|
159
|
-
sha,
|
|
160
|
-
subject: subject.length > 0 ? subject : '(empty)',
|
|
161
|
-
committedAt: committedAt.length > 0 ? committedAt : new Date(0).toISOString(),
|
|
162
|
-
};
|
|
163
|
-
});
|
|
164
|
-
return gitGraphSchema.parse({ commits });
|
|
165
|
-
}
|
|
166
|
-
function splitLogLine(line) {
|
|
167
|
-
const first = line.indexOf('\t');
|
|
168
|
-
if (first < 0)
|
|
169
|
-
return [line, '', ''];
|
|
170
|
-
const second = line.indexOf('\t', first + 1);
|
|
171
|
-
if (second < 0)
|
|
172
|
-
return [line.slice(0, first), line.slice(first + 1), ''];
|
|
173
|
-
return [line.slice(0, first), line.slice(first + 1, second), line.slice(second + 1)];
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Commit and push stay on the branch the workspace is bound to. A switch made in a shell or by an
|
|
177
|
-
* agent is reported, never followed, so work cannot land on another Remix.
|
|
178
|
-
*/
|
|
179
|
-
async function assertBoundBranch(rootReal, boundBranch) {
|
|
180
|
-
if (!boundBranch)
|
|
181
|
-
return;
|
|
182
|
-
const current = await gitAllowFail(rootReal, ['symbolic-ref', '--quiet', '--short', 'HEAD']);
|
|
183
|
-
const branch = current.code === 0 ? current.stdout.trim() : '';
|
|
184
|
-
if (branch !== boundBranch) {
|
|
185
|
-
throw new GitWorkspaceError('workspace_branch_mismatch', branch || 'detached HEAD');
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* A push the remote refused for this ref is a definite outcome, unlike a lost response. Porcelain
|
|
190
|
-
* output marks the ref with "!" and says whether the remote has commits this checkout lacks.
|
|
191
|
-
*/
|
|
192
|
-
/**
|
|
193
|
-
* The gateway or the host refused the push over HTTP before any ref changed: no write access, the
|
|
194
|
-
* gateway's writes switched off, or no such repository. Like a protected-branch refusal, nothing
|
|
195
|
-
* was pushed and the commit is kept.
|
|
196
|
-
*/
|
|
197
|
-
function httpRefusal(stderr) {
|
|
198
|
-
return /returned error: (?:401|403|404)\b|Authentication failed|Access denied|could not read Username|repository '[^']*' not found/i.test(stderr)
|
|
199
|
-
? 'git_push_rejected'
|
|
200
|
-
: undefined;
|
|
201
|
-
}
|
|
202
|
-
function pushRejection(stdout) {
|
|
203
|
-
const line = stdout.split('\n').find((item) => item.startsWith('!\t'));
|
|
204
|
-
if (!line)
|
|
205
|
-
return undefined;
|
|
206
|
-
if (/\[rejected\] \((?:non-fast-forward|fetch first)\)/.test(line))
|
|
207
|
-
return 'git_push_remote_ahead';
|
|
208
|
-
return 'git_push_rejected';
|
|
209
|
-
}
|
|
210
|
-
export async function commitWorkspaceGit(root, payload, boundBranch) {
|
|
211
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
212
|
-
await assertBoundBranch(rootReal, boundBranch);
|
|
213
|
-
if (payload.stagedOnly) {
|
|
214
|
-
// The index is the commit; nothing is added.
|
|
215
|
-
}
|
|
216
|
-
else if (payload.paths && payload.paths.length > 0) {
|
|
217
|
-
assertWorkspacePaths(payload.paths);
|
|
218
|
-
await git(rootReal, ['add', '--', ...payload.paths]);
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
await git(rootReal, ['add', '-A']);
|
|
222
|
-
}
|
|
223
|
-
const cached = await gitAllowFail(rootReal, ['diff', '--cached', '--quiet']);
|
|
224
|
-
if (cached.code === 0) {
|
|
225
|
-
throw new GitWorkspaceError('nothing_to_commit', payload.message);
|
|
226
|
-
}
|
|
227
|
-
await git(rootReal, ['commit', '-m', payload.message], 20000);
|
|
228
|
-
return (await git(rootReal, ['rev-parse', 'HEAD'])).trim();
|
|
229
|
-
}
|
|
230
|
-
export async function pushWorkspaceGit(root, boundBranch) {
|
|
231
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
232
|
-
await assertBoundBranch(rootReal, boundBranch);
|
|
233
|
-
const origin = await gitAllowFail(rootReal, ['remote', 'get-url', 'origin']);
|
|
234
|
-
if (origin.code !== 0) {
|
|
235
|
-
throw new GitWorkspaceError('remote_not_configured', 'origin');
|
|
236
|
-
}
|
|
237
|
-
const ref = (await git(rootReal, ['symbolic-ref', '--quiet', 'HEAD'])).trim();
|
|
238
|
-
if (!ref.startsWith('refs/heads/'))
|
|
239
|
-
throw new GitWorkspaceError('git_command_failed', 'git_branch_required');
|
|
240
|
-
const commit = (await git(rootReal, ['rev-parse', '--verify', 'HEAD^{commit}'])).trim();
|
|
241
|
-
let pushed;
|
|
242
|
-
try {
|
|
243
|
-
pushed = await gitAllowFail(rootReal, ['push', '--porcelain', 'origin', `${commit}:${ref}`], 90000);
|
|
244
|
-
}
|
|
245
|
-
catch { /* A lost response does not prove the remote rejected the update. */ }
|
|
246
|
-
if (pushed?.code === 0)
|
|
247
|
-
return;
|
|
248
|
-
const rejected = pushed ? pushRejection(pushed.stdout) ?? httpRefusal(pushed.stderr) : undefined;
|
|
249
|
-
if (rejected)
|
|
250
|
-
throw new GitWorkspaceError(rejected, rejected);
|
|
251
|
-
try {
|
|
252
|
-
const remote = await gitAllowFail(rootReal, ['ls-remote', '--refs', 'origin', ref], 20000);
|
|
253
|
-
if (remote.code === 0 && remote.stdout.trim() === `${commit}\t${ref}`)
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
catch { /* Preserve uncertainty; never replay a write automatically. */ }
|
|
257
|
-
throw new GitWorkspaceError('git_push_outcome_unknown', 'git_push_outcome_unknown');
|
|
258
|
-
}
|
|
259
|
-
/**
|
|
260
|
-
* Credential files a save never commits, even when `.gitignore` misses them: env files (their
|
|
261
|
-
* `.example` / `.sample` / `.template` stay), SSH keys, netrc and Git credential stores, and key
|
|
262
|
-
* or certificate bundles.
|
|
263
|
-
*/
|
|
264
|
-
const CREDENTIAL_FILE = /^(?:\.env(?:\.(?!(?:example|sample|template)$)[^/]+)?|id_(?:rsa|dsa|ecdsa|ed25519)|\.netrc|\.git-credentials|[^/]*\.(?:pem|key|p12|pfx))$/i;
|
|
265
|
-
export function isCredentialFile(path) {
|
|
266
|
-
return CREDENTIAL_FILE.test(path.split('/').pop() ?? '');
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* Commits every change in the work tree (`git add -A`, credential files aside) and, when asked,
|
|
270
|
-
* pushes the branch. A save with nothing new commits nothing and pushes only what the remote
|
|
271
|
-
* lacks, so a commit whose push failed goes out with the next save.
|
|
272
|
-
*/
|
|
273
|
-
export async function saveWorkspaceGit(root, input) {
|
|
274
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
275
|
-
await assertBoundBranch(rootReal, input.boundBranch);
|
|
276
|
-
await git(rootReal, ['add', '-A'], 60000);
|
|
277
|
-
const staged = (await git(rootReal, ['diff', '--cached', '--name-only', '-z'], 60000)).split('\0').filter(Boolean);
|
|
278
|
-
const withheld = staged.filter(isCredentialFile);
|
|
279
|
-
await unstage(rootReal, withheld);
|
|
280
|
-
const cached = await gitAllowFail(rootReal, ['diff', '--cached', '--quiet'], 60000);
|
|
281
|
-
if (cached.code !== 0 && cached.code !== 1) {
|
|
282
|
-
throw new GitWorkspaceError('git_command_failed', cached.stderr.trim() || 'git_diff_failed');
|
|
283
|
-
}
|
|
284
|
-
let commit;
|
|
285
|
-
if (cached.code === 1) {
|
|
286
|
-
await git(rootReal, [...(await commitIdentity(rootReal)), 'commit', '-q', '-m', input.message], 60000);
|
|
287
|
-
commit = (await git(rootReal, ['rev-parse', 'HEAD'])).trim();
|
|
288
|
-
}
|
|
289
|
-
if (!input.push || (await remoteHasHead(rootReal)))
|
|
290
|
-
return { ...(commit ? { commit } : {}), pushed: false, withheld };
|
|
291
|
-
await pushWorkspaceGit(rootReal, input.boundBranch);
|
|
292
|
-
await recordPushedHead(rootReal);
|
|
293
|
-
return { ...(commit ? { commit } : {}), pushed: true, withheld };
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Connects a standalone checkout to the repository it was just given, keeping all its history and
|
|
297
|
-
* uncommitted work. The new repository holds one empty commit on `branch`; it becomes the base of an
|
|
298
|
-
* unborn checkout, or is merged into existing history, so the next push is a plain fast-forward.
|
|
299
|
-
* Credentials come from the workspace's configured helper and never enter the remote URL.
|
|
300
|
-
* Safe to run again after a failure part way.
|
|
301
|
-
*/
|
|
302
|
-
export async function attachWorkspaceRemote(root, repositoryUrl, branch) {
|
|
303
|
-
const rootReal = await ensureWorkspaceGit(root);
|
|
304
|
-
const origin = await gitAllowFail(rootReal, ['remote', 'get-url', 'origin']);
|
|
305
|
-
if (origin.code !== 0)
|
|
306
|
-
await git(rootReal, ['remote', 'add', 'origin', repositoryUrl]);
|
|
307
|
-
else if (origin.stdout.trim() !== repositoryUrl)
|
|
308
|
-
throw new GitWorkspaceError('remote_not_configured', 'origin_mismatch');
|
|
309
|
-
const remoteRef = `refs/remotes/origin/${branch}`;
|
|
310
|
-
const fetched = await gitAllowFail(rootReal, ['fetch', '--no-tags', 'origin', `+refs/heads/${branch}:${remoteRef}`], 90000);
|
|
311
|
-
if (fetched.code !== 0)
|
|
312
|
-
throw new GitWorkspaceError(httpRefusal(fetched.stderr) ?? 'git_command_failed', 'git_fetch_failed');
|
|
313
|
-
const current = (await gitAllowFail(rootReal, ['symbolic-ref', '--quiet', '--short', 'HEAD'])).stdout.trim();
|
|
314
|
-
const born = (await gitAllowFail(rootReal, ['rev-parse', '-q', '--verify', 'HEAD^{commit}'])).code === 0;
|
|
315
|
-
if (current !== branch) {
|
|
316
|
-
await git(rootReal, born ? ['branch', '-M', branch] : ['symbolic-ref', 'HEAD', `refs/heads/${branch}`]);
|
|
317
|
-
}
|
|
318
|
-
if (!born) {
|
|
319
|
-
// Nothing committed yet: start the branch at the remote commit; the files stay as changes.
|
|
320
|
-
await git(rootReal, ['update-ref', `refs/heads/${branch}`, remoteRef]);
|
|
321
|
-
}
|
|
322
|
-
else if ((await gitAllowFail(rootReal, ['merge-base', '--is-ancestor', remoteRef, 'HEAD'])).code !== 0) {
|
|
323
|
-
const merged = await gitAllowFail(rootReal, [...(await commitIdentity(rootReal)), 'merge', '--allow-unrelated-histories', '--no-edit', remoteRef], 60000);
|
|
324
|
-
if (merged.code !== 0) {
|
|
325
|
-
await gitAllowFail(rootReal, ['merge', '--abort']);
|
|
326
|
-
throw new GitWorkspaceError('git_command_failed', merged.stderr.trim() || 'git_merge_failed');
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
await git(rootReal, ['branch', `--set-upstream-to=origin/${branch}`, branch]);
|
|
330
|
-
}
|
|
331
|
-
async function unstage(rootReal, paths) {
|
|
332
|
-
if (paths.length === 0)
|
|
333
|
-
return;
|
|
334
|
-
const born = (await gitAllowFail(rootReal, ['rev-parse', '-q', '--verify', 'HEAD'])).code === 0;
|
|
335
|
-
for (let start = 0; start < paths.length; start += 100) {
|
|
336
|
-
const chunk = paths.slice(start, start + 100);
|
|
337
|
-
// Before the first commit there is no HEAD to reset to; dropping the entries does the same.
|
|
338
|
-
await git(rootReal, born ? ['reset', '-q', '--', ...chunk] : ['rm', '-q', '--cached', '--', ...chunk]);
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
/** A clone carries no committer; the sandbox image sets none either. */
|
|
342
|
-
async function commitIdentity(rootReal) {
|
|
343
|
-
const email = await gitAllowFail(rootReal, ['config', 'user.email']);
|
|
344
|
-
if (email.code === 0 && email.stdout.trim())
|
|
345
|
-
return [];
|
|
346
|
-
return ['-c', 'user.name=PlayCraft Workspace', '-c', 'user.email=workspace@playcraft.local'];
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* The remote-tracking ref of the current branch. A fork clone has no fetch refspec for its own
|
|
350
|
-
* branch, so the ref is written here after a confirmed push.
|
|
351
|
-
*/
|
|
352
|
-
async function trackingRef(rootReal) {
|
|
353
|
-
const branch = (await gitAllowFail(rootReal, ['symbolic-ref', '--quiet', '--short', 'HEAD'])).stdout.trim();
|
|
354
|
-
return branch ? `refs/remotes/origin/${branch}` : undefined;
|
|
355
|
-
}
|
|
356
|
-
async function remoteHasHead(rootReal) {
|
|
357
|
-
const ref = await trackingRef(rootReal);
|
|
358
|
-
if (!ref)
|
|
359
|
-
return false;
|
|
360
|
-
const [head, tracked] = await Promise.all([
|
|
361
|
-
gitAllowFail(rootReal, ['rev-parse', '-q', '--verify', 'HEAD^{commit}']),
|
|
362
|
-
gitAllowFail(rootReal, ['rev-parse', '-q', '--verify', `${ref}^{commit}`]),
|
|
363
|
-
]);
|
|
364
|
-
return head.code === 0 && tracked.code === 0 && head.stdout.trim() === tracked.stdout.trim();
|
|
365
|
-
}
|
|
366
|
-
async function recordPushedHead(rootReal) {
|
|
367
|
-
const ref = await trackingRef(rootReal);
|
|
368
|
-
if (ref)
|
|
369
|
-
await gitAllowFail(rootReal, ['update-ref', ref, 'HEAD']);
|
|
370
|
-
}
|
|
371
|
-
export async function ensureWorkspaceGit(root) {
|
|
372
|
-
const rootReal = await realpath(root);
|
|
373
|
-
if (!(await gitMarkerExists(rootReal))) {
|
|
374
|
-
const init = await gitAllowFail(rootReal, ['init']);
|
|
375
|
-
if (init.code !== 0) {
|
|
376
|
-
throw new GitWorkspaceError('git_command_failed', init.stderr.trim() || 'git_init_failed');
|
|
377
|
-
}
|
|
378
|
-
await git(rootReal, ['config', 'user.name', 'PlayCraft Workspace']);
|
|
379
|
-
await git(rootReal, ['config', 'user.email', 'workspace@playcraft.local']);
|
|
380
|
-
}
|
|
381
|
-
await assertWorkspaceGitRoot(rootReal);
|
|
382
|
-
await excludeFromGit(rootReal, '/.tmp/user_uploads/');
|
|
383
|
-
return rootReal;
|
|
384
|
-
}
|
|
385
|
-
async function gitMarkerExists(rootReal) {
|
|
386
|
-
try {
|
|
387
|
-
await stat(join(rootReal, '.git'));
|
|
388
|
-
return true;
|
|
389
|
-
}
|
|
390
|
-
catch (error) {
|
|
391
|
-
if (error.code === 'ENOENT')
|
|
392
|
-
return false;
|
|
393
|
-
throw error;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
async function assertWorkspaceGitRoot(rootReal) {
|
|
397
|
-
const marker = join(rootReal, '.git');
|
|
398
|
-
const info = await stat(marker);
|
|
399
|
-
if (info.isDirectory()) {
|
|
400
|
-
assertInsideRoot(rootReal, await realpath(marker));
|
|
401
|
-
return;
|
|
402
|
-
}
|
|
403
|
-
if (info.isFile()) {
|
|
404
|
-
const text = await readFile(marker, 'utf8');
|
|
405
|
-
const match = /^gitdir:\s*(.+)$/m.exec(text);
|
|
406
|
-
if (!match)
|
|
407
|
-
throw new GitWorkspaceError('git_root_escape', 'invalid_gitdir');
|
|
408
|
-
const target = resolve(rootReal, match[1].trim());
|
|
409
|
-
assertInsideRoot(rootReal, await realpath(target));
|
|
410
|
-
return;
|
|
411
|
-
}
|
|
412
|
-
throw new GitWorkspaceError('git_root_escape', marker);
|
|
413
|
-
}
|
|
414
|
-
function assertInsideRoot(rootReal, target) {
|
|
415
|
-
const rel = relative(rootReal, target);
|
|
416
|
-
if (rel === '' || rel.startsWith(`..${sep}`) || rel === '..' || rel.startsWith('..')) {
|
|
417
|
-
throw new GitWorkspaceError('git_root_escape', target);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
function parsePorcelain(raw) {
|
|
421
|
-
const parts = raw.split('\0').filter((part) => part.length > 0);
|
|
422
|
-
const files = [];
|
|
423
|
-
for (let i = 0; i < parts.length; i += 1) {
|
|
424
|
-
const entry = parts[i] ?? '';
|
|
425
|
-
if (entry.length < 3)
|
|
426
|
-
continue;
|
|
427
|
-
const indexStatus = entry[0] ?? ' ';
|
|
428
|
-
const worktreeStatus = entry[1] ?? ' ';
|
|
429
|
-
const pathPart = entry.slice(3);
|
|
430
|
-
if (indexStatus === 'R' || indexStatus === 'C' || worktreeStatus === 'R' || worktreeStatus === 'C') {
|
|
431
|
-
const dest = parts[i + 1] ?? pathPart;
|
|
432
|
-
i += 1;
|
|
433
|
-
files.push({ path: dest, indexStatus, worktreeStatus });
|
|
434
|
-
continue;
|
|
435
|
-
}
|
|
436
|
-
files.push({ path: pathPart, indexStatus, worktreeStatus });
|
|
437
|
-
}
|
|
438
|
-
return files;
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* `diff --numstat -z`: `added<TAB>removed<TAB>path<NUL>`, or for a rename an empty path followed by
|
|
442
|
-
* `old<NUL>new<NUL>`. Binary files report `-` and are left out.
|
|
443
|
-
*/
|
|
444
|
-
function parseNumstat(raw) {
|
|
445
|
-
const counts = new Map();
|
|
446
|
-
const parts = raw.split('\0');
|
|
447
|
-
for (let index = 0; index < parts.length; index += 1) {
|
|
448
|
-
const match = /^(\d+|-)\t(\d+|-)\t(.*)$/s.exec(parts[index] ?? '');
|
|
449
|
-
if (!match)
|
|
450
|
-
continue;
|
|
451
|
-
let path = match[3];
|
|
452
|
-
if (path === '') {
|
|
453
|
-
path = parts[index + 2] ?? '';
|
|
454
|
-
index += 2;
|
|
455
|
-
}
|
|
456
|
-
if (match[1] === '-' || !path)
|
|
457
|
-
continue;
|
|
458
|
-
counts.set(path, { added: Number(match[1]), removed: Number(match[2]) });
|
|
459
|
-
}
|
|
460
|
-
return counts;
|
|
461
|
-
}
|
|
462
|
-
async function git(root, args, timeoutMs = 15000) {
|
|
463
|
-
const result = await gitAllowFail(root, args, timeoutMs);
|
|
464
|
-
if (result.code !== 0) {
|
|
465
|
-
throw new GitWorkspaceError('git_command_failed', result.stderr.trim() || `git_exit_${result.code}`);
|
|
466
|
-
}
|
|
467
|
-
return result.stdout;
|
|
468
|
-
}
|
|
469
|
-
async function gitAllowFail(root, args, timeoutMs = 15000) {
|
|
470
|
-
return runGitProcess(root, [...GIT_PREFIX, `--git-dir=${join(root, '.git')}`, `--work-tree=${root}`, ...args], timeoutMs);
|
|
471
|
-
}
|
|
472
|
-
function gitEnv() {
|
|
473
|
-
const env = { ...process.env };
|
|
474
|
-
delete env.GIT_DIR;
|
|
475
|
-
delete env.GIT_WORK_TREE;
|
|
476
|
-
delete env.GIT_COMMON_DIR;
|
|
477
|
-
delete env.GIT_INDEX_FILE;
|
|
478
|
-
delete env.GIT_OBJECT_DIRECTORY;
|
|
479
|
-
delete env.GIT_ALTERNATE_OBJECT_DIRECTORIES;
|
|
480
|
-
env.GIT_OPTIONAL_LOCKS = '0';
|
|
481
|
-
env.GIT_TERMINAL_PROMPT = '0';
|
|
482
|
-
return env;
|
|
483
|
-
}
|
|
484
|
-
function runGitProcess(cwd, args, timeoutMs) {
|
|
485
|
-
return new Promise((resolve, reject) => {
|
|
486
|
-
const child = spawn('git', args, { cwd, env: gitEnv(), windowsHide: true });
|
|
487
|
-
// Chunks are decoded together; a character split across two chunks would otherwise be corrupted.
|
|
488
|
-
const stdout = [];
|
|
489
|
-
const stderr = [];
|
|
490
|
-
const timer = setTimeout(() => {
|
|
491
|
-
child.kill('SIGKILL');
|
|
492
|
-
reject(new GitWorkspaceError('git_command_failed', 'timeout'));
|
|
493
|
-
}, timeoutMs);
|
|
494
|
-
child.stdout.on('data', (chunk) => {
|
|
495
|
-
stdout.push(Buffer.from(chunk));
|
|
496
|
-
});
|
|
497
|
-
child.stderr.on('data', (chunk) => {
|
|
498
|
-
stderr.push(Buffer.from(chunk));
|
|
499
|
-
});
|
|
500
|
-
child.on('error', (error) => {
|
|
501
|
-
clearTimeout(timer);
|
|
502
|
-
if (error.code === 'ENOENT') {
|
|
503
|
-
reject(new GitWorkspaceError('git_binary_missing', 'git'));
|
|
504
|
-
return;
|
|
505
|
-
}
|
|
506
|
-
reject(error);
|
|
507
|
-
});
|
|
508
|
-
child.on('close', (code) => {
|
|
509
|
-
clearTimeout(timer);
|
|
510
|
-
resolve({
|
|
511
|
-
stdout: Buffer.concat(stdout).toString('utf8'),
|
|
512
|
-
stderr: Buffer.concat(stderr).toString('utf8'),
|
|
513
|
-
code: code ?? 1,
|
|
514
|
-
});
|
|
515
|
-
});
|
|
516
|
-
});
|
|
517
|
-
}
|