@statewalker/vcs-commands 0.5.0 → 0.7.0
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 +123 -8
- package/dist/commands/add-command.d.ts +265 -34
- package/dist/commands/add-command.d.ts.map +1 -1
- package/dist/commands/add-patch/add-interactive.d.ts +46 -0
- package/dist/commands/add-patch/add-interactive.d.ts.map +1 -0
- package/dist/commands/add-patch/add-patch.d.ts +139 -0
- package/dist/commands/add-patch/add-patch.d.ts.map +1 -0
- package/dist/commands/add-patch/index.d.ts +6 -0
- package/dist/commands/add-patch/index.d.ts.map +1 -0
- package/dist/commands/add-patch/patch-backend.d.ts +80 -0
- package/dist/commands/add-patch/patch-backend.d.ts.map +1 -0
- package/dist/commands/add-patch/patch-modes.d.ts +71 -0
- package/dist/commands/add-patch/patch-modes.d.ts.map +1 -0
- package/dist/commands/add-patch/patch-session.d.ts +49 -0
- package/dist/commands/add-patch/patch-session.d.ts.map +1 -0
- package/dist/commands/am/am-command.d.ts +175 -0
- package/dist/commands/am/am-command.d.ts.map +1 -0
- package/dist/commands/am/index.d.ts +3 -0
- package/dist/commands/am/index.d.ts.map +1 -0
- package/dist/commands/am/mailinfo.d.ts +58 -0
- package/dist/commands/am/mailinfo.d.ts.map +1 -0
- package/dist/commands/am/patch-format.d.ts +24 -0
- package/dist/commands/am/patch-format.d.ts.map +1 -0
- package/dist/commands/apply/apply-command.d.ts +153 -0
- package/dist/commands/apply/apply-command.d.ts.map +1 -0
- package/dist/commands/apply/apply-engine.d.ts +195 -0
- package/dist/commands/apply/apply-engine.d.ts.map +1 -0
- package/dist/commands/apply/apply-parse.d.ts +86 -0
- package/dist/commands/apply/apply-parse.d.ts.map +1 -0
- package/dist/commands/apply/apply-types.d.ts +70 -0
- package/dist/commands/apply/apply-types.d.ts.map +1 -0
- package/dist/commands/apply/index.d.ts +6 -0
- package/dist/commands/apply/index.d.ts.map +1 -0
- package/dist/commands/apply/latin1.d.ts +32 -0
- package/dist/commands/apply/latin1.d.ts.map +1 -0
- package/dist/commands/apply/whitespace.d.ts +58 -0
- package/dist/commands/apply/whitespace.d.ts.map +1 -0
- package/dist/commands/attributes-source.d.ts +26 -0
- package/dist/commands/attributes-source.d.ts.map +1 -0
- package/dist/commands/backfill-command.d.ts +110 -0
- package/dist/commands/backfill-command.d.ts.map +1 -0
- package/dist/commands/bisect/bisect-checkout.d.ts +64 -0
- package/dist/commands/bisect/bisect-checkout.d.ts.map +1 -0
- package/dist/commands/bisect/bisect-command.d.ts +201 -0
- package/dist/commands/bisect/bisect-command.d.ts.map +1 -0
- package/dist/commands/bisect/bisect-engine.d.ts +219 -0
- package/dist/commands/bisect/bisect-engine.d.ts.map +1 -0
- package/dist/commands/bisect/bisect-graph.d.ts +127 -0
- package/dist/commands/bisect/bisect-graph.d.ts.map +1 -0
- package/dist/commands/bisect/bisect-quote.d.ts +27 -0
- package/dist/commands/bisect/bisect-quote.d.ts.map +1 -0
- package/dist/commands/bisect/index.d.ts +5 -0
- package/dist/commands/bisect/index.d.ts.map +1 -0
- package/dist/commands/blame-command.d.ts +122 -131
- package/dist/commands/blame-command.d.ts.map +1 -1
- package/dist/commands/branch-command.d.ts +52 -2
- package/dist/commands/branch-command.d.ts.map +1 -1
- package/dist/commands/branch-tracking.d.ts +98 -0
- package/dist/commands/branch-tracking.d.ts.map +1 -0
- package/dist/commands/checkout-command.d.ts +300 -18
- package/dist/commands/checkout-command.d.ts.map +1 -1
- package/dist/commands/checkout-paths.d.ts +124 -0
- package/dist/commands/checkout-paths.d.ts.map +1 -0
- package/dist/commands/cherry-pick-command.d.ts +50 -42
- package/dist/commands/cherry-pick-command.d.ts.map +1 -1
- package/dist/commands/clean-command.d.ts +49 -16
- package/dist/commands/clean-command.d.ts.map +1 -1
- package/dist/commands/clone-command.d.ts +134 -0
- package/dist/commands/clone-command.d.ts.map +1 -1
- package/dist/commands/comment-char-config.d.ts +35 -0
- package/dist/commands/comment-char-config.d.ts.map +1 -0
- package/dist/commands/commit-command.d.ts +311 -17
- package/dist/commands/commit-command.d.ts.map +1 -1
- package/dist/commands/core-config.d.ts +78 -1
- package/dist/commands/core-config.d.ts.map +1 -1
- package/dist/commands/date-format.d.ts +58 -0
- package/dist/commands/date-format.d.ts.map +1 -0
- package/dist/commands/default-branch.d.ts +70 -0
- package/dist/commands/default-branch.d.ts.map +1 -0
- package/dist/commands/describe-command.d.ts.map +1 -1
- package/dist/commands/diff-command.d.ts +377 -30
- package/dist/commands/diff-command.d.ts.map +1 -1
- package/dist/commands/diff-textconv.d.ts +40 -0
- package/dist/commands/diff-textconv.d.ts.map +1 -0
- package/dist/commands/fetch-command.d.ts +149 -10
- package/dist/commands/fetch-command.d.ts.map +1 -1
- package/dist/commands/format-patch-command.d.ts +164 -0
- package/dist/commands/format-patch-command.d.ts.map +1 -0
- package/dist/commands/fsck/fetch-fsck.d.ts +60 -0
- package/dist/commands/fsck/fetch-fsck.d.ts.map +1 -0
- package/dist/commands/fsck/fsck-command.d.ts +227 -0
- package/dist/commands/fsck/fsck-command.d.ts.map +1 -0
- package/dist/commands/fsck/fsck-loose.d.ts +71 -0
- package/dist/commands/fsck/fsck-loose.d.ts.map +1 -0
- package/dist/commands/fsck/fsck-msg-ids.d.ts +122 -0
- package/dist/commands/fsck/fsck-msg-ids.d.ts.map +1 -0
- package/dist/commands/fsck/fsck-object-checks.d.ts +159 -0
- package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -0
- package/dist/commands/fsck/fsck-object-table.d.ts +140 -0
- package/dist/commands/fsck/fsck-object-table.d.ts.map +1 -0
- package/dist/commands/fsck/fsck-pack.d.ts +122 -0
- package/dist/commands/fsck/fsck-pack.d.ts.map +1 -0
- package/dist/commands/fsck/index.d.ts +6 -0
- package/dist/commands/fsck/index.d.ts.map +1 -0
- package/dist/commands/fsck/verify-pack-command.d.ts +81 -0
- package/dist/commands/fsck/verify-pack-command.d.ts.map +1 -0
- package/dist/commands/gc-command.d.ts +106 -39
- package/dist/commands/gc-command.d.ts.map +1 -1
- package/dist/commands/git-date.d.ts +2 -0
- package/dist/commands/git-date.d.ts.map +1 -0
- package/dist/commands/git-describe.d.ts +67 -0
- package/dist/commands/git-describe.d.ts.map +1 -0
- package/dist/commands/git-hooks.d.ts +424 -0
- package/dist/commands/git-hooks.d.ts.map +1 -0
- package/dist/commands/git-ident.d.ts +128 -0
- package/dist/commands/git-ident.d.ts.map +1 -0
- package/dist/commands/hook-command.d.ts +96 -0
- package/dist/commands/hook-command.d.ts.map +1 -0
- package/dist/commands/index-adder.d.ts +233 -0
- package/dist/commands/index-adder.d.ts.map +1 -0
- package/dist/commands/index-host.d.ts +86 -0
- package/dist/commands/index-host.d.ts.map +1 -0
- package/dist/commands/index.d.ts +56 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +3 -2
- package/dist/commands/init-command.d.ts +62 -4
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/legacy-remotes.d.ts +55 -0
- package/dist/commands/legacy-remotes.d.ts.map +1 -0
- package/dist/commands/local-clone.d.ts +60 -0
- package/dist/commands/local-clone.d.ts.map +1 -0
- package/dist/commands/local-transport.d.ts +117 -0
- package/dist/commands/local-transport.d.ts.map +1 -0
- package/dist/commands/log-command.d.ts +216 -24
- package/dist/commands/log-command.d.ts.map +1 -1
- package/dist/commands/log-graph.d.ts +120 -0
- package/dist/commands/log-graph.d.ts.map +1 -0
- package/dist/commands/log-output.d.ts +323 -0
- package/dist/commands/log-output.d.ts.map +1 -0
- package/dist/commands/ls-remote-command.d.ts +23 -7
- package/dist/commands/ls-remote-command.d.ts.map +1 -1
- package/dist/commands/mailmap.d.ts +31 -0
- package/dist/commands/mailmap.d.ts.map +1 -0
- package/dist/commands/merge-bases.d.ts +85 -0
- package/dist/commands/merge-bases.d.ts.map +1 -0
- package/dist/commands/merge-command.d.ts +221 -74
- package/dist/commands/merge-command.d.ts.map +1 -1
- package/dist/commands/merge-config.d.ts +13 -0
- package/dist/commands/merge-config.d.ts.map +1 -0
- package/dist/commands/merge-drivers.d.ts +130 -0
- package/dist/commands/merge-drivers.d.ts.map +1 -0
- package/dist/commands/merge-support.d.ts +260 -0
- package/dist/commands/merge-support.d.ts.map +1 -0
- package/dist/commands/message-cleanup.d.ts +86 -0
- package/dist/commands/message-cleanup.d.ts.map +1 -0
- package/dist/commands/mv-command.d.ts +94 -0
- package/dist/commands/mv-command.d.ts.map +1 -0
- package/dist/commands/notes-command.d.ts +256 -0
- package/dist/commands/notes-command.d.ts.map +1 -0
- package/dist/commands/notes-commands.d.ts +41 -0
- package/dist/commands/notes-commands.d.ts.map +1 -0
- package/dist/commands/notes-merge-command.d.ts +105 -0
- package/dist/commands/notes-merge-command.d.ts.map +1 -0
- package/dist/commands/notes-support.d.ts +125 -0
- package/dist/commands/notes-support.d.ts.map +1 -0
- package/dist/commands/notes-tree.d.ts +116 -0
- package/dist/commands/notes-tree.d.ts.map +1 -0
- package/dist/commands/object-id-input.d.ts +20 -0
- package/dist/commands/object-id-input.d.ts.map +1 -0
- package/dist/commands/octopus-merge.d.ts +59 -0
- package/dist/commands/octopus-merge.d.ts.map +1 -0
- package/dist/commands/ort-merge.d.ts +126 -0
- package/dist/commands/ort-merge.d.ts.map +1 -0
- package/dist/commands/ort-recursive.d.ts +53 -0
- package/dist/commands/ort-recursive.d.ts.map +1 -0
- package/dist/commands/pack-refs-command.d.ts +20 -0
- package/dist/commands/pack-refs-command.d.ts.map +1 -1
- package/dist/commands/patch-id.d.ts +44 -0
- package/dist/commands/patch-id.d.ts.map +1 -0
- package/dist/commands/pathspec-attributes.d.ts +64 -0
- package/dist/commands/pathspec-attributes.d.ts.map +1 -0
- package/dist/commands/pathspec.d.ts +324 -0
- package/dist/commands/pathspec.d.ts.map +1 -0
- package/dist/commands/post-index-change.d.ts +33 -0
- package/dist/commands/post-index-change.d.ts.map +1 -0
- package/dist/commands/pretty-format.d.ts +222 -0
- package/dist/commands/pretty-format.d.ts.map +1 -0
- package/dist/commands/promisor-remote.d.ts +80 -0
- package/dist/commands/promisor-remote.d.ts.map +1 -0
- package/dist/commands/pull-command.d.ts +59 -8
- package/dist/commands/pull-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts +24 -8
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/commands/read-tree-command.d.ts +112 -0
- package/dist/commands/read-tree-command.d.ts.map +1 -0
- package/dist/commands/rebase-command.d.ts +507 -75
- package/dist/commands/rebase-command.d.ts.map +1 -1
- package/dist/commands/rebase-todo.d.ts +130 -0
- package/dist/commands/rebase-todo.d.ts.map +1 -0
- package/dist/commands/ref-decoration-filter.d.ts +53 -0
- package/dist/commands/ref-decoration-filter.d.ts.map +1 -0
- package/dist/commands/reflog-messages.d.ts +98 -0
- package/dist/commands/reflog-messages.d.ts.map +1 -0
- package/dist/commands/remote-command.d.ts +31 -0
- package/dist/commands/remote-command.d.ts.map +1 -1
- package/dist/commands/remote-transport.d.ts +120 -0
- package/dist/commands/remote-transport.d.ts.map +1 -0
- package/dist/commands/reset-command.d.ts +161 -7
- package/dist/commands/reset-command.d.ts.map +1 -1
- package/dist/commands/restore-command.d.ts +103 -0
- package/dist/commands/restore-command.d.ts.map +1 -0
- package/dist/commands/rev-list-command.d.ts +78 -0
- package/dist/commands/rev-list-command.d.ts.map +1 -0
- package/dist/commands/rev-parse-command.d.ts +89 -0
- package/dist/commands/rev-parse-command.d.ts.map +1 -0
- package/dist/commands/revert-command.d.ts +46 -38
- package/dist/commands/revert-command.d.ts.map +1 -1
- package/dist/commands/revision-filenames.d.ts +95 -0
- package/dist/commands/revision-filenames.d.ts.map +1 -0
- package/dist/commands/revision-range.d.ts +67 -0
- package/dist/commands/revision-range.d.ts.map +1 -0
- package/dist/commands/revision-resolver.d.ts +325 -0
- package/dist/commands/revision-resolver.d.ts.map +1 -0
- package/dist/commands/rm-command.d.ts +69 -7
- package/dist/commands/rm-command.d.ts.map +1 -1
- package/dist/commands/sequencer-pick.d.ts +191 -0
- package/dist/commands/sequencer-pick.d.ts.map +1 -0
- package/dist/commands/sequencer-support.d.ts +125 -0
- package/dist/commands/sequencer-support.d.ts.map +1 -0
- package/dist/commands/serve-repository.d.ts +52 -0
- package/dist/commands/serve-repository.d.ts.map +1 -0
- package/dist/commands/sparse-checkout-command.d.ts +190 -0
- package/dist/commands/sparse-checkout-command.d.ts.map +1 -0
- package/dist/commands/sparse-checkout-support.d.ts +268 -0
- package/dist/commands/sparse-checkout-support.d.ts.map +1 -0
- package/dist/commands/stash-apply-command.d.ts +42 -8
- package/dist/commands/stash-apply-command.d.ts.map +1 -1
- package/dist/commands/stash-create-command.d.ts +156 -2
- package/dist/commands/stash-create-command.d.ts.map +1 -1
- package/dist/commands/stash-drop-command.d.ts +2 -0
- package/dist/commands/stash-drop-command.d.ts.map +1 -1
- package/dist/commands/stash-working-tree.d.ts +23 -0
- package/dist/commands/stash-working-tree.d.ts.map +1 -1
- package/dist/commands/status-command.d.ts +300 -15
- package/dist/commands/status-command.d.ts.map +1 -1
- package/dist/commands/status-format.d.ts +155 -0
- package/dist/commands/status-format.d.ts.map +1 -0
- package/dist/commands/status-header.d.ts +64 -0
- package/dist/commands/status-header.d.ts.map +1 -0
- package/dist/commands/strerror.d.ts +7 -0
- package/dist/commands/strerror.d.ts.map +1 -0
- package/dist/commands/submodule-config.d.ts +38 -0
- package/dist/commands/submodule-config.d.ts.map +1 -0
- package/dist/commands/submodule-summary-command.d.ts +80 -0
- package/dist/commands/submodule-summary-command.d.ts.map +1 -0
- package/dist/commands/submodule-summary.d.ts +150 -0
- package/dist/commands/submodule-summary.d.ts.map +1 -0
- package/dist/commands/switch-command.d.ts +132 -0
- package/dist/commands/switch-command.d.ts.map +1 -0
- package/dist/commands/tag-command.d.ts +23 -2
- package/dist/commands/tag-command.d.ts.map +1 -1
- package/dist/commands/tracking-info.d.ts +64 -0
- package/dist/commands/tracking-info.d.ts.map +1 -0
- package/dist/commands/trailers.d.ts +73 -0
- package/dist/commands/trailers.d.ts.map +1 -0
- package/dist/commands/tree-restore.d.ts +85 -1
- package/dist/commands/tree-restore.d.ts.map +1 -1
- package/dist/commands/untracked-cache-support.d.ts +54 -0
- package/dist/commands/untracked-cache-support.d.ts.map +1 -0
- package/dist/commands/worktree-add-command.d.ts +108 -0
- package/dist/commands/worktree-add-command.d.ts.map +1 -0
- package/dist/commands/worktree-list-command.d.ts +69 -0
- package/dist/commands/worktree-list-command.d.ts.map +1 -0
- package/dist/commands/worktree-lock-command.d.ts +44 -0
- package/dist/commands/worktree-lock-command.d.ts.map +1 -0
- package/dist/commands/worktree-overwrite-checks.d.ts +146 -6
- package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
- package/dist/commands/worktree-prune-command.d.ts +48 -0
- package/dist/commands/worktree-prune-command.d.ts.map +1 -0
- package/dist/commands/worktree-remove-command.d.ts +59 -0
- package/dist/commands/worktree-remove-command.d.ts.map +1 -0
- package/dist/commands/worktree-repair-command.d.ts +35 -0
- package/dist/commands/worktree-repair-command.d.ts.map +1 -0
- package/dist/commands/worktree-support.d.ts +64 -0
- package/dist/commands/worktree-support.d.ts.map +1 -0
- package/dist/commands-DIJ91NnD.js +62225 -0
- package/dist/core-commands/add.command.impl.d.ts.map +1 -1
- package/dist/errors/add-errors.d.ts +55 -0
- package/dist/errors/add-errors.d.ts.map +1 -1
- package/dist/errors/checkout-errors.d.ts +16 -0
- package/dist/errors/checkout-errors.d.ts.map +1 -1
- package/dist/errors/command-errors.d.ts +77 -0
- package/dist/errors/command-errors.d.ts.map +1 -1
- package/dist/errors/commit-errors.d.ts +9 -0
- package/dist/errors/commit-errors.d.ts.map +1 -1
- package/dist/errors/git-api-error.d.ts +59 -2
- package/dist/errors/git-api-error.d.ts.map +1 -1
- package/dist/errors/index.d.ts +1 -0
- package/dist/errors/index.d.ts.map +1 -1
- package/dist/errors/index.js +354 -19
- package/dist/errors/rebase-errors.d.ts +9 -0
- package/dist/errors/rebase-errors.d.ts.map +1 -1
- package/dist/errors/ref-errors.d.ts +32 -0
- package/dist/errors/ref-errors.d.ts.map +1 -1
- package/dist/errors/transport-errors.d.ts +12 -2
- package/dist/errors/transport-errors.d.ts.map +1 -1
- package/dist/errors/worktree-errors.d.ts +35 -0
- package/dist/errors/worktree-errors.d.ts.map +1 -0
- package/dist/git-command-BGiOJ8Zk.js +7246 -0
- package/dist/git-command.d.ts +428 -32
- package/dist/git-command.d.ts.map +1 -1
- package/dist/git.d.ts +315 -5
- package/dist/git.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/remote-config/index.d.ts +1 -1
- package/dist/remote-config/index.d.ts.map +1 -1
- package/dist/remote-config/remote-config-store.d.ts +167 -6
- package/dist/remote-config/remote-config-store.d.ts.map +1 -1
- package/dist/remote-config/remote-refs.d.ts +7 -3
- package/dist/remote-config/remote-refs.d.ts.map +1 -1
- package/dist/rename/detect-renames.d.ts +148 -0
- package/dist/rename/detect-renames.d.ts.map +1 -0
- package/dist/rename/git-similarity.d.ts +44 -0
- package/dist/rename/git-similarity.d.ts.map +1 -0
- package/dist/rename/index.d.ts +2 -0
- package/dist/rename/index.d.ts.map +1 -1
- package/dist/results/binary-patch.d.ts +11 -0
- package/dist/results/binary-patch.d.ts.map +1 -0
- package/dist/results/blame-format.d.ts +55 -0
- package/dist/results/blame-format.d.ts.map +1 -0
- package/dist/results/cherry-pick-result.d.ts +10 -0
- package/dist/results/cherry-pick-result.d.ts.map +1 -1
- package/dist/results/clone-result.d.ts +18 -0
- package/dist/results/clone-result.d.ts.map +1 -1
- package/dist/results/combined-diff.d.ts +77 -0
- package/dist/results/combined-diff.d.ts.map +1 -0
- package/dist/results/diff-entry.d.ts +32 -0
- package/dist/results/diff-entry.d.ts.map +1 -1
- package/dist/results/diff-formatter.d.ts +34 -60
- package/dist/results/diff-formatter.d.ts.map +1 -1
- package/dist/results/diff-order.d.ts +25 -0
- package/dist/results/diff-order.d.ts.map +1 -0
- package/dist/results/diff-output.d.ts +249 -0
- package/dist/results/diff-output.d.ts.map +1 -0
- package/dist/results/fetch-result.d.ts +15 -0
- package/dist/results/fetch-result.d.ts.map +1 -1
- package/dist/results/index.d.ts +5 -0
- package/dist/results/index.d.ts.map +1 -1
- package/dist/results/index.js +2 -252
- package/dist/results/init-result.d.ts +7 -0
- package/dist/results/init-result.d.ts.map +1 -1
- package/dist/results/merge-result.d.ts +15 -1
- package/dist/results/merge-result.d.ts.map +1 -1
- package/dist/results/pull-result.d.ts +17 -3
- package/dist/results/pull-result.d.ts.map +1 -1
- package/dist/results/push-result.d.ts +47 -0
- package/dist/results/push-result.d.ts.map +1 -1
- package/dist/results/quote-path.d.ts +27 -0
- package/dist/results/quote-path.d.ts.map +1 -0
- package/dist/results/rebase-result.d.ts +28 -1
- package/dist/results/rebase-result.d.ts.map +1 -1
- package/dist/results/revert-result.d.ts +10 -0
- package/dist/results/revert-result.d.ts.map +1 -1
- package/dist/results/stash-result.d.ts +5 -0
- package/dist/results/stash-result.d.ts.map +1 -1
- package/dist/results/status-result.d.ts +60 -6
- package/dist/results/status-result.d.ts.map +1 -1
- package/dist/results-WVlBhVb1.js +2935 -0
- package/dist/transport-command.d.ts +44 -1
- package/dist/transport-command.d.ts.map +1 -1
- package/package.json +15 -11
- package/src/commands/add-command.ts +1291 -226
- package/src/commands/add-patch/add-interactive.ts +941 -0
- package/src/commands/add-patch/add-patch.ts +1169 -0
- package/src/commands/add-patch/index.ts +5 -0
- package/src/commands/add-patch/patch-backend.ts +239 -0
- package/src/commands/add-patch/patch-modes.ts +249 -0
- package/src/commands/add-patch/patch-session.ts +159 -0
- package/src/commands/am/am-command.ts +1111 -0
- package/src/commands/am/index.ts +2 -0
- package/src/commands/am/mailinfo.ts +1015 -0
- package/src/commands/am/patch-format.ts +121 -0
- package/src/commands/apply/apply-command.ts +842 -0
- package/src/commands/apply/apply-engine.ts +1602 -0
- package/src/commands/apply/apply-parse.ts +1210 -0
- package/src/commands/apply/apply-types.ts +112 -0
- package/src/commands/apply/index.ts +5 -0
- package/src/commands/apply/latin1.ts +171 -0
- package/src/commands/apply/whitespace.ts +260 -0
- package/src/commands/attributes-source.ts +96 -0
- package/src/commands/backfill-command.ts +694 -0
- package/src/commands/bisect/bisect-checkout.ts +217 -0
- package/src/commands/bisect/bisect-command.ts +788 -0
- package/src/commands/bisect/bisect-engine.ts +1080 -0
- package/src/commands/bisect/bisect-graph.ts +595 -0
- package/src/commands/bisect/bisect-quote.ts +92 -0
- package/src/commands/bisect/index.ts +4 -0
- package/src/commands/blame-command.ts +711 -986
- package/src/commands/branch-command.ts +354 -32
- package/src/commands/branch-tracking.ts +331 -0
- package/src/commands/checkout-command.ts +1290 -243
- package/src/commands/checkout-paths.ts +784 -0
- package/src/commands/cherry-pick-command.ts +156 -396
- package/src/commands/clean-command.ts +504 -156
- package/src/commands/clone-command.ts +748 -69
- package/src/commands/comment-char-config.ts +160 -0
- package/src/commands/commit-command.ts +1782 -166
- package/src/commands/core-config.ts +166 -6
- package/src/commands/date-format.ts +668 -0
- package/src/commands/default-branch.ts +196 -0
- package/src/commands/describe-command.ts +2 -0
- package/src/commands/diff-command.ts +1916 -110
- package/src/commands/diff-textconv.ts +50 -0
- package/src/commands/fetch-command.ts +622 -68
- package/src/commands/format-patch-command.ts +974 -0
- package/src/commands/fsck/fetch-fsck.ts +229 -0
- package/src/commands/fsck/fsck-command.ts +3464 -0
- package/src/commands/fsck/fsck-loose.ts +297 -0
- package/src/commands/fsck/fsck-msg-ids.ts +176 -0
- package/src/commands/fsck/fsck-object-checks.ts +1085 -0
- package/src/commands/fsck/fsck-object-table.ts +373 -0
- package/src/commands/fsck/fsck-pack.ts +634 -0
- package/src/commands/fsck/index.ts +5 -0
- package/src/commands/fsck/verify-pack-command.ts +252 -0
- package/src/commands/gc-command.ts +324 -77
- package/src/commands/git-date.ts +9 -0
- package/src/commands/git-describe.ts +316 -0
- package/src/commands/git-hooks.ts +1483 -0
- package/src/commands/git-ident.ts +344 -0
- package/src/commands/hook-command.ts +247 -0
- package/src/commands/index-adder.ts +711 -0
- package/src/commands/index-host.ts +291 -0
- package/src/commands/index.ts +84 -0
- package/src/commands/init-command.ts +194 -14
- package/src/commands/legacy-remotes.ts +224 -0
- package/src/commands/local-clone.ts +183 -0
- package/src/commands/local-transport.ts +762 -0
- package/src/commands/log-command.ts +1930 -170
- package/src/commands/log-graph.ts +671 -0
- package/src/commands/log-output.ts +1359 -0
- package/src/commands/ls-remote-command.ts +105 -58
- package/src/commands/mailmap.ts +129 -0
- package/src/commands/merge-bases.ts +296 -0
- package/src/commands/merge-command.ts +1416 -491
- package/src/commands/merge-config.ts +25 -0
- package/src/commands/merge-drivers.ts +306 -0
- package/src/commands/merge-support.ts +1219 -0
- package/src/commands/message-cleanup.ts +275 -0
- package/src/commands/mv-command.ts +976 -0
- package/src/commands/notes-command.ts +890 -0
- package/src/commands/notes-commands.ts +87 -0
- package/src/commands/notes-merge-command.ts +777 -0
- package/src/commands/notes-support.ts +402 -0
- package/src/commands/notes-tree.ts +617 -0
- package/src/commands/object-id-input.ts +27 -0
- package/src/commands/octopus-merge.ts +378 -0
- package/src/commands/ort-merge.ts +1660 -0
- package/src/commands/ort-recursive.ts +160 -0
- package/src/commands/pack-refs-command.ts +39 -22
- package/src/commands/patch-id.ts +204 -0
- package/src/commands/pathspec-attributes.ts +162 -0
- package/src/commands/pathspec.ts +1199 -0
- package/src/commands/post-index-change.ts +62 -0
- package/src/commands/pretty-format.ts +1432 -0
- package/src/commands/promisor-remote.ts +298 -0
- package/src/commands/pull-command.ts +326 -33
- package/src/commands/push-command.ts +257 -29
- package/src/commands/read-tree-command.ts +861 -0
- package/src/commands/rebase-command.ts +3820 -372
- package/src/commands/rebase-todo.ts +584 -0
- package/src/commands/ref-decoration-filter.ts +118 -0
- package/src/commands/reflog-messages.ts +177 -0
- package/src/commands/remote-command.ts +124 -23
- package/src/commands/remote-transport.ts +383 -0
- package/src/commands/reset-command.ts +821 -104
- package/src/commands/restore-command.ts +350 -0
- package/src/commands/rev-list-command.ts +167 -0
- package/src/commands/rev-parse-command.ts +299 -0
- package/src/commands/revert-command.ts +152 -399
- package/src/commands/revision-filenames.ts +254 -0
- package/src/commands/revision-range.ts +222 -0
- package/src/commands/revision-resolver.ts +1397 -0
- package/src/commands/rm-command.ts +613 -67
- package/src/commands/sequencer-pick.ts +874 -0
- package/src/commands/sequencer-support.ts +876 -0
- package/src/commands/serve-repository.ts +125 -0
- package/src/commands/sparse-checkout-command.ts +1389 -0
- package/src/commands/sparse-checkout-support.ts +793 -0
- package/src/commands/stash-apply-command.ts +376 -343
- package/src/commands/stash-create-command.ts +625 -42
- package/src/commands/stash-drop-command.ts +15 -2
- package/src/commands/stash-working-tree.ts +168 -24
- package/src/commands/status-command.ts +1326 -105
- package/src/commands/status-format.ts +775 -0
- package/src/commands/status-header.ts +332 -0
- package/src/commands/strerror.ts +29 -0
- package/src/commands/submodule-config.ts +94 -0
- package/src/commands/submodule-summary-command.ts +333 -0
- package/src/commands/submodule-summary.ts +446 -0
- package/src/commands/switch-command.ts +402 -0
- package/src/commands/tag-command.ts +129 -41
- package/src/commands/tracking-info.ts +201 -0
- package/src/commands/trailers.ts +417 -0
- package/src/commands/tree-restore.ts +204 -4
- package/src/commands/untracked-cache-support.ts +198 -0
- package/src/commands/worktree-add-command.ts +458 -0
- package/src/commands/worktree-list-command.ts +206 -0
- package/src/commands/worktree-lock-command.ts +96 -0
- package/src/commands/worktree-overwrite-checks.ts +452 -26
- package/src/commands/worktree-prune-command.ts +99 -0
- package/src/commands/worktree-remove-command.ts +186 -0
- package/src/commands/worktree-repair-command.ts +59 -0
- package/src/commands/worktree-support.ts +214 -0
- package/src/core-commands/add.command.impl.ts +8 -2
- package/src/errors/add-errors.ts +45 -0
- package/src/errors/checkout-errors.ts +26 -2
- package/src/errors/command-errors.ts +101 -1
- package/src/errors/commit-errors.ts +18 -2
- package/src/errors/git-api-error.ts +82 -2
- package/src/errors/index.ts +1 -0
- package/src/errors/merge-errors.ts +2 -2
- package/src/errors/rebase-errors.ts +12 -0
- package/src/errors/ref-errors.ts +56 -2
- package/src/errors/remote-errors.ts +2 -2
- package/src/errors/stash-errors.ts +1 -1
- package/src/errors/transport-errors.ts +20 -4
- package/src/errors/worktree-errors.ts +49 -0
- package/src/git-command.ts +1207 -118
- package/src/git.ts +538 -12
- package/src/index.ts +1 -1
- package/src/remote-config/index.ts +2 -0
- package/src/remote-config/remote-config-store.ts +378 -18
- package/src/remote-config/remote-refs.ts +22 -6
- package/src/rename/detect-renames.ts +715 -0
- package/src/rename/git-similarity.ts +116 -0
- package/src/rename/index.ts +2 -0
- package/src/results/binary-patch.ts +401 -0
- package/src/results/blame-format.ts +239 -0
- package/src/results/cherry-pick-result.ts +10 -0
- package/src/results/clone-result.ts +21 -0
- package/src/results/combined-diff.ts +585 -0
- package/src/results/diff-entry.ts +35 -0
- package/src/results/diff-formatter.ts +152 -288
- package/src/results/diff-order.ts +64 -0
- package/src/results/diff-output.ts +1053 -0
- package/src/results/fetch-result.ts +15 -0
- package/src/results/index.ts +5 -0
- package/src/results/init-result.ts +9 -0
- package/src/results/merge-result.ts +15 -1
- package/src/results/pull-result.ts +17 -3
- package/src/results/push-result.ts +48 -0
- package/src/results/quote-path.ts +103 -0
- package/src/results/rebase-result.ts +37 -0
- package/src/results/revert-result.ts +10 -0
- package/src/results/stash-result.ts +6 -0
- package/src/results/status-result.ts +127 -46
- package/src/transport-command.ts +121 -2
- package/dist/commands-CsLNyvZ1.js +0 -12587
- package/dist/status-result-BpyUK1Sg.js +0 -519
|
@@ -0,0 +1,3464 @@
|
|
|
1
|
+
import {
|
|
2
|
+
badPackIntIdAt,
|
|
3
|
+
type CommitGraph,
|
|
4
|
+
commitGraphCompatible,
|
|
5
|
+
type FilesApi,
|
|
6
|
+
type GitObject,
|
|
7
|
+
type GitObjects,
|
|
8
|
+
type GraphCommitData,
|
|
9
|
+
graftFileBaseDir,
|
|
10
|
+
graftFileReader,
|
|
11
|
+
isSymbolicRef,
|
|
12
|
+
loadMultiPackIndex,
|
|
13
|
+
objectFormatOf,
|
|
14
|
+
objectsWithoutLazyFetch,
|
|
15
|
+
openPack,
|
|
16
|
+
type PackFile,
|
|
17
|
+
type PackIndex,
|
|
18
|
+
PromisorObjectReadError,
|
|
19
|
+
parseGraftFile,
|
|
20
|
+
parseGraphCommit,
|
|
21
|
+
type Refs,
|
|
22
|
+
readCommitGraph,
|
|
23
|
+
readPackIndex,
|
|
24
|
+
runSteps,
|
|
25
|
+
type Steps,
|
|
26
|
+
scanPackObjects,
|
|
27
|
+
type Trees,
|
|
28
|
+
verifyCommitGraph,
|
|
29
|
+
verifyReverseIndexFile,
|
|
30
|
+
} from "@statewalker/vcs-core";
|
|
31
|
+
import { multiPackIndexVerify } from "@statewalker/vcs-store-files";
|
|
32
|
+
import { decodeGitPath } from "@statewalker/vcs-utils/encoding";
|
|
33
|
+
import { readFile } from "@statewalker/vcs-utils/files";
|
|
34
|
+
import { isNullObjectId, type ObjectFormat } from "@statewalker/vcs-utils/hash";
|
|
35
|
+
import { bytesToHex } from "@statewalker/vcs-utils/hash/utils";
|
|
36
|
+
import {
|
|
37
|
+
type CacheTree,
|
|
38
|
+
IndexCorruptError,
|
|
39
|
+
parseCacheTree,
|
|
40
|
+
parseResolveUndo,
|
|
41
|
+
type ResolveUndoRecord,
|
|
42
|
+
readIndexFileEntries,
|
|
43
|
+
type Staging,
|
|
44
|
+
} from "@statewalker/vcs-working-tree";
|
|
45
|
+
import { GitCommand } from "../../git-command.js";
|
|
46
|
+
import { RemoteConfigStore } from "../../remote-config/index.js";
|
|
47
|
+
import { hostEnvironmentOf } from "../git-ident.js";
|
|
48
|
+
import { configBool, configValue } from "../merge-config.js";
|
|
49
|
+
import { hasPromisorRemote } from "../promisor-remote.js";
|
|
50
|
+
import { DEFAULT_BIG_FILE_THRESHOLD, readLooseObject } from "./fsck-loose.js";
|
|
51
|
+
import { FSCK_MSG_IDS, FsckMessageTypes, type FsckMsgId } from "./fsck-msg-ids.js";
|
|
52
|
+
import {
|
|
53
|
+
canonMode,
|
|
54
|
+
fsckBlob,
|
|
55
|
+
fsckCommit,
|
|
56
|
+
fsckTag,
|
|
57
|
+
fsckTree,
|
|
58
|
+
hexIdAt,
|
|
59
|
+
parseCommitLinks,
|
|
60
|
+
parseTagBuffer,
|
|
61
|
+
rawIdAt,
|
|
62
|
+
scanTree,
|
|
63
|
+
type TreeEntryTable,
|
|
64
|
+
} from "./fsck-object-checks.js";
|
|
65
|
+
import {
|
|
66
|
+
HAS_OBJ,
|
|
67
|
+
OBJ_BLOB,
|
|
68
|
+
OBJ_COMMIT,
|
|
69
|
+
OBJ_NONE,
|
|
70
|
+
OBJ_TAG,
|
|
71
|
+
OBJ_TREE,
|
|
72
|
+
ObjectTable,
|
|
73
|
+
type ObjType,
|
|
74
|
+
PARSED,
|
|
75
|
+
REACHABLE,
|
|
76
|
+
SEEN,
|
|
77
|
+
TYPE_NAME,
|
|
78
|
+
typeOf,
|
|
79
|
+
USED,
|
|
80
|
+
} from "./fsck-object-table.js";
|
|
81
|
+
import { checkPack, collect, objectIdOf } from "./fsck-pack.js";
|
|
82
|
+
|
|
83
|
+
/** Exit status bits, as `git fsck` returns them (git 2.43's builtin/fsck.c). */
|
|
84
|
+
export const FSCK_ERROR_OBJECT = 0o1;
|
|
85
|
+
export const FSCK_ERROR_REACHABLE = 0o2;
|
|
86
|
+
export const FSCK_ERROR_PACK = 0o4;
|
|
87
|
+
export const FSCK_ERROR_REFS = 0o10;
|
|
88
|
+
/** `git commit-graph verify` failed (git's ERROR_COMMIT_GRAPH). */
|
|
89
|
+
export const FSCK_ERROR_COMMIT_GRAPH = 0o20;
|
|
90
|
+
/** `git multi-pack-index verify` failed, in git 2.43 (its ERROR_MULTI_PACK_INDEX; 2.5x: object bit). */
|
|
91
|
+
export const FSCK_ERROR_MULTI_PACK_INDEX_243 = 0o40;
|
|
92
|
+
/** A pack's `.rev` file is invalid, in git 2.43 (its ERROR_PACK_REV_INDEX; 2.5x: object bit). */
|
|
93
|
+
export const FSCK_ERROR_PACK_REV_INDEX_243 = 0o100;
|
|
94
|
+
/** A bitmap file's checksum is wrong, in git 2.43 (its ERROR_BITMAP; 2.5x sets the object bit). */
|
|
95
|
+
export const FSCK_ERROR_BITMAP_243 = 0o200;
|
|
96
|
+
|
|
97
|
+
/** What one line of fsck's output reports. */
|
|
98
|
+
export type FsckFindingKind =
|
|
99
|
+
/** a reachable object that is not stored (`missing <type> <id>`) */
|
|
100
|
+
| "missing"
|
|
101
|
+
/** an unreachable object nothing points to (`dangling <type> <id>`) */
|
|
102
|
+
| "dangling"
|
|
103
|
+
/** an unreachable object, with `--unreachable` */
|
|
104
|
+
| "unreachable"
|
|
105
|
+
/** a reachable object points to one that is not stored (`broken link from ...`) */
|
|
106
|
+
| "broken-link"
|
|
107
|
+
/** a message of git's object checks (`error in tree <id>: duplicateEntries: ...`) */
|
|
108
|
+
| "object"
|
|
109
|
+
/** `root <id>`, with `--root` */
|
|
110
|
+
| "root"
|
|
111
|
+
/** `tagged <type> <id> (<name>) in <id>`, with `--tags` */
|
|
112
|
+
| "tagged"
|
|
113
|
+
/** anything else git prints with `error()` (corrupt objects and packs, bad refs) */
|
|
114
|
+
| "error"
|
|
115
|
+
/** `notice: ...` and `bad sha1 file: ...` */
|
|
116
|
+
| "notice"
|
|
117
|
+
/** what `git commit-graph verify` prints (fsck runs it unless `core.commitGraph` is false) */
|
|
118
|
+
| "commit-graph"
|
|
119
|
+
/** what `git multi-pack-index verify` prints (unless `core.multiPackIndex` is false) */
|
|
120
|
+
| "multi-pack-index";
|
|
121
|
+
|
|
122
|
+
/** One line of output, as git prints it, with what it is about. */
|
|
123
|
+
export interface FsckFinding {
|
|
124
|
+
kind: FsckFindingKind;
|
|
125
|
+
/** Where git prints it. */
|
|
126
|
+
stream: "stdout" | "stderr";
|
|
127
|
+
/** The line, without its newline. */
|
|
128
|
+
text: string;
|
|
129
|
+
objectId?: string;
|
|
130
|
+
objectType?: string;
|
|
131
|
+
/** The message id of an `object` finding. */
|
|
132
|
+
msgId?: FsckMsgId;
|
|
133
|
+
/** How an `object` finding was reported. */
|
|
134
|
+
severity?: "error" | "warning";
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Result of `git fsck`. */
|
|
138
|
+
export interface FsckResult {
|
|
139
|
+
/** git's exit status: the `FSCK_ERROR_*` bits that apply (0 when nothing is wrong). */
|
|
140
|
+
exitCode: number;
|
|
141
|
+
/** Every line, in the order git prints them. */
|
|
142
|
+
findings: FsckFinding[];
|
|
143
|
+
/** What git prints on stdout. */
|
|
144
|
+
output: string;
|
|
145
|
+
/** What git prints on stderr. */
|
|
146
|
+
errorOutput: string;
|
|
147
|
+
/**
|
|
148
|
+
* The work behind the result: objects read whole from the store (`objectsRead`; the
|
|
149
|
+
* sequential pack checks of a full run are not counted) and, in `--connectivity-only` runs,
|
|
150
|
+
* commits whose tree and parents came from the commit-graph instead (`commitsFromGraph`).
|
|
151
|
+
* A `--connectivity-only` run unpacks the commits and trees of each pack in one read in
|
|
152
|
+
* pack order first (`objectsScanned` of them); `objectsRead` counts the objects the walk
|
|
153
|
+
* takes from that read as well, once each, as it counts those it reads from the store.
|
|
154
|
+
* What is kept in memory from the reads in pack order is bounded (`setMemoryLimits`):
|
|
155
|
+
* `linksKept` tree entries and commit links (the walk reads the others from the store) and
|
|
156
|
+
* `graphCommitsKept` commits for the commit-graph verification (it reads the others).
|
|
157
|
+
*/
|
|
158
|
+
statistics: {
|
|
159
|
+
objectsRead: number;
|
|
160
|
+
commitsFromGraph: number;
|
|
161
|
+
objectsScanned: number;
|
|
162
|
+
linksKept: number;
|
|
163
|
+
graphCommitsKept: number;
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** What a run keeps in memory from the reads in pack order (`FsckCommand.setMemoryLimits`). */
|
|
168
|
+
export interface FsckMemoryLimits {
|
|
169
|
+
/**
|
|
170
|
+
* Tree entries and commit links kept for the walk (4 bytes each), in all: those of the
|
|
171
|
+
* pack-order scan and of the trees the walk records. 32 Mi by default; 0 keeps none.
|
|
172
|
+
*/
|
|
173
|
+
links?: number;
|
|
174
|
+
/** Commits kept for the commit-graph verification: 256 Ki by default. */
|
|
175
|
+
graphCommits?: number;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const DEFAULT_LINKS_KEPT = 32 * 1024 * 1024;
|
|
179
|
+
const DEFAULT_GRAPH_COMMITS_KEPT = 256 * 1024;
|
|
180
|
+
|
|
181
|
+
/** The git version whose output `FsckCommand` reproduces where versions differ. */
|
|
182
|
+
export type FsckCompatibility = "2.43" | "2.56";
|
|
183
|
+
|
|
184
|
+
/** The files of a git repository, for the checks that read them (`setRepositoryFiles`). */
|
|
185
|
+
export interface FsckRepositoryFiles {
|
|
186
|
+
files: FilesApi;
|
|
187
|
+
/** Path of the `.git` directory within `files` (default ".git"). */
|
|
188
|
+
gitDir?: string;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const OID = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/;
|
|
192
|
+
const PACK_IDX = /^pack-(?:[0-9a-f]{40}|[0-9a-f]{64})\.idx$/;
|
|
193
|
+
/** Refs one worktree has of its own (git's `is_per_worktree_ref`). */
|
|
194
|
+
const PER_WORKTREE = /^(?:refs\/(?:bisect|worktree|rewritten)\/|[A-Z_]+$)/;
|
|
195
|
+
const dec = new TextDecoder();
|
|
196
|
+
const EMPTY = new Uint8Array(0);
|
|
197
|
+
|
|
198
|
+
/** A config value git dies on (`bad numeric config value`), as `call` reports it. */
|
|
199
|
+
export class FsckConfigError extends Error {
|
|
200
|
+
override name = "FsckConfigError";
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* A git size setting as `git_config_ulong` parses it: `strtoumax` (leading blanks, an optional
|
|
205
|
+
* `+`, decimal, `0x` hex or `0` octal) then an optional k, m or g. Undefined when unset; a value
|
|
206
|
+
* git dies on throws `FsckConfigError` with git's message.
|
|
207
|
+
*/
|
|
208
|
+
function sizeValue(key: string, value: string | undefined): number | undefined {
|
|
209
|
+
if (value === undefined) return undefined;
|
|
210
|
+
const bad = (why: string) =>
|
|
211
|
+
new FsckConfigError(`bad numeric config value '${value}' for '${key.toLowerCase()}': ${why}`);
|
|
212
|
+
if (value.includes("-")) throw bad("invalid unit");
|
|
213
|
+
const m = /^[ \t\n\v\f\r]*\+?(?:(0[xX])([0-9a-fA-F]+)|(0[0-7]*)|([1-9][0-9]*))(.*)$/s.exec(value);
|
|
214
|
+
if (!m) throw bad("invalid unit");
|
|
215
|
+
const digits = m[2] ?? m[3] ?? (m[4] as string);
|
|
216
|
+
const n =
|
|
217
|
+
m[2] !== undefined
|
|
218
|
+
? BigInt(`0x${digits}`)
|
|
219
|
+
: m[3] !== undefined
|
|
220
|
+
? BigInt(`0o${digits}`)
|
|
221
|
+
: BigInt(digits);
|
|
222
|
+
const unit = ({ "": 1n, k: 1024n, m: 1024n ** 2n, g: 1024n ** 3n } as Record<string, bigint>)[
|
|
223
|
+
(m[5] as string).toLowerCase()
|
|
224
|
+
];
|
|
225
|
+
if (unit === undefined) throw bad("invalid unit");
|
|
226
|
+
if (n * unit > 2n ** 64n - 1n) throw bad("out of range");
|
|
227
|
+
return Number(n * unit);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** A path within the repository's files, as git prints it (`.git/objects/ab/cd...`). */
|
|
231
|
+
const pathOf = (...parts: string[]) => parts.join("/");
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Whether git's parse_commit_buffer reads past a commit's tree line (to its grafts): the data
|
|
235
|
+
* is longer than `tree <40 hex digits>\n`, starts with it, and the digits are hex.
|
|
236
|
+
*/
|
|
237
|
+
function treeLineSound(data: Uint8Array): boolean {
|
|
238
|
+
if (data.length <= 46 || data[45] !== 0x0a) return false;
|
|
239
|
+
for (let i = 0; i < 5; i++) if (data[i] !== "tree ".charCodeAt(i)) return false;
|
|
240
|
+
for (let i = 5; i < 45; i++) {
|
|
241
|
+
const c = data[i] as number;
|
|
242
|
+
if (!((c >= 0x30 && c <= 0x39) || (c >= 0x61 && c <= 0x66) || (c >= 0x41 && c <= 0x46)))
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Verify the connectivity and validity of the objects in the repository.
|
|
250
|
+
*
|
|
251
|
+
* Equivalent to `git fsck --no-references`, after git's `builtin/fsck.c` and `fsck.c` (2.56 by
|
|
252
|
+
* default, 2.43 with `setCompatibility`): every stored object is read and checked (git's
|
|
253
|
+
* message ids, severities and texts, `fsck.<msg-id>` config and `--strict`), then everything reachable from the refs, the reflogs, the index (its
|
|
254
|
+
* entries, cache-tree and resolve-undo), the HEADs of linked worktrees and the stash is
|
|
255
|
+
* walked, and objects that are missing, dangling or (with `--unreachable`) unreachable are
|
|
256
|
+
* listed in git's order.
|
|
257
|
+
*
|
|
258
|
+
* Over a files repository (`setRepositoryFiles`) loose objects are read from their files
|
|
259
|
+
* (zlib streams, headers and hashes checked as git checks them) and packs are verified as
|
|
260
|
+
* git's `verify_pack` does (idx and pack checksums, each entry's CRC-32 and hash); reflogs,
|
|
261
|
+
* index files and linked worktrees are read from `.git`. Over any other store the objects of
|
|
262
|
+
* `history.objects` are read and checked, the reflogs come from `refs.getReflog` and the index
|
|
263
|
+
* from the staging area.
|
|
264
|
+
*
|
|
265
|
+
* Memory holds per object only its type, a few flags and a commit's tree and parents (as git
|
|
266
|
+
* does), and for a tree checked in full the object numbers of its entries (so the connectivity
|
|
267
|
+
* walk does not unpack it again); contents are read one object at a time. Packs are checked in
|
|
268
|
+
* one sequential read each (see `fsck-pack.ts`); `--connectivity-only` inflates no blob (a
|
|
269
|
+
* dangling one's type comes from its pack entry's header).
|
|
270
|
+
*
|
|
271
|
+
* Over a files repository the commit-graph is verified as git's fsck runs `git commit-graph
|
|
272
|
+
* verify` (unless `core.commitGraph` is false): its messages follow the rest on stderr and a
|
|
273
|
+
* failure sets exit bit 16 (`FSCK_ERROR_COMMIT_GRAPH`).
|
|
274
|
+
*
|
|
275
|
+
* In a partial clone (a promisor remote) the promisor objects are taken as git's fsck takes
|
|
276
|
+
* them (`is_promisor_object`: the objects of promisor packs and the objects they name): marked
|
|
277
|
+
* reachable without being walked further, never reported missing, valid as ref and reflog
|
|
278
|
+
* values; nothing is fetched lazily while fsck runs.
|
|
279
|
+
*
|
|
280
|
+
* The multi-pack-index is verified as git runs `git multi-pack-index verify` for each source
|
|
281
|
+
* (unless `core.multiPackIndex` is false): 2.5x after the source's packs (object bit), 2.43 last
|
|
282
|
+
* (exit bit 040); a midx git refuses fatally on load ends fsck with status 128.
|
|
283
|
+
*
|
|
284
|
+
* Not done: the ref database checks of `git refs verify` (git 2.45+),
|
|
285
|
+
* reverse-index verification, the commit-graphs
|
|
286
|
+
* of alternates, `fsck.skipList`, and explicit `<object>` heads. The objects of alternates
|
|
287
|
+
* (`history.objects.alternates`) are read and checked as git's default `--full` reads them
|
|
288
|
+
* (their dangling objects listed too): 2.56 source by source, 2.43 every source's loose
|
|
289
|
+
* objects, then every pack.
|
|
290
|
+
*
|
|
291
|
+
* @example
|
|
292
|
+
* ```typescript
|
|
293
|
+
* const result = await git.fsck().setRepositoryFiles({ files }).setUnreachable(true).call();
|
|
294
|
+
* process.stdout.write(result.output);
|
|
295
|
+
* process.stderr.write(result.errorOutput);
|
|
296
|
+
* process.exitCode = result.exitCode;
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
export class FsckCommand extends GitCommand<FsckResult> {
|
|
300
|
+
private _strict = false;
|
|
301
|
+
|
|
302
|
+
/** git's fsck and gc read the objects as stored, never through replace refs. */
|
|
303
|
+
protected override readsReplacedObjects(): boolean {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
private _connectivityOnly = false;
|
|
307
|
+
private _dangling = true;
|
|
308
|
+
private _unreachable = false;
|
|
309
|
+
private _reflogs = true;
|
|
310
|
+
private _nameObjects = false;
|
|
311
|
+
private _root = false;
|
|
312
|
+
private _tags = false;
|
|
313
|
+
private _lostFound = false;
|
|
314
|
+
private _compatibility: FsckCompatibility = "2.56";
|
|
315
|
+
private _limits: FsckMemoryLimits = {};
|
|
316
|
+
private _repository?: FsckRepositoryFiles;
|
|
317
|
+
private readonly _messageTypes: [string, string][] = [];
|
|
318
|
+
|
|
319
|
+
/** `--strict`: warnings are errors, and mode 100664 a bad file mode. */
|
|
320
|
+
setStrict(strict: boolean): this {
|
|
321
|
+
this.checkCallable();
|
|
322
|
+
this._strict = strict;
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** `--connectivity-only`: check only that reachable objects are there, not their contents. */
|
|
327
|
+
setConnectivityOnly(connectivityOnly: boolean): this {
|
|
328
|
+
this.checkCallable();
|
|
329
|
+
this._connectivityOnly = connectivityOnly;
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** `--[no-]dangling` (on by default): list unreachable objects nothing points to. */
|
|
334
|
+
setDangling(dangling: boolean): this {
|
|
335
|
+
this.checkCallable();
|
|
336
|
+
this._dangling = dangling;
|
|
337
|
+
return this;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** `--unreachable`: list every unreachable object. */
|
|
341
|
+
setUnreachable(unreachable: boolean): this {
|
|
342
|
+
this.checkCallable();
|
|
343
|
+
this._unreachable = unreachable;
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** `--[no-]reflogs` (on by default): reflog entries are roots. */
|
|
348
|
+
setReflogs(reflogs: boolean): this {
|
|
349
|
+
this.checkCallable();
|
|
350
|
+
this._reflogs = reflogs;
|
|
351
|
+
return this;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** `--name-objects`: name reachable objects by how they were reached (`HEAD~2:src/`). */
|
|
355
|
+
setNameObjects(nameObjects: boolean): this {
|
|
356
|
+
this.checkCallable();
|
|
357
|
+
this._nameObjects = nameObjects;
|
|
358
|
+
return this;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** `--root`: report root commits. */
|
|
362
|
+
setRoot(root: boolean): this {
|
|
363
|
+
this.checkCallable();
|
|
364
|
+
this._root = root;
|
|
365
|
+
return this;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** `--tags`: report tags. */
|
|
369
|
+
setTags(tags: boolean): this {
|
|
370
|
+
this.checkCallable();
|
|
371
|
+
this._tags = tags;
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* `--lost-found`: write dangling objects to `.git/lost-found/commit/<id>` and
|
|
377
|
+
* `.git/lost-found/other/<id>` (blobs with their content, others with their id). Needs
|
|
378
|
+
* `setRepositoryFiles`; turns reflogs off, as git does.
|
|
379
|
+
*/
|
|
380
|
+
setLostFound(lostFound: boolean): this {
|
|
381
|
+
this.checkCallable();
|
|
382
|
+
this._lostFound = lostFound;
|
|
383
|
+
return this;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Which git's output to reproduce where versions differ (default `2.56`):
|
|
388
|
+
* - `2.56` (the default) is `git fsck --no-references` of git 2.45+: the refs and worktree
|
|
389
|
+
* HEADs are read (and created in the object hash) before the objects, then their reflogs;
|
|
390
|
+
* HEAD itself is not checked (`HEAD points to an unborn branch`, `HEAD points to something
|
|
391
|
+
* strange`, `detached HEAD points at nothing` and `invalid HEAD` moved to git's
|
|
392
|
+
* `refs verify`, which is not implemented, and which plain `git fsck` runs first); an
|
|
393
|
+
* unparsable object is `<id>: object corrupt or missing`; a bad pack sets bit 1; a tag's
|
|
394
|
+
* `gpgsig` header after the tagger is not an extra header;
|
|
395
|
+
* - `2.43`: refs are read after the objects; HEAD and the HEADs of linked worktrees are
|
|
396
|
+
* checked (`notice: HEAD points to an unborn branch`, `HEAD points to something strange`,
|
|
397
|
+
* `detached HEAD points at nothing`, following symbolic refs to the end of the chain); an
|
|
398
|
+
* object that cannot be parsed is `<id>: object could not be parsed: <path>`; a bad pack
|
|
399
|
+
* sets exit bit 4 (`FSCK_ERROR_PACK`); a tag's `gpgsig` header is an `extraHeaderEntry`.
|
|
400
|
+
*/
|
|
401
|
+
setCompatibility(version: FsckCompatibility): this {
|
|
402
|
+
this.checkCallable();
|
|
403
|
+
this._compatibility = version;
|
|
404
|
+
return this;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Bounds what the run keeps from its reads in pack order (the trees and commits a full run
|
|
409
|
+
* checks, the commits and trees a `--connectivity-only` run unpacks first, the commits the
|
|
410
|
+
* commit-graph verification compares): past them the objects are read from the store again
|
|
411
|
+
* when needed. The output is the same whatever the limits.
|
|
412
|
+
*/
|
|
413
|
+
setMemoryLimits(limits: FsckMemoryLimits): this {
|
|
414
|
+
this.checkCallable();
|
|
415
|
+
this._limits = { ...limits };
|
|
416
|
+
return this;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** `fsck.<msg-id>=<type>` for this run (`error`, `warn` or `ignore`), after the config's. */
|
|
420
|
+
setMessageType(msgId: string, type: string): this {
|
|
421
|
+
this.checkCallable();
|
|
422
|
+
this._messageTypes.push([msgId, type]);
|
|
423
|
+
return this;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* The repository's files: loose objects, packs, reflogs, index files and linked worktrees
|
|
428
|
+
* are then read and checked as git reads them.
|
|
429
|
+
*/
|
|
430
|
+
setRepositoryFiles(repository: FsckRepositoryFiles): this {
|
|
431
|
+
this.checkCallable();
|
|
432
|
+
this._repository = repository;
|
|
433
|
+
return this;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
async call(): Promise<FsckResult> {
|
|
437
|
+
this.checkCallable();
|
|
438
|
+
this.setCallable(false);
|
|
439
|
+
const types = new FsckMessageTypes({ strict: this._strict });
|
|
440
|
+
for (const id of Object.keys(FSCK_MSG_IDS)) {
|
|
441
|
+
const value = configValue(this.workingCopy, `fsck.${id}`);
|
|
442
|
+
if (value !== undefined) types.set(id, value);
|
|
443
|
+
}
|
|
444
|
+
for (const [id, type] of this._messageTypes) types.set(id, type);
|
|
445
|
+
// git dies on a bad value when it reads the config, before it checks anything
|
|
446
|
+
const bigFileThreshold =
|
|
447
|
+
sizeValue("core.bigFileThreshold", configValue(this.workingCopy, "core.bigFileThreshold")) ??
|
|
448
|
+
DEFAULT_BIG_FILE_THRESHOLD;
|
|
449
|
+
const stored = this.workingCopy.history.objects;
|
|
450
|
+
if (!stored) throw new Error("fsck needs the object store (history.objects)");
|
|
451
|
+
// fsck knows how to handle missing promisor objects (fetch_if_missing = 0): in a partial
|
|
452
|
+
// clone it reads through the store's view without lazy fetch
|
|
453
|
+
const partial =
|
|
454
|
+
stored.promisor !== undefined && hasPromisorRemote(RemoteConfigStore.from(this.workingCopy));
|
|
455
|
+
const objects = partial ? objectsWithoutLazyFetch(stored) : stored;
|
|
456
|
+
const stores: RunStores = {
|
|
457
|
+
refs: this.refsStore,
|
|
458
|
+
staging: () => this.staging,
|
|
459
|
+
trees: () => this.storedTrees,
|
|
460
|
+
};
|
|
461
|
+
const promisor = objects.promisor;
|
|
462
|
+
const loadPromised =
|
|
463
|
+
promisor && partial
|
|
464
|
+
? (skipUnreadable?: boolean) => promisor.promisedObjects({ skipUnreadable })
|
|
465
|
+
: undefined;
|
|
466
|
+
const run = new FsckRun(stores, objects, types, {
|
|
467
|
+
...(loadPromised ? { loadPromised } : {}),
|
|
468
|
+
connectivityOnly: this._connectivityOnly,
|
|
469
|
+
dangling: this._dangling,
|
|
470
|
+
unreachable: this._unreachable,
|
|
471
|
+
reflogs: this._reflogs && !this._lostFound,
|
|
472
|
+
nameObjects: this._nameObjects,
|
|
473
|
+
root: this._root,
|
|
474
|
+
tags: this._tags,
|
|
475
|
+
lostFound: this._lostFound,
|
|
476
|
+
compatibility: this._compatibility,
|
|
477
|
+
repository: this._repository,
|
|
478
|
+
// git's fsck reads every source (`--full`, the default): the alternates' objects too
|
|
479
|
+
alternateDirs: this._repository
|
|
480
|
+
? ((await objects.alternates?.list()) ?? []).map((a) => a.objectsDir)
|
|
481
|
+
: [],
|
|
482
|
+
bigFileThreshold,
|
|
483
|
+
linksKept: Math.max(0, this._limits.links ?? DEFAULT_LINKS_KEPT),
|
|
484
|
+
graphCommitsKept: Math.max(0, this._limits.graphCommits ?? DEFAULT_GRAPH_COMMITS_KEPT),
|
|
485
|
+
graftsAdvice: configBool(this.workingCopy, "advice.graftFileDeprecated", true),
|
|
486
|
+
readGraftFile: graftFileReader(
|
|
487
|
+
hostEnvironmentOf(this.workingCopy),
|
|
488
|
+
this._repository
|
|
489
|
+
? {
|
|
490
|
+
files: this._repository.files,
|
|
491
|
+
dir: graftFileBaseDir(
|
|
492
|
+
this._repository.gitDir ?? ".git",
|
|
493
|
+
configBool(this.workingCopy, "core.bare", false) || undefined,
|
|
494
|
+
),
|
|
495
|
+
}
|
|
496
|
+
: this.graftFileBase(),
|
|
497
|
+
),
|
|
498
|
+
multiPackIndex: configBool(this.workingCopy, "core.multiPackIndex", true),
|
|
499
|
+
commitGraph: {
|
|
500
|
+
enabled: configBool(this.workingCopy, "core.commitGraph", true),
|
|
501
|
+
generationVersion:
|
|
502
|
+
intValue(configValue(this.workingCopy, "commitGraph.generationVersion")) ?? 2,
|
|
503
|
+
changedPathsVersion:
|
|
504
|
+
intValue(configValue(this.workingCopy, "commitGraph.changedPathsVersion")) ??
|
|
505
|
+
(configBool(this.workingCopy, "commitGraph.readChangedPaths", true) ? -1 : 0),
|
|
506
|
+
},
|
|
507
|
+
});
|
|
508
|
+
const result = await run.run();
|
|
509
|
+
// git prints the alternates' error lines when it first reads an object: first on stderr
|
|
510
|
+
// (once here; git repeats them in its child processes)
|
|
511
|
+
const alternates = this.alternatesErrors();
|
|
512
|
+
if (alternates.length === 0) return result;
|
|
513
|
+
return {
|
|
514
|
+
...result,
|
|
515
|
+
findings: [
|
|
516
|
+
...alternates.map((text): FsckFinding => ({ kind: "error", stream: "stderr", text })),
|
|
517
|
+
...result.findings,
|
|
518
|
+
],
|
|
519
|
+
errorOutput: `${alternates.map((l) => `${l}\n`).join("")}${result.errorOutput}`,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/** The stores a run reads besides the objects. */
|
|
525
|
+
interface RunStores {
|
|
526
|
+
refs: Refs;
|
|
527
|
+
/** The trees as stored (a sparse index expands from them) */
|
|
528
|
+
trees: () => Pick<Trees, "load" | "store">;
|
|
529
|
+
/** The staging area (throws when the working copy has none). */
|
|
530
|
+
staging: () => Staging;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
interface RunOptions {
|
|
534
|
+
/**
|
|
535
|
+
* A partial clone's promisor objects (git's `is_promisor_object`): reachable without being
|
|
536
|
+
* walked, never missing, valid ref and reflog values; absent outside a partial clone
|
|
537
|
+
*/
|
|
538
|
+
promised?: ReadonlySet<string>;
|
|
539
|
+
/**
|
|
540
|
+
* Reads {@link promised} when the walk first asks (git's `is_promisor_object` builds the
|
|
541
|
+
* set on its first call): an object of a promisor pack that cannot be read is git's death
|
|
542
|
+
* there (2.5x asks for every reachable object; 2.43 only for a missing one, so ours reads
|
|
543
|
+
* no set then)
|
|
544
|
+
*/
|
|
545
|
+
loadPromised?: (skipUnreadable?: boolean) => Promise<ReadonlySet<string>>;
|
|
546
|
+
connectivityOnly: boolean;
|
|
547
|
+
dangling: boolean;
|
|
548
|
+
unreachable: boolean;
|
|
549
|
+
reflogs: boolean;
|
|
550
|
+
nameObjects: boolean;
|
|
551
|
+
root: boolean;
|
|
552
|
+
tags: boolean;
|
|
553
|
+
lostFound: boolean;
|
|
554
|
+
compatibility: FsckCompatibility;
|
|
555
|
+
repository?: FsckRepositoryFiles;
|
|
556
|
+
/** `core.bigFileThreshold`: larger loose blobs are streamed */
|
|
557
|
+
bigFileThreshold: number;
|
|
558
|
+
/** `advice.graftFileDeprecated` (on by default) */
|
|
559
|
+
graftsAdvice: boolean;
|
|
560
|
+
/**
|
|
561
|
+
* The grafts file's text, read in place of `info/grafts` (git's `GIT_GRAFT_FILE`);
|
|
562
|
+
* undefined when that file does not exist. Unset: `info/grafts` is read.
|
|
563
|
+
*/
|
|
564
|
+
readGraftFile?: () => Promise<string | undefined>;
|
|
565
|
+
/** Tree entries and commit links kept from the reads in pack order, at most */
|
|
566
|
+
linksKept: number;
|
|
567
|
+
/** Commits kept for the commit-graph verification, at most */
|
|
568
|
+
graphCommitsKept: number;
|
|
569
|
+
/** `core.commitGraph` and the graph read settings, for `git commit-graph verify` */
|
|
570
|
+
commitGraph: { enabled: boolean; generationVersion: number; changedPathsVersion: number };
|
|
571
|
+
/** `core.multiPackIndex`: fsck runs `git multi-pack-index verify` for each source */
|
|
572
|
+
multiPackIndex?: boolean;
|
|
573
|
+
/** The alternates' objects directories in `repository.files`, in git's order */
|
|
574
|
+
alternateDirs?: readonly string[];
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/** A git integer setting (decimal), undefined when unset or not a number. */
|
|
578
|
+
function intValue(value: string | undefined): number | undefined {
|
|
579
|
+
if (value === undefined || !/^\s*[-+]?\d+\s*$/.test(value)) return undefined;
|
|
580
|
+
return Number.parseInt(value, 10);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/** A pack of the repository, with its idx read (for `has_object_pack`). */
|
|
584
|
+
interface RepoPack {
|
|
585
|
+
packPath: string;
|
|
586
|
+
idxPath: string;
|
|
587
|
+
idx: PackIndex;
|
|
588
|
+
/** The idx file, read once for the listing, the checks and the open */
|
|
589
|
+
idxBytes: Uint8Array;
|
|
590
|
+
/** The object directory it is in: 0 the repository's, then its alternates' */
|
|
591
|
+
source: number;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/** An object read whole. */
|
|
595
|
+
interface Whole {
|
|
596
|
+
type: string;
|
|
597
|
+
content: Uint8Array;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/** The reflog of one ref: its name as git prints it and its entries, oldest first. */
|
|
601
|
+
interface Reflog {
|
|
602
|
+
name: string;
|
|
603
|
+
/** The file it was read from, as git prints it (none when read through the refs store) */
|
|
604
|
+
path?: string;
|
|
605
|
+
entries: { oldId: string; newId: string; timestamp: number }[];
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/** One worktree's state: its HEAD, its reflogs and its index. */
|
|
609
|
+
interface WorktreeState {
|
|
610
|
+
/** `HEAD`, or `worktrees/<id>/HEAD` */
|
|
611
|
+
headName: string;
|
|
612
|
+
/** Where the worktree's files are (the `.git` dir or `.git/worktrees/<id>`) */
|
|
613
|
+
dir?: string;
|
|
614
|
+
current: boolean;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/** One `git fsck` run (builtin/fsck.c's globals). */
|
|
618
|
+
class FsckRun {
|
|
619
|
+
private readonly t: ObjectTable;
|
|
620
|
+
/** The repository's object format: its ids' lengths in trees, commits, tags and names */
|
|
621
|
+
private readonly format: ObjectFormat;
|
|
622
|
+
/** Whether the format is SHA-1 (its own fast hash paths) */
|
|
623
|
+
private readonly sha1: boolean;
|
|
624
|
+
/** A loose object's file name: the id after its fan-out directory (38 or 62 digits) */
|
|
625
|
+
private readonly looseName: RegExp;
|
|
626
|
+
private readonly findings: FsckFinding[] = [];
|
|
627
|
+
private errorsFound = 0;
|
|
628
|
+
private readonly pending: number[] = [];
|
|
629
|
+
private readonly names?: Map<string, string>;
|
|
630
|
+
private defaultRefs = 0;
|
|
631
|
+
private showUnreachable: boolean;
|
|
632
|
+
private readonly gitmodulesFound = new Set<string>();
|
|
633
|
+
private readonly gitmodulesDone = new Set<string>();
|
|
634
|
+
private readonly gitattributesFound = new Set<string>();
|
|
635
|
+
private readonly gitattributesDone = new Set<string>();
|
|
636
|
+
private shallow = new Set<string>();
|
|
637
|
+
/** A partial clone's promisor objects, once read (see `RunOptions.loadPromised`) */
|
|
638
|
+
private promised?: ReadonlySet<string>;
|
|
639
|
+
/** git 2.43's death when the walk first asks about a promisor object (see `run`) */
|
|
640
|
+
private promisorDeathAt?: { at: number; error: PromisorObjectReadError };
|
|
641
|
+
/** the first midx entry whose pack-int-id is out of range (git dies when it reads it) */
|
|
642
|
+
private midxBadEntry?: { id: string; line: string };
|
|
643
|
+
/**
|
|
644
|
+
* The commit grafts of `info/grafts` (git's parse_commit_buffer gives a grafted commit those
|
|
645
|
+
* parents, its own not looked up), and what reading them reported, printed when the first
|
|
646
|
+
* commit is parsed
|
|
647
|
+
*/
|
|
648
|
+
private grafts?: Map<string, string[]>;
|
|
649
|
+
private graftErrors: string[] = [];
|
|
650
|
+
/** The grafts file's errors, as every process that reads the file prints them */
|
|
651
|
+
private graftErrorLines: string[] = [];
|
|
652
|
+
/** Whether there is a grafts file (`info/grafts`, or the one `GIT_GRAFT_FILE` names) */
|
|
653
|
+
private graftFileExists = false;
|
|
654
|
+
private packs: RepoPack[] = [];
|
|
655
|
+
/**
|
|
656
|
+
* Trees checked in full, as their entries' objects: object number -> start in `kids`, where
|
|
657
|
+
* the entry count comes first, then `(object << 1) | isTree` per entry (gitlinks left out).
|
|
658
|
+
* Only for trees whose entries all resolved, in full runs without `--name-objects`, so the
|
|
659
|
+
* connectivity walk need not read them again (git reads them again: a few bytes per entry
|
|
660
|
+
* here against unpacking every tree twice). Indexed by object number, holding start + 1 (0 for
|
|
661
|
+
* a tree not recorded).
|
|
662
|
+
*/
|
|
663
|
+
private treeStart = new Int32Array(0);
|
|
664
|
+
/** The table `walkUsed` decodes each tree into (one tree at a time). */
|
|
665
|
+
private readonly usedTable: TreeEntryTable = { count: 0, fields: new Int32Array(0) };
|
|
666
|
+
/** The table the connectivity walk decodes each tree into. */
|
|
667
|
+
private readonly walkTable: TreeEntryTable = { count: 0, fields: new Int32Array(0) };
|
|
668
|
+
private kids = new Int32Array(0);
|
|
669
|
+
private kidsLength = 0;
|
|
670
|
+
/** The tree `walkUsed` decoded last, for `fsckTree` to check without decoding it again. */
|
|
671
|
+
private lastTree?: { content: Uint8Array; table: TreeEntryTable };
|
|
672
|
+
/** Whether `walkUsed` records the trees it walks (`treeEntries`). */
|
|
673
|
+
private readonly recordTrees: boolean;
|
|
674
|
+
/**
|
|
675
|
+
* Objects with a copy that failed a check: the connectivity walk reads these from the store
|
|
676
|
+
* (which may read that copy), never from `treeEntries`.
|
|
677
|
+
*/
|
|
678
|
+
private readonly badCopy = new Set<string>();
|
|
679
|
+
/** Where an id parsed from hex is written for a lookup. */
|
|
680
|
+
private readonly idScratch: Uint8Array;
|
|
681
|
+
/**
|
|
682
|
+
* The object `parseObject` read last: the connectivity walk of a tree it parsed walks these
|
|
683
|
+
* bytes instead of reading the tree again (git's parse_tree reads it once).
|
|
684
|
+
*/
|
|
685
|
+
private lastRead?: { id: string; obj: Whole };
|
|
686
|
+
/**
|
|
687
|
+
* The commit-graph (connectivity-only runs): git's repo_parse_commit takes a commit's tree
|
|
688
|
+
* and parents from it (parse_commit_in_graph) instead of reading the commit.
|
|
689
|
+
*/
|
|
690
|
+
private graph?: CommitGraph;
|
|
691
|
+
/** Commits parsed from the graph: commits not read from the object store */
|
|
692
|
+
private graphParsed = 0;
|
|
693
|
+
/** Objects read whole from the store (`readWhole`) */
|
|
694
|
+
private objectsRead = 0;
|
|
695
|
+
/** Objects the pack-order scan of a connectivity-only run unpacked. */
|
|
696
|
+
private objectsScanned = 0;
|
|
697
|
+
/**
|
|
698
|
+
* What the pack-order scan (`scanPacks`) found of each commit and tree it unpacked: object
|
|
699
|
+
* number -> start + 1 in `scanKids` (0 for an object not scanned). At the start
|
|
700
|
+
* `(count << 2) | type`, then a tree's entries as `(object << 1) | isTree` (gitlinks left
|
|
701
|
+
* out), or a commit's tree and parents. Only objects whose every link names an object the
|
|
702
|
+
* table holds, so the walk replays the lookups git makes on them in its own order.
|
|
703
|
+
*/
|
|
704
|
+
private scanAt = new Int32Array(0);
|
|
705
|
+
/**
|
|
706
|
+
* Commits as `git commit-graph verify` compares them with the graph, by id, kept from the
|
|
707
|
+
* reads in pack order (the object pass of a full run, the scan of a connectivity-only one)
|
|
708
|
+
* when the repository has a commit-graph: the verification then reads only the others.
|
|
709
|
+
*/
|
|
710
|
+
private graphCommits?: Map<string, GraphCommitData | null>;
|
|
711
|
+
/** Commits `graphCommits` holds (its `null`s left out). */
|
|
712
|
+
private graphCommitsKept = 0;
|
|
713
|
+
private scanKids = new Int32Array(0);
|
|
714
|
+
private scanKidsLength = 0;
|
|
715
|
+
private readonly files?: FilesApi;
|
|
716
|
+
private readonly gitDir: string;
|
|
717
|
+
|
|
718
|
+
constructor(
|
|
719
|
+
private readonly stores: RunStores,
|
|
720
|
+
private readonly objects: GitObjects,
|
|
721
|
+
private readonly types: FsckMessageTypes,
|
|
722
|
+
private readonly o: RunOptions,
|
|
723
|
+
) {
|
|
724
|
+
this.format = objectFormatOf(objects);
|
|
725
|
+
this.sha1 = this.format.name === "sha1";
|
|
726
|
+
this.t = new ObjectTable(this.format.rawLength);
|
|
727
|
+
this.idScratch = new Uint8Array(this.format.rawLength);
|
|
728
|
+
this.looseName = new RegExp(`^[0-9a-f]{${this.format.hexLength - 2}}$`);
|
|
729
|
+
if (o.nameObjects) this.names = new Map();
|
|
730
|
+
this.showUnreachable = o.unreachable;
|
|
731
|
+
this.recordTrees = !o.connectivityOnly && !o.nameObjects;
|
|
732
|
+
this.files = o.repository?.files;
|
|
733
|
+
this.gitDir = (o.repository?.gitDir ?? ".git").replace(/\/+$/, "");
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
/** The commit-graph read settings, in the repository's object format. */
|
|
737
|
+
private get graphSettings() {
|
|
738
|
+
return { ...this.o.commitGraph, objectFormat: this.format };
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
async run(): Promise<FsckResult> {
|
|
742
|
+
// git loads the midx when it first reads a packed object and dies on one it refuses
|
|
743
|
+
// fatally (a wrong signature or version, a required chunk missing; 2.5x: a fan-out out of
|
|
744
|
+
// order too)
|
|
745
|
+
if (this.files && this.o.multiPackIndex !== false) {
|
|
746
|
+
const loaded = await loadMultiPackIndex(this.files, pathOf(this.looseDir(), "pack"), {
|
|
747
|
+
compatibility: this.o.compatibility,
|
|
748
|
+
objectFormat: this.format,
|
|
749
|
+
});
|
|
750
|
+
if (loaded.fatal) return this.death(loaded.errors);
|
|
751
|
+
const bad = loaded.midx && badPackIntIdAt(loaded.midx);
|
|
752
|
+
if (bad && loaded.midx)
|
|
753
|
+
this.midxBadEntry = { id: loaded.midx.idAt(bad.pos), line: `fatal: ${bad.message}` };
|
|
754
|
+
}
|
|
755
|
+
this.graftFileExists = (await this.graftFileText()) !== undefined;
|
|
756
|
+
this.graftsFile = this.graftFileExists && this.o.graftsAdvice;
|
|
757
|
+
this.graftsHint = this.graftsFile;
|
|
758
|
+
if (this.files && this.o.commitGraph.enabled && (await this.hasCommitGraphFile())) {
|
|
759
|
+
this.graphCommits = new Map();
|
|
760
|
+
}
|
|
761
|
+
const refs = this.stores.refs;
|
|
762
|
+
this.shallow = new Set((await refs.getShallow?.()) ?? []);
|
|
763
|
+
await this.readGrafts();
|
|
764
|
+
if (this.files) this.packs = await this.listPacks();
|
|
765
|
+
// git 2.45+: the refs are snapshotted before the objects are read
|
|
766
|
+
const snapshot = this.o.compatibility === "2.56" ? await this.snapshotRefs() : undefined;
|
|
767
|
+
// 2.5x parses each ref's object at the snapshot, through the midx: a tip whose pack-int-id
|
|
768
|
+
// is out of range dies there
|
|
769
|
+
const badMidx = this.midxBadEntry;
|
|
770
|
+
if (snapshot && badMidx && snapshot.some((r) => r.id === badMidx.id))
|
|
771
|
+
return this.death([badMidx.line]);
|
|
772
|
+
|
|
773
|
+
/** connectivity-only: each pack's objects by idx position, and the loose ones */
|
|
774
|
+
let packed: Int32Array[] | undefined;
|
|
775
|
+
let loose: number[] | undefined;
|
|
776
|
+
if (this.o.connectivityOnly) {
|
|
777
|
+
const t = this.t;
|
|
778
|
+
const scan = this.files !== undefined && !this.names && this.packs.length > 0;
|
|
779
|
+
if (scan) {
|
|
780
|
+
packed = this.packs.map((p) => new Int32Array(p.idx.objectCount));
|
|
781
|
+
loose = [];
|
|
782
|
+
}
|
|
783
|
+
await this.forEachStored((raw, at, pack, pos) => {
|
|
784
|
+
// git's lookup_unknown_object
|
|
785
|
+
let n = t.lookupRaw(raw, at);
|
|
786
|
+
if (n < 0) n = t.createRaw(raw, at, OBJ_NONE);
|
|
787
|
+
t.addFlags(n, HAS_OBJ);
|
|
788
|
+
if (packed) {
|
|
789
|
+
if (pack < 0) loose?.push(n);
|
|
790
|
+
else (packed[pack] as Int32Array)[pos] = n;
|
|
791
|
+
}
|
|
792
|
+
});
|
|
793
|
+
} else {
|
|
794
|
+
if (this.files) {
|
|
795
|
+
for (const step of this.sourceSteps()) {
|
|
796
|
+
if ("loose" in step) await this.fsckLooseObjects(step.loose);
|
|
797
|
+
else {
|
|
798
|
+
const packs = step.packs.map((k) => this.packs[k] as RepoPack);
|
|
799
|
+
await this.fsckPacks(packs);
|
|
800
|
+
// 2.5x's verify_reverse_indices, then verify_bitmap_files
|
|
801
|
+
if (this.o.compatibility === "2.56" && (await this.verifyReverseIndexes(packs)))
|
|
802
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
803
|
+
// 2.5x's verify_bitmap_files, after the source's packs (2.43: at the end)
|
|
804
|
+
if (
|
|
805
|
+
this.o.compatibility === "2.56" &&
|
|
806
|
+
(await this.verifyBitmapFiles(packs, step.dir ?? this.looseDir()))
|
|
807
|
+
)
|
|
808
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
809
|
+
// then 2.5x's verify_midx: `git multi-pack-index verify --object-dir <source>`
|
|
810
|
+
if (this.o.compatibility === "2.56" && (await this.verifyMultiPackIndex(step.dir)))
|
|
811
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
} else {
|
|
815
|
+
await this.fsckStoredObjects();
|
|
816
|
+
}
|
|
817
|
+
if (await this.fsckFinish()) this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// is_promisor_object's set, built when the walk from the refs first asks
|
|
821
|
+
this.promised = this.o.promised;
|
|
822
|
+
if (this.o.loadPromised) {
|
|
823
|
+
try {
|
|
824
|
+
const loaded = await this.o.loadPromised();
|
|
825
|
+
this.promised = loaded.size > 0 ? loaded : undefined;
|
|
826
|
+
} catch (error) {
|
|
827
|
+
if (!(error instanceof PromisorObjectReadError)) throw error;
|
|
828
|
+
if (this.o.compatibility === "2.56") {
|
|
829
|
+
for (const pack of this.opened.values()) pack?.close();
|
|
830
|
+
return this.promisorDeath(error);
|
|
831
|
+
}
|
|
832
|
+
// 2.43 asks only for an object that is missing: it dies there, at the first question
|
|
833
|
+
const loaded = await this.o.loadPromised(true);
|
|
834
|
+
const findings = this.findings;
|
|
835
|
+
const run = this;
|
|
836
|
+
this.promised = {
|
|
837
|
+
has(id: string) {
|
|
838
|
+
run.promisorDeathAt ??= { at: findings.length, error };
|
|
839
|
+
return loaded.has(id);
|
|
840
|
+
},
|
|
841
|
+
} as ReadonlySet<string>;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
const walkAt = this.findings.length;
|
|
845
|
+
if (snapshot) await this.processSnapshot(snapshot);
|
|
846
|
+
else await this.processRefs();
|
|
847
|
+
const indexDeath = await this.processIndexes();
|
|
848
|
+
if (indexDeath) {
|
|
849
|
+
for (const pack of this.opened.values()) pack?.close();
|
|
850
|
+
return this.death(indexDeath);
|
|
851
|
+
}
|
|
852
|
+
if (this.o.connectivityOnly) this.graph = await this.loadCommitGraph();
|
|
853
|
+
if (packed) await this.scanPacks(packed, loose ?? []);
|
|
854
|
+
try {
|
|
855
|
+
await this.checkConnectivity();
|
|
856
|
+
// the walk reads through the midx what it parses again (every object but blobs when
|
|
857
|
+
// connectivity only, else the trees, whose buffers git frees once checked): an object of
|
|
858
|
+
// a pack-int-id out of range dies there, after the lines printed before the walk
|
|
859
|
+
if (badMidx) {
|
|
860
|
+
const n = this.t.lookup(badMidx.id);
|
|
861
|
+
if (
|
|
862
|
+
n >= 0 &&
|
|
863
|
+
this.t.flags(n) & REACHABLE &&
|
|
864
|
+
(this.o.connectivityOnly ? this.t.type(n) !== OBJ_BLOB : this.t.type(n) === OBJ_TREE)
|
|
865
|
+
) {
|
|
866
|
+
this.findings.length = walkAt;
|
|
867
|
+
return this.death([badMidx.line]);
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
await this.verifyCommitGraph();
|
|
871
|
+
// 2.43's check_pack_rev_indexes, --connectivity-only too (ERROR_PACK_REV_INDEX, 0100)
|
|
872
|
+
if (
|
|
873
|
+
this.files &&
|
|
874
|
+
this.o.compatibility === "2.43" &&
|
|
875
|
+
(await this.verifyReverseIndexes(this.packs))
|
|
876
|
+
)
|
|
877
|
+
this.errorsFound |= FSCK_ERROR_PACK_REV_INDEX_243;
|
|
878
|
+
// 2.43 checks every pack's bitmap last, --connectivity-only too (ERROR_BITMAP, 0200)
|
|
879
|
+
if (
|
|
880
|
+
this.files &&
|
|
881
|
+
this.o.compatibility === "2.43" &&
|
|
882
|
+
(await this.verifyBitmapFiles(this.packs))
|
|
883
|
+
)
|
|
884
|
+
this.errorsFound |= FSCK_ERROR_BITMAP_243;
|
|
885
|
+
// 2.43 runs `git multi-pack-index verify` for every source last (ERROR_MULTI_PACK_INDEX)
|
|
886
|
+
if (this.files && this.o.compatibility === "2.43") {
|
|
887
|
+
for (const dir of this.sourceDirs())
|
|
888
|
+
if (await this.verifyMultiPackIndex(dir))
|
|
889
|
+
this.errorsFound |= FSCK_ERROR_MULTI_PACK_INDEX_243;
|
|
890
|
+
}
|
|
891
|
+
} finally {
|
|
892
|
+
for (const pack of this.opened.values()) pack?.close();
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
if (this.promisorDeathAt) {
|
|
896
|
+
this.findings.length = this.promisorDeathAt.at;
|
|
897
|
+
return this.promisorDeath(this.promisorDeathAt.error);
|
|
898
|
+
}
|
|
899
|
+
const out = this.findings.filter((f) => f.stream === "stdout").map((f) => `${f.text}\n`);
|
|
900
|
+
const err = this.findings.filter((f) => f.stream === "stderr").map((f) => `${f.text}\n`);
|
|
901
|
+
return {
|
|
902
|
+
exitCode: this.errorsFound,
|
|
903
|
+
findings: this.findings,
|
|
904
|
+
output: out.join(""),
|
|
905
|
+
errorOutput: err.join(""),
|
|
906
|
+
statistics: {
|
|
907
|
+
objectsRead: this.objectsRead,
|
|
908
|
+
commitsFromGraph: this.graphParsed,
|
|
909
|
+
objectsScanned: this.objectsScanned,
|
|
910
|
+
linksKept: this.kidsLength + this.scanKidsLength,
|
|
911
|
+
graphCommitsKept: this.graphCommitsKept,
|
|
912
|
+
},
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* One `git multi-pack-index verify --object-dir <objectsDir>` child of fsck: its stderr
|
|
918
|
+
* follows; true when it failed. Nothing without `core.multiPackIndex` or a FilesApi.
|
|
919
|
+
*/
|
|
920
|
+
private async verifyMultiPackIndex(objectsDir: string | undefined): Promise<boolean> {
|
|
921
|
+
if (!this.files || objectsDir === undefined || this.o.multiPackIndex === false) return false;
|
|
922
|
+
const result = await multiPackIndexVerify({
|
|
923
|
+
files: this.files,
|
|
924
|
+
objectsDir,
|
|
925
|
+
compatibility: this.o.compatibility,
|
|
926
|
+
});
|
|
927
|
+
for (const text of result.stderr)
|
|
928
|
+
this.emit({ kind: "multi-pack-index", stream: "stderr", text });
|
|
929
|
+
return result.exitCode !== 0;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
/** A death with git's lines on stderr: exit 128. */
|
|
933
|
+
private death(lines: readonly string[]): FsckResult {
|
|
934
|
+
for (const text of lines) this.emit({ kind: "error", stream: "stderr", text });
|
|
935
|
+
const err = this.findings.filter((f) => f.stream === "stderr").map((f) => `${f.text}\n`);
|
|
936
|
+
const out = this.findings.filter((f) => f.stream === "stdout").map((f) => `${f.text}\n`);
|
|
937
|
+
return {
|
|
938
|
+
exitCode: 128,
|
|
939
|
+
findings: this.findings,
|
|
940
|
+
output: out.join(""),
|
|
941
|
+
errorOutput: err.join(""),
|
|
942
|
+
statistics: {
|
|
943
|
+
objectsRead: this.objectsRead,
|
|
944
|
+
commitsFromGraph: this.graphParsed,
|
|
945
|
+
objectsScanned: this.objectsScanned,
|
|
946
|
+
linksKept: this.kidsLength + this.scanKidsLength,
|
|
947
|
+
graphCommitsKept: this.graphCommitsKept,
|
|
948
|
+
},
|
|
949
|
+
};
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/** add_promisor_object's death on a promisor pack's unreadable object: exit 128. */
|
|
953
|
+
private promisorDeath(error: PromisorObjectReadError): FsckResult {
|
|
954
|
+
const pack = this.packs.find((p) => p.idx.has(error.objectId));
|
|
955
|
+
const where = pack ? pack.packPath : `pack-${error.packId}.pack`;
|
|
956
|
+
this.emit({
|
|
957
|
+
kind: "error",
|
|
958
|
+
stream: "stderr",
|
|
959
|
+
text: `fatal: packed object ${error.objectId} (stored in ${where}) is corrupt`,
|
|
960
|
+
});
|
|
961
|
+
const out = this.findings.filter((f) => f.stream === "stdout").map((f) => `${f.text}\n`);
|
|
962
|
+
const err = this.findings.filter((f) => f.stream === "stderr").map((f) => `${f.text}\n`);
|
|
963
|
+
return {
|
|
964
|
+
exitCode: 128,
|
|
965
|
+
findings: this.findings,
|
|
966
|
+
output: out.join(""),
|
|
967
|
+
errorOutput: err.join(""),
|
|
968
|
+
statistics: {
|
|
969
|
+
objectsRead: this.objectsRead,
|
|
970
|
+
commitsFromGraph: this.graphParsed,
|
|
971
|
+
objectsScanned: this.objectsScanned,
|
|
972
|
+
linksKept: this.kidsLength + this.scanKidsLength,
|
|
973
|
+
graphCommitsKept: this.graphCommitsKept,
|
|
974
|
+
},
|
|
975
|
+
};
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/**
|
|
979
|
+
* git's fsck runs `git commit-graph verify --object-dir <objects>` for every object
|
|
980
|
+
* directory (the repository's, then each alternate's) when `core.commitGraph` is on: their
|
|
981
|
+
* stderr follows, and a failure sets ERROR_COMMIT_GRAPH.
|
|
982
|
+
*/
|
|
983
|
+
private async verifyCommitGraph(): Promise<void> {
|
|
984
|
+
const settings = this.graphSettings;
|
|
985
|
+
if (!this.files || !settings.enabled) return;
|
|
986
|
+
// fsck itself loads the graph when it parses the commit of a ref (parse_object looks it
|
|
987
|
+
// up there first; fsck disables replace refs): what loading prints comes once from fsck,
|
|
988
|
+
// for each directory read until one has a graph (prepare_commit_graph)
|
|
989
|
+
if (
|
|
990
|
+
(await this.hasRefs()) &&
|
|
991
|
+
(await commitGraphCompatible({
|
|
992
|
+
files: this.files,
|
|
993
|
+
gitDir: this.gitDir,
|
|
994
|
+
useReplaceRefs: false,
|
|
995
|
+
objectFormat: this.format,
|
|
996
|
+
readGraftFile: () => this.graftFileText(),
|
|
997
|
+
}))
|
|
998
|
+
) {
|
|
999
|
+
for (const dir of this.sourceDirs()) {
|
|
1000
|
+
const read = await readCommitGraph(this.files, dir, settings, this.sourceDirs());
|
|
1001
|
+
for (const d of read.diagnostics) {
|
|
1002
|
+
this.emit({ kind: "commit-graph", stream: "stderr", text: `${d.level}: ${d.message}` });
|
|
1003
|
+
}
|
|
1004
|
+
if (read.graph) break;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
for (const dir of this.sourceDirs()) await this.verifySourceCommitGraph(dir);
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/** One `git commit-graph verify --object-dir <objectsDir>` child of fsck. */
|
|
1011
|
+
private async verifySourceCommitGraph(objectsDir: string): Promise<void> {
|
|
1012
|
+
const settings = this.graphSettings;
|
|
1013
|
+
if (!this.files) return;
|
|
1014
|
+
// git's `git commit-graph verify` reads each commit (in the graph's order) from the first
|
|
1015
|
+
// pack of its most-recently-used list that has it: a commit in several packs is read from
|
|
1016
|
+
// the one the commit before it came from, when that one has it too
|
|
1017
|
+
const packs = this.packs;
|
|
1018
|
+
const mru = [...packs];
|
|
1019
|
+
const packOf = (id: string): RepoPack | undefined => {
|
|
1020
|
+
for (let i = 0; i < mru.length; i++) {
|
|
1021
|
+
const p = mru[i] as RepoPack;
|
|
1022
|
+
if (!p.idx.has(id)) continue;
|
|
1023
|
+
if (i > 0) {
|
|
1024
|
+
mru.splice(i, 1);
|
|
1025
|
+
mru.unshift(p);
|
|
1026
|
+
}
|
|
1027
|
+
return p;
|
|
1028
|
+
}
|
|
1029
|
+
return undefined;
|
|
1030
|
+
};
|
|
1031
|
+
// The verification is a process of its own, which reads the grafts file again (and gives
|
|
1032
|
+
// its hint again) when it parses its first commit: parse_commit_buffer looks the commit's
|
|
1033
|
+
// grafts up once the tree line is read, so not for a commit that is missing, of another
|
|
1034
|
+
// type, or without a sound tree line.
|
|
1035
|
+
// Once read, a grafted commit reads with its grafted parents, and the commits it grafts
|
|
1036
|
+
// lose the parents they were given from the graph (register_commit_graft unparses them):
|
|
1037
|
+
// their graph parent lists read as empty.
|
|
1038
|
+
let childGrafts = this.graftFileExists;
|
|
1039
|
+
let graftsRead = false;
|
|
1040
|
+
const reachGrafts = (note: (line: string) => void, data?: Uint8Array) => {
|
|
1041
|
+
if (!childGrafts || (data && !treeLineSound(data))) return;
|
|
1042
|
+
childGrafts = false;
|
|
1043
|
+
graftsRead = true;
|
|
1044
|
+
if (this.graftsFile) for (const line of this.graftsHintLines()) note(line);
|
|
1045
|
+
for (const line of this.graftErrorLines) note(line);
|
|
1046
|
+
};
|
|
1047
|
+
const grafted = (data: GraphCommitData | undefined, id: string) => {
|
|
1048
|
+
const parents = graftsRead && data ? this.graftedParents(id) : undefined;
|
|
1049
|
+
return parents && data ? { ...data, parents } : data;
|
|
1050
|
+
};
|
|
1051
|
+
const result = await verifyCommitGraph(
|
|
1052
|
+
this.files,
|
|
1053
|
+
objectsDir,
|
|
1054
|
+
(id, note) => {
|
|
1055
|
+
const read = this.readVerifiedCommit(id, note, packs, packOf, reachGrafts);
|
|
1056
|
+
return read instanceof Promise ? read.then((d) => grafted(d, id)) : grafted(read, id);
|
|
1057
|
+
},
|
|
1058
|
+
{
|
|
1059
|
+
settings,
|
|
1060
|
+
// the child's layers come from every object directory of the repository
|
|
1061
|
+
objectDirs: this.sourceDirs(),
|
|
1062
|
+
...(this.grafts
|
|
1063
|
+
? {
|
|
1064
|
+
ungraphedParents: (id: string) => graftsRead && this.graftedParents(id) !== undefined,
|
|
1065
|
+
}
|
|
1066
|
+
: {}),
|
|
1067
|
+
},
|
|
1068
|
+
);
|
|
1069
|
+
for (const text of result.messages) this.emit({ kind: "commit-graph", stream: "stderr", text });
|
|
1070
|
+
if (result.exitCode !== 0) this.errorsFound |= FSCK_ERROR_COMMIT_GRAPH;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/** Commit `id` as `git commit-graph verify` reads it from the object database. */
|
|
1074
|
+
private readVerifiedCommit(
|
|
1075
|
+
id: string,
|
|
1076
|
+
note: (line: string) => void,
|
|
1077
|
+
packs: RepoPack[],
|
|
1078
|
+
packOf: (id: string) => RepoPack | undefined,
|
|
1079
|
+
reachGrafts: (note: (line: string) => void, data?: Uint8Array) => void,
|
|
1080
|
+
): GraphCommitData | undefined | Promise<GraphCommitData | undefined> {
|
|
1081
|
+
const objects = this.objects;
|
|
1082
|
+
const from = packs.length > 1 ? packOf(id) : undefined;
|
|
1083
|
+
if (from && from !== packs.find((p) => p.idx.has(id))) {
|
|
1084
|
+
// not the copy kept (the newest pack's): that pack's own
|
|
1085
|
+
return this.readPackedCommit(from, id, (data) => reachGrafts(note, data));
|
|
1086
|
+
}
|
|
1087
|
+
const kept = this.graphCommits?.get(id);
|
|
1088
|
+
if (kept) {
|
|
1089
|
+
reachGrafts(note);
|
|
1090
|
+
return kept;
|
|
1091
|
+
}
|
|
1092
|
+
const parse = (o: GitObject | undefined, data: Uint8Array) => {
|
|
1093
|
+
if (o?.type !== "commit") return undefined;
|
|
1094
|
+
reachGrafts(note, data);
|
|
1095
|
+
return parseGraphCommit(data, id, this.format);
|
|
1096
|
+
};
|
|
1097
|
+
const slow = async (o: GitObject | undefined | Promise<GitObject | undefined>) => {
|
|
1098
|
+
try {
|
|
1099
|
+
const obj = await o;
|
|
1100
|
+
if (!obj) return undefined;
|
|
1101
|
+
return parse(obj, obj.bytes ?? (await collect(obj)));
|
|
1102
|
+
} catch {
|
|
1103
|
+
return undefined;
|
|
1104
|
+
}
|
|
1105
|
+
};
|
|
1106
|
+
try {
|
|
1107
|
+
// without a promise when the store has the commit in memory
|
|
1108
|
+
const o = objects.getNow ? objects.getNow(id) : objects.get(id);
|
|
1109
|
+
if (o instanceof Promise || (o && !o.bytes)) return slow(o);
|
|
1110
|
+
return o ? parse(o, o.bytes as Uint8Array) : undefined;
|
|
1111
|
+
} catch {
|
|
1112
|
+
return undefined;
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
/**
|
|
1117
|
+
* Commit `id` as pack `p` holds it, unchecked, for the commit-graph verification; `parsing`
|
|
1118
|
+
* sees its data before it is parsed.
|
|
1119
|
+
*/
|
|
1120
|
+
private async readPackedCommit(
|
|
1121
|
+
p: RepoPack,
|
|
1122
|
+
id: string,
|
|
1123
|
+
parsing: (data: Uint8Array) => void,
|
|
1124
|
+
): Promise<GraphCommitData | undefined> {
|
|
1125
|
+
try {
|
|
1126
|
+
const pack = await this.openRepoPack(p);
|
|
1127
|
+
const obj = await pack?.get(id);
|
|
1128
|
+
if (obj?.type !== "commit") return undefined;
|
|
1129
|
+
const data = await collect(obj);
|
|
1130
|
+
parsing(data);
|
|
1131
|
+
return parseGraphCommit(data, id, this.format);
|
|
1132
|
+
} catch {
|
|
1133
|
+
return undefined;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
/** Whether the objects directory has a commit-graph file or chain. */
|
|
1138
|
+
private async hasCommitGraphFile(): Promise<boolean> {
|
|
1139
|
+
const files = this.files as FilesApi;
|
|
1140
|
+
const info = pathOf(this.gitDir, "objects", "info");
|
|
1141
|
+
try {
|
|
1142
|
+
if ((await files.stats(pathOf(info, "commit-graph")))?.kind === "file") return true;
|
|
1143
|
+
return (await files.stats(pathOf(info, "commit-graphs")))?.kind === "directory";
|
|
1144
|
+
} catch {
|
|
1145
|
+
return false;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
/**
|
|
1150
|
+
* A commit read in pack order, kept for the commit-graph verification (`graphCommits`), when
|
|
1151
|
+
* this is the copy the verification reads (git's repo_read_object_file: the first pack in
|
|
1152
|
+
* pack order that has it, unchecked): the callers pass each pack's copies in that order and
|
|
1153
|
+
* `skipGraphCommit` for a copy they could not unpack. At most `graphCommitsKept` of them.
|
|
1154
|
+
*/
|
|
1155
|
+
private keepGraphCommit(id: string, content: Uint8Array): void {
|
|
1156
|
+
const map = this.graphCommits;
|
|
1157
|
+
if (!map || this.graphCommitsKept >= this.o.graphCommitsKept || map.has(id)) return;
|
|
1158
|
+
try {
|
|
1159
|
+
map.set(id, parseGraphCommit(content, id, this.format));
|
|
1160
|
+
this.graphCommitsKept++;
|
|
1161
|
+
} catch {
|
|
1162
|
+
// read again (and failed again) by the verification
|
|
1163
|
+
map.set(id, null);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
/** A copy `keepGraphCommit` cannot take: the verification reads the object from the store. */
|
|
1168
|
+
private skipGraphCommit(id: string): void {
|
|
1169
|
+
const map = this.graphCommits;
|
|
1170
|
+
if (map && !map.has(id)) map.set(id, null);
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
/**
|
|
1174
|
+
* The graph git's repo_parse_commit reads commits from: `core.commitGraph` on, a repository
|
|
1175
|
+
* the graph is valid for (no grafts, not shallow; fsck disables replace refs), a graph that
|
|
1176
|
+
* loads. Undefined otherwise: commits are read from the object store.
|
|
1177
|
+
*/
|
|
1178
|
+
private async loadCommitGraph(): Promise<CommitGraph | undefined> {
|
|
1179
|
+
const settings = this.graphSettings;
|
|
1180
|
+
if (!this.files || !settings.enabled) return undefined;
|
|
1181
|
+
try {
|
|
1182
|
+
if (
|
|
1183
|
+
!(await commitGraphCompatible({
|
|
1184
|
+
files: this.files,
|
|
1185
|
+
gitDir: this.gitDir,
|
|
1186
|
+
useReplaceRefs: false,
|
|
1187
|
+
objectFormat: this.format,
|
|
1188
|
+
readGraftFile: () => this.graftFileText(),
|
|
1189
|
+
}))
|
|
1190
|
+
) {
|
|
1191
|
+
return undefined;
|
|
1192
|
+
}
|
|
1193
|
+
// the first graph among the object directories (prepare_commit_graph)
|
|
1194
|
+
for (const dir of this.sourceDirs()) {
|
|
1195
|
+
const graph = (await readCommitGraph(this.files, dir, settings, this.sourceDirs())).graph;
|
|
1196
|
+
if (graph) return graph;
|
|
1197
|
+
}
|
|
1198
|
+
return undefined;
|
|
1199
|
+
} catch {
|
|
1200
|
+
return undefined;
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
/**
|
|
1205
|
+
* git's parse_commit_in_graph for object `n`, a commit not parsed yet: its parents (looked up
|
|
1206
|
+
* first, as fill_commit_in_graph inserts them) and its tree from the graph. False when the
|
|
1207
|
+
* graph does not have it (or names something of another type): the commit is read then.
|
|
1208
|
+
*/
|
|
1209
|
+
private parseCommitInGraph(n: number, id: string): boolean {
|
|
1210
|
+
const g = this.graph as CommitGraph;
|
|
1211
|
+
const t = this.t;
|
|
1212
|
+
const pos = g.positionOfBytes(t.idBytes(n));
|
|
1213
|
+
if (pos < 0) return false;
|
|
1214
|
+
const parents: number[] = [];
|
|
1215
|
+
if (!this.shallow.has(id)) {
|
|
1216
|
+
for (const pp of g.parentPositionsAt(pos)) {
|
|
1217
|
+
const [layer, local] = g.locate(pp);
|
|
1218
|
+
const parent = t.lookupTypedRaw(
|
|
1219
|
+
layer.bytes,
|
|
1220
|
+
layer.oidLookup.offset + 20 * local,
|
|
1221
|
+
OBJ_COMMIT,
|
|
1222
|
+
);
|
|
1223
|
+
if (typeof parent !== "number") return false;
|
|
1224
|
+
parents.push(parent);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
const [layer, local] = g.locate(pos);
|
|
1228
|
+
const tree = t.lookupTypedRaw(layer.bytes, layer.dataOffset(local), OBJ_TREE);
|
|
1229
|
+
if (typeof tree !== "number") return false;
|
|
1230
|
+
t.links.set(n, { tree, parents });
|
|
1231
|
+
t.addFlags(n, PARSED);
|
|
1232
|
+
this.graphParsed++;
|
|
1233
|
+
return true;
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
/**
|
|
1237
|
+
* git's prepare_commit_graft for fsck: the grafts file read (`info/grafts`, or the file
|
|
1238
|
+
* `GIT_GRAFT_FILE` names; fsck applies grafts, as every parse_commit does; replace refs it
|
|
1239
|
+
* turns off), its bad lines kept to be reported.
|
|
1240
|
+
*/
|
|
1241
|
+
private async readGrafts(): Promise<void> {
|
|
1242
|
+
const text = await this.graftFileText();
|
|
1243
|
+
if (text === undefined) return;
|
|
1244
|
+
const { grafts, messages } = parseGraftFile(text);
|
|
1245
|
+
if (grafts.size > 0) this.grafts = grafts;
|
|
1246
|
+
this.graftErrors = messages;
|
|
1247
|
+
this.graftErrorLines = messages;
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
/**
|
|
1251
|
+
* The grafts file's text (git's repo_get_graft_file: `GIT_GRAFT_FILE`, else
|
|
1252
|
+
* `<GIT_DIR>/info/grafts`), read once; undefined when there is none or it cannot be read
|
|
1253
|
+
* (git warns and goes on).
|
|
1254
|
+
*/
|
|
1255
|
+
private graftFileText(): Promise<string | undefined> {
|
|
1256
|
+
if (!this.graftText) {
|
|
1257
|
+
const read = this.o.readGraftFile;
|
|
1258
|
+
const files = this.files;
|
|
1259
|
+
const path = pathOf(this.gitDir, "info", "grafts");
|
|
1260
|
+
this.graftText = (async () => {
|
|
1261
|
+
if (read) return read();
|
|
1262
|
+
if (!files || (await files.stats(path))?.kind !== "file") return undefined;
|
|
1263
|
+
return new TextDecoder().decode(await readFile(files, path));
|
|
1264
|
+
})().catch(() => undefined);
|
|
1265
|
+
}
|
|
1266
|
+
return this.graftText;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
/** The grafts file's text once read (see graftFileText). */
|
|
1270
|
+
private graftText?: Promise<string | undefined>;
|
|
1271
|
+
|
|
1272
|
+
/** The grafted parents of commit `id`; undefined when not grafted (or shallow). */
|
|
1273
|
+
private graftedParents(id: string): string[] | undefined {
|
|
1274
|
+
if (!this.grafts || this.shallow.has(id)) return undefined;
|
|
1275
|
+
return this.grafts.get(id);
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
/** The grafts file's errors (`error: bad graft data: <line>`), once. */
|
|
1279
|
+
private reportGraftErrors(): void {
|
|
1280
|
+
const errors = this.graftErrors;
|
|
1281
|
+
this.graftErrors = [];
|
|
1282
|
+
for (const text of errors) this.emit({ kind: "error", stream: "stderr", text });
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
/** Whether HEAD or any ref names an object. */
|
|
1286
|
+
private async hasRefs(): Promise<boolean> {
|
|
1287
|
+
const refs = this.stores.refs;
|
|
1288
|
+
for await (const ref of refs.list("refs/")) if (!isSymbolicRef(ref)) return true;
|
|
1289
|
+
return (await refs.resolve("HEAD").catch(() => undefined))?.objectId !== undefined;
|
|
1290
|
+
}
|
|
1291
|
+
|
|
1292
|
+
// ------------------------------------------------------------------------------------------
|
|
1293
|
+
// Output
|
|
1294
|
+
|
|
1295
|
+
private emit(finding: FsckFinding): void {
|
|
1296
|
+
this.findings.push(finding);
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
/** Whether the grafts file's deprecation hint is still to be printed (`adviseGrafts`). */
|
|
1300
|
+
private graftsHint = false;
|
|
1301
|
+
/** Whether `<GIT_DIR>/info/grafts` is a file and `advice.graftFileDeprecated` is on. */
|
|
1302
|
+
private graftsFile = false;
|
|
1303
|
+
|
|
1304
|
+
/**
|
|
1305
|
+
* git's read_graft_file, when the first commit is parsed (prepare_commit_graft): the hint
|
|
1306
|
+
* that `<GIT_DIR>/info/grafts` is deprecated, once, as each version words it.
|
|
1307
|
+
*/
|
|
1308
|
+
private adviseGrafts(): void {
|
|
1309
|
+
if (!this.graftsHint) return;
|
|
1310
|
+
this.graftsHint = false;
|
|
1311
|
+
for (const text of this.graftsHintLines())
|
|
1312
|
+
this.emit({ kind: "notice", stream: "stderr", text });
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
/** The grafts file's deprecation hint, as each version words it. */
|
|
1316
|
+
private graftsHintLines(): string[] {
|
|
1317
|
+
const v243 = this.o.compatibility === "2.43";
|
|
1318
|
+
const blank = v243 ? "hint: " : "hint:";
|
|
1319
|
+
const lines = [
|
|
1320
|
+
"hint: Support for <GIT_DIR>/info/grafts is deprecated",
|
|
1321
|
+
"hint: and will be removed in a future Git version.",
|
|
1322
|
+
blank,
|
|
1323
|
+
'hint: Please use "git replace --convert-graft-file"',
|
|
1324
|
+
"hint: to convert the grafts into replace refs.",
|
|
1325
|
+
blank,
|
|
1326
|
+
"hint: Turn this message off by running",
|
|
1327
|
+
v243
|
|
1328
|
+
? 'hint: "git config advice.graftFileDeprecated false"'
|
|
1329
|
+
: 'hint: "git config set advice.graftFileDeprecated false"',
|
|
1330
|
+
];
|
|
1331
|
+
return lines;
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
/** git's `error()`: `error: <message>` on stderr. */
|
|
1335
|
+
private error(message: string, objectId?: string): void {
|
|
1336
|
+
this.emit({ kind: "error", stream: "stderr", text: `error: ${message}`, objectId });
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
private describe(id: string): string {
|
|
1340
|
+
const name = this.names?.get(id);
|
|
1341
|
+
return name === undefined ? id : `${id} (${name})`;
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
private putName(id: string, name: string): void {
|
|
1345
|
+
if (this.names && !this.names.has(id)) this.names.set(id, name);
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/** git's `printable_type`: the object's type, read from the store when not known yet. */
|
|
1349
|
+
private async printableType(n: number): Promise<string> {
|
|
1350
|
+
const type = this.t.type(n);
|
|
1351
|
+
if (type !== OBJ_NONE) return TYPE_NAME[type];
|
|
1352
|
+
const stored = await this.storeType(this.t.id(n));
|
|
1353
|
+
return stored ?? "unknown";
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
/** git's `objerror`. */
|
|
1357
|
+
private async objerror(n: number, message: string): Promise<void> {
|
|
1358
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
1359
|
+
const id = this.t.id(n);
|
|
1360
|
+
const type = await this.printableType(n);
|
|
1361
|
+
this.emit({
|
|
1362
|
+
kind: "error",
|
|
1363
|
+
stream: "stderr",
|
|
1364
|
+
text: `error in ${type} ${this.describe(id)}: ${message}`,
|
|
1365
|
+
objectId: id,
|
|
1366
|
+
objectType: type,
|
|
1367
|
+
});
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
/** The report function of git's object checks for one object. */
|
|
1371
|
+
private reporter(id: string, type: string) {
|
|
1372
|
+
return (msgId: FsckMsgId, message: string): number => {
|
|
1373
|
+
const severity = this.types.severity(msgId);
|
|
1374
|
+
if (severity === "ignore") return 0;
|
|
1375
|
+
const label = severity === "error" ? "error" : "warning";
|
|
1376
|
+
this.emit({
|
|
1377
|
+
kind: "object",
|
|
1378
|
+
stream: "stderr",
|
|
1379
|
+
text: `${label} in ${type} ${this.describe(id)}: ${msgId}: ${message}`,
|
|
1380
|
+
objectId: id,
|
|
1381
|
+
objectType: type,
|
|
1382
|
+
msgId,
|
|
1383
|
+
severity,
|
|
1384
|
+
});
|
|
1385
|
+
return severity === "error" ? 1 : 0;
|
|
1386
|
+
};
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
// ------------------------------------------------------------------------------------------
|
|
1390
|
+
// The object store
|
|
1391
|
+
|
|
1392
|
+
/** The zlib error of the last failed read, as git's `git_inflate` prints it. */
|
|
1393
|
+
private lastZlibError?: string;
|
|
1394
|
+
|
|
1395
|
+
private async readObject(id: string): Promise<GitObject | undefined> {
|
|
1396
|
+
this.lastZlibError = undefined;
|
|
1397
|
+
try {
|
|
1398
|
+
return await this.objects.get(id);
|
|
1399
|
+
} catch (e) {
|
|
1400
|
+
this.noteReadError(e);
|
|
1401
|
+
return undefined;
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
private noteReadError(e: unknown): void {
|
|
1406
|
+
const inflate = /inflate failed: (.*)$/.exec(e instanceof Error ? e.message : "")?.[1];
|
|
1407
|
+
if (inflate) this.lastZlibError = `inflate: data stream error (${inflate})`;
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* The whole object from the store, without a promise when the store holds it in memory (a
|
|
1412
|
+
* packed object in a loaded window or the base cache: `getNow`).
|
|
1413
|
+
*/
|
|
1414
|
+
private readWhole(id: string): Whole | undefined | Promise<Whole | undefined> {
|
|
1415
|
+
this.lastZlibError = undefined;
|
|
1416
|
+
this.objectsRead++;
|
|
1417
|
+
let obj: GitObject | undefined | Promise<GitObject | undefined>;
|
|
1418
|
+
try {
|
|
1419
|
+
obj = this.objects.getNow ? this.objects.getNow(id) : this.objects.get(id);
|
|
1420
|
+
} catch (e) {
|
|
1421
|
+
this.noteReadError(e);
|
|
1422
|
+
return undefined;
|
|
1423
|
+
}
|
|
1424
|
+
if (obj instanceof Promise) {
|
|
1425
|
+
return obj.then(
|
|
1426
|
+
(o) => this.wholeOf(o),
|
|
1427
|
+
(e) => {
|
|
1428
|
+
this.noteReadError(e);
|
|
1429
|
+
return undefined;
|
|
1430
|
+
},
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1433
|
+
return this.wholeOf(obj);
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
private wholeOf(obj: GitObject | undefined): Whole | undefined | Promise<Whole | undefined> {
|
|
1437
|
+
if (!obj) return undefined;
|
|
1438
|
+
// in memory already (the buffer is ours, and `content` needs no release)
|
|
1439
|
+
if (obj.bytes) return { type: obj.type, content: obj.bytes };
|
|
1440
|
+
return collect(obj).then(
|
|
1441
|
+
(content) => ({ type: obj.type, content }),
|
|
1442
|
+
(e) => {
|
|
1443
|
+
this.noteReadError(e);
|
|
1444
|
+
return undefined;
|
|
1445
|
+
},
|
|
1446
|
+
);
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
private async storeType(id: string): Promise<string | undefined> {
|
|
1450
|
+
if (this.o.connectivityOnly) {
|
|
1451
|
+
const type = await this.packedType(id);
|
|
1452
|
+
if (type) return type;
|
|
1453
|
+
}
|
|
1454
|
+
const obj = await this.readObject(id);
|
|
1455
|
+
if (!obj) return undefined;
|
|
1456
|
+
await obj.content[Symbol.asyncIterator]().return?.();
|
|
1457
|
+
return obj.type;
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
/** The repository's packs, opened to read entry headers (`packedType`). */
|
|
1461
|
+
private readonly opened = new Map<RepoPack, PackFile | undefined>();
|
|
1462
|
+
|
|
1463
|
+
/** Pack `p`, opened once (undefined when it cannot be). */
|
|
1464
|
+
private async openRepoPack(p: RepoPack): Promise<PackFile | undefined> {
|
|
1465
|
+
if (!this.opened.has(p)) {
|
|
1466
|
+
const pack = await openPack(this.files as FilesApi, p.packPath, p.idxPath, {
|
|
1467
|
+
idxBytes: p.idxBytes,
|
|
1468
|
+
}).catch(() => undefined);
|
|
1469
|
+
this.opened.set(p, pack);
|
|
1470
|
+
}
|
|
1471
|
+
return this.opened.get(p);
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
/**
|
|
1475
|
+
* The type of a packed object from its entry header, following a delta's bases through
|
|
1476
|
+
* theirs, as git's `packed_object_info`: nothing is inflated (connectivity-only runs read no
|
|
1477
|
+
* blob). Undefined when the headers do not tell (the object is not packed, a base is
|
|
1478
|
+
* elsewhere, a header is corrupt): the store reads the object then.
|
|
1479
|
+
*/
|
|
1480
|
+
private async packedType(id: string): Promise<string | undefined> {
|
|
1481
|
+
const p = this.packs.find((x) => x.idx.has(id));
|
|
1482
|
+
if (!p) return undefined;
|
|
1483
|
+
const pack = await this.openRepoPack(p);
|
|
1484
|
+
if (!pack || p.idx.version < 2) return undefined;
|
|
1485
|
+
try {
|
|
1486
|
+
let at = id;
|
|
1487
|
+
for (let depth = 0; depth <= 4096; depth++) {
|
|
1488
|
+
const e = await pack.entry(at);
|
|
1489
|
+
if (!e) return undefined;
|
|
1490
|
+
if (e.kind <= 4) return TYPE_NAME[e.kind as ObjType];
|
|
1491
|
+
if (!e.baseId || !pack.has(e.baseId)) return undefined;
|
|
1492
|
+
at = e.baseId;
|
|
1493
|
+
}
|
|
1494
|
+
} catch {
|
|
1495
|
+
// a corrupt entry: read through the store
|
|
1496
|
+
}
|
|
1497
|
+
return undefined;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
/** git's `has_object`. */
|
|
1501
|
+
private async hasObject(id: string): Promise<boolean> {
|
|
1502
|
+
try {
|
|
1503
|
+
return await this.objects.has(id);
|
|
1504
|
+
} catch {
|
|
1505
|
+
return false;
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
/** git's `has_object_pack`. */
|
|
1510
|
+
private inPack(id: string): boolean {
|
|
1511
|
+
return this.packs.some((p) => p.idx.has(id));
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
private looseDir(): string {
|
|
1515
|
+
return pathOf(this.gitDir, "objects");
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
/** The object directories: the repository's, then its alternates' (git's odb sources). */
|
|
1519
|
+
private sourceDirs(): string[] {
|
|
1520
|
+
return [this.looseDir(), ...(this.o.alternateDirs ?? [])];
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
/**
|
|
1524
|
+
* Every pack of every source, in the order git lists them: 2.56 source by source (each
|
|
1525
|
+
* source's packs newest first), 2.43 the repository's packs, then all of its alternates'
|
|
1526
|
+
* (get_all_packs' sort_pack: local first, then the most recently modified).
|
|
1527
|
+
*/
|
|
1528
|
+
private async listPacks(): Promise<RepoPack[]> {
|
|
1529
|
+
const dirs = this.sourceDirs();
|
|
1530
|
+
const all: (RepoPack & { mtime: number })[] = [];
|
|
1531
|
+
for (let i = 0; i < dirs.length; i++)
|
|
1532
|
+
all.push(...(await this.listSourcePacks(dirs[i] as string, i)));
|
|
1533
|
+
if (this.o.compatibility === "2.43") {
|
|
1534
|
+
all.sort(
|
|
1535
|
+
(a, b) =>
|
|
1536
|
+
Number(a.source !== 0) - Number(b.source !== 0) ||
|
|
1537
|
+
b.mtime - a.mtime ||
|
|
1538
|
+
(a.packPath < b.packPath ? -1 : 1),
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
return all;
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
private async listSourcePacks(
|
|
1545
|
+
objectsDir: string,
|
|
1546
|
+
source: number,
|
|
1547
|
+
): Promise<(RepoPack & { mtime: number })[]> {
|
|
1548
|
+
const files = this.files as FilesApi;
|
|
1549
|
+
const dir = pathOf(objectsDir, "pack");
|
|
1550
|
+
if ((await files.stats(dir))?.kind !== "directory") return [];
|
|
1551
|
+
const found: (RepoPack & { mtime: number })[] = [];
|
|
1552
|
+
for await (const e of files.list(dir)) {
|
|
1553
|
+
// the repository's format: a pack named in another is not one of its packs
|
|
1554
|
+
if (e.kind !== "file" || !PACK_IDX.test(e.name)) continue;
|
|
1555
|
+
if (e.name.length !== "pack-.idx".length + this.format.hexLength) continue;
|
|
1556
|
+
const idxPath = pathOf(dir, e.name);
|
|
1557
|
+
const packPath = `${idxPath.slice(0, -4)}.pack`;
|
|
1558
|
+
const stats = await files.stats(packPath);
|
|
1559
|
+
if (stats?.kind !== "file") continue;
|
|
1560
|
+
try {
|
|
1561
|
+
const idxBytes = await readAll(files, idxPath);
|
|
1562
|
+
const idx = readPackIndex(idxBytes, this.format.rawLength);
|
|
1563
|
+
found.push({ packPath, idxPath, idx, idxBytes, source, mtime: stats.lastModified ?? 0 });
|
|
1564
|
+
} catch {
|
|
1565
|
+
// git does not list a pack whose idx cannot be opened
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
// git's sort_pack: the most recently modified first
|
|
1569
|
+
found.sort((a, b) => b.mtime - a.mtime || (a.packPath < b.packPath ? -1 : 1));
|
|
1570
|
+
return found;
|
|
1571
|
+
}
|
|
1572
|
+
|
|
1573
|
+
/**
|
|
1574
|
+
* The loose object files of each fan-out directory of `objectsDir` (default the
|
|
1575
|
+
* repository's), in directory order, and its other entries.
|
|
1576
|
+
*/
|
|
1577
|
+
private async *looseEntries(
|
|
1578
|
+
objectsDir = this.looseDir(),
|
|
1579
|
+
): AsyncIterable<{ id?: string; path: string; name: string }> {
|
|
1580
|
+
const files = this.files as FilesApi;
|
|
1581
|
+
for (let i = 0; i < 256; i++) {
|
|
1582
|
+
const sub = i.toString(16).padStart(2, "0");
|
|
1583
|
+
const dir = pathOf(objectsDir, sub);
|
|
1584
|
+
if ((await files.stats(dir))?.kind !== "directory") continue;
|
|
1585
|
+
const entries: { name: string; path: string }[] = [];
|
|
1586
|
+
for await (const e of files.list(dir)) entries.push({ name: e.name, path: e.path });
|
|
1587
|
+
// in the order the files list them, as git's readdir loop: it decides hash collisions
|
|
1588
|
+
for (const e of entries) {
|
|
1589
|
+
const path = pathOf(dir, e.name);
|
|
1590
|
+
yield this.looseName.test(e.name)
|
|
1591
|
+
? { id: sub + e.name, path, name: e.name }
|
|
1592
|
+
: { path, name: e.name };
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
/**
|
|
1598
|
+
* The order git reads the sources in: 2.56 one source at a time (its loose objects, then its
|
|
1599
|
+
* packs: odb_fsck / odb_for_each_object), 2.43 the loose objects of every source, then every
|
|
1600
|
+
* pack. Each step names a source's loose objects (`loose`) or the packs to read (`packs`,
|
|
1601
|
+
* indexes into `this.packs`).
|
|
1602
|
+
*/
|
|
1603
|
+
private sourceSteps(): ({ loose: string } | { packs: number[]; dir?: string })[] {
|
|
1604
|
+
const dirs = this.sourceDirs();
|
|
1605
|
+
const all = this.packs.map((_, k) => k);
|
|
1606
|
+
if (this.o.compatibility === "2.43") {
|
|
1607
|
+
return [...dirs.map((loose) => ({ loose })), { packs: all }];
|
|
1608
|
+
}
|
|
1609
|
+
return dirs.flatMap((loose, i) => [
|
|
1610
|
+
{ loose },
|
|
1611
|
+
{ packs: all.filter((k) => (this.packs[k] as RepoPack).source === i), dir: loose },
|
|
1612
|
+
]);
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
/**
|
|
1616
|
+
* Every stored id, as its 20 bytes at `at` in `raw` (valid during the call): loose ones, then
|
|
1617
|
+
* each pack's in idx order (git's for_each_*_object). A pack's ids are read from its idx.
|
|
1618
|
+
*/
|
|
1619
|
+
private async forEachStored(
|
|
1620
|
+
visit: (raw: Uint8Array, at: number, pack: number, pos: number) => void | Promise<void>,
|
|
1621
|
+
): Promise<void> {
|
|
1622
|
+
const raw = this.format.rawLength;
|
|
1623
|
+
const scratch = new Uint8Array(raw);
|
|
1624
|
+
// ids listed here are 40 (64) lowercase hex digits
|
|
1625
|
+
const nibble = (c: number) => (c <= 0x39 ? c - 0x30 : c - 0x57);
|
|
1626
|
+
const parsed = (id: string) => {
|
|
1627
|
+
for (let i = 0; i < raw; i++)
|
|
1628
|
+
scratch[i] = (nibble(id.charCodeAt(2 * i)) << 4) | nibble(id.charCodeAt(2 * i + 1));
|
|
1629
|
+
return scratch;
|
|
1630
|
+
};
|
|
1631
|
+
if (!this.files) {
|
|
1632
|
+
const ids: string[] = [];
|
|
1633
|
+
for await (const id of this.objects.list()) ids.push(id);
|
|
1634
|
+
ids.sort();
|
|
1635
|
+
for (const id of ids) {
|
|
1636
|
+
const r = visit(parsed(id), 0, -1, -1);
|
|
1637
|
+
if (r) await r;
|
|
1638
|
+
}
|
|
1639
|
+
return;
|
|
1640
|
+
}
|
|
1641
|
+
for (const step of this.sourceSteps()) {
|
|
1642
|
+
if ("loose" in step) {
|
|
1643
|
+
for await (const e of this.looseEntries(step.loose)) {
|
|
1644
|
+
if (!e.id) continue;
|
|
1645
|
+
const r = visit(parsed(e.id), 0, -1, -1);
|
|
1646
|
+
if (r) await r;
|
|
1647
|
+
}
|
|
1648
|
+
continue;
|
|
1649
|
+
}
|
|
1650
|
+
for (const k of step.packs) {
|
|
1651
|
+
const p = this.packs[k] as RepoPack;
|
|
1652
|
+
const v2 = p.idx.version > 1;
|
|
1653
|
+
const idsAt = v2 ? 8 + 1024 : 1024 + 4;
|
|
1654
|
+
const stride = v2 ? 20 : 24;
|
|
1655
|
+
for (let i = 0, n = p.idx.objectCount; i < n; i++) {
|
|
1656
|
+
const r = visit(p.idxBytes, idsAt + i * stride, k, i);
|
|
1657
|
+
if (r) await r;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
// ------------------------------------------------------------------------------------------
|
|
1664
|
+
// Reading and checking every object
|
|
1665
|
+
|
|
1666
|
+
/**
|
|
1667
|
+
* git's `parse_object_buffer`: the object, or undefined (git's errors printed). `raw` holds
|
|
1668
|
+
* the id's 20 bytes at `rawAt`, when the caller has them (an idx).
|
|
1669
|
+
*/
|
|
1670
|
+
private parseObjectBuffer(
|
|
1671
|
+
id: string,
|
|
1672
|
+
type: ObjType,
|
|
1673
|
+
content: Uint8Array,
|
|
1674
|
+
raw?: Uint8Array,
|
|
1675
|
+
rawAt = 0,
|
|
1676
|
+
): number | undefined {
|
|
1677
|
+
const t = this.t;
|
|
1678
|
+
const self = raw ? t.lookupTypedRaw(raw, rawAt, type) : t.lookupTyped(id, type);
|
|
1679
|
+
if (typeof self !== "number") {
|
|
1680
|
+
this.error(self.error, id);
|
|
1681
|
+
return undefined;
|
|
1682
|
+
}
|
|
1683
|
+
const n = self;
|
|
1684
|
+
if (type === OBJ_BLOB || type === OBJ_TREE) {
|
|
1685
|
+
t.addFlags(n, PARSED);
|
|
1686
|
+
return n;
|
|
1687
|
+
}
|
|
1688
|
+
if (t.flags(n) & PARSED) return n;
|
|
1689
|
+
if (type === OBJ_COMMIT) {
|
|
1690
|
+
if (this.graftsHint) this.adviseGrafts();
|
|
1691
|
+
if (this.graftErrors.length > 0) this.reportGraftErrors();
|
|
1692
|
+
const parsed = parseCommitLinks(content, id, this.format);
|
|
1693
|
+
if ("error" in parsed) {
|
|
1694
|
+
this.error(parsed.error, id);
|
|
1695
|
+
return undefined;
|
|
1696
|
+
}
|
|
1697
|
+
const raw = this.idScratch;
|
|
1698
|
+
const tree = t.lookupTypedRaw(rawIdAt(content, parsed.tree, raw), 0, OBJ_TREE);
|
|
1699
|
+
if (typeof tree !== "number") {
|
|
1700
|
+
this.error(tree.error);
|
|
1701
|
+
this.error(
|
|
1702
|
+
`bad tree pointer ${hexIdAt(content, parsed.tree, this.format)} in commit ${id}`,
|
|
1703
|
+
id,
|
|
1704
|
+
);
|
|
1705
|
+
return undefined;
|
|
1706
|
+
}
|
|
1707
|
+
const parents: number[] = [];
|
|
1708
|
+
const grafted = this.graftedParents(id);
|
|
1709
|
+
if (grafted) {
|
|
1710
|
+
for (const p of grafted) {
|
|
1711
|
+
const parent = t.lookupTyped(p, OBJ_COMMIT);
|
|
1712
|
+
if (typeof parent !== "number") {
|
|
1713
|
+
this.error(parent.error);
|
|
1714
|
+
this.error(`bad graft parent ${p} in commit ${id}`, id);
|
|
1715
|
+
return undefined;
|
|
1716
|
+
}
|
|
1717
|
+
parents.push(parent);
|
|
1718
|
+
}
|
|
1719
|
+
} else if (!this.shallow.has(id)) {
|
|
1720
|
+
for (const at of parsed.parents) {
|
|
1721
|
+
const parent = t.lookupTypedRaw(rawIdAt(content, at, raw), 0, OBJ_COMMIT);
|
|
1722
|
+
if (typeof parent !== "number") {
|
|
1723
|
+
this.error(parent.error);
|
|
1724
|
+
this.error(`bad parent ${hexIdAt(content, at, this.format)} in commit ${id}`, id);
|
|
1725
|
+
return undefined;
|
|
1726
|
+
}
|
|
1727
|
+
parents.push(parent);
|
|
1728
|
+
}
|
|
1729
|
+
}
|
|
1730
|
+
t.links.set(n, { tree, parents });
|
|
1731
|
+
t.addFlags(n, PARSED);
|
|
1732
|
+
return n;
|
|
1733
|
+
}
|
|
1734
|
+
// tag
|
|
1735
|
+
const parsed = parseTagBuffer(content, id, this.format);
|
|
1736
|
+
if ("error" in parsed) {
|
|
1737
|
+
if (parsed.error) this.error(parsed.error, id);
|
|
1738
|
+
return undefined;
|
|
1739
|
+
}
|
|
1740
|
+
const tagged = t.lookupTyped(parsed.object, typeOf(parsed.type));
|
|
1741
|
+
if (typeof tagged !== "number") {
|
|
1742
|
+
this.error(tagged.error);
|
|
1743
|
+
this.error(`bad tag pointer to ${parsed.object} in ${id}`, id);
|
|
1744
|
+
return undefined;
|
|
1745
|
+
}
|
|
1746
|
+
if (parsed.tag === undefined) return undefined;
|
|
1747
|
+
t.links.set(n, { tagged, tag: parsed.tag });
|
|
1748
|
+
t.addFlags(n, PARSED);
|
|
1749
|
+
return n;
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
/**
|
|
1753
|
+
* git's `parse_object`: an object already parsed, or read from the store with its hash
|
|
1754
|
+
* checked (`hash mismatch <id>`, and undefined, when its bytes hash to another id: the copy
|
|
1755
|
+
* the store reads first, packed before loose, as git's) and parsed. The roots (refs, the
|
|
1756
|
+
* cache-tree, resolve-undo) and the objects the walk meets untyped (reflog entries in
|
|
1757
|
+
* connectivity-only runs) are parsed this way, trees again each time; the walk reads typed objects unchecked
|
|
1758
|
+
* (`parseAs`, git's repo_parse_commit, parse_tree and parse_tag).
|
|
1759
|
+
*/
|
|
1760
|
+
private parseObject(id: string): number | undefined | Promise<number | undefined> {
|
|
1761
|
+
const known = this.t.lookup(id);
|
|
1762
|
+
// a tree is read again: git frees each tree's buffer once checked or walked (parsed = 0)
|
|
1763
|
+
if (known >= 0 && this.t.flags(known) & PARSED && this.t.type(known) !== OBJ_TREE) {
|
|
1764
|
+
return known;
|
|
1765
|
+
}
|
|
1766
|
+
const obj = this.readWhole(id);
|
|
1767
|
+
if (obj instanceof Promise) return obj.then((o) => this.parseVerified(id, o));
|
|
1768
|
+
return this.parseVerified(id, obj);
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
/**
|
|
1772
|
+
* `parseRead` once the bytes hash to `id` (git's check_object_signature). Hashed here, not by
|
|
1773
|
+
* the store: a verified read would mark the copy bad there, and git reads it again as it is.
|
|
1774
|
+
*/
|
|
1775
|
+
private parseVerified(id: string, obj: Whole | undefined): number | undefined {
|
|
1776
|
+
if (
|
|
1777
|
+
obj &&
|
|
1778
|
+
objectIdOf(obj.type, obj.content, this.sha1 ? undefined : this.format.createHasher) !== id
|
|
1779
|
+
) {
|
|
1780
|
+
this.error(`hash mismatch ${id}`, id);
|
|
1781
|
+
return undefined;
|
|
1782
|
+
}
|
|
1783
|
+
return this.parseRead(id, obj);
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
/**
|
|
1787
|
+
* git's repo_parse_commit and parse_tag of object `n`, a commit or a tag not parsed yet: read
|
|
1788
|
+
* without a hash check, `Could not read <id>` when that fails and `Object <id> not a <type>`
|
|
1789
|
+
* when the store holds another type.
|
|
1790
|
+
*/
|
|
1791
|
+
private parseAs(n: number, id: string): void | Promise<void> {
|
|
1792
|
+
const obj = this.readWhole(id);
|
|
1793
|
+
if (obj instanceof Promise) return obj.then((o) => this.parseAsRead(n, id, o));
|
|
1794
|
+
this.parseAsRead(n, id, obj);
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
private parseAsRead(n: number, id: string, obj: Whole | undefined): void {
|
|
1798
|
+
const want = this.t.type(n);
|
|
1799
|
+
if (!obj) {
|
|
1800
|
+
if (this.lastZlibError) this.error(this.lastZlibError);
|
|
1801
|
+
this.error(`Could not read ${id}`);
|
|
1802
|
+
return;
|
|
1803
|
+
}
|
|
1804
|
+
if (typeOf(obj.type) !== want) {
|
|
1805
|
+
this.error(`Object ${id} not a ${TYPE_NAME[want]}`);
|
|
1806
|
+
return;
|
|
1807
|
+
}
|
|
1808
|
+
this.lastRead = { id, obj };
|
|
1809
|
+
this.parseObjectBuffer(id, want, obj.content);
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
private parseRead(id: string, obj: Whole | undefined): number | undefined {
|
|
1813
|
+
if (!obj) {
|
|
1814
|
+
if (this.lastZlibError) this.error(this.lastZlibError);
|
|
1815
|
+
return undefined;
|
|
1816
|
+
}
|
|
1817
|
+
this.lastRead = { id, obj };
|
|
1818
|
+
return this.parseObjectBuffer(id, typeOf(obj.type), obj.content);
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
/**
|
|
1822
|
+
* git's `fsck_obj_buffer` / the end of `fsck_loose`: parses the object, marks it stored and
|
|
1823
|
+
* checks it. Returns nonzero when the object is bad.
|
|
1824
|
+
*/
|
|
1825
|
+
private fsckObjBuffer(
|
|
1826
|
+
id: string,
|
|
1827
|
+
type: ObjType,
|
|
1828
|
+
content: Uint8Array | undefined,
|
|
1829
|
+
unparsable: string,
|
|
1830
|
+
size = content?.length ?? 0,
|
|
1831
|
+
raw?: Uint8Array,
|
|
1832
|
+
rawAt = 0,
|
|
1833
|
+
): number | Promise<number> {
|
|
1834
|
+
// no content: a blob streamed past core.bigFileThreshold (git's NULL buffer)
|
|
1835
|
+
const n = this.parseObjectBuffer(id, type, content ?? EMPTY, raw, rawAt);
|
|
1836
|
+
if (n === undefined) {
|
|
1837
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
1838
|
+
this.error(unparsable, id);
|
|
1839
|
+
return -1;
|
|
1840
|
+
}
|
|
1841
|
+
this.t.clearFlags(n, REACHABLE | SEEN);
|
|
1842
|
+
this.t.addFlags(n, HAS_OBJ);
|
|
1843
|
+
return this.fsckObj(n, id, content, size);
|
|
1844
|
+
}
|
|
1845
|
+
|
|
1846
|
+
/** git's `fsck_obj`: without a promise unless it reports through the store. */
|
|
1847
|
+
private fsckObj(
|
|
1848
|
+
n: number,
|
|
1849
|
+
id: string,
|
|
1850
|
+
content: Uint8Array | undefined,
|
|
1851
|
+
size: number,
|
|
1852
|
+
): number | Promise<number> {
|
|
1853
|
+
const t = this.t;
|
|
1854
|
+
if (t.flags(n) & SEEN) return 0;
|
|
1855
|
+
t.addFlags(n, SEEN);
|
|
1856
|
+
if (this.walkUsed(n, content ?? EMPTY, this.recordTrees)) {
|
|
1857
|
+
return this.objerror(n, "broken links").then(() => this.fsckObjChecks(n, id, content, size));
|
|
1858
|
+
}
|
|
1859
|
+
return this.fsckObjChecks(n, id, content, size);
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
/** The rest of `fsck_obj`: the object checks, `--root` and `--tags`. */
|
|
1863
|
+
private fsckObjChecks(
|
|
1864
|
+
n: number,
|
|
1865
|
+
id: string,
|
|
1866
|
+
content: Uint8Array | undefined,
|
|
1867
|
+
size: number,
|
|
1868
|
+
): number | Promise<number> {
|
|
1869
|
+
const t = this.t;
|
|
1870
|
+
const type = t.type(n);
|
|
1871
|
+
const err = this.fsckBuffer(id, type, content, size);
|
|
1872
|
+
if (err) return err;
|
|
1873
|
+
if (type === OBJ_COMMIT && this.o.root && t.links.get(n)?.parents?.length === 0) {
|
|
1874
|
+
this.emit({
|
|
1875
|
+
kind: "root",
|
|
1876
|
+
stream: "stdout",
|
|
1877
|
+
text: `root ${this.describe(id)}`,
|
|
1878
|
+
objectId: id,
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
if (type === OBJ_TAG && this.o.tags) {
|
|
1882
|
+
const links = t.links.get(n);
|
|
1883
|
+
if (links?.tagged !== undefined) {
|
|
1884
|
+
const target = links.tagged;
|
|
1885
|
+
return this.printableType(target).then((printable) => {
|
|
1886
|
+
this.emit({
|
|
1887
|
+
kind: "tagged",
|
|
1888
|
+
stream: "stdout",
|
|
1889
|
+
text: `tagged ${printable} ${this.describe(t.id(target))} (${links.tag}) in ${this.describe(id)}`,
|
|
1890
|
+
objectId: id,
|
|
1891
|
+
});
|
|
1892
|
+
return 0;
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
return 0;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
/** Records that tree `n`'s entries start at `start` in `kids` (`treeStart` holds start + 1). */
|
|
1900
|
+
private setTreeStart(n: number, start: number): void {
|
|
1901
|
+
if (n >= this.treeStart.length) {
|
|
1902
|
+
const grown = new Int32Array(Math.max(1024, this.t.size * 2, n + 1));
|
|
1903
|
+
grown.set(this.treeStart);
|
|
1904
|
+
this.treeStart = grown;
|
|
1905
|
+
}
|
|
1906
|
+
this.treeStart[n] = start + 1;
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
/**
|
|
1910
|
+
* Room in `kids` for `more` values past `kidsLength`, within what `linksKept` leaves after
|
|
1911
|
+
* `scanKids` (the limit is the two together); false past it. The array grows by doubling up
|
|
1912
|
+
* to that limit, never past it.
|
|
1913
|
+
*/
|
|
1914
|
+
private reserveKids(more: number): boolean {
|
|
1915
|
+
const need = this.kidsLength + more;
|
|
1916
|
+
const limit = this.o.linksKept - this.scanKidsLength;
|
|
1917
|
+
if (need > limit) return false;
|
|
1918
|
+
if (need <= this.kids.length) return true;
|
|
1919
|
+
let size = Math.max(this.kids.length * 2, 1 << 12);
|
|
1920
|
+
while (size < need) size *= 2;
|
|
1921
|
+
const kids = new Int32Array(Math.min(size, limit));
|
|
1922
|
+
kids.set(this.kids.subarray(0, this.kidsLength));
|
|
1923
|
+
this.kids = kids;
|
|
1924
|
+
return true;
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
/**
|
|
1928
|
+
* git's `fsck_walk` with `mark_used`: marks what the object points to as used. With `record`,
|
|
1929
|
+
* a tree whose entries all resolve is remembered as its entries' objects (`treeEntries`).
|
|
1930
|
+
*/
|
|
1931
|
+
private walkUsed(n: number, content: Uint8Array, record = false): number {
|
|
1932
|
+
const t = this.t;
|
|
1933
|
+
const type = t.type(n);
|
|
1934
|
+
if (type === OBJ_TREE) {
|
|
1935
|
+
const walk = scanTree(content, this.usedTable, this.format);
|
|
1936
|
+
this.lastTree = { content, table: walk };
|
|
1937
|
+
if (walk.initError) {
|
|
1938
|
+
this.error(walk.initError);
|
|
1939
|
+
return -1;
|
|
1940
|
+
}
|
|
1941
|
+
let res = 0;
|
|
1942
|
+
const start = this.kidsLength;
|
|
1943
|
+
// past the limit the walk reads the tree again
|
|
1944
|
+
if (record && !this.reserveKids(1 + walk.count)) record = false;
|
|
1945
|
+
let at = start + 1;
|
|
1946
|
+
const f = walk.fields;
|
|
1947
|
+
for (let i = 0; i < walk.count; i++) {
|
|
1948
|
+
const mode = canonMode(f[3 * i] as number);
|
|
1949
|
+
if (mode === 0o160000) continue;
|
|
1950
|
+
const isTree = mode === 0o040000;
|
|
1951
|
+
const child = t.lookupTypedRaw(
|
|
1952
|
+
content,
|
|
1953
|
+
f[3 * i + 2] as number,
|
|
1954
|
+
isTree ? OBJ_TREE : OBJ_BLOB,
|
|
1955
|
+
);
|
|
1956
|
+
if (typeof child !== "number") {
|
|
1957
|
+
this.error(child.error);
|
|
1958
|
+
res ||= 1;
|
|
1959
|
+
continue;
|
|
1960
|
+
}
|
|
1961
|
+
t.addFlags(child, USED);
|
|
1962
|
+
if (record) this.kids[at++] = (child << 1) | (isTree ? 1 : 0);
|
|
1963
|
+
}
|
|
1964
|
+
if (walk.error) this.error(walk.error);
|
|
1965
|
+
if (record && res === 0 && !walk.error) {
|
|
1966
|
+
this.kids[start] = at - start - 1;
|
|
1967
|
+
this.kidsLength = at;
|
|
1968
|
+
this.setTreeStart(n, start);
|
|
1969
|
+
}
|
|
1970
|
+
return res;
|
|
1971
|
+
}
|
|
1972
|
+
const links = t.links.get(n);
|
|
1973
|
+
if (type === OBJ_COMMIT && links) {
|
|
1974
|
+
if (links.tree !== undefined) t.addFlags(links.tree, USED);
|
|
1975
|
+
for (const p of links.parents ?? []) t.addFlags(p, USED);
|
|
1976
|
+
} else if (type === OBJ_TAG && links?.tagged !== undefined) {
|
|
1977
|
+
t.addFlags(links.tagged, USED);
|
|
1978
|
+
}
|
|
1979
|
+
return 0;
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
/** git's `fsck_buffer`: the object checks of `fsck.c`. */
|
|
1983
|
+
private fsckBuffer(
|
|
1984
|
+
id: string,
|
|
1985
|
+
type: ObjType,
|
|
1986
|
+
buffer: Uint8Array | undefined,
|
|
1987
|
+
size: number,
|
|
1988
|
+
): number {
|
|
1989
|
+
const report = this.reporter(id, TYPE_NAME[type]);
|
|
1990
|
+
if (type === OBJ_BLOB) return this.fsckBlobOf(id, buffer, size, report);
|
|
1991
|
+
const content = buffer ?? EMPTY;
|
|
1992
|
+
switch (type) {
|
|
1993
|
+
case OBJ_TREE: {
|
|
1994
|
+
const found = { gitmodules: [] as string[], gitattributes: [] as string[] };
|
|
1995
|
+
// the walk just before decoded this tree already
|
|
1996
|
+
const last = this.lastTree;
|
|
1997
|
+
this.lastTree = undefined;
|
|
1998
|
+
const r = fsckTree(
|
|
1999
|
+
content,
|
|
2000
|
+
this.types,
|
|
2001
|
+
report,
|
|
2002
|
+
(m) => this.error(m),
|
|
2003
|
+
found,
|
|
2004
|
+
last?.content === content ? last.table : undefined,
|
|
2005
|
+
this.format,
|
|
2006
|
+
);
|
|
2007
|
+
for (const g of found.gitmodules) this.gitmodulesFound.add(g);
|
|
2008
|
+
for (const g of found.gitattributes) this.gitattributesFound.add(g);
|
|
2009
|
+
return r;
|
|
2010
|
+
}
|
|
2011
|
+
case OBJ_COMMIT:
|
|
2012
|
+
return fsckCommit(content, report, this.format);
|
|
2013
|
+
case OBJ_TAG:
|
|
2014
|
+
return fsckTag(content, report, {
|
|
2015
|
+
gpgsig: this.o.compatibility !== "2.43",
|
|
2016
|
+
shape: this.format,
|
|
2017
|
+
});
|
|
2018
|
+
default:
|
|
2019
|
+
return report("unknownType", `unknown type '${type}' (internal fsck error)`);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
private fsckBlobOf(
|
|
2024
|
+
id: string,
|
|
2025
|
+
content: Uint8Array | undefined,
|
|
2026
|
+
size: number,
|
|
2027
|
+
report: ReturnType<FsckRun["reporter"]>,
|
|
2028
|
+
): number {
|
|
2029
|
+
const gitmodules = this.gitmodulesFound.has(id);
|
|
2030
|
+
const gitattributes = this.gitattributesFound.has(id);
|
|
2031
|
+
if (!gitmodules && !gitattributes) return 0;
|
|
2032
|
+
if (gitmodules) this.gitmodulesDone.add(id);
|
|
2033
|
+
if (gitattributes) this.gitattributesDone.add(id);
|
|
2034
|
+
if (gitmodules && content && content.length < size) {
|
|
2035
|
+
// a loose blob whose stream ended early: git parses its zeros too (`.gitattributes` is
|
|
2036
|
+
// read up to the first NUL, the same with them or without)
|
|
2037
|
+
const padded = new Uint8Array(size);
|
|
2038
|
+
padded.set(content);
|
|
2039
|
+
content = padded;
|
|
2040
|
+
}
|
|
2041
|
+
return fsckBlob(content, size, { gitmodules, gitattributes }, report);
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
/** git's `fsck_finish`: `.gitmodules` and `.gitattributes` blobs not checked yet. */
|
|
2045
|
+
private async fsckFinish(): Promise<number> {
|
|
2046
|
+
let ret = 0;
|
|
2047
|
+
const pass = async (
|
|
2048
|
+
found: Set<string>,
|
|
2049
|
+
done: Set<string>,
|
|
2050
|
+
missing: FsckMsgId,
|
|
2051
|
+
nonBlob: FsckMsgId,
|
|
2052
|
+
name: string,
|
|
2053
|
+
) => {
|
|
2054
|
+
for (const id of found) {
|
|
2055
|
+
if (done.has(id)) continue;
|
|
2056
|
+
const obj = await this.readWhole(id);
|
|
2057
|
+
if (!obj) {
|
|
2058
|
+
ret |= this.reporter(id, "blob")(missing, `unable to read ${name} blob`);
|
|
2059
|
+
continue;
|
|
2060
|
+
}
|
|
2061
|
+
if (obj.type === "blob") {
|
|
2062
|
+
ret |= this.fsckBlobOf(id, obj.content, obj.content.length, this.reporter(id, "blob"));
|
|
2063
|
+
} else {
|
|
2064
|
+
ret |= this.reporter(id, obj.type)(nonBlob, `non-blob found at ${name}`);
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
};
|
|
2068
|
+
await pass(
|
|
2069
|
+
this.gitmodulesFound,
|
|
2070
|
+
this.gitmodulesDone,
|
|
2071
|
+
"gitmodulesMissing",
|
|
2072
|
+
"gitmodulesBlob",
|
|
2073
|
+
".gitmodules",
|
|
2074
|
+
);
|
|
2075
|
+
await pass(
|
|
2076
|
+
this.gitattributesFound,
|
|
2077
|
+
this.gitattributesDone,
|
|
2078
|
+
"gitattributesMissing",
|
|
2079
|
+
"gitattributesBlob",
|
|
2080
|
+
".gitattributes",
|
|
2081
|
+
);
|
|
2082
|
+
return ret;
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
/** Blobs whose content a check needs (`.gitmodules`, `.gitattributes`). */
|
|
2086
|
+
private keepBlob = (id: string): boolean =>
|
|
2087
|
+
this.gitmodulesFound.has(id) || this.gitattributesFound.has(id);
|
|
2088
|
+
|
|
2089
|
+
/** git's `fsck_object_dir`: every loose object file of `objectsDir`. */
|
|
2090
|
+
private async fsckLooseObjects(objectsDir: string): Promise<void> {
|
|
2091
|
+
const files = this.files as FilesApi;
|
|
2092
|
+
for await (const e of this.looseEntries(objectsDir)) {
|
|
2093
|
+
if (!e.id) {
|
|
2094
|
+
if (!e.name.startsWith("tmp_obj_")) {
|
|
2095
|
+
this.emit({ kind: "notice", stream: "stderr", text: `bad sha1 file: ${e.path}` });
|
|
2096
|
+
}
|
|
2097
|
+
continue;
|
|
2098
|
+
}
|
|
2099
|
+
const read = await readLooseObject(files, e.path, e.id, {
|
|
2100
|
+
bigFileThreshold: this.o.bigFileThreshold,
|
|
2101
|
+
newHasher: this.format.createHasher,
|
|
2102
|
+
});
|
|
2103
|
+
for (const m of read.errors) this.error(m, e.id);
|
|
2104
|
+
if (read.errors.length > 0) this.badCopy.add(e.id);
|
|
2105
|
+
if (!read.object) {
|
|
2106
|
+
this.badCopy.add(e.id);
|
|
2107
|
+
if (read.realId)
|
|
2108
|
+
this.error(`${read.realId}: hash-path mismatch, found at: ${e.path}`, e.id);
|
|
2109
|
+
else this.error(`${e.id}: object corrupt or missing: ${e.path}`, e.id);
|
|
2110
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
2111
|
+
continue;
|
|
2112
|
+
}
|
|
2113
|
+
const r = await this.fsckObjBuffer(
|
|
2114
|
+
e.id,
|
|
2115
|
+
typeOf(read.object.type),
|
|
2116
|
+
read.object.content,
|
|
2117
|
+
this.o.compatibility === "2.56"
|
|
2118
|
+
? `${e.id}: object corrupt or missing`
|
|
2119
|
+
: `${e.id}: object could not be parsed: ${e.path}`,
|
|
2120
|
+
read.object.size,
|
|
2121
|
+
);
|
|
2122
|
+
if (r > 0) this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
/**
|
|
2127
|
+
* git's check of each pack's `.rev` (`verify_reverse_indices`; 2.43's
|
|
2128
|
+
* check_pack_rev_indexes): a missing file is fine. True if one is wrong.
|
|
2129
|
+
*/
|
|
2130
|
+
private async verifyReverseIndexes(packs: readonly RepoPack[]): Promise<boolean> {
|
|
2131
|
+
const files = this.files as FilesApi;
|
|
2132
|
+
let bad = false;
|
|
2133
|
+
for (const p of packs) {
|
|
2134
|
+
const lines = await verifyReverseIndexFile(
|
|
2135
|
+
files,
|
|
2136
|
+
p.packPath,
|
|
2137
|
+
p.idx.objectCount,
|
|
2138
|
+
(i) => p.idx.getOffset(i),
|
|
2139
|
+
this.format,
|
|
2140
|
+
);
|
|
2141
|
+
if (!lines) continue;
|
|
2142
|
+
for (const line of lines) this.error(line.replace(/^error: /, ""));
|
|
2143
|
+
bad = true;
|
|
2144
|
+
}
|
|
2145
|
+
return bad;
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
/**
|
|
2149
|
+
* git's `verify_bitmap_files`: the trailing hash (SHA-1, or SHA-256 in a SHA-256 repository)
|
|
2150
|
+
* of each pack's `.bitmap` (a missing file is fine). True if one is wrong.
|
|
2151
|
+
*/
|
|
2152
|
+
private async verifyBitmapFiles(
|
|
2153
|
+
packs: readonly RepoPack[],
|
|
2154
|
+
objectsDir: string = this.looseDir(),
|
|
2155
|
+
): Promise<boolean> {
|
|
2156
|
+
const files = this.files as FilesApi;
|
|
2157
|
+
let bad = false;
|
|
2158
|
+
// the multi-pack-index's bitmap first (its name carries the midx checksum)
|
|
2159
|
+
const paths: string[] = [];
|
|
2160
|
+
if (this.o.multiPackIndex !== false) {
|
|
2161
|
+
const loaded = await loadMultiPackIndex(files, pathOf(objectsDir, "pack"), {
|
|
2162
|
+
compatibility: this.o.compatibility,
|
|
2163
|
+
objectFormat: this.format,
|
|
2164
|
+
});
|
|
2165
|
+
if (loaded.midx)
|
|
2166
|
+
paths.push(pathOf(objectsDir, "pack", `multi-pack-index-${loaded.midx.checksum}.bitmap`));
|
|
2167
|
+
}
|
|
2168
|
+
for (const p of packs) paths.push(`${p.packPath.replace(/\.pack$/, "")}.bitmap`);
|
|
2169
|
+
for (const path of paths) {
|
|
2170
|
+
const st = await files.stats(path).catch(() => undefined);
|
|
2171
|
+
if (st?.kind !== "file") continue;
|
|
2172
|
+
const size = st.size ?? 0;
|
|
2173
|
+
const raw = this.format.rawLength;
|
|
2174
|
+
let valid = size >= raw;
|
|
2175
|
+
if (valid) {
|
|
2176
|
+
const hasher = this.format.createHasher();
|
|
2177
|
+
for await (const c of files.read(path, { start: 0, length: size - raw })) hasher.update(c);
|
|
2178
|
+
const sum = hasher.digest();
|
|
2179
|
+
const tail: number[] = [];
|
|
2180
|
+
for await (const c of files.read(path, { start: size - raw, length: raw })) tail.push(...c);
|
|
2181
|
+
valid = tail.length === raw && sum.every((b, i) => b === tail[i]);
|
|
2182
|
+
}
|
|
2183
|
+
if (!valid) {
|
|
2184
|
+
this.error(`bitmap file '${path}' has invalid checksum`);
|
|
2185
|
+
bad = true;
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
return bad;
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
/** git's `verify_pack` for each of `packs`, with `fsck_obj_buffer` for each object. */
|
|
2192
|
+
private async fsckPacks(packs: readonly RepoPack[]): Promise<void> {
|
|
2193
|
+
const files = this.files as FilesApi;
|
|
2194
|
+
for (const p of packs) {
|
|
2195
|
+
let err = 0;
|
|
2196
|
+
// git prints the pack checksum errors before any entry's output
|
|
2197
|
+
let checksumAt = 0;
|
|
2198
|
+
const result = await checkPack(
|
|
2199
|
+
files,
|
|
2200
|
+
p.packPath,
|
|
2201
|
+
p.idxPath,
|
|
2202
|
+
{
|
|
2203
|
+
error: (m) => this.error(m),
|
|
2204
|
+
opened: () => {
|
|
2205
|
+
checksumAt = this.findings.length;
|
|
2206
|
+
},
|
|
2207
|
+
checksumError: (m) => {
|
|
2208
|
+
this.findings.splice(checksumAt++, 0, {
|
|
2209
|
+
kind: "error",
|
|
2210
|
+
stream: "stderr",
|
|
2211
|
+
text: `error: ${m}`,
|
|
2212
|
+
objectId: undefined,
|
|
2213
|
+
});
|
|
2214
|
+
},
|
|
2215
|
+
entry: (entry, unpacked) => {
|
|
2216
|
+
if (p.idx.version > 1 && !entry.crcOk) {
|
|
2217
|
+
this.error(
|
|
2218
|
+
`index CRC mismatch for object ${entry.id} from ${p.packPath} at offset ${entry.offset}`,
|
|
2219
|
+
entry.id,
|
|
2220
|
+
);
|
|
2221
|
+
err = 1;
|
|
2222
|
+
}
|
|
2223
|
+
if ("error" in unpacked) {
|
|
2224
|
+
if (unpacked.zlib) this.error(unpacked.zlib);
|
|
2225
|
+
if (unpacked.base) this.error(unpacked.base);
|
|
2226
|
+
this.error(unpacked.error, entry.id);
|
|
2227
|
+
this.badCopy.add(entry.id);
|
|
2228
|
+
// the verification reads this copy (git does not check its hash there)
|
|
2229
|
+
this.skipGraphCommit(entry.id);
|
|
2230
|
+
err = 1;
|
|
2231
|
+
return;
|
|
2232
|
+
}
|
|
2233
|
+
if (unpacked.type === "commit") this.keepGraphCommit(entry.id, unpacked.content);
|
|
2234
|
+
const r = this.fsckObjBuffer(
|
|
2235
|
+
entry.id,
|
|
2236
|
+
typeOf(unpacked.type),
|
|
2237
|
+
unpacked.content,
|
|
2238
|
+
`${entry.id}: object corrupt or missing`,
|
|
2239
|
+
unpacked.content.length,
|
|
2240
|
+
entry.ids,
|
|
2241
|
+
entry.idAt,
|
|
2242
|
+
);
|
|
2243
|
+
if (r instanceof Promise) {
|
|
2244
|
+
return r.then((x) => {
|
|
2245
|
+
if (x !== 0) err = 1;
|
|
2246
|
+
});
|
|
2247
|
+
}
|
|
2248
|
+
if (r !== 0) err = 1;
|
|
2249
|
+
},
|
|
2250
|
+
},
|
|
2251
|
+
{
|
|
2252
|
+
externalBase: (id) => this.readObject(id),
|
|
2253
|
+
read: { bytes: p.idxBytes, idx: p.idx },
|
|
2254
|
+
keepBlob: this.keepBlob,
|
|
2255
|
+
...(this.sha1 ? {} : { objectFormat: this.format }),
|
|
2256
|
+
},
|
|
2257
|
+
);
|
|
2258
|
+
if (result.failed) err = 1;
|
|
2259
|
+
if (err) {
|
|
2260
|
+
this.errorsFound |= this.o.compatibility === "2.56" ? FSCK_ERROR_OBJECT : FSCK_ERROR_PACK;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2265
|
+
/** Every object of a store without files: read, hash-checked and checked. */
|
|
2266
|
+
private async fsckStoredObjects(): Promise<void> {
|
|
2267
|
+
const ids: string[] = [];
|
|
2268
|
+
for await (const id of this.objects.list()) ids.push(id);
|
|
2269
|
+
ids.sort();
|
|
2270
|
+
for (const id of ids) {
|
|
2271
|
+
let read: { type: string; content: Uint8Array } | undefined;
|
|
2272
|
+
try {
|
|
2273
|
+
const obj = await this.objects.get(id, { verify: true });
|
|
2274
|
+
if (obj) {
|
|
2275
|
+
if (obj.type === "blob" && !this.keepBlob(id)) {
|
|
2276
|
+
for await (const _ of obj.content) {
|
|
2277
|
+
// hashed as it streams
|
|
2278
|
+
}
|
|
2279
|
+
read = { type: obj.type, content: new Uint8Array(0) };
|
|
2280
|
+
} else {
|
|
2281
|
+
read = { type: obj.type, content: await collect(obj) };
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
} catch {
|
|
2285
|
+
read = undefined;
|
|
2286
|
+
}
|
|
2287
|
+
if (!read) {
|
|
2288
|
+
this.error(`${id}: object corrupt or missing`, id);
|
|
2289
|
+
this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
2290
|
+
continue;
|
|
2291
|
+
}
|
|
2292
|
+
const r = await this.fsckObjBuffer(
|
|
2293
|
+
id,
|
|
2294
|
+
typeOf(read.type),
|
|
2295
|
+
read.content,
|
|
2296
|
+
`${id}: object corrupt or missing`,
|
|
2297
|
+
);
|
|
2298
|
+
if (r > 0) this.errorsFound |= FSCK_ERROR_OBJECT;
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
// ------------------------------------------------------------------------------------------
|
|
2303
|
+
// Roots: refs, HEADs, reflogs, index files
|
|
2304
|
+
|
|
2305
|
+
/**
|
|
2306
|
+
* `markObject` where it needs no await: an object of the expected type that is reachable
|
|
2307
|
+
* already, or stored (then queued). False, with nothing done, otherwise.
|
|
2308
|
+
*/
|
|
2309
|
+
private markReachable(n: number | undefined, type: ObjType): boolean {
|
|
2310
|
+
const t = this.t;
|
|
2311
|
+
if (n === undefined || t.type(n) !== type) return false;
|
|
2312
|
+
const flags = t.flags(n);
|
|
2313
|
+
if (flags & REACHABLE) return true;
|
|
2314
|
+
if (this.promised?.has(t.id(n))) {
|
|
2315
|
+
// a promisor object: reachable, not walked further (mark_object)
|
|
2316
|
+
t.addFlags(n, REACHABLE);
|
|
2317
|
+
return true;
|
|
2318
|
+
}
|
|
2319
|
+
if (!(flags & HAS_OBJ)) return false;
|
|
2320
|
+
t.addFlags(n, REACHABLE);
|
|
2321
|
+
this.pending.push(n);
|
|
2322
|
+
return true;
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
/** git's `mark_object`: without a promise when it reports nothing. */
|
|
2326
|
+
private markObject(
|
|
2327
|
+
n: number | undefined,
|
|
2328
|
+
type: ObjType | "any",
|
|
2329
|
+
parent: number | undefined,
|
|
2330
|
+
): number | Promise<number> {
|
|
2331
|
+
const t = this.t;
|
|
2332
|
+
if (n !== undefined && (type === "any" || parent === undefined || t.type(n) === type)) {
|
|
2333
|
+
const flags = t.flags(n);
|
|
2334
|
+
if (flags & REACHABLE) return 0;
|
|
2335
|
+
if (this.promised?.has(t.id(n))) {
|
|
2336
|
+
t.addFlags(n, REACHABLE);
|
|
2337
|
+
return 0;
|
|
2338
|
+
}
|
|
2339
|
+
if (flags & HAS_OBJ) {
|
|
2340
|
+
t.addFlags(n, REACHABLE);
|
|
2341
|
+
this.pending.push(n);
|
|
2342
|
+
return 0;
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
return this.markObjectReporting(n, type, parent);
|
|
2346
|
+
}
|
|
2347
|
+
|
|
2348
|
+
/** `markObject` where it may report: a broken link, a wrong type, an object not stored. */
|
|
2349
|
+
private async markObjectReporting(
|
|
2350
|
+
n: number | undefined,
|
|
2351
|
+
type: ObjType | "any",
|
|
2352
|
+
parent: number | undefined,
|
|
2353
|
+
): Promise<number> {
|
|
2354
|
+
const t = this.t;
|
|
2355
|
+
if (n === undefined) {
|
|
2356
|
+
const p = parent as number;
|
|
2357
|
+
this.emit({
|
|
2358
|
+
kind: "broken-link",
|
|
2359
|
+
stream: "stdout",
|
|
2360
|
+
text: `broken link from ${(await this.printableType(p)).padStart(7)} ${this.describe(t.id(p))}`,
|
|
2361
|
+
objectId: t.id(p),
|
|
2362
|
+
});
|
|
2363
|
+
this.emit({
|
|
2364
|
+
kind: "broken-link",
|
|
2365
|
+
stream: "stdout",
|
|
2366
|
+
text: `broken link from ${(type === "any" ? "unknown" : TYPE_NAME[type]).padStart(7)} unknown`,
|
|
2367
|
+
});
|
|
2368
|
+
this.errorsFound |= FSCK_ERROR_REACHABLE;
|
|
2369
|
+
return 1;
|
|
2370
|
+
}
|
|
2371
|
+
if (type !== "any" && t.type(n) !== type && parent !== undefined) {
|
|
2372
|
+
await this.objerror(parent, "wrong object type in link");
|
|
2373
|
+
}
|
|
2374
|
+
if (t.flags(n) & REACHABLE) return 0;
|
|
2375
|
+
t.addFlags(n, REACHABLE);
|
|
2376
|
+
// a promisor object needs no further recursion (it is not missing: it will be fetched);
|
|
2377
|
+
// 2.43 asks only for an object it does not have
|
|
2378
|
+
if (
|
|
2379
|
+
(this.o.compatibility === "2.56" || !(t.flags(n) & HAS_OBJ)) &&
|
|
2380
|
+
this.promised?.has(t.id(n))
|
|
2381
|
+
) {
|
|
2382
|
+
return 0;
|
|
2383
|
+
}
|
|
2384
|
+
if (!(t.flags(n) & HAS_OBJ)) {
|
|
2385
|
+
const id = t.id(n);
|
|
2386
|
+
if (parent !== undefined && !(await this.hasObject(id))) {
|
|
2387
|
+
const pid = t.id(parent);
|
|
2388
|
+
this.emit({
|
|
2389
|
+
kind: "broken-link",
|
|
2390
|
+
stream: "stdout",
|
|
2391
|
+
text: `broken link from ${(await this.printableType(parent)).padStart(7)} ${this.describe(pid)}`,
|
|
2392
|
+
objectId: pid,
|
|
2393
|
+
});
|
|
2394
|
+
this.emit({
|
|
2395
|
+
kind: "broken-link",
|
|
2396
|
+
stream: "stdout",
|
|
2397
|
+
text: ` to ${(await this.printableType(n)).padStart(7)} ${this.describe(id)}`,
|
|
2398
|
+
objectId: id,
|
|
2399
|
+
});
|
|
2400
|
+
this.errorsFound |= FSCK_ERROR_REACHABLE;
|
|
2401
|
+
}
|
|
2402
|
+
return 1;
|
|
2403
|
+
}
|
|
2404
|
+
this.pending.push(n);
|
|
2405
|
+
return 0;
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
/** git's `fsck_handle_ref`. */
|
|
2409
|
+
private async handleRef(name: string, id: string): Promise<void> {
|
|
2410
|
+
const n = await this.parseObject(id);
|
|
2411
|
+
if (n === undefined) {
|
|
2412
|
+
// a promisor object: a valid ref
|
|
2413
|
+
if (this.promised?.has(id)) {
|
|
2414
|
+
this.defaultRefs++;
|
|
2415
|
+
return;
|
|
2416
|
+
}
|
|
2417
|
+
this.error(`${name}: invalid sha1 pointer ${id}`, id);
|
|
2418
|
+
this.errorsFound |= FSCK_ERROR_REACHABLE;
|
|
2419
|
+
return;
|
|
2420
|
+
}
|
|
2421
|
+
if (this.t.type(n) !== OBJ_COMMIT && name.startsWith("refs/heads/")) {
|
|
2422
|
+
this.error(`${name}: not a commit`, id);
|
|
2423
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2424
|
+
}
|
|
2425
|
+
this.defaultRefs++;
|
|
2426
|
+
this.t.addFlags(n, USED);
|
|
2427
|
+
this.putName(id, name);
|
|
2428
|
+
await this.markObject(n, "any", undefined);
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
/** git's `fsck_handle_reflog_oid`. */
|
|
2432
|
+
private async handleReflogOid(refname: string, id: string, timestamp: number): Promise<void> {
|
|
2433
|
+
if (isNullObjectId(id)) return;
|
|
2434
|
+
const n = this.t.lookup(id);
|
|
2435
|
+
if (n >= 0 && this.t.flags(n) & HAS_OBJ) {
|
|
2436
|
+
if (timestamp) this.putName(id, `${refname}@{${timestamp}}`);
|
|
2437
|
+
this.t.addFlags(n, USED);
|
|
2438
|
+
await this.markObject(n, "any", undefined);
|
|
2439
|
+
} else if (!this.promised?.has(id)) {
|
|
2440
|
+
this.error(`${refname}: invalid reflog entry ${id}`, id);
|
|
2441
|
+
this.errorsFound |= FSCK_ERROR_REACHABLE;
|
|
2442
|
+
}
|
|
2443
|
+
}
|
|
2444
|
+
|
|
2445
|
+
/**
|
|
2446
|
+
* What a listed ref resolves to, or the null id when it does not resolve (a dangling symbolic
|
|
2447
|
+
* ref, or one deeper than git's 5 reads: git's `for_each_rawref` passes those with a null id).
|
|
2448
|
+
*/
|
|
2449
|
+
private async resolvedId(name: string): Promise<string> {
|
|
2450
|
+
try {
|
|
2451
|
+
return (await this.followChain(name, 0))?.id || this.format.nullId;
|
|
2452
|
+
} catch {
|
|
2453
|
+
return this.format.nullId;
|
|
2454
|
+
}
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
/** git's `get_default_heads`. */
|
|
2458
|
+
private async processRefs(): Promise<void> {
|
|
2459
|
+
const refs = this.stores.refs;
|
|
2460
|
+
const listed: string[] = [];
|
|
2461
|
+
for await (const ref of refs.list("refs/")) listed.push(ref.name);
|
|
2462
|
+
listed.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
2463
|
+
for (const name of [...new Set(listed)]) {
|
|
2464
|
+
// a symbolic ref that resolves to nothing is the null id to git (`invalid sha1 pointer`)
|
|
2465
|
+
await this.handleRef(name, await this.resolvedId(name));
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
for (const wt of await this.worktrees()) {
|
|
2469
|
+
await this.headLink(wt);
|
|
2470
|
+
if (this.o.reflogs) await this.processReflogs(wt);
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
if (!this.defaultRefs) {
|
|
2474
|
+
this.emit({ kind: "notice", stream: "stderr", text: "notice: No default references" });
|
|
2475
|
+
this.showUnreachable = false;
|
|
2476
|
+
}
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
/** git 2.45+'s `snapshot_refs`: every ref, then each worktree's HEAD, parsed before the objects. */
|
|
2480
|
+
private async snapshotRefs(): Promise<{ name: string; id: string }[]> {
|
|
2481
|
+
const refs = this.stores.refs;
|
|
2482
|
+
const snapshot: { name: string; id: string }[] = [];
|
|
2483
|
+
const snapshotRef = async (name: string, id: string) => {
|
|
2484
|
+
const n = await this.parseObject(id);
|
|
2485
|
+
if (n === undefined) {
|
|
2486
|
+
if (this.promised?.has(id)) {
|
|
2487
|
+
this.defaultRefs++;
|
|
2488
|
+
return;
|
|
2489
|
+
}
|
|
2490
|
+
this.error(`${name}: invalid sha1 pointer ${id}`, id);
|
|
2491
|
+
this.errorsFound |= FSCK_ERROR_REACHABLE;
|
|
2492
|
+
return;
|
|
2493
|
+
}
|
|
2494
|
+
if (this.t.type(n) !== OBJ_COMMIT && name.startsWith("refs/heads/")) {
|
|
2495
|
+
this.error(`${name}: not a commit`, id);
|
|
2496
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2497
|
+
}
|
|
2498
|
+
this.defaultRefs++;
|
|
2499
|
+
snapshot.push({ name, id });
|
|
2500
|
+
};
|
|
2501
|
+
const listed: string[] = [];
|
|
2502
|
+
for await (const ref of refs.list("refs/")) listed.push(ref.name);
|
|
2503
|
+
listed.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
2504
|
+
for (const name of [...new Set(listed)]) await snapshotRef(name, await this.resolvedId(name));
|
|
2505
|
+
for (const wt of await this.worktrees()) {
|
|
2506
|
+
const id = await this.headId(wt);
|
|
2507
|
+
if (id && !isNullObjectId(id)) await snapshotRef(wt.headName, id);
|
|
2508
|
+
}
|
|
2509
|
+
return snapshot;
|
|
2510
|
+
}
|
|
2511
|
+
|
|
2512
|
+
/** git 2.45+'s `process_refs`: the snapshotted refs, then every worktree's reflogs. */
|
|
2513
|
+
private async processSnapshot(snapshot: { name: string; id: string }[]): Promise<void> {
|
|
2514
|
+
for (const { name, id } of snapshot) {
|
|
2515
|
+
const n = await this.parseObject(id);
|
|
2516
|
+
if (n === undefined) continue;
|
|
2517
|
+
this.t.addFlags(n, USED);
|
|
2518
|
+
this.putName(id, name);
|
|
2519
|
+
await this.markObject(n, "any", undefined);
|
|
2520
|
+
}
|
|
2521
|
+
if (this.o.reflogs) {
|
|
2522
|
+
for (const wt of await this.worktrees()) await this.processReflogs(wt);
|
|
2523
|
+
}
|
|
2524
|
+
if (!this.defaultRefs) {
|
|
2525
|
+
this.emit({ kind: "notice", stream: "stderr", text: "notice: No default references" });
|
|
2526
|
+
this.showUnreachable = false;
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
|
|
2530
|
+
private async processReflogs(wt: WorktreeState): Promise<void> {
|
|
2531
|
+
for (const log of await this.reflogs(wt)) {
|
|
2532
|
+
// git 2.43's reflog iterator resolves each ref first and skips one deeper than 5 reads
|
|
2533
|
+
if (this.o.compatibility === "2.43" && log.path && (await this.tooDeep(wt, log.name))) {
|
|
2534
|
+
this.error(`bad ref for ${log.path}`);
|
|
2535
|
+
continue;
|
|
2536
|
+
}
|
|
2537
|
+
for (const e of log.entries) {
|
|
2538
|
+
await this.handleReflogOid(log.name, e.oldId, 0);
|
|
2539
|
+
await this.handleReflogOid(log.name, e.newId, e.timestamp);
|
|
2540
|
+
}
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
|
|
2544
|
+
/** What a worktree's HEAD resolves to, if anything (within git's 5 reads, HEAD's included). */
|
|
2545
|
+
private async headId(wt: WorktreeState): Promise<string | undefined> {
|
|
2546
|
+
// a reftable repository keeps a linked worktree's HEAD in that worktree's stack (its
|
|
2547
|
+
// `HEAD` file is git's `ref: refs/heads/.invalid` stub), read as `worktrees/<id>/HEAD`
|
|
2548
|
+
if (wt.current || this.isReftable()) {
|
|
2549
|
+
const head = await this.repositoryRefs().get(wt.headName);
|
|
2550
|
+
if (!head) return undefined;
|
|
2551
|
+
if (!isSymbolicRef(head)) return head.objectId;
|
|
2552
|
+
return (await this.followChain(head.target, 1))?.id;
|
|
2553
|
+
}
|
|
2554
|
+
const text = (await readText(this.files as FilesApi, pathOf(wt.dir as string, "HEAD")))?.trim();
|
|
2555
|
+
if (!text) return undefined;
|
|
2556
|
+
if (text.startsWith("ref:")) return (await this.followChain(text.slice(4).trim(), 1))?.id;
|
|
2557
|
+
const id = text.slice(0, this.format.hexLength);
|
|
2558
|
+
return OID.test(id) ? id : undefined;
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
/**
|
|
2562
|
+
* Whether the ref of the reflog `logName` is a symbolic ref chain deeper than git's 5 reads
|
|
2563
|
+
* (`resolve_ref_unsafe` fails: git 2.43 prints `bad ref for <log>`; a missing ref is fine).
|
|
2564
|
+
*/
|
|
2565
|
+
private async tooDeep(wt: WorktreeState, logName: string): Promise<boolean> {
|
|
2566
|
+
try {
|
|
2567
|
+
if (logName === "HEAD" && wt.current) {
|
|
2568
|
+
const head = await this.stores.refs.get("HEAD");
|
|
2569
|
+
return !!head && isSymbolicRef(head) && !(await this.followChain(head.target, 1));
|
|
2570
|
+
}
|
|
2571
|
+
if (logName.startsWith("worktrees/")) {
|
|
2572
|
+
if (!wt.dir || !logName.endsWith("/HEAD") || logName.split("/").length !== 3) return false;
|
|
2573
|
+
if (this.isReftable()) {
|
|
2574
|
+
const head = await this.repositoryRefs().get(logName);
|
|
2575
|
+
return !!head && isSymbolicRef(head) && !(await this.followChain(head.target, 1));
|
|
2576
|
+
}
|
|
2577
|
+
const text = (await readText(this.files as FilesApi, pathOf(wt.dir, "HEAD")))?.trim();
|
|
2578
|
+
return !!text?.startsWith("ref:") && !(await this.followChain(text.slice(4).trim(), 1));
|
|
2579
|
+
}
|
|
2580
|
+
return !(await this.followChain(logName, 0));
|
|
2581
|
+
} catch {
|
|
2582
|
+
return false;
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
|
|
2586
|
+
/**
|
|
2587
|
+
* The refs of the repository's own git directory: a reftable store opened for a linked
|
|
2588
|
+
* worktree reads that worktree's HEAD, so fsck asks the repository's store
|
|
2589
|
+
* (`ReftableRefStore.repositoryStore`) for the main worktree's; other stores as they are.
|
|
2590
|
+
*/
|
|
2591
|
+
private repositoryRefs(): Refs {
|
|
2592
|
+
const refs = this.stores.refs as Refs & { repositoryStore?(): Refs };
|
|
2593
|
+
return refs.repositoryStore?.() ?? refs;
|
|
2594
|
+
}
|
|
2595
|
+
|
|
2596
|
+
/** Whether the refs are a reftable store (whose linked worktrees' HEADs are in their stacks). */
|
|
2597
|
+
private isReftable(): boolean {
|
|
2598
|
+
return "reflogNames" in this.stores.refs;
|
|
2599
|
+
}
|
|
2600
|
+
|
|
2601
|
+
/** The main worktree, then the linked ones (`.git/worktrees/<id>`). */
|
|
2602
|
+
private async worktrees(): Promise<WorktreeState[]> {
|
|
2603
|
+
const list: WorktreeState[] = [{ headName: "HEAD", dir: this.gitDir, current: true }];
|
|
2604
|
+
const files = this.files;
|
|
2605
|
+
if (!files) return list;
|
|
2606
|
+
const dir = pathOf(this.gitDir, "worktrees");
|
|
2607
|
+
if ((await files.stats(dir))?.kind !== "directory") return list;
|
|
2608
|
+
const names: string[] = [];
|
|
2609
|
+
for await (const e of files.list(dir)) {
|
|
2610
|
+
if (e.kind === "directory" && (await files.stats(pathOf(e.path, "HEAD")))?.kind === "file") {
|
|
2611
|
+
names.push(e.name);
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
names.sort();
|
|
2615
|
+
for (const name of names) {
|
|
2616
|
+
list.push({ headName: `worktrees/${name}/HEAD`, dir: pathOf(dir, name), current: false });
|
|
2617
|
+
}
|
|
2618
|
+
return list;
|
|
2619
|
+
}
|
|
2620
|
+
|
|
2621
|
+
/**
|
|
2622
|
+
* Where the symbolic ref chain from `target` ends, as git's `resolve_ref_unsafe` follows it:
|
|
2623
|
+
* the last name and its id (none when that ref does not exist), or undefined when the chain
|
|
2624
|
+
* takes more than git's SYMREF_MAXDEPTH (5) reads in all. `reads` is how many were made
|
|
2625
|
+
* before `target` (1 when HEAD, read first, points at it).
|
|
2626
|
+
*/
|
|
2627
|
+
private async followChain(
|
|
2628
|
+
target: string,
|
|
2629
|
+
reads: number,
|
|
2630
|
+
): Promise<{ name: string; id?: string } | undefined> {
|
|
2631
|
+
let name = target;
|
|
2632
|
+
for (let read = reads + 1; ; read++) {
|
|
2633
|
+
const value = await this.stores.refs.get(name);
|
|
2634
|
+
if (!value) return { name };
|
|
2635
|
+
if (!isSymbolicRef(value)) return { name, id: value.objectId };
|
|
2636
|
+
if (read >= 5) return undefined;
|
|
2637
|
+
name = value.target;
|
|
2638
|
+
}
|
|
2639
|
+
}
|
|
2640
|
+
|
|
2641
|
+
/** git's `fsck_head_link` and the HEAD of a worktree as a root. */
|
|
2642
|
+
private async headLink(wt: WorktreeState): Promise<void> {
|
|
2643
|
+
const name = wt.headName;
|
|
2644
|
+
let target: string | undefined;
|
|
2645
|
+
let id: string | undefined;
|
|
2646
|
+
const invalid = () => {
|
|
2647
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2648
|
+
this.error(`invalid ${name}`);
|
|
2649
|
+
};
|
|
2650
|
+
let symref: string | undefined;
|
|
2651
|
+
if (wt.current) {
|
|
2652
|
+
const head = await this.repositoryRefs().get("HEAD");
|
|
2653
|
+
if (!head) return invalid();
|
|
2654
|
+
if (isSymbolicRef(head)) symref = head.target;
|
|
2655
|
+
else id = head.objectId;
|
|
2656
|
+
} else if (this.isReftable()) {
|
|
2657
|
+
// a reftable repository: the linked worktree's HEAD is in its own stack
|
|
2658
|
+
const head = await this.repositoryRefs().get(name);
|
|
2659
|
+
if (!head) return invalid();
|
|
2660
|
+
if (isSymbolicRef(head)) symref = head.target;
|
|
2661
|
+
else id = head.objectId;
|
|
2662
|
+
} else {
|
|
2663
|
+
const text = (
|
|
2664
|
+
await readText(this.files as FilesApi, pathOf(wt.dir as string, "HEAD"))
|
|
2665
|
+
)?.trim();
|
|
2666
|
+
if (!text) return invalid();
|
|
2667
|
+
if (text.startsWith("ref:")) symref = text.slice(4).trim();
|
|
2668
|
+
else if (OID.test(text.slice(0, this.format.hexLength)))
|
|
2669
|
+
id = text.slice(0, this.format.hexLength);
|
|
2670
|
+
else return invalid();
|
|
2671
|
+
}
|
|
2672
|
+
if (symref !== undefined) {
|
|
2673
|
+
// the whole chain: HEAD -> refs/heads/alias -> refs/remotes/origin/x points at the last
|
|
2674
|
+
// HEAD itself was the first of git's 5 reads
|
|
2675
|
+
const end = await this.followChain(symref, 1);
|
|
2676
|
+
if (!end) return invalid();
|
|
2677
|
+
target = end.name;
|
|
2678
|
+
id = end.id;
|
|
2679
|
+
}
|
|
2680
|
+
if (target !== undefined && !target.startsWith("refs/heads/")) {
|
|
2681
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2682
|
+
this.error(`${name} points to something strange (${target})`);
|
|
2683
|
+
// git still takes what HEAD resolves to as a root
|
|
2684
|
+
if (id && !isNullObjectId(id)) await this.handleRef(name, id);
|
|
2685
|
+
return;
|
|
2686
|
+
}
|
|
2687
|
+
if (!id || isNullObjectId(id)) {
|
|
2688
|
+
if (target === undefined) {
|
|
2689
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2690
|
+
this.error(`${name}: detached HEAD points at nothing`);
|
|
2691
|
+
return;
|
|
2692
|
+
}
|
|
2693
|
+
this.emit({
|
|
2694
|
+
kind: "notice",
|
|
2695
|
+
stream: "stderr",
|
|
2696
|
+
text: `notice: ${name} points to an unborn branch (${target.slice(11)})`,
|
|
2697
|
+
});
|
|
2698
|
+
return;
|
|
2699
|
+
}
|
|
2700
|
+
await this.handleRef(name, id);
|
|
2701
|
+
}
|
|
2702
|
+
|
|
2703
|
+
/** The reflogs of one worktree, in git's order (sorted by name, as git 2.45+ lists them). */
|
|
2704
|
+
private async reflogs(wt: WorktreeState): Promise<Reflog[]> {
|
|
2705
|
+
const files = this.files;
|
|
2706
|
+
// a reftable repository keeps its reflogs in its tables: listed by the ref store
|
|
2707
|
+
const reftable = this.repositoryRefs() as {
|
|
2708
|
+
reflogNames?(worktree?: string): Promise<string[]>;
|
|
2709
|
+
};
|
|
2710
|
+
if (reftable.reflogNames) {
|
|
2711
|
+
const dir = wt.dir as string;
|
|
2712
|
+
const id = wt.current ? undefined : dir.slice(dir.lastIndexOf("/") + 1);
|
|
2713
|
+
return this.storeReflogs(await reftable.reflogNames(id), this.repositoryRefs());
|
|
2714
|
+
}
|
|
2715
|
+
if (!files) return wt.current ? this.storeReflogs() : [];
|
|
2716
|
+
const logs: Reflog[] = [];
|
|
2717
|
+
const collectDir = async (base: string, keep: (name: string) => boolean, prefix: string) => {
|
|
2718
|
+
const found: { name: string; path: string }[] = [];
|
|
2719
|
+
const walk = async (dir: string, rel: string) => {
|
|
2720
|
+
if ((await files.stats(dir))?.kind !== "directory") return;
|
|
2721
|
+
const entries: { name: string; path: string; kind: string }[] = [];
|
|
2722
|
+
for await (const e of files.list(dir)) entries.push(e);
|
|
2723
|
+
entries.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
2724
|
+
for (const e of entries) {
|
|
2725
|
+
const name = rel ? `${rel}/${e.name}` : e.name;
|
|
2726
|
+
if (e.kind === "directory") await walk(e.path, name);
|
|
2727
|
+
else if (keep(name)) found.push({ name, path: e.path });
|
|
2728
|
+
}
|
|
2729
|
+
};
|
|
2730
|
+
await walk(base, "");
|
|
2731
|
+
for (const f of found) {
|
|
2732
|
+
logs.push({
|
|
2733
|
+
name: prefix + f.name,
|
|
2734
|
+
path: pathOf(base, f.name),
|
|
2735
|
+
entries: parseReflog(await readText(files, f.path), this.format.hexLength),
|
|
2736
|
+
});
|
|
2737
|
+
}
|
|
2738
|
+
};
|
|
2739
|
+
if (wt.current) {
|
|
2740
|
+
await collectDir(pathOf(this.gitDir, "logs"), () => true, "");
|
|
2741
|
+
} else {
|
|
2742
|
+
const dir = wt.dir as string;
|
|
2743
|
+
const id = dir.slice(dir.lastIndexOf("/") + 1);
|
|
2744
|
+
await collectDir(pathOf(dir, "logs"), (n) => PER_WORKTREE.test(n), `worktrees/${id}/`);
|
|
2745
|
+
await collectDir(pathOf(this.gitDir, "logs"), (n) => !PER_WORKTREE.test(n), "");
|
|
2746
|
+
logs.sort((a, b) => {
|
|
2747
|
+
const x = a.name.replace(/^worktrees\/[^/]+\//, "");
|
|
2748
|
+
const y = b.name.replace(/^worktrees\/[^/]+\//, "");
|
|
2749
|
+
return x < y ? -1 : x > y ? 1 : 0;
|
|
2750
|
+
});
|
|
2751
|
+
}
|
|
2752
|
+
return logs;
|
|
2753
|
+
}
|
|
2754
|
+
|
|
2755
|
+
/** Reflogs through the refs store (a store without files): HEAD and every ref. */
|
|
2756
|
+
private async storeReflogs(listed?: string[], refs = this.stores.refs): Promise<Reflog[]> {
|
|
2757
|
+
if (!refs.getReflog) return [];
|
|
2758
|
+
let sorted = listed;
|
|
2759
|
+
if (!sorted) {
|
|
2760
|
+
const names = ["HEAD"];
|
|
2761
|
+
for await (const ref of refs.list("refs/")) names.push(ref.name);
|
|
2762
|
+
sorted = [...new Set(names)].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
|
2763
|
+
}
|
|
2764
|
+
const logs: Reflog[] = [];
|
|
2765
|
+
for (const name of sorted) {
|
|
2766
|
+
const reader = await refs.getReflog(name);
|
|
2767
|
+
if (!reader) continue;
|
|
2768
|
+
const entries = (await reader.getReverseEntries()).reverse();
|
|
2769
|
+
if (entries.length === 0) continue;
|
|
2770
|
+
logs.push({
|
|
2771
|
+
name,
|
|
2772
|
+
entries: entries.map((e) => ({
|
|
2773
|
+
oldId: e.oldId,
|
|
2774
|
+
newId: e.newId,
|
|
2775
|
+
timestamp: e.who.timestamp,
|
|
2776
|
+
})),
|
|
2777
|
+
});
|
|
2778
|
+
}
|
|
2779
|
+
return logs;
|
|
2780
|
+
}
|
|
2781
|
+
|
|
2782
|
+
/**
|
|
2783
|
+
* git's index pass: every worktree's index (`fsck_index`). Returns git's lines when it dies
|
|
2784
|
+
* reading one (`read_index_from` with `verify_index_checksum`: a wrong trailer, of the index
|
|
2785
|
+
* or its shared index, is `error: bad index file sha1 signature` then `fatal: index file
|
|
2786
|
+
* corrupt`, exit 128).
|
|
2787
|
+
*/
|
|
2788
|
+
private async processIndexes(): Promise<readonly string[] | undefined> {
|
|
2789
|
+
const files = this.files;
|
|
2790
|
+
if (!files) {
|
|
2791
|
+
await this.storeIndex();
|
|
2792
|
+
return undefined;
|
|
2793
|
+
}
|
|
2794
|
+
for (const wt of await this.worktrees()) {
|
|
2795
|
+
const indexPath = pathOf(wt.dir as string, "index");
|
|
2796
|
+
if ((await files.stats(indexPath))?.kind !== "file") continue;
|
|
2797
|
+
// a split index merged with its shared index, a sparse index expanded (repo_read_index)
|
|
2798
|
+
let read: Awaited<ReturnType<typeof readIndexFileEntries>>;
|
|
2799
|
+
try {
|
|
2800
|
+
// fsck alone checks the index's trailing hash (verify_index_checksum)
|
|
2801
|
+
read = await readIndexFileEntries(files, indexPath, {
|
|
2802
|
+
gitDir: wt.dir as string,
|
|
2803
|
+
trees: this.stores.trees(),
|
|
2804
|
+
verifyChecksum: true,
|
|
2805
|
+
objectFormat: this.format,
|
|
2806
|
+
});
|
|
2807
|
+
} catch (error) {
|
|
2808
|
+
if (error instanceof IndexCorruptError) return error.lines;
|
|
2809
|
+
// a sparse directory whose tree is missing: its own entry stands for it (the index
|
|
2810
|
+
// and its shared index were hashed before the expansion failed)
|
|
2811
|
+
try {
|
|
2812
|
+
read = await readIndexFileEntries(files, indexPath, {
|
|
2813
|
+
gitDir: wt.dir as string,
|
|
2814
|
+
objectFormat: this.format,
|
|
2815
|
+
});
|
|
2816
|
+
} catch (again) {
|
|
2817
|
+
if (again instanceof IndexCorruptError) return again.lines;
|
|
2818
|
+
throw again;
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
if (!read) continue;
|
|
2822
|
+
const parsed = { entries: read.entries, extensionData: read.extensions };
|
|
2823
|
+
const prefix = wt.current ? "" : indexPath;
|
|
2824
|
+
const entries = parsed.entries.map((e) => ({
|
|
2825
|
+
path: e.path,
|
|
2826
|
+
mode: e.mode,
|
|
2827
|
+
objectId: e.objectId,
|
|
2828
|
+
}));
|
|
2829
|
+
let cacheTree: CacheTree | undefined;
|
|
2830
|
+
let resolveUndo: ResolveUndoRecord[] = [];
|
|
2831
|
+
for (const ext of parsed.extensionData) {
|
|
2832
|
+
const raw = this.format.rawLength;
|
|
2833
|
+
if (ext.signature === "TREE") cacheTree = parseCacheTree(ext.data, raw);
|
|
2834
|
+
else if (ext.signature === "REUC") resolveUndo = parseResolveUndo(ext.data, raw);
|
|
2835
|
+
}
|
|
2836
|
+
await this.fsckIndex(entries, cacheTree, resolveUndo, indexPath, prefix);
|
|
2837
|
+
}
|
|
2838
|
+
return undefined;
|
|
2839
|
+
}
|
|
2840
|
+
|
|
2841
|
+
/** The staging area of a store without files, as the current worktree's index. */
|
|
2842
|
+
private async storeIndex(): Promise<void> {
|
|
2843
|
+
let staging: Staging;
|
|
2844
|
+
try {
|
|
2845
|
+
staging = this.stores.staging();
|
|
2846
|
+
} catch {
|
|
2847
|
+
return;
|
|
2848
|
+
}
|
|
2849
|
+
const entries: { path: string; mode: number; objectId: string }[] = [];
|
|
2850
|
+
for await (const e of staging.entries()) {
|
|
2851
|
+
entries.push({ path: e.path, mode: e.mode, objectId: e.objectId });
|
|
2852
|
+
}
|
|
2853
|
+
await this.fsckIndex(entries, staging.getCacheTree?.(), [], `${this.gitDir}/index`, "");
|
|
2854
|
+
}
|
|
2855
|
+
|
|
2856
|
+
/** git's `fsck_index`. */
|
|
2857
|
+
private async fsckIndex(
|
|
2858
|
+
entries: { path: string; mode: number; objectId: string }[],
|
|
2859
|
+
cacheTree: CacheTree | undefined,
|
|
2860
|
+
resolveUndo: ResolveUndoRecord[],
|
|
2861
|
+
indexPath: string,
|
|
2862
|
+
prefix: string,
|
|
2863
|
+
): Promise<void> {
|
|
2864
|
+
for (const e of entries) {
|
|
2865
|
+
if ((e.mode & 0o170000) === 0o160000) continue;
|
|
2866
|
+
const blob = this.t.lookupTyped(e.objectId, OBJ_BLOB);
|
|
2867
|
+
if (typeof blob !== "number") {
|
|
2868
|
+
this.error(blob.error);
|
|
2869
|
+
continue;
|
|
2870
|
+
}
|
|
2871
|
+
this.t.addFlags(blob, USED);
|
|
2872
|
+
this.putName(e.objectId, `${prefix}:${e.path}`);
|
|
2873
|
+
await this.markObject(blob, "any", undefined);
|
|
2874
|
+
}
|
|
2875
|
+
if (cacheTree) await this.fsckCacheTree(cacheTree, indexPath);
|
|
2876
|
+
// resolve-undo: git keeps it as a string list sorted by path
|
|
2877
|
+
const sorted = [...resolveUndo].sort((a, b) =>
|
|
2878
|
+
a.path < b.path ? -1 : a.path > b.path ? 1 : 0,
|
|
2879
|
+
);
|
|
2880
|
+
for (const record of sorted) {
|
|
2881
|
+
for (let i = 0; i < 3; i++) {
|
|
2882
|
+
const mode = record.modes[i] ?? 0;
|
|
2883
|
+
const id = record.objectIds[i];
|
|
2884
|
+
if (!mode || (mode & 0o170000) !== 0o100000 || !id) continue;
|
|
2885
|
+
const n = await this.parseObject(id);
|
|
2886
|
+
if (n === undefined) {
|
|
2887
|
+
this.error(`${id}: invalid sha1 pointer in resolve-undo of ${indexPath}`, id);
|
|
2888
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2889
|
+
continue;
|
|
2890
|
+
}
|
|
2891
|
+
this.t.addFlags(n, USED);
|
|
2892
|
+
this.putName(id, `:(${i}):${record.path}`);
|
|
2893
|
+
await this.markObject(n, "any", undefined);
|
|
2894
|
+
}
|
|
2895
|
+
}
|
|
2896
|
+
}
|
|
2897
|
+
|
|
2898
|
+
/** git's `fsck_cache_tree`. */
|
|
2899
|
+
private async fsckCacheTree(node: CacheTree, indexPath: string): Promise<void> {
|
|
2900
|
+
if (node.entryCount >= 0 && node.id) {
|
|
2901
|
+
const n = await this.parseObject(node.id);
|
|
2902
|
+
if (n === undefined) {
|
|
2903
|
+
this.error(`${node.id}: invalid sha1 pointer in cache-tree of ${indexPath}`, node.id);
|
|
2904
|
+
this.errorsFound |= FSCK_ERROR_REFS;
|
|
2905
|
+
return;
|
|
2906
|
+
}
|
|
2907
|
+
this.t.addFlags(n, USED);
|
|
2908
|
+
this.putName(node.id, ":");
|
|
2909
|
+
await this.markObject(n, "any", undefined);
|
|
2910
|
+
if (this.t.type(n) !== OBJ_TREE) await this.objerror(n, "non-tree in cache-tree");
|
|
2911
|
+
}
|
|
2912
|
+
for (const sub of node.subtrees) await this.fsckCacheTree(sub.tree, indexPath);
|
|
2913
|
+
}
|
|
2914
|
+
|
|
2915
|
+
// ------------------------------------------------------------------------------------------
|
|
2916
|
+
// Connectivity
|
|
2917
|
+
|
|
2918
|
+
/**
|
|
2919
|
+
* The pack-order scan of a connectivity-only run (`scanPackObjects`): the commits (unless a
|
|
2920
|
+
* commit-graph gives them) and trees of each pack, unpacked in one read and kept as the
|
|
2921
|
+
* objects they link to (`scanAt`). An object is taken from the first pack that holds it,
|
|
2922
|
+
* as the store reads it, and never when it is loose too (the store may read that copy); a
|
|
2923
|
+
* pack that cannot be read leaves its objects to the walk's reads. At most `linksKept`
|
|
2924
|
+
* links are kept: the scan stops there, and the walk reads the rest from the store.
|
|
2925
|
+
*/
|
|
2926
|
+
private async scanPacks(packed: Int32Array[], loose: number[]): Promise<void> {
|
|
2927
|
+
const files = this.files as FilesApi;
|
|
2928
|
+
const t = this.t;
|
|
2929
|
+
const claimed = new Uint8Array(t.size);
|
|
2930
|
+
for (const n of loose) claimed[n] = 1;
|
|
2931
|
+
this.scanAt = new Int32Array(t.size);
|
|
2932
|
+
this.scanKids = new Int32Array(
|
|
2933
|
+
Math.max(0, Math.min(1 << 16, this.o.linksKept - this.kidsLength)),
|
|
2934
|
+
);
|
|
2935
|
+
const withCommits = !this.graph;
|
|
2936
|
+
const keepCommits = this.graphCommits !== undefined;
|
|
2937
|
+
/** objects whose copy the commit-graph verification reads is decided (an earlier pack's) */
|
|
2938
|
+
const decided = keepCommits ? new Uint8Array(t.size) : undefined;
|
|
2939
|
+
let full = false;
|
|
2940
|
+
for (let k = 0; k < this.packs.length && !full; k++) {
|
|
2941
|
+
const p = this.packs[k] as RepoPack;
|
|
2942
|
+
const ns = packed[k] as Int32Array;
|
|
2943
|
+
try {
|
|
2944
|
+
this.objectsScanned += await scanPackObjects(
|
|
2945
|
+
files,
|
|
2946
|
+
p.packPath,
|
|
2947
|
+
p.idxBytes,
|
|
2948
|
+
p.idx,
|
|
2949
|
+
(type) => type === OBJ_TREE || (type === OBJ_COMMIT && (withCommits || keepCommits)),
|
|
2950
|
+
(pos, type, content) => {
|
|
2951
|
+
const n = ns[pos] as number;
|
|
2952
|
+
if (type === OBJ_COMMIT && decided && !decided[n]) {
|
|
2953
|
+
decided[n] = 1;
|
|
2954
|
+
const raw = this.format.rawLength;
|
|
2955
|
+
const at = p.idx.version > 1 ? 8 + 1024 + pos * raw : 1024 + 4 + pos * (raw + 4);
|
|
2956
|
+
this.keepGraphCommit(bytesToHex(p.idxBytes.subarray(at, at + raw)), content);
|
|
2957
|
+
}
|
|
2958
|
+
if (type === OBJ_COMMIT && !withCommits) return;
|
|
2959
|
+
if (claimed[n]) return;
|
|
2960
|
+
claimed[n] = 1;
|
|
2961
|
+
if (!this.recordScanned(n, type, content)) {
|
|
2962
|
+
full = true;
|
|
2963
|
+
return false;
|
|
2964
|
+
}
|
|
2965
|
+
},
|
|
2966
|
+
);
|
|
2967
|
+
} catch {
|
|
2968
|
+
// the walk reads what this pack holds through the store
|
|
2969
|
+
}
|
|
2970
|
+
for (let i = 0; i < ns.length; i++) {
|
|
2971
|
+
claimed[ns[i] as number] = 1;
|
|
2972
|
+
if (decided) decided[ns[i] as number] = 1;
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
|
|
2977
|
+
/**
|
|
2978
|
+
* `scanAt` for object `n`, a commit or a tree, from its content. False when it does not fit
|
|
2979
|
+
* in `linksKept` (nothing recorded).
|
|
2980
|
+
*/
|
|
2981
|
+
private recordScanned(n: number, type: number, content: Uint8Array): boolean {
|
|
2982
|
+
const t = this.t;
|
|
2983
|
+
const start = this.scanKidsLength;
|
|
2984
|
+
let at = start + 1;
|
|
2985
|
+
if (type === OBJ_TREE) {
|
|
2986
|
+
// the entries as `scanTree` decodes them, for a tree it decodes without an error (any
|
|
2987
|
+
// other is left to the walk, which reports it)
|
|
2988
|
+
const len = content.length;
|
|
2989
|
+
const raw = this.format.rawLength;
|
|
2990
|
+
if (len > 0 && (len < raw + 3 || content[len - raw - 1] !== 0)) return true;
|
|
2991
|
+
if (!this.reserveScanKids(1 + Math.floor(len / (raw + 3)))) return false;
|
|
2992
|
+
const kids = this.scanKids;
|
|
2993
|
+
let p = 0;
|
|
2994
|
+
while (p < len) {
|
|
2995
|
+
if (len - p < raw + 3 || content[p] === 0x20) return true;
|
|
2996
|
+
let mode = 0;
|
|
2997
|
+
for (;;) {
|
|
2998
|
+
const c = content[p++] as number;
|
|
2999
|
+
if (c === 0x20) break;
|
|
3000
|
+
if (c < 0x30 || c > 0x37) return true;
|
|
3001
|
+
mode = (mode * 8 + (c - 0x30)) & 0xffff;
|
|
3002
|
+
}
|
|
3003
|
+
if (content[p] === 0) return true;
|
|
3004
|
+
while (p < len && content[p] !== 0) p++;
|
|
3005
|
+
if (p + 1 + raw > len) return true;
|
|
3006
|
+
const idAt = p + 1;
|
|
3007
|
+
p = idAt + raw;
|
|
3008
|
+
const format = mode & 0o170000;
|
|
3009
|
+
// gitlinks are not walked (canon_mode makes every other format one)
|
|
3010
|
+
if (format !== 0o100000 && format !== 0o120000 && format !== 0o040000) continue;
|
|
3011
|
+
const c = t.peekRaw(content, idAt);
|
|
3012
|
+
if (c < 0) return true;
|
|
3013
|
+
kids[at++] = (c << 1) | (format === 0o040000 ? 1 : 0);
|
|
3014
|
+
}
|
|
3015
|
+
} else {
|
|
3016
|
+
const parsed = parseCommitLinks(content, "", this.format);
|
|
3017
|
+
if ("error" in parsed) return true;
|
|
3018
|
+
if (!this.reserveScanKids(2 + parsed.parents.length)) return false;
|
|
3019
|
+
const kids = this.scanKids;
|
|
3020
|
+
const raw = this.idScratch;
|
|
3021
|
+
const tree = t.peekRaw(rawIdAt(content, parsed.tree, raw), 0);
|
|
3022
|
+
if (tree < 0) return true;
|
|
3023
|
+
kids[at++] = tree;
|
|
3024
|
+
for (const p of parsed.parents) {
|
|
3025
|
+
const c = t.peekRaw(rawIdAt(content, p, raw), 0);
|
|
3026
|
+
if (c < 0) return true;
|
|
3027
|
+
kids[at++] = c;
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
3030
|
+
this.scanKids[start] = ((at - start - 1) << 2) | type;
|
|
3031
|
+
this.scanKidsLength = at;
|
|
3032
|
+
this.scanAt[n] = start + 1;
|
|
3033
|
+
return true;
|
|
3034
|
+
}
|
|
3035
|
+
|
|
3036
|
+
/**
|
|
3037
|
+
* Room in `scanKids` for `more` values, within what `linksKept` leaves after `kids` (the
|
|
3038
|
+
* limit is the two together); false past it.
|
|
3039
|
+
*/
|
|
3040
|
+
private reserveScanKids(more: number): boolean {
|
|
3041
|
+
const need = this.scanKidsLength + more;
|
|
3042
|
+
const limit = this.o.linksKept - this.kidsLength;
|
|
3043
|
+
if (need > limit) return false;
|
|
3044
|
+
if (need <= this.scanKids.length) return true;
|
|
3045
|
+
let size = Math.max(this.scanKids.length * 2, 16);
|
|
3046
|
+
while (size < need) size *= 2;
|
|
3047
|
+
const kids = new Int32Array(Math.min(size, limit));
|
|
3048
|
+
kids.set(this.scanKids.subarray(0, this.scanKidsLength));
|
|
3049
|
+
this.scanKids = kids;
|
|
3050
|
+
return true;
|
|
3051
|
+
}
|
|
3052
|
+
|
|
3053
|
+
/**
|
|
3054
|
+
* git's parse of commit `n` from what the scan kept (`scanAt` start `s`): the lookups
|
|
3055
|
+
* `parse_commit_buffer` makes, in its order. False, with nothing done, when one of them would
|
|
3056
|
+
* fail (an object of another type): the walk then reads the commit as it reads any other.
|
|
3057
|
+
*/
|
|
3058
|
+
private parseScannedCommit(n: number, s: number): boolean {
|
|
3059
|
+
const t = this.t;
|
|
3060
|
+
const kids = this.scanKids;
|
|
3061
|
+
const count = (kids[s] as number) >> 2;
|
|
3062
|
+
const tree = kids[s + 1] as number;
|
|
3063
|
+
const have = t.type(tree);
|
|
3064
|
+
if (have !== OBJ_NONE && have !== OBJ_TREE) return false;
|
|
3065
|
+
// a grafted commit is read as any other (its parents are not the ones scanned)
|
|
3066
|
+
if (this.grafts?.has(t.id(n))) return false;
|
|
3067
|
+
const shallow = this.shallow.size > 0 && this.shallow.has(t.id(n));
|
|
3068
|
+
if (!shallow) {
|
|
3069
|
+
for (let i = s + 2; i <= s + count; i++) {
|
|
3070
|
+
const p = kids[i] as number;
|
|
3071
|
+
const pt = t.type(p);
|
|
3072
|
+
if (p === tree || (pt !== OBJ_NONE && pt !== OBJ_COMMIT)) return false;
|
|
3073
|
+
}
|
|
3074
|
+
}
|
|
3075
|
+
if (this.graftsHint) this.adviseGrafts();
|
|
3076
|
+
if (this.graftErrors.length > 0) this.reportGraftErrors();
|
|
3077
|
+
// parse_object's lookup, then parse_object_buffer's
|
|
3078
|
+
t.touch(n);
|
|
3079
|
+
t.setType(n, OBJ_COMMIT);
|
|
3080
|
+
this.objectsRead++;
|
|
3081
|
+
t.lookupTypedN(tree, OBJ_TREE);
|
|
3082
|
+
const parents: number[] = [];
|
|
3083
|
+
if (!shallow) {
|
|
3084
|
+
for (let i = s + 2; i <= s + count; i++) {
|
|
3085
|
+
const p = kids[i] as number;
|
|
3086
|
+
t.lookupTypedN(p, OBJ_COMMIT);
|
|
3087
|
+
parents.push(p);
|
|
3088
|
+
}
|
|
3089
|
+
}
|
|
3090
|
+
t.links.set(n, { tree, parents });
|
|
3091
|
+
t.addFlags(n, PARSED);
|
|
3092
|
+
return true;
|
|
3093
|
+
}
|
|
3094
|
+
|
|
3095
|
+
/**
|
|
3096
|
+
* git's `fsck_walk` with `mark_object` (`traverse_one_object`), for `runSteps`: it yields only
|
|
3097
|
+
* the promises it waits for, so an object whose reads are in memory is walked synchronously.
|
|
3098
|
+
*/
|
|
3099
|
+
private *traverseOne(n: number): Steps<void> {
|
|
3100
|
+
const t = this.t;
|
|
3101
|
+
// fsck_walk returns at once for a blob (no id to build, nothing to read)
|
|
3102
|
+
if (t.type(n) === OBJ_BLOB) return;
|
|
3103
|
+
if (t.type(n) === OBJ_TREE && t.flags(n) & PARSED) {
|
|
3104
|
+
const start = n < this.treeStart.length ? (this.treeStart[n] as number) - 1 : -1;
|
|
3105
|
+
if (start >= 0) {
|
|
3106
|
+
if (this.badCopy.size === 0 || !this.badCopy.has(t.id(n))) {
|
|
3107
|
+
// checked in full already: its entries again, as they resolved then
|
|
3108
|
+
const kids = this.kids;
|
|
3109
|
+
for (let i = start + 1, end = start + 1 + (kids[start] as number); i < end; i++) {
|
|
3110
|
+
const v = kids[i] as number;
|
|
3111
|
+
const c = v >> 1;
|
|
3112
|
+
const type = v & 1 ? OBJ_TREE : OBJ_BLOB;
|
|
3113
|
+
t.touch(c); // the lookup git makes moves it in the hash
|
|
3114
|
+
if (!this.markReachable(c, type)) {
|
|
3115
|
+
const r = this.markObject(c, type, n);
|
|
3116
|
+
if (r instanceof Promise) yield r;
|
|
3117
|
+
}
|
|
3118
|
+
}
|
|
3119
|
+
return;
|
|
3120
|
+
}
|
|
3121
|
+
}
|
|
3122
|
+
}
|
|
3123
|
+
// what the pack-order scan kept of it, when it holds an object of the type the walk expects
|
|
3124
|
+
// (an object met untyped is parse_object's, which checks its hash: read from the store)
|
|
3125
|
+
const s = n < this.scanAt.length ? (this.scanAt[n] as number) - 1 : -1;
|
|
3126
|
+
let scanned = false;
|
|
3127
|
+
if (s >= 0 && !(t.flags(n) & PARSED)) {
|
|
3128
|
+
const kids = this.scanKids;
|
|
3129
|
+
const head = kids[s] as number;
|
|
3130
|
+
const have = t.type(n);
|
|
3131
|
+
if ((head & 3) === OBJ_TREE && have === OBJ_TREE) {
|
|
3132
|
+
const rest = this.walkScannedTree(n, s);
|
|
3133
|
+
if (rest) yield* rest;
|
|
3134
|
+
return;
|
|
3135
|
+
}
|
|
3136
|
+
if ((head & 3) === OBJ_COMMIT && have === OBJ_COMMIT) {
|
|
3137
|
+
scanned = this.parseScannedCommit(n, s);
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
const id = t.id(n);
|
|
3141
|
+
this.lastRead = undefined;
|
|
3142
|
+
if (t.type(n) === OBJ_NONE) {
|
|
3143
|
+
// fsck_walk's parse_object, hash-checked
|
|
3144
|
+
const r = this.parseObject(id);
|
|
3145
|
+
if (r instanceof Promise) yield r;
|
|
3146
|
+
} else if (
|
|
3147
|
+
!scanned &&
|
|
3148
|
+
(t.type(n) === OBJ_COMMIT || t.type(n) === OBJ_TAG) &&
|
|
3149
|
+
!(t.flags(n) & PARSED)
|
|
3150
|
+
) {
|
|
3151
|
+
// repo_parse_commit (from the commit-graph when it has the commit) / parse_tag
|
|
3152
|
+
if (!(t.type(n) === OBJ_COMMIT && this.graph && this.parseCommitInGraph(n, id))) {
|
|
3153
|
+
const r = this.parseAs(n, id);
|
|
3154
|
+
if (r instanceof Promise) yield r;
|
|
3155
|
+
}
|
|
3156
|
+
}
|
|
3157
|
+
const name = this.names?.get(id);
|
|
3158
|
+
switch (t.type(n)) {
|
|
3159
|
+
case OBJ_BLOB:
|
|
3160
|
+
return;
|
|
3161
|
+
case OBJ_TREE: {
|
|
3162
|
+
// the tree just parsed, or read now
|
|
3163
|
+
const last = this.lastRead as { id: string; obj: Whole } | undefined;
|
|
3164
|
+
let obj: Whole | undefined | Promise<Whole | undefined> =
|
|
3165
|
+
last !== undefined && last.id === id ? last.obj : this.readWhole(id);
|
|
3166
|
+
this.lastRead = undefined;
|
|
3167
|
+
if (obj instanceof Promise) obj = (yield obj) as Whole | undefined;
|
|
3168
|
+
// git's parse_tree
|
|
3169
|
+
if (!obj) {
|
|
3170
|
+
if (this.lastZlibError) this.error(this.lastZlibError);
|
|
3171
|
+
this.error(`Could not read ${id}`);
|
|
3172
|
+
return;
|
|
3173
|
+
}
|
|
3174
|
+
if (obj.type !== "tree") {
|
|
3175
|
+
this.error(`Object ${id} not a tree`);
|
|
3176
|
+
return;
|
|
3177
|
+
}
|
|
3178
|
+
t.addFlags(n, PARSED);
|
|
3179
|
+
const walk = scanTree(obj.content, this.walkTable, this.format);
|
|
3180
|
+
if (walk.initError) {
|
|
3181
|
+
this.error(walk.initError);
|
|
3182
|
+
return;
|
|
3183
|
+
}
|
|
3184
|
+
const content = obj.content;
|
|
3185
|
+
const f = walk.fields;
|
|
3186
|
+
for (let i = 0; i < walk.count; i++) {
|
|
3187
|
+
const mode = canonMode(f[3 * i] as number);
|
|
3188
|
+
if (mode === 0o160000) continue;
|
|
3189
|
+
const isTree = mode === 0o040000;
|
|
3190
|
+
const idAt = f[3 * i + 2] as number;
|
|
3191
|
+
const child = t.lookupTypedRaw(content, idAt, isTree ? OBJ_TREE : OBJ_BLOB);
|
|
3192
|
+
let c: number | undefined;
|
|
3193
|
+
if (typeof child === "number") c = child;
|
|
3194
|
+
else this.error(child.error);
|
|
3195
|
+
if (name !== undefined && c !== undefined) {
|
|
3196
|
+
const entryName = content.subarray(f[3 * i + 1] as number, idAt - 1);
|
|
3197
|
+
this.putName(t.id(c), `${name}${decodeGitPath(entryName)}${isTree ? "/" : ""}`);
|
|
3198
|
+
}
|
|
3199
|
+
if (!this.markReachable(c, isTree ? OBJ_TREE : OBJ_BLOB)) {
|
|
3200
|
+
const r = this.markObject(c, isTree ? OBJ_TREE : OBJ_BLOB, n);
|
|
3201
|
+
if (r instanceof Promise) yield r;
|
|
3202
|
+
}
|
|
3203
|
+
}
|
|
3204
|
+
if (walk.error) this.error(walk.error);
|
|
3205
|
+
return;
|
|
3206
|
+
}
|
|
3207
|
+
case OBJ_COMMIT: {
|
|
3208
|
+
// the parse reported why it failed
|
|
3209
|
+
if (!(t.flags(n) & PARSED)) return;
|
|
3210
|
+
const links = t.links.get(n);
|
|
3211
|
+
if (!links || links.tree === undefined) return;
|
|
3212
|
+
if (name !== undefined) this.putName(t.id(links.tree), `${name}:`);
|
|
3213
|
+
const marked = this.markObject(links.tree, OBJ_TREE, n);
|
|
3214
|
+
if (marked instanceof Promise) yield marked;
|
|
3215
|
+
const parents = links.parents ?? [];
|
|
3216
|
+
let generation = 0;
|
|
3217
|
+
let prefixLength = 0;
|
|
3218
|
+
if (name !== undefined && parents.length > 0) {
|
|
3219
|
+
if (name.endsWith("^")) {
|
|
3220
|
+
generation = 1;
|
|
3221
|
+
prefixLength = name.length - 1;
|
|
3222
|
+
} else {
|
|
3223
|
+
const m = /~(\d+)$/.exec(name);
|
|
3224
|
+
if (m && (m[1] as string).length > 0) {
|
|
3225
|
+
generation = Number(m[1]);
|
|
3226
|
+
prefixLength = name.length - (m[1] as string).length - 1;
|
|
3227
|
+
} else {
|
|
3228
|
+
generation = 0;
|
|
3229
|
+
prefixLength = name.length;
|
|
3230
|
+
}
|
|
3231
|
+
}
|
|
3232
|
+
}
|
|
3233
|
+
let counter = 0;
|
|
3234
|
+
for (const p of parents) {
|
|
3235
|
+
if (name !== undefined) {
|
|
3236
|
+
const pid = t.id(p);
|
|
3237
|
+
if (counter++) this.putName(pid, `${name}^${counter}`);
|
|
3238
|
+
else if (generation > 0)
|
|
3239
|
+
this.putName(pid, `${name.slice(0, prefixLength)}~${generation + 1}`);
|
|
3240
|
+
else this.putName(pid, `${name}^`);
|
|
3241
|
+
}
|
|
3242
|
+
const r = this.markObject(p, OBJ_COMMIT, n);
|
|
3243
|
+
if (r instanceof Promise) yield r;
|
|
3244
|
+
}
|
|
3245
|
+
return;
|
|
3246
|
+
}
|
|
3247
|
+
case OBJ_TAG: {
|
|
3248
|
+
const links = t.links.get(n);
|
|
3249
|
+
if (!(t.flags(n) & PARSED)) return;
|
|
3250
|
+
if (links?.tagged === undefined) return;
|
|
3251
|
+
if (name !== undefined) this.putName(t.id(links.tagged), name);
|
|
3252
|
+
const marked = this.markObject(links.tagged, "any", n);
|
|
3253
|
+
if (marked instanceof Promise) yield marked;
|
|
3254
|
+
return;
|
|
3255
|
+
}
|
|
3256
|
+
default:
|
|
3257
|
+
this.error(`Unknown object type for ${this.describe(id)}`);
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
|
|
3261
|
+
/**
|
|
3262
|
+
* The walk of tree `n` from what the scan kept (`scanAt` start `s`): parse_object's lookup
|
|
3263
|
+
* and parse, then the tree's entries, looked up in order and marked. Synchronous while every
|
|
3264
|
+
* entry is marked without a report; the steps for the rest otherwise.
|
|
3265
|
+
*/
|
|
3266
|
+
private walkScannedTree(n: number, s: number): Steps<void> | undefined {
|
|
3267
|
+
const t = this.t;
|
|
3268
|
+
const kids = this.scanKids;
|
|
3269
|
+
t.touch(n);
|
|
3270
|
+
t.setType(n, OBJ_TREE);
|
|
3271
|
+
t.addFlags(n, PARSED);
|
|
3272
|
+
this.objectsRead++;
|
|
3273
|
+
for (let i = s + 1, end = s + 1 + ((kids[s] as number) >> 2); i < end; i++) {
|
|
3274
|
+
const v = kids[i] as number;
|
|
3275
|
+
const type = v & 1 ? OBJ_TREE : OBJ_BLOB;
|
|
3276
|
+
const child = t.lookupTypedN(v >> 1, type);
|
|
3277
|
+
if (typeof child === "number" && this.markReachable(child, type)) continue;
|
|
3278
|
+
return this.walkScannedRest(n, s, i, child);
|
|
3279
|
+
}
|
|
3280
|
+
return undefined;
|
|
3281
|
+
}
|
|
3282
|
+
|
|
3283
|
+
/** `walkScannedTree` from entry `i` on, whose lookup gave `child`. */
|
|
3284
|
+
private *walkScannedRest(
|
|
3285
|
+
n: number,
|
|
3286
|
+
s: number,
|
|
3287
|
+
i: number,
|
|
3288
|
+
child: number | { error: string },
|
|
3289
|
+
): Steps<void> {
|
|
3290
|
+
const t = this.t;
|
|
3291
|
+
const kids = this.scanKids;
|
|
3292
|
+
for (const end = s + 1 + ((kids[s] as number) >> 2); ; ) {
|
|
3293
|
+
const type = (kids[i] as number) & 1 ? OBJ_TREE : OBJ_BLOB;
|
|
3294
|
+
let c: number | undefined;
|
|
3295
|
+
if (typeof child === "number") c = child;
|
|
3296
|
+
else this.error(child.error);
|
|
3297
|
+
if (!this.markReachable(c, type)) {
|
|
3298
|
+
const r = this.markObject(c, type, n);
|
|
3299
|
+
if (r instanceof Promise) yield r;
|
|
3300
|
+
}
|
|
3301
|
+
if (++i >= end) return;
|
|
3302
|
+
const v = kids[i] as number;
|
|
3303
|
+
child = t.lookupTypedN(v >> 1, v & 1 ? OBJ_TREE : OBJ_BLOB);
|
|
3304
|
+
}
|
|
3305
|
+
}
|
|
3306
|
+
|
|
3307
|
+
/** git's `mark_unreachable_referents` (connectivity-only runs). */
|
|
3308
|
+
private markUnreachableReferents(raw: Uint8Array, at: number): void | Promise<void> {
|
|
3309
|
+
const t = this.t;
|
|
3310
|
+
const n = t.lookupRaw(raw, at);
|
|
3311
|
+
if (n < 0 || !(t.flags(n) & HAS_OBJ) || t.flags(n) & REACHABLE) return;
|
|
3312
|
+
return this.markReferents(n);
|
|
3313
|
+
}
|
|
3314
|
+
|
|
3315
|
+
private async markReferents(n: number): Promise<void> {
|
|
3316
|
+
const t = this.t;
|
|
3317
|
+
const id = t.id(n);
|
|
3318
|
+
if (t.type(n) === OBJ_NONE) {
|
|
3319
|
+
const type = typeOf((await this.storeType(id)) ?? "");
|
|
3320
|
+
if (type !== OBJ_NONE) t.setType(n, type);
|
|
3321
|
+
}
|
|
3322
|
+
const type = t.type(n);
|
|
3323
|
+
if (type === OBJ_TREE) {
|
|
3324
|
+
const obj = await this.readWhole(id);
|
|
3325
|
+
if (!obj) return;
|
|
3326
|
+
if (obj.type !== "tree") {
|
|
3327
|
+
// git's parse_tree
|
|
3328
|
+
this.error(`Object ${id} not a tree`);
|
|
3329
|
+
return;
|
|
3330
|
+
}
|
|
3331
|
+
this.walkUsed(n, obj.content);
|
|
3332
|
+
} else if (type === OBJ_COMMIT || type === OBJ_TAG) {
|
|
3333
|
+
// repo_parse_commit / parse_tag: no hash check
|
|
3334
|
+
if (!(t.flags(n) & PARSED)) await this.parseAs(n, id);
|
|
3335
|
+
if (t.flags(n) & PARSED) this.walkUsed(n, new Uint8Array(0));
|
|
3336
|
+
}
|
|
3337
|
+
}
|
|
3338
|
+
|
|
3339
|
+
/** git's `check_connectivity`. */
|
|
3340
|
+
private async checkConnectivity(): Promise<void> {
|
|
3341
|
+
const t = this.t;
|
|
3342
|
+
const pending = this.pending;
|
|
3343
|
+
const scanAt = this.scanAt;
|
|
3344
|
+
while (pending.length > 0) {
|
|
3345
|
+
const n = pending.pop() as number;
|
|
3346
|
+
// fsck_walk returns at once for a blob
|
|
3347
|
+
if (t.type(n) === OBJ_BLOB) continue;
|
|
3348
|
+
// a tree the scan kept, walked without steps while it needs no await
|
|
3349
|
+
const s = n < scanAt.length ? (scanAt[n] as number) - 1 : -1;
|
|
3350
|
+
if (
|
|
3351
|
+
s >= 0 &&
|
|
3352
|
+
((this.scanKids[s] as number) & 3) === OBJ_TREE &&
|
|
3353
|
+
!(t.flags(n) & PARSED) &&
|
|
3354
|
+
t.type(n) === OBJ_TREE
|
|
3355
|
+
) {
|
|
3356
|
+
const rest = this.walkScannedTree(n, s);
|
|
3357
|
+
if (rest) {
|
|
3358
|
+
const r = runSteps(rest);
|
|
3359
|
+
if (r instanceof Promise) await r;
|
|
3360
|
+
}
|
|
3361
|
+
continue;
|
|
3362
|
+
}
|
|
3363
|
+
const r = runSteps(this.traverseOne(n));
|
|
3364
|
+
if (r instanceof Promise) await r;
|
|
3365
|
+
}
|
|
3366
|
+
if (this.o.connectivityOnly && (this.o.dangling || this.o.lostFound)) {
|
|
3367
|
+
await this.forEachStored((raw, at) => this.markUnreachableReferents(raw, at));
|
|
3368
|
+
}
|
|
3369
|
+
for (const n of [...t.inSlotOrder()]) {
|
|
3370
|
+
const id = t.id(n);
|
|
3371
|
+
const flags = t.flags(n);
|
|
3372
|
+
if (flags & REACHABLE) {
|
|
3373
|
+
if (this.promised?.has(id) || flags & HAS_OBJ || this.inPack(id)) continue;
|
|
3374
|
+
const type = await this.printableType(n);
|
|
3375
|
+
this.emit({
|
|
3376
|
+
kind: "missing",
|
|
3377
|
+
stream: "stdout",
|
|
3378
|
+
text: `missing ${type} ${this.describe(id)}`,
|
|
3379
|
+
objectId: id,
|
|
3380
|
+
objectType: type,
|
|
3381
|
+
});
|
|
3382
|
+
this.errorsFound |= FSCK_ERROR_REACHABLE;
|
|
3383
|
+
continue;
|
|
3384
|
+
}
|
|
3385
|
+
if (!(flags & HAS_OBJ)) continue;
|
|
3386
|
+
if (this.showUnreachable) {
|
|
3387
|
+
const type = await this.printableType(n);
|
|
3388
|
+
this.emit({
|
|
3389
|
+
kind: "unreachable",
|
|
3390
|
+
stream: "stdout",
|
|
3391
|
+
text: `unreachable ${type} ${this.describe(id)}`,
|
|
3392
|
+
objectId: id,
|
|
3393
|
+
objectType: type,
|
|
3394
|
+
});
|
|
3395
|
+
continue;
|
|
3396
|
+
}
|
|
3397
|
+
if (!(flags & USED)) {
|
|
3398
|
+
const type = await this.printableType(n);
|
|
3399
|
+
if (this.o.dangling) {
|
|
3400
|
+
this.emit({
|
|
3401
|
+
kind: "dangling",
|
|
3402
|
+
stream: "stdout",
|
|
3403
|
+
text: `dangling ${type} ${this.describe(id)}`,
|
|
3404
|
+
objectId: id,
|
|
3405
|
+
objectType: type,
|
|
3406
|
+
});
|
|
3407
|
+
}
|
|
3408
|
+
if (this.o.lostFound) await this.writeLostFound(id, type);
|
|
3409
|
+
}
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
|
|
3413
|
+
/** `--lost-found`: a dangling object into `.git/lost-found/{commit,other}/<id>`. */
|
|
3414
|
+
private async writeLostFound(id: string, type: string): Promise<void> {
|
|
3415
|
+
const files = this.files;
|
|
3416
|
+
if (!files) return;
|
|
3417
|
+
const dir = pathOf(this.gitDir, "lost-found", type === "commit" ? "commit" : "other");
|
|
3418
|
+
await files.mkdir(dir);
|
|
3419
|
+
const path = pathOf(dir, id);
|
|
3420
|
+
if (type === "blob") {
|
|
3421
|
+
const obj = await this.readObject(id);
|
|
3422
|
+
if (obj) await files.write(path, obj.content);
|
|
3423
|
+
} else {
|
|
3424
|
+
await files.write(path, [new TextEncoder().encode(`${id}\n`)]);
|
|
3425
|
+
}
|
|
3426
|
+
}
|
|
3427
|
+
}
|
|
3428
|
+
|
|
3429
|
+
async function readAll(files: FilesApi, path: string): Promise<Uint8Array> {
|
|
3430
|
+
const parts: Uint8Array[] = [];
|
|
3431
|
+
let n = 0;
|
|
3432
|
+
for await (const c of files.read(path)) {
|
|
3433
|
+
parts.push(c);
|
|
3434
|
+
n += c.length;
|
|
3435
|
+
}
|
|
3436
|
+
if (parts.length === 1) return parts[0] as Uint8Array;
|
|
3437
|
+
const out = new Uint8Array(n);
|
|
3438
|
+
let o = 0;
|
|
3439
|
+
for (const p of parts) {
|
|
3440
|
+
out.set(p, o);
|
|
3441
|
+
o += p.length;
|
|
3442
|
+
}
|
|
3443
|
+
return out;
|
|
3444
|
+
}
|
|
3445
|
+
|
|
3446
|
+
async function readText(files: FilesApi, path: string): Promise<string | undefined> {
|
|
3447
|
+
if ((await files.stats(path))?.kind !== "file") return undefined;
|
|
3448
|
+
return dec.decode(await readAll(files, path));
|
|
3449
|
+
}
|
|
3450
|
+
|
|
3451
|
+
/** The entries of a reflog file, oldest first: old id, new id and the entry's timestamp. */
|
|
3452
|
+
function parseReflog(text: string | undefined, hex = 40): Reflog["entries"] {
|
|
3453
|
+
const entries: Reflog["entries"] = [];
|
|
3454
|
+
for (const line of text?.split("\n") ?? []) {
|
|
3455
|
+
const oldId = line.slice(0, hex);
|
|
3456
|
+
const newId = line.slice(hex + 1, 2 * hex + 1);
|
|
3457
|
+
if (!OID.test(oldId) || !OID.test(newId)) continue;
|
|
3458
|
+
const tab = line.indexOf("\t");
|
|
3459
|
+
const ident = tab < 0 ? line.slice(2 * hex + 2) : line.slice(2 * hex + 2, tab);
|
|
3460
|
+
const m = /> (\d+) [+-]\d{4}$/.exec(ident);
|
|
3461
|
+
entries.push({ oldId, newId, timestamp: m ? Number(m[1]) : 0 });
|
|
3462
|
+
}
|
|
3463
|
+
return entries;
|
|
3464
|
+
}
|