@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
|
@@ -1,125 +1,247 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
type Commit,
|
|
3
|
+
isSymbolicRef,
|
|
4
|
+
listObjectsWithPrefix,
|
|
5
|
+
type ObjectId,
|
|
6
|
+
type PersonIdent,
|
|
7
|
+
type RefLogOptions,
|
|
8
|
+
type TreeEntry,
|
|
9
|
+
} from "@statewalker/vcs-core";
|
|
10
|
+
import { parseGitBool } from "@statewalker/vcs-utils";
|
|
11
|
+
import { compareGitPaths } from "@statewalker/vcs-utils/encoding";
|
|
12
|
+
import { isIndexUnborn, type Worktree } from "@statewalker/vcs-working-tree";
|
|
2
13
|
|
|
3
14
|
import {
|
|
4
15
|
CommitNotFoundError,
|
|
5
16
|
InvalidArgumentError,
|
|
6
17
|
NoHeadError,
|
|
7
18
|
NoRebaseInProgressError,
|
|
19
|
+
RebaseTodoMissingError,
|
|
20
|
+
RefNotFoundError,
|
|
8
21
|
UpstreamRequiredError,
|
|
9
22
|
} from "../errors/index.js";
|
|
10
23
|
import { GitCommand } from "../git-command.js";
|
|
11
|
-
import {
|
|
24
|
+
import { RemoteConfigStore } from "../remote-config/index.js";
|
|
25
|
+
import {
|
|
26
|
+
type ContentMergeStrategy,
|
|
27
|
+
FastForwardMode,
|
|
28
|
+
type MergeResult,
|
|
29
|
+
MergeStatus,
|
|
30
|
+
MergeStrategy,
|
|
31
|
+
} from "../results/merge-result.js";
|
|
12
32
|
import type { RebaseResult, RebaseTodoLine } from "../results/rebase-result.js";
|
|
13
33
|
import { RebaseAction, RebaseStatus } from "../results/rebase-result.js";
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
34
|
+
import { coreBool, createIdAbbreviator, worktreeModeTrust } from "./core-config.js";
|
|
35
|
+
import { createDescriber, type DescribeOptions } from "./git-describe.js";
|
|
36
|
+
import {
|
|
37
|
+
type GitCommitHookRun,
|
|
38
|
+
gitCommitAfterHooks,
|
|
39
|
+
gitCommitMessageHooks,
|
|
40
|
+
runPostCheckoutHook,
|
|
41
|
+
sequencerPostCommit,
|
|
42
|
+
sequencerPrepareMessage,
|
|
43
|
+
} from "./git-hooks.js";
|
|
44
|
+
import { hostEnvironmentOf, localTimezoneOffset } from "./git-ident.js";
|
|
45
|
+
import { favorOf, MergeCommand } from "./merge-command.js";
|
|
46
|
+
import { configBool, configValue, conflictStyleOf } from "./merge-config.js";
|
|
47
|
+
import { ortConfigOptions } from "./merge-drivers.js";
|
|
48
|
+
import {
|
|
49
|
+
conflictsComment,
|
|
50
|
+
FlatTreeCache,
|
|
51
|
+
flatTree,
|
|
52
|
+
indexChanges,
|
|
53
|
+
OperationState,
|
|
54
|
+
readIndex,
|
|
55
|
+
refsWatched,
|
|
56
|
+
storeFlatTreeOver,
|
|
57
|
+
subjectOf,
|
|
58
|
+
switchTrees,
|
|
59
|
+
treeDelta,
|
|
60
|
+
} from "./merge-support.js";
|
|
61
|
+
import { CleanupMode, commentPrefixOf, stripspace } from "./message-cleanup.js";
|
|
62
|
+
import { copyNotesForAmend, copyNotesForRebase } from "./notes-support.js";
|
|
63
|
+
import { ortResultEntries } from "./ort-merge.js";
|
|
64
|
+
import { mergeBaseTree, mergeStoredTrees, storedBase } from "./ort-recursive.js";
|
|
65
|
+
import { type PatchIdTrees, patchId } from "./patch-id.js";
|
|
66
|
+
import { formatCommitPretty, loadRefDecorations, type RefDecorations } from "./pretty-format.js";
|
|
67
|
+
import {
|
|
68
|
+
addExecCommands,
|
|
69
|
+
combinedMessage,
|
|
70
|
+
commentLines,
|
|
71
|
+
countTodoCommands,
|
|
72
|
+
formatTodo,
|
|
73
|
+
isFinalFixup,
|
|
74
|
+
isFixup,
|
|
75
|
+
isNoop,
|
|
76
|
+
isPickLike,
|
|
77
|
+
messageBody,
|
|
78
|
+
parseTodo,
|
|
79
|
+
peekCommand,
|
|
80
|
+
rearrangeSquash,
|
|
81
|
+
squashMessageForFixup,
|
|
82
|
+
squashTexts,
|
|
83
|
+
subjectLength,
|
|
84
|
+
type TodoItem,
|
|
85
|
+
takesCommit,
|
|
86
|
+
todoHelp,
|
|
87
|
+
} from "./rebase-todo.js";
|
|
88
|
+
import { reflogMessages, updateHeadExpecting } from "./reflog-messages.js";
|
|
89
|
+
import { revisionMergeBases } from "./rev-parse-command.js";
|
|
90
|
+
import { expandCommitId } from "./sequencer-pick.js";
|
|
91
|
+
import {
|
|
92
|
+
applyAutostash,
|
|
93
|
+
cleanupMessage,
|
|
94
|
+
createAutostash,
|
|
95
|
+
moveHeadAsReset,
|
|
96
|
+
removeBranchState,
|
|
97
|
+
resetMerge,
|
|
98
|
+
} from "./sequencer-support.js";
|
|
99
|
+
import { appendSignoff, trailerConfigOf } from "./trailers.js";
|
|
100
|
+
import { worktreeModified } from "./worktree-overwrite-checks.js";
|
|
101
|
+
import { usesBranch } from "./worktree-support.js";
|
|
29
102
|
|
|
30
103
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* Based on JGit's RebaseCommand.Operation.
|
|
104
|
+
* Rebase operation types.
|
|
34
105
|
*/
|
|
35
106
|
export enum RebaseOperation {
|
|
36
|
-
/**
|
|
107
|
+
/** Start a new rebase */
|
|
37
108
|
BEGIN = "begin",
|
|
38
|
-
|
|
39
|
-
/** Continues after conflict resolution */
|
|
109
|
+
/** Continue after resolving conflicts */
|
|
40
110
|
CONTINUE = "continue",
|
|
41
|
-
|
|
42
|
-
/** Skips the current commit */
|
|
111
|
+
/** Skip the current commit */
|
|
43
112
|
SKIP = "skip",
|
|
44
|
-
|
|
45
|
-
/** Aborts and resets the current rebase */
|
|
113
|
+
/** Abort and restore original state */
|
|
46
114
|
ABORT = "abort",
|
|
47
|
-
|
|
48
|
-
/** Process steps (internal) */
|
|
115
|
+
/** Process steps (for interactive rebase) */
|
|
49
116
|
PROCESS_STEPS = "process-steps",
|
|
117
|
+
/** `--edit-todo`: the rest of the todo list edited (the interactive handler's editTodo) */
|
|
118
|
+
EDIT_TODO = "edit-todo",
|
|
50
119
|
}
|
|
51
120
|
|
|
52
121
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* Based on JGit's RebaseCommand.InteractiveHandler.
|
|
122
|
+
* Handler for interactive rebase (`git rebase -i`): it plays the editors git runs.
|
|
56
123
|
*/
|
|
57
124
|
export interface InteractiveHandler {
|
|
58
125
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
126
|
+
* The sequence editor: the todo list as git shows it in the editor (abbreviated ids, the
|
|
127
|
+
* help after it), returned edited. Run before {@link prepareSteps}.
|
|
61
128
|
*/
|
|
62
|
-
|
|
129
|
+
editTodo?(todo: string): string | Promise<string>;
|
|
63
130
|
|
|
64
131
|
/**
|
|
65
|
-
* Modify
|
|
132
|
+
* Modify the steps before execution (the todo list's commands, comments left out).
|
|
133
|
+
* @param steps List of rebase steps to modify
|
|
66
134
|
*/
|
|
67
|
-
|
|
135
|
+
prepareSteps?(steps: RebaseTodoLine[]): void;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The commit message editor (`reword`, the end of a `squash` chain, `fixup -c`, `merge
|
|
139
|
+
* -c`): the text git puts in the editor (the message, or the chain's `# This is a
|
|
140
|
+
* combination of N commits.` text), returned edited; git then strips the comments.
|
|
141
|
+
* Without it the text is kept (as with an editor that changes nothing).
|
|
142
|
+
*/
|
|
143
|
+
modifyCommitMessage?(message: string): string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** What an `exec` command did (git runs it in a shell). */
|
|
147
|
+
export interface RebaseExecResult {
|
|
148
|
+
/** The exit status (0: success; 127, command not found, counts as 1) */
|
|
149
|
+
exitCode: number;
|
|
150
|
+
/** What the command printed, put in the result's output */
|
|
151
|
+
output?: string[];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Runs the command of an `exec` todo line (`exec <command>`, `--exec <command>`): git runs it
|
|
156
|
+
* with the shell in the worktree; a portable rebase leaves that to its host. The index and
|
|
157
|
+
* the worktree are read again after it, as git reads them.
|
|
158
|
+
*/
|
|
159
|
+
export type RebaseExecHandler = (command: string) => RebaseExecResult | Promise<RebaseExecResult>;
|
|
160
|
+
|
|
161
|
+
/** `--empty`: what becomes of a commit that becomes empty when picked. */
|
|
162
|
+
export type RebaseEmptyMode = "drop" | "keep" | "stop";
|
|
163
|
+
|
|
164
|
+
/** One entry of `rebase-merge/update-refs`: the ref, its value at the start, the new one. */
|
|
165
|
+
interface UpdateRefRecord {
|
|
166
|
+
ref: string;
|
|
167
|
+
before: ObjectId;
|
|
168
|
+
after: ObjectId;
|
|
68
169
|
}
|
|
69
170
|
|
|
171
|
+
/** What a pick (do_pick_commit) did. */
|
|
172
|
+
type PickOutcome =
|
|
173
|
+
| { kind: "ok" }
|
|
174
|
+
| { kind: "dropped" }
|
|
175
|
+
| {
|
|
176
|
+
kind: "conflicts";
|
|
177
|
+
/** do_pick_commit's lines (`error: could not apply ...` and hints, git commit's refusal) */
|
|
178
|
+
lines: string[];
|
|
179
|
+
conflicts: string[];
|
|
180
|
+
/** What error_with_patch writes to `rebase-merge/message` */
|
|
181
|
+
message?: string;
|
|
182
|
+
}
|
|
183
|
+
| { kind: "reschedule"; result: RebaseResult & { reschedule?: boolean } };
|
|
184
|
+
|
|
70
185
|
/**
|
|
71
|
-
*
|
|
186
|
+
* What a todo command came to: nothing (go on), `checkTodo` (go on, the list read again if
|
|
187
|
+
* it changed), or a result that stops the rebase (`reschedule`: the command goes back).
|
|
72
188
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
189
|
+
type StepOutcome =
|
|
190
|
+
| (Partial<RebaseResult> & { reschedule?: boolean; checkTodo?: boolean })
|
|
191
|
+
| undefined;
|
|
76
192
|
|
|
77
|
-
|
|
78
|
-
|
|
193
|
+
/** sequencer.c's staged_changes_advice (no `-S`). */
|
|
194
|
+
const STAGED_CHANGES_ADVICE = `error: you have staged changes in your working tree
|
|
195
|
+
If these changes are meant to be squashed into the previous commit, run:
|
|
79
196
|
|
|
80
|
-
|
|
81
|
-
onto: ObjectId;
|
|
197
|
+
git commit --amend
|
|
82
198
|
|
|
83
|
-
|
|
84
|
-
ontoName: string;
|
|
199
|
+
If they are meant to go into a new commit, run:
|
|
85
200
|
|
|
86
|
-
|
|
87
|
-
todoSteps: RebaseTodoLine[];
|
|
201
|
+
git commit
|
|
88
202
|
|
|
89
|
-
|
|
90
|
-
doneSteps: RebaseTodoLine[];
|
|
203
|
+
In both cases, once you're done, continue with:
|
|
91
204
|
|
|
92
|
-
|
|
93
|
-
inProgress: boolean;
|
|
205
|
+
git rebase --continue`;
|
|
94
206
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
207
|
+
/** rebase-interactive.c's edit_todo_list_advice. */
|
|
208
|
+
const EDIT_TODO_ADVICE = [
|
|
209
|
+
"You can fix this with 'git rebase --edit-todo' and then run 'git rebase --continue'.",
|
|
210
|
+
"Or you can abort the rebase with 'git rebase --abort'.",
|
|
211
|
+
];
|
|
212
|
+
|
|
213
|
+
/** Tracked files checked at once against the worktree (stats and hashes in flight). */
|
|
214
|
+
const WORKTREE_CHECK_BATCH = 32;
|
|
215
|
+
|
|
216
|
+
/** The state directory of git's merge backend. */
|
|
217
|
+
const DIR = "rebase-merge";
|
|
218
|
+
|
|
219
|
+
/** sequencer.c's GIT_MAX_LABEL_LENGTH (NAME_MAX - ".lock" - 16) */
|
|
220
|
+
const MAX_LABEL_LENGTH = 255 - 5 - 16;
|
|
221
|
+
|
|
222
|
+
/** git's quoting of a value in `author-script` (`'...'`, a quote as `'\''`). */
|
|
223
|
+
const sq = (value: string) => `'${value.replaceAll("'", "'\\''")}'`;
|
|
98
224
|
|
|
99
225
|
/**
|
|
100
|
-
* Rebase
|
|
226
|
+
* Rebase the current branch onto another commit.
|
|
101
227
|
*
|
|
102
|
-
* Equivalent to `git rebase
|
|
103
|
-
*
|
|
104
|
-
*
|
|
228
|
+
* Equivalent to `git rebase <upstream>` with git's merge backend: the commits HEAD has that
|
|
229
|
+
* upstream has not (merges dropped) are picked one by one onto upstream by git's merge-ort,
|
|
230
|
+
* the working tree and the index written as git writes them. A conflict stops the rebase
|
|
231
|
+
* with git's markers (`<<<<<<< HEAD` / `>>>>>>> <abbrev> (<subject>)`), stages 1-3 and
|
|
232
|
+
* git's state (`.git/rebase-merge/`, `REBASE_HEAD`, `MERGE_MSG`, `AUTO_MERGE`), so native
|
|
233
|
+
* `git rebase --continue / --skip / --abort` resumes what we started, and we resume what
|
|
234
|
+
* native git started. A rebase is refused on unstaged or staged changes, as git refuses it.
|
|
235
|
+
* With {@link RebaseCommand.setRebaseMerges} (`--rebase-merges`) the merges are kept: git's
|
|
236
|
+
* todo list of `label`, `reset` and `merge -C` commands, run as git's sequencer runs them.
|
|
105
237
|
*
|
|
106
238
|
* @example
|
|
107
239
|
* ```typescript
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* .call();
|
|
112
|
-
*
|
|
113
|
-
* // Abort ongoing rebase
|
|
114
|
-
* const result = await git.rebase()
|
|
115
|
-
* .setOperation(RebaseOperation.ABORT)
|
|
116
|
-
* .call();
|
|
117
|
-
*
|
|
118
|
-
* // Rebase with merge strategy
|
|
119
|
-
* const result = await git.rebase()
|
|
120
|
-
* .setUpstream("main")
|
|
121
|
-
* .setStrategy(MergeStrategy.OURS)
|
|
122
|
-
* .call();
|
|
240
|
+
* const result = await git.rebase().setUpstream(mainId).call();
|
|
241
|
+
* if (result.status === RebaseStatus.STOPPED) {
|
|
242
|
+
* // resolve, `git add`, then
|
|
243
|
+
* await git.rebase().setOperation(RebaseOperation.CONTINUE).call();
|
|
244
|
+
* }
|
|
123
245
|
* ```
|
|
124
246
|
*/
|
|
125
247
|
export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
@@ -130,16 +252,65 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
130
252
|
private contentStrategy?: ContentMergeStrategy;
|
|
131
253
|
private preserveMerges = false;
|
|
132
254
|
private interactiveHandler?: InteractiveHandler;
|
|
255
|
+
private committer?: PersonIdent;
|
|
256
|
+
/** `--committer-date-is-author-date` (kept in `rebase-merge/cdate_is_adate`) */
|
|
257
|
+
private committerDateIsAuthorDate = false;
|
|
258
|
+
/** `--ignore-date` / `--reset-author-date` (kept in `rebase-merge/ignore_date`) */
|
|
259
|
+
private ignoreDate = false;
|
|
260
|
+
/** `--signoff` (kept in `rebase-merge/signoff`) */
|
|
261
|
+
private signoff = false;
|
|
262
|
+
private autoStash?: boolean;
|
|
263
|
+
private reapplyCherryPicks = false;
|
|
264
|
+
/** `--rebase-merges` (undefined: `rebase.rebaseMerges`) */
|
|
265
|
+
private rebaseMerges?: boolean;
|
|
266
|
+
/** `--rebase-merges=rebase-cousins` */
|
|
267
|
+
private rebaseCousins = false;
|
|
268
|
+
/** The commits read by the walks that make the todo list */
|
|
269
|
+
private loadedCommits = new Map<ObjectId, Commit>();
|
|
270
|
+
/** The flat trees the picks read (each pick's result is the next one's HEAD) */
|
|
271
|
+
private flatTrees?: FlatTreeCache;
|
|
272
|
+
/** Commit subjects, for the todo and done lines */
|
|
273
|
+
private readonly subjects = new Map<ObjectId, string>();
|
|
274
|
+
/** The refs' decorations, for `%d` in rebase.instructionFormat (loaded once) */
|
|
275
|
+
private decorations?: RefDecorations;
|
|
276
|
+
/** What loading the decorations printed (`warning: invalid replace ref <name>`) */
|
|
277
|
+
private decorationWarnings: string[] = [];
|
|
278
|
+
/** `--exec <command>` (each run after every pick) */
|
|
279
|
+
private execCommands: string[] = [];
|
|
280
|
+
/** Runs the `exec` commands */
|
|
281
|
+
private execHandler?: RebaseExecHandler;
|
|
282
|
+
/** `--[no-]reschedule-failed-exec` (undefined: rebase.rescheduleFailedExec) */
|
|
283
|
+
private rescheduleFailedExec?: boolean;
|
|
284
|
+
/** `--empty=drop|keep|stop` (undefined: git's default for the kind of rebase) */
|
|
285
|
+
private emptyMode?: RebaseEmptyMode;
|
|
286
|
+
/** `--[no-]keep-empty`: commits empty from the start are picked (default) */
|
|
287
|
+
private keepEmpty = true;
|
|
288
|
+
/** `--[no-]update-refs` (undefined: rebase.updateRefs) */
|
|
289
|
+
private updateRefs?: boolean;
|
|
290
|
+
/** `--[no-]autosquash` (undefined: rebase.autoSquash with an interactive rebase) */
|
|
291
|
+
private autosquash?: boolean;
|
|
292
|
+
/** `--root`: every commit of the branch rebased (onto the upstream given, else a new root) */
|
|
293
|
+
private root = false;
|
|
294
|
+
private noVerify = false;
|
|
133
295
|
|
|
134
|
-
|
|
135
|
-
|
|
296
|
+
/** `--no-verify`: the pre-rebase hook does not run. */
|
|
297
|
+
setNoVerify(noVerify: boolean): this {
|
|
298
|
+
this.checkCallable();
|
|
299
|
+
this.noVerify = noVerify;
|
|
300
|
+
return this;
|
|
301
|
+
}
|
|
302
|
+
/** The command after the one running (git's peek_command), for record_in_rewritten */
|
|
303
|
+
private nextCommand?: RebaseAction;
|
|
136
304
|
|
|
137
305
|
/**
|
|
138
|
-
* Set the upstream
|
|
306
|
+
* Set the upstream to rebase onto: a commit id or any revision (`main`, `HEAD~2`,
|
|
307
|
+
* `v1.0`), resolved to a commit when the rebase starts (git's `lookup_commit_reference`
|
|
308
|
+
* of `<upstream>`; "fatal: invalid upstream '<name>'" when it names none). The name given
|
|
309
|
+
* is kept for the reflog and the messages (`checkout main`).
|
|
139
310
|
*
|
|
140
|
-
* @param upstream
|
|
311
|
+
* @param upstream Upstream commit ID or revision
|
|
141
312
|
*/
|
|
142
|
-
setUpstream(upstream: ObjectId): this {
|
|
313
|
+
setUpstream(upstream: ObjectId | string): this {
|
|
143
314
|
this.checkCallable();
|
|
144
315
|
this.upstreamCommit = upstream;
|
|
145
316
|
this.upstreamCommitName = upstream;
|
|
@@ -147,25 +318,41 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
147
318
|
}
|
|
148
319
|
|
|
149
320
|
/**
|
|
150
|
-
* Set the upstream branch name
|
|
321
|
+
* Set the upstream by branch name.
|
|
151
322
|
*
|
|
152
|
-
* @param upstream
|
|
323
|
+
* @param upstream Branch name to rebase onto
|
|
153
324
|
*/
|
|
154
325
|
async setUpstreamBranch(upstream: string): Promise<this> {
|
|
155
326
|
this.checkCallable();
|
|
156
|
-
this.upstreamCommit = await this.
|
|
327
|
+
this.upstreamCommit = await this.resolveUpstream(upstream);
|
|
157
328
|
this.upstreamCommitName = upstream;
|
|
158
329
|
return this;
|
|
159
330
|
}
|
|
160
331
|
|
|
161
332
|
/**
|
|
162
|
-
*
|
|
333
|
+
* builtin/rebase.c's `lookup_commit_reference_by_name(options.upstream_name)`: the commit
|
|
334
|
+
* an upstream names, or git's "fatal: invalid upstream '<name>'" (exit 128).
|
|
335
|
+
*/
|
|
336
|
+
private async resolveUpstream(name: string): Promise<ObjectId> {
|
|
337
|
+
const fatal = (n: string) => `fatal: invalid upstream '${n}'`;
|
|
338
|
+
try {
|
|
339
|
+
return await this.resolveRef(name, fatal);
|
|
340
|
+
} catch (error) {
|
|
341
|
+
if (error instanceof RefNotFoundError) {
|
|
342
|
+
throw new RefNotFoundError(name, fatal(name)).withExitCode(128);
|
|
343
|
+
}
|
|
344
|
+
throw error;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Set the upstream name for messages.
|
|
163
350
|
*
|
|
164
|
-
* @param name
|
|
351
|
+
* @param name Display name for upstream
|
|
165
352
|
*/
|
|
166
353
|
setUpstreamName(name: string): this {
|
|
167
354
|
this.checkCallable();
|
|
168
|
-
if (
|
|
355
|
+
if (this.upstreamCommit === undefined) {
|
|
169
356
|
throw new InvalidArgumentError(
|
|
170
357
|
"upstreamName",
|
|
171
358
|
name,
|
|
@@ -176,10 +363,15 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
176
363
|
return this;
|
|
177
364
|
}
|
|
178
365
|
|
|
366
|
+
/** The upstream's name (the branch given, else its id). */
|
|
367
|
+
getUpstreamName(): string | undefined {
|
|
368
|
+
return this.upstreamCommitName;
|
|
369
|
+
}
|
|
370
|
+
|
|
179
371
|
/**
|
|
180
372
|
* Set the operation to perform.
|
|
181
373
|
*
|
|
182
|
-
* @param operation
|
|
374
|
+
* @param operation Rebase operation
|
|
183
375
|
*/
|
|
184
376
|
setOperation(operation: RebaseOperation): this {
|
|
185
377
|
this.checkCallable();
|
|
@@ -195,9 +387,9 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
195
387
|
}
|
|
196
388
|
|
|
197
389
|
/**
|
|
198
|
-
* Set the merge strategy
|
|
390
|
+
* Set the merge strategy (only git's merge-ort is run; kept for compatibility).
|
|
199
391
|
*
|
|
200
|
-
* @param strategy
|
|
392
|
+
* @param strategy Merge strategy
|
|
201
393
|
*/
|
|
202
394
|
setStrategy(strategy: MergeStrategy): this {
|
|
203
395
|
this.checkCallable();
|
|
@@ -213,9 +405,9 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
213
405
|
}
|
|
214
406
|
|
|
215
407
|
/**
|
|
216
|
-
* Set
|
|
408
|
+
* Set content merge strategy for conflicts (`-X ours`, `-X theirs`).
|
|
217
409
|
*
|
|
218
|
-
* @param strategy
|
|
410
|
+
* @param strategy Content merge strategy
|
|
219
411
|
*/
|
|
220
412
|
setContentMergeStrategy(strategy: ContentMergeStrategy): this {
|
|
221
413
|
this.checkCallable();
|
|
@@ -231,9 +423,10 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
231
423
|
}
|
|
232
424
|
|
|
233
425
|
/**
|
|
234
|
-
* Set whether to preserve merges
|
|
426
|
+
* Set whether to preserve merge commits (not supported: merges are dropped, as git's
|
|
427
|
+
* default rebase drops them).
|
|
235
428
|
*
|
|
236
|
-
* @param preserve
|
|
429
|
+
* @param preserve Whether to preserve merges
|
|
237
430
|
*/
|
|
238
431
|
setPreserveMerges(preserve: boolean): this {
|
|
239
432
|
this.checkCallable();
|
|
@@ -249,9 +442,9 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
249
442
|
}
|
|
250
443
|
|
|
251
444
|
/**
|
|
252
|
-
* Enable interactive
|
|
445
|
+
* Enable interactive mode with handler.
|
|
253
446
|
*
|
|
254
|
-
* @param handler
|
|
447
|
+
* @param handler Interactive handler
|
|
255
448
|
*/
|
|
256
449
|
runInteractively(handler: InteractiveHandler): this {
|
|
257
450
|
this.checkCallable();
|
|
@@ -260,406 +453,3661 @@ export class RebaseCommand extends GitCommand<RebaseResult> {
|
|
|
260
453
|
}
|
|
261
454
|
|
|
262
455
|
/**
|
|
263
|
-
*
|
|
456
|
+
* Pick commits whose change upstream already has (`--reapply-cherry-picks`); by default
|
|
457
|
+
* (`--no-reapply-cherry-picks`) they are left out, with git's "skipped previously applied
|
|
458
|
+
* commit" warnings, as their patch-id is found upstream.
|
|
264
459
|
*/
|
|
265
|
-
|
|
460
|
+
setReapplyCherryPicks(reapply: boolean): this {
|
|
266
461
|
this.checkCallable();
|
|
267
|
-
this.
|
|
462
|
+
this.reapplyCherryPicks = reapply;
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
268
465
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
466
|
+
/**
|
|
467
|
+
* `--rebase-merges[=rebase-cousins|no-rebase-cousins]`: the branch's merges are kept. The
|
|
468
|
+
* todo list (git's make_script_with_merges) labels each side branch's tip (`label`),
|
|
469
|
+
* starts each branch from where it forked (`reset`), and recreates each merge with its
|
|
470
|
+
* original message (`merge -C <merge> <label> # <oneline>`); a side branch that forked
|
|
471
|
+
* off below the merge base stays on its fork point unless `rebase-cousins`. `false`
|
|
472
|
+
* (`--no-rebase-merges`) drops the merges; default `rebase.rebaseMerges`.
|
|
473
|
+
*/
|
|
474
|
+
setRebaseMerges(mode: boolean | "rebase-cousins" | "no-rebase-cousins"): this {
|
|
475
|
+
this.checkCallable();
|
|
476
|
+
this.rebaseMerges = mode !== false;
|
|
477
|
+
this.rebaseCousins = mode === "rebase-cousins";
|
|
478
|
+
return this;
|
|
479
|
+
}
|
|
272
480
|
|
|
273
|
-
|
|
274
|
-
|
|
481
|
+
/**
|
|
482
|
+
* Stash local changes away for the rebase and apply them after it (`--autostash`;
|
|
483
|
+
* default `rebase.autoStash`, false).
|
|
484
|
+
*/
|
|
485
|
+
setAutoStash(autoStash: boolean): this {
|
|
486
|
+
this.checkCallable();
|
|
487
|
+
this.autoStash = autoStash;
|
|
488
|
+
return this;
|
|
489
|
+
}
|
|
275
490
|
|
|
276
|
-
|
|
277
|
-
|
|
491
|
+
/**
|
|
492
|
+
* The committer of the rebased commits (default: resolved as git resolves it,
|
|
493
|
+
* `GIT_COMMITTER_*`, `committer.*`, `user.*`). The picks keep their authors.
|
|
494
|
+
*/
|
|
495
|
+
setCommitter(committer: PersonIdent): this {
|
|
496
|
+
this.checkCallable();
|
|
497
|
+
this.committer = committer;
|
|
498
|
+
return this;
|
|
499
|
+
}
|
|
278
500
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
501
|
+
/**
|
|
502
|
+
* `--committer-date-is-author-date`: each rebased commit's committer date is its author
|
|
503
|
+
* date. Like git, it implies `--force-rebase`: a branch already on its upstream is
|
|
504
|
+
* rebased all the same.
|
|
505
|
+
*/
|
|
506
|
+
setCommitterDateIsAuthorDate(value: boolean): this {
|
|
507
|
+
this.checkCallable();
|
|
508
|
+
this.committerDateIsAuthorDate = value;
|
|
509
|
+
return this;
|
|
283
510
|
}
|
|
284
511
|
|
|
285
512
|
/**
|
|
286
|
-
*
|
|
513
|
+
* `--ignore-date` (`--reset-author-date`): each rebased commit's author date is the
|
|
514
|
+
* current time (git's `ignore_author_date`: the clock, not `GIT_AUTHOR_DATE`). Implies
|
|
515
|
+
* `--force-rebase`, as in git.
|
|
287
516
|
*/
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
517
|
+
setIgnoreDate(value: boolean): this {
|
|
518
|
+
this.checkCallable();
|
|
519
|
+
this.ignoreDate = value;
|
|
520
|
+
return this;
|
|
521
|
+
}
|
|
292
522
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
523
|
+
/**
|
|
524
|
+
* `--signoff`: each rebased commit gets git's `Signed-off-by: <committer>` trailer
|
|
525
|
+
* (append_signoff: added unless the message's trailer block already ends with it), its
|
|
526
|
+
* message cleaned up as git's `--signoff` commits clean it (whitespace). Like git, it
|
|
527
|
+
* implies `--force-rebase`; the recreated merges are not signed off (git commits them
|
|
528
|
+
* without `--signoff`).
|
|
529
|
+
*/
|
|
530
|
+
setSignoff(value: boolean): this {
|
|
531
|
+
this.checkCallable();
|
|
532
|
+
this.signoff = value;
|
|
533
|
+
return this;
|
|
534
|
+
}
|
|
298
535
|
|
|
299
|
-
|
|
536
|
+
/**
|
|
537
|
+
* `--exec <command>` (`-x`): an `exec <command>` line after each pick (after its fixup and
|
|
538
|
+
* squash chain) and each merge; implies an interactive list (no fast-forward shortcut) and
|
|
539
|
+
* `--empty=keep` unless given. The commands run through {@link setExecHandler}.
|
|
540
|
+
*/
|
|
541
|
+
setExec(commands: string[]): this {
|
|
542
|
+
this.checkCallable();
|
|
543
|
+
this.execCommands = [...commands];
|
|
544
|
+
return this;
|
|
545
|
+
}
|
|
300
546
|
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
547
|
+
/**
|
|
548
|
+
* Runs the `exec` commands (git runs them with the shell). Without one an `exec` fails as a
|
|
549
|
+
* command not found does.
|
|
550
|
+
*/
|
|
551
|
+
setExecHandler(handler: RebaseExecHandler): this {
|
|
552
|
+
this.checkCallable();
|
|
553
|
+
this.execHandler = handler;
|
|
554
|
+
return this;
|
|
555
|
+
}
|
|
304
556
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
557
|
+
/**
|
|
558
|
+
* `--reschedule-failed-exec`: an `exec` that fails goes back to the todo list's head, so
|
|
559
|
+
* `--continue` runs it again (kept in `rebase-merge/reschedule-failed-exec`); default
|
|
560
|
+
* `rebase.rescheduleFailedExec`, false.
|
|
561
|
+
*/
|
|
562
|
+
setRescheduleFailedExec(value: boolean): this {
|
|
563
|
+
this.checkCallable();
|
|
564
|
+
this.rescheduleFailedExec = value;
|
|
565
|
+
return this;
|
|
566
|
+
}
|
|
313
567
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
568
|
+
/**
|
|
569
|
+
* `--empty=drop|keep|stop`: a pick that becomes empty (its change already in HEAD) is
|
|
570
|
+
* dropped (`drop_redundant_commits`), kept as an empty commit (`keep_redundant_commits`) or
|
|
571
|
+
* stops the rebase (git commit's "The previous cherry-pick is now empty"). git 2.5x's
|
|
572
|
+
* default: `stop` for an interactive rebase, `keep` with `--exec`, else `drop`.
|
|
573
|
+
*/
|
|
574
|
+
setEmpty(mode: RebaseEmptyMode): this {
|
|
575
|
+
this.checkCallable();
|
|
576
|
+
this.emptyMode = mode;
|
|
577
|
+
return this;
|
|
578
|
+
}
|
|
324
579
|
|
|
325
|
-
|
|
326
|
-
|
|
580
|
+
/**
|
|
581
|
+
* `--keep-empty` (default) / `--no-keep-empty`: commits that are empty from the start are
|
|
582
|
+
* picked (` # empty` in the todo list) or left out.
|
|
583
|
+
*/
|
|
584
|
+
setKeepEmpty(value: boolean): this {
|
|
585
|
+
this.checkCallable();
|
|
586
|
+
this.keepEmpty = value;
|
|
587
|
+
return this;
|
|
588
|
+
}
|
|
327
589
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
590
|
+
/**
|
|
591
|
+
* `--update-refs`: the other branches that point at rebased commits move with them
|
|
592
|
+
* (`update-ref <ref>` lines, `rebase-merge/update-refs`, written at the end); branches
|
|
593
|
+
* checked out in other worktrees are left alone. Default `rebase.updateRefs`.
|
|
594
|
+
*/
|
|
595
|
+
setUpdateRefs(value: boolean): this {
|
|
596
|
+
this.checkCallable();
|
|
597
|
+
this.updateRefs = value;
|
|
598
|
+
return this;
|
|
599
|
+
}
|
|
334
600
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
601
|
+
/**
|
|
602
|
+
* `--autosquash`: `fixup! `, `amend! ` and `squash! ` commits moved after the commit they
|
|
603
|
+
* name, as `fixup`, `fixup -C` and `squash`. Default: `rebase.autoSquash` for an
|
|
604
|
+
* interactive rebase.
|
|
605
|
+
*/
|
|
606
|
+
setAutosquash(value: boolean): this {
|
|
607
|
+
this.checkCallable();
|
|
608
|
+
this.autosquash = value;
|
|
609
|
+
return this;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* `--root`: all the commits of the branch are rebased, its root commit too. With an
|
|
614
|
+
* upstream set ({@link setUpstream}) that is `--root --onto <upstream>`; without one the
|
|
615
|
+
* commits go onto a new root (git's squash_onto, a commit of the empty tree kept in
|
|
616
|
+
* `rebase-merge/squash-onto`), so an unchanged list keeps the history as it is.
|
|
617
|
+
*/
|
|
618
|
+
setRoot(value: boolean): this {
|
|
619
|
+
this.checkCallable();
|
|
620
|
+
this.root = value;
|
|
621
|
+
return this;
|
|
622
|
+
}
|
|
347
623
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
624
|
+
async call(): Promise<RebaseResult> {
|
|
625
|
+
this.checkCallable();
|
|
626
|
+
this.setCallable(false);
|
|
627
|
+
// the index as it is now, before the worktree or a ref is touched: one another writer
|
|
628
|
+
// replaced is read again (git reads the index at its start), unless this instance holds
|
|
629
|
+
// unwritten edits (refused)
|
|
630
|
+
await this.readCurrentIndex();
|
|
631
|
+
const worktree = this.worktreeAccess;
|
|
632
|
+
const run = () => this.run(worktree);
|
|
633
|
+
return worktree?.batch ? worktree.batch(run) : run();
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
private async run(worktree: Worktree | undefined): Promise<RebaseResult> {
|
|
637
|
+
const state = new OperationState(this.workingCopy.checkout, this.refsStore);
|
|
638
|
+
// git's rebase_config, read before anything else (any operation): a rebase.rebaseMerges
|
|
639
|
+
// that is neither a boolean nor a cousins mode dies
|
|
640
|
+
for (const value of RemoteConfigStore.from(this.workingCopy).values("rebase.rebaseMerges")) {
|
|
641
|
+
if (
|
|
642
|
+
parseGitBool(value) === undefined &&
|
|
643
|
+
value !== "rebase-cousins" &&
|
|
644
|
+
value !== "no-rebase-cousins"
|
|
645
|
+
) {
|
|
646
|
+
return {
|
|
647
|
+
status: RebaseStatus.FAILED,
|
|
648
|
+
message: `fatal: Unknown rebase-merges mode: ${value}`,
|
|
649
|
+
};
|
|
650
|
+
}
|
|
351
651
|
}
|
|
652
|
+
switch (this.operation) {
|
|
653
|
+
case RebaseOperation.ABORT:
|
|
654
|
+
return this.abort(state, worktree);
|
|
655
|
+
case RebaseOperation.CONTINUE:
|
|
656
|
+
return this.continueRebase(state, worktree);
|
|
657
|
+
case RebaseOperation.SKIP:
|
|
658
|
+
return this.skip(state, worktree);
|
|
659
|
+
case RebaseOperation.EDIT_TODO:
|
|
660
|
+
return this.editTodo(state);
|
|
661
|
+
default:
|
|
662
|
+
return this.begin(state, worktree);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
352
665
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
666
|
+
private context(worktree: Worktree | undefined) {
|
|
667
|
+
return {
|
|
668
|
+
workingCopy: this.workingCopy,
|
|
669
|
+
worktree,
|
|
670
|
+
staging: this.staging,
|
|
671
|
+
blobs: this.blobs,
|
|
672
|
+
trees: this.trees,
|
|
673
|
+
commits: this.commits,
|
|
674
|
+
refs: this.refsStore,
|
|
362
675
|
};
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
// Reset HEAD to onto
|
|
366
|
-
await this.updateHead(headName, this.upstreamCommit);
|
|
676
|
+
}
|
|
367
677
|
|
|
368
|
-
|
|
369
|
-
return
|
|
678
|
+
private async inProgress(state: OperationState): Promise<boolean> {
|
|
679
|
+
return (
|
|
680
|
+
(await state.read(`${DIR}/head-name`)) !== undefined ||
|
|
681
|
+
(await state.read(`${DIR}/onto`)) !== undefined ||
|
|
682
|
+
(await state.listDir(DIR)).length > 0
|
|
683
|
+
);
|
|
370
684
|
}
|
|
371
685
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
private async processSteps(
|
|
376
|
-
state: RebaseState,
|
|
377
|
-
commitsToRebase: ObjectId[],
|
|
686
|
+
private async begin(
|
|
687
|
+
state: OperationState,
|
|
688
|
+
worktree: Worktree | undefined,
|
|
378
689
|
): Promise<RebaseResult> {
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
690
|
+
if (this.upstreamCommit === undefined && !this.root) {
|
|
691
|
+
throw new UpstreamRequiredError("Upstream commit is required for rebase BEGIN operation");
|
|
692
|
+
}
|
|
693
|
+
// the upstream as given (setUpstream takes a name too) peeled to the commit it names
|
|
694
|
+
// before anything is written, so HEAD is never set to a name ("main\n" in .git/HEAD)
|
|
695
|
+
// an empty name too: "fatal: invalid upstream ''"
|
|
696
|
+
if (this.upstreamCommit !== undefined) {
|
|
697
|
+
this.upstreamCommit = await this.resolveUpstream(this.upstreamCommit);
|
|
698
|
+
}
|
|
699
|
+
if (await this.inProgress(state)) {
|
|
700
|
+
return {
|
|
701
|
+
status: RebaseStatus.FAILED,
|
|
702
|
+
message:
|
|
703
|
+
"fatal: It seems that there is already a rebase-merge directory, and\nI wonder if you are in the middle of another rebase.",
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
// check_exec_cmd
|
|
707
|
+
for (const command of this.execCommands) {
|
|
708
|
+
if (command.includes("\n")) {
|
|
709
|
+
return {
|
|
710
|
+
status: RebaseStatus.FAILED,
|
|
711
|
+
message: "error: exec commands cannot contain newlines",
|
|
712
|
+
};
|
|
386
713
|
}
|
|
714
|
+
if (command === "")
|
|
715
|
+
return { status: RebaseStatus.FAILED, message: "error: empty exec command" };
|
|
716
|
+
}
|
|
717
|
+
const headResolved = await this.refsStore.resolve("HEAD");
|
|
718
|
+
if (!headResolved?.objectId) throw new NoHeadError("HEAD cannot be resolved");
|
|
719
|
+
const headId = headResolved.objectId;
|
|
720
|
+
const headRef = await this.refsStore.get("HEAD");
|
|
721
|
+
const headName = headRef && isSymbolicRef(headRef) ? headRef.target : "detached HEAD";
|
|
722
|
+
// --root without --onto: the commits go onto git's squash_onto, a commit of the empty
|
|
723
|
+
// tree with no parent (their root is then a root again)
|
|
724
|
+
let squashOnto: ObjectId | undefined;
|
|
725
|
+
if (!this.upstreamCommit) {
|
|
726
|
+
squashOnto = await this.createSquashOnto();
|
|
727
|
+
this.upstreamCommitName = squashOnto;
|
|
728
|
+
}
|
|
729
|
+
const onto = (this.upstreamCommit ?? squashOnto) as ObjectId;
|
|
387
730
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
731
|
+
// git's require_clean_work_tree: no unstaged, no staged changes, unless they are
|
|
732
|
+
// stashed away for the rebase (rebase.autoStash) and applied again after it
|
|
733
|
+
const output: string[] = [];
|
|
734
|
+
let autostash: ObjectId | undefined;
|
|
735
|
+
const refusal = await this.uncleanWorktree(headId, worktree);
|
|
736
|
+
if (refusal) {
|
|
737
|
+
if (!(this.autoStash ?? configBool(this.workingCopy, "rebase.autoStash", false))) {
|
|
738
|
+
return refusal;
|
|
392
739
|
}
|
|
740
|
+
if (!state.supported) {
|
|
741
|
+
// the stash could be kept nowhere: refused rather than stranded
|
|
742
|
+
throw new InvalidArgumentError(
|
|
743
|
+
"autoStash",
|
|
744
|
+
true,
|
|
745
|
+
"rebase --autostash needs a checkout that keeps state files (rebase-merge/autostash)",
|
|
746
|
+
);
|
|
747
|
+
}
|
|
748
|
+
autostash = await createAutostash(this.context(worktree), headId);
|
|
749
|
+
if (autostash) {
|
|
750
|
+
output.push(`Created autostash: ${await this.abbreviateObjectId(autostash)}`);
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
const unstash = async () => {
|
|
754
|
+
if (autostash) output.push(...(await applyAutostash(this.context(worktree), autostash)));
|
|
755
|
+
};
|
|
393
756
|
|
|
394
|
-
|
|
395
|
-
|
|
757
|
+
// builtin/rebase.c's options as cmd_rebase settles them
|
|
758
|
+
const config = this.workingCopy;
|
|
759
|
+
const interactive = this.interactiveHandler !== undefined;
|
|
760
|
+
const execs = this.execCommands;
|
|
761
|
+
const autosquash =
|
|
762
|
+
this.autosquash ?? (interactive && configBool(config, "rebase.autoSquash", false));
|
|
763
|
+
const updateRefs = this.updateRefs ?? configBool(config, "rebase.updateRefs", false);
|
|
764
|
+
const emptyMode: RebaseEmptyMode =
|
|
765
|
+
this.emptyMode ?? (interactive ? "stop" : execs.length > 0 ? "keep" : "drop");
|
|
766
|
+
const reschedule =
|
|
767
|
+
this.rescheduleFailedExec ?? configBool(config, "rebase.rescheduleFailedExec", false);
|
|
768
|
+
const abbreviate = configBool(config, "rebase.abbreviateCommands", false);
|
|
769
|
+
const comment = commentPrefixOf(config);
|
|
396
770
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
771
|
+
// git's can_fast_forward: HEAD already on upstream with a linear history since, nothing
|
|
772
|
+
// to do (unless forced, as the date options and --signoff do); an interactive rebase,
|
|
773
|
+
// --exec and --autosquash never take the shortcut (allow_preemptive_ff)
|
|
774
|
+
const forced = this.committerDateIsAuthorDate || this.ignoreDate || this.signoff;
|
|
775
|
+
const preemptive = !interactive && execs.length === 0 && this.autosquash !== true;
|
|
776
|
+
if (preemptive && !this.root && (await this.canFastForward(onto, headId))) {
|
|
777
|
+
const upToDate = headName.startsWith("refs/")
|
|
778
|
+
? `Current branch ${headName.replace(/^refs\/heads\//, "")} is up to date`
|
|
779
|
+
: "HEAD is up to date";
|
|
780
|
+
if (!forced) {
|
|
781
|
+
output.push(`${upToDate}.`);
|
|
782
|
+
// finish_rebase: what an earlier rebase left removed
|
|
783
|
+
await state.removeRef("REBASE_HEAD", "AUTO_MERGE");
|
|
784
|
+
await unstash();
|
|
785
|
+
return { status: RebaseStatus.UP_TO_DATE, newHead: headId, output };
|
|
402
786
|
}
|
|
787
|
+
output.push(`${upToDate}, rebase forced.`);
|
|
788
|
+
}
|
|
403
789
|
|
|
404
|
-
|
|
405
|
-
|
|
790
|
+
// the pre-rebase hook, told the upstream as given (`--root` with --root), may refuse
|
|
791
|
+
if (!this.noVerify) {
|
|
792
|
+
const upstreamArg =
|
|
793
|
+
this.root && !this.upstreamCommit ? "--root" : (this.upstreamCommitName ?? onto);
|
|
794
|
+
const pre = await this.hooks.run("pre-rebase", { args: [upstreamArg] });
|
|
795
|
+
if (pre && pre.exitCode !== 0) {
|
|
796
|
+
await unstash();
|
|
797
|
+
return {
|
|
798
|
+
status: RebaseStatus.FAILED,
|
|
799
|
+
// 2.43 dies (exit 128), 2.5x returns an error (exit 1)
|
|
800
|
+
message: `${this.hooks.compatibility === "2.43" ? "fatal" : "error"}: The pre-rebase hook refused to rebase.`,
|
|
801
|
+
output,
|
|
802
|
+
};
|
|
406
803
|
}
|
|
804
|
+
}
|
|
407
805
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
806
|
+
const headTree = await flatTree(this.trees, await this.commits.getTree(headId));
|
|
807
|
+
const ontoTreeId = await this.commits.getTree(onto);
|
|
808
|
+
const ontoTree = await flatTree(this.trees, ontoTreeId);
|
|
809
|
+
// HEAD moved to upstream with nothing replayed: HEAD detached at upstream ("(start)"),
|
|
810
|
+
// then the branch moved, as git's rebase does for an empty (noop) todo
|
|
811
|
+
const moveToOnto = async (status: RebaseStatus): Promise<RebaseResult> => {
|
|
812
|
+
const refused = await switchTrees(this.context(worktree), headTree, ontoTree, {
|
|
813
|
+
action: "checkout",
|
|
814
|
+
operation: "rebase",
|
|
815
|
+
targetTree: ontoTreeId,
|
|
816
|
+
});
|
|
817
|
+
if (refused) {
|
|
818
|
+
await unstash();
|
|
819
|
+
// the merge backend's checkout_onto, a fast-forward included
|
|
413
820
|
return {
|
|
414
|
-
status: RebaseStatus.
|
|
415
|
-
|
|
416
|
-
|
|
821
|
+
status: RebaseStatus.FAILED,
|
|
822
|
+
message: `${refused.message}\nerror: could not detach HEAD`,
|
|
823
|
+
output,
|
|
417
824
|
};
|
|
418
825
|
}
|
|
826
|
+
await state.setRef("ORIG_HEAD", headId);
|
|
827
|
+
await this.refsStore.set("HEAD", onto, this.startLog());
|
|
828
|
+
await runPostCheckoutHook(this.hooks, headId, onto, true);
|
|
829
|
+
await this.finishBranch(headName, onto, onto);
|
|
830
|
+
await unstash();
|
|
831
|
+
return { status, newHead: onto, output };
|
|
832
|
+
};
|
|
833
|
+
// nothing of HEAD's to replay: HEAD moves to upstream
|
|
834
|
+
if (await this.isAncestor(headId, onto)) return moveToOnto(RebaseStatus.FAST_FORWARD);
|
|
419
835
|
|
|
420
|
-
|
|
836
|
+
// `--rebase-merges` (else `rebase.rebaseMerges`, a boolean or a cousins mode)
|
|
837
|
+
let rebaseMerges = this.rebaseMerges;
|
|
838
|
+
let rebaseCousins = this.rebaseCousins;
|
|
839
|
+
if (rebaseMerges === undefined) {
|
|
840
|
+
const value = configValue(this.workingCopy, "rebase.rebaseMerges");
|
|
841
|
+
if (value !== undefined) {
|
|
842
|
+
const bool = parseGitBool(value);
|
|
843
|
+
rebaseMerges = bool ?? true;
|
|
844
|
+
rebaseCousins = bool === undefined && value === "rebase-cousins";
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
// the commits to pick: HEAD's, not upstream's, merges dropped (kept with
|
|
848
|
+
// --rebase-merges), oldest first
|
|
849
|
+
const range = await this.commitsToRebase(headId, onto, !!rebaseMerges);
|
|
850
|
+
// only merges since upstream (dropped without --rebase-merges): git's todo is a `noop`,
|
|
851
|
+
// and the branch still moves to upstream
|
|
852
|
+
if (range.length === 0) return moveToOnto(RebaseStatus.OK);
|
|
853
|
+
// commits whose change upstream has already (`--cherry-mark`): left out, with git's
|
|
854
|
+
// warnings (`make_script`), unless --reapply-cherry-picks
|
|
855
|
+
const skipped = this.reapplyCherryPicks
|
|
856
|
+
? new Set<ObjectId>()
|
|
857
|
+
: await this.appliedUpstream(headId, onto, range);
|
|
858
|
+
const ids = range.filter((id) => !skipped.has(id));
|
|
859
|
+
if (skipped.size > 0) {
|
|
860
|
+
for (const id of range) {
|
|
861
|
+
if (skipped.has(id))
|
|
862
|
+
output.push(
|
|
863
|
+
`warning: skipped previously applied commit ${await this.abbreviateObjectId(id)}`,
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
if (configBool(this.workingCopy, "advice.skippedCherryPicks", true)) {
|
|
867
|
+
output.push(
|
|
868
|
+
"hint: use --reapply-cherry-picks to include skipped commits",
|
|
869
|
+
'hint: Disable this message with "git config set advice.skippedCherryPicks false"',
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
// sequencer_make_script: a commit empty from the start says so (` # empty`), or is left
|
|
874
|
+
// out with --no-keep-empty
|
|
875
|
+
let script = "";
|
|
876
|
+
if (rebaseMerges) {
|
|
877
|
+
script = await this.makeScriptWithMerges(
|
|
878
|
+
headId,
|
|
879
|
+
onto,
|
|
880
|
+
range,
|
|
881
|
+
skipped,
|
|
882
|
+
rebaseCousins,
|
|
883
|
+
this.keepEmpty,
|
|
884
|
+
);
|
|
885
|
+
} else {
|
|
886
|
+
for (const id of ids) {
|
|
887
|
+
const empty = await this.originallyEmpty(id);
|
|
888
|
+
if (empty && !this.keepEmpty) continue;
|
|
889
|
+
script += `pick ${id} ${await this.instructionText(id)}${empty ? ` ${comment} empty` : ""}\n`;
|
|
890
|
+
}
|
|
421
891
|
}
|
|
892
|
+
let items = parseTodo(script, comment).items;
|
|
893
|
+
// complete_action: a list with nothing is a `noop`, then --update-refs, --autosquash
|
|
894
|
+
// and --exec rework it before it is edited
|
|
895
|
+
if (items.length === 0) items = [{ action: RebaseAction.NOOP, id: "", rest: "" }];
|
|
896
|
+
// load_ref_decorations' warnings, printed while the todo list is made
|
|
897
|
+
output.push(...this.decorationWarnings.splice(0));
|
|
422
898
|
|
|
423
|
-
//
|
|
424
|
-
state.
|
|
425
|
-
|
|
899
|
+
// init_basic_state / write_basic_state
|
|
900
|
+
await state.write(`${DIR}/head-name`, `${headName}\n`);
|
|
901
|
+
await state.write(`${DIR}/onto`, `${onto}\n`);
|
|
902
|
+
await state.write(`${DIR}/orig-head`, `${headId}\n`);
|
|
903
|
+
if (this.signoff) await state.write(`${DIR}/signoff`, "--signoff\n");
|
|
904
|
+
if (emptyMode === "drop") await state.write(`${DIR}/drop_redundant_commits`, "");
|
|
905
|
+
if (emptyMode === "keep") await state.write(`${DIR}/keep_redundant_commits`, "");
|
|
906
|
+
if (this.committerDateIsAuthorDate) await state.write(`${DIR}/cdate_is_adate`, "");
|
|
907
|
+
if (this.ignoreDate) await state.write(`${DIR}/ignore_date`, "");
|
|
908
|
+
await state.write(
|
|
909
|
+
`${DIR}/${reschedule ? "reschedule-failed-exec" : "no-reschedule-failed-exec"}`,
|
|
910
|
+
"",
|
|
911
|
+
);
|
|
912
|
+
await state.write(`${DIR}/interactive`, "");
|
|
913
|
+
if (autostash) await state.write(`${DIR}/autostash`, `${autostash}\n`);
|
|
914
|
+
if (squashOnto) await state.write(`${DIR}/squash-onto`, `${squashOnto}\n`);
|
|
426
915
|
|
|
427
|
-
|
|
428
|
-
|
|
916
|
+
if (updateRefs) items = await this.addUpdateRefCommands(state, items, comment);
|
|
917
|
+
// the list's commits abbreviated once (the rearrangements below keep the same commits):
|
|
918
|
+
// for the editor, and to read the names back without a look at the store
|
|
919
|
+
const shorts = await this.shortIds(items);
|
|
920
|
+
if (autosquash) {
|
|
921
|
+
const rearranged = await rearrangeSquash(
|
|
922
|
+
items,
|
|
923
|
+
async (id) => subjectOf((await this.loadCommit(id))?.message ?? ""),
|
|
924
|
+
(name) => this.autosquashTarget(name, shorts),
|
|
925
|
+
);
|
|
926
|
+
if (!rearranged) {
|
|
927
|
+
await state.remove(DIR);
|
|
928
|
+
await unstash();
|
|
929
|
+
return {
|
|
930
|
+
status: RebaseStatus.FAILED,
|
|
931
|
+
message: "error: the script was already rearranged.",
|
|
932
|
+
output,
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
items = rearranged;
|
|
936
|
+
}
|
|
937
|
+
if (execs.length > 0) items = addExecCommands(items, execs);
|
|
429
938
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
939
|
+
// edit_todo_list: the list (abbreviated ids, git's help) in the editor, the backup with
|
|
940
|
+
// the full ids
|
|
941
|
+
const missingCheck = this.missingCommitsCheckLevel(output);
|
|
942
|
+
const commandTotal = countTodoCommands(items);
|
|
943
|
+
const shortOnto = await this.abbreviateObjectId(onto);
|
|
944
|
+
const help = todoHelp(
|
|
945
|
+
commandTotal,
|
|
946
|
+
comment,
|
|
947
|
+
{
|
|
948
|
+
shortRevisions: this.root
|
|
949
|
+
? await this.abbreviateObjectId(headId)
|
|
950
|
+
: `${shortOnto}..${await this.abbreviateObjectId(headId)}`,
|
|
951
|
+
shortOnto,
|
|
952
|
+
},
|
|
953
|
+
missingCheck === "error",
|
|
954
|
+
);
|
|
955
|
+
const editorText = `${formatTodo(items, { abbreviate, idOf: (id) => shorts.get(id) ?? id })}${help}`;
|
|
956
|
+
await state.write(`${DIR}/git-rebase-todo`, editorText);
|
|
957
|
+
await state.write(
|
|
958
|
+
`${DIR}/git-rebase-todo.backup`,
|
|
959
|
+
`${formatTodo(items, { abbreviate })}${help}`,
|
|
960
|
+
);
|
|
961
|
+
let edited: string;
|
|
962
|
+
try {
|
|
963
|
+
edited = await this.runSequenceEditor(editorText, shorts);
|
|
964
|
+
} catch (error) {
|
|
965
|
+
// complete_action's -2 (launch_sequence_editor failed): the autostash applied, the
|
|
966
|
+
// state removed, HEAD where it was
|
|
967
|
+
await state.remove(DIR);
|
|
968
|
+
await unstash();
|
|
969
|
+
return { status: RebaseStatus.FAILED, message: sequenceEditorFailure(error), output };
|
|
970
|
+
}
|
|
971
|
+
await state.write(`${DIR}/git-rebase-todo`, edited);
|
|
972
|
+
const strippedTodo = stripspace(edited, comment);
|
|
973
|
+
if (strippedTodo === "") {
|
|
974
|
+
// an empty list aborts the rebase (complete_action's "nothing to do")
|
|
975
|
+
await state.remove(DIR);
|
|
976
|
+
await unstash();
|
|
977
|
+
return { status: RebaseStatus.FAILED, message: "error: nothing to do", output };
|
|
978
|
+
}
|
|
979
|
+
// HEAD detached at upstream, the rebase left for `--edit-todo` (complete_action's -4)
|
|
980
|
+
const fixTodo = async (lines: string[]): Promise<RebaseResult> => {
|
|
981
|
+
const refused = await switchTrees(this.context(worktree), headTree, ontoTree, {
|
|
982
|
+
action: "checkout",
|
|
983
|
+
operation: "rebase",
|
|
984
|
+
targetTree: ontoTreeId,
|
|
985
|
+
});
|
|
986
|
+
if (!refused) {
|
|
987
|
+
await state.setRef("ORIG_HEAD", headId);
|
|
988
|
+
await this.refsStore.set("HEAD", onto, this.startLog());
|
|
989
|
+
}
|
|
990
|
+
return { status: RebaseStatus.FAILED, message: lines.join("\n"), output };
|
|
433
991
|
};
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
throw new CommitNotFoundError(commitId);
|
|
992
|
+
const parsed = await this.resolveTodo(parseTodo(strippedTodo, comment).items, {
|
|
993
|
+
known: shorts,
|
|
994
|
+
});
|
|
995
|
+
output.push(...parsed.warnings);
|
|
996
|
+
if (parsed.errors.length > 0) return fixTodo([...parsed.errors, ...EDIT_TODO_ADVICE]);
|
|
997
|
+
const missing = await this.missingCommits(items, parsed.items, missingCheck);
|
|
998
|
+
if (missing.length > 0) {
|
|
999
|
+
if (missingCheck === "error") {
|
|
1000
|
+
await state.write(`${DIR}/dropped`, "");
|
|
1001
|
+
return fixTodo(missing);
|
|
1002
|
+
}
|
|
1003
|
+
output.push(...missing);
|
|
447
1004
|
}
|
|
1005
|
+
await this.filterUpdateRefs(state, parsed.items);
|
|
1006
|
+
// the ids expanded, the list written again as git writes it
|
|
1007
|
+
let todo = parsed.items.map(({ line: _, ...item }) => item as TodoItem);
|
|
448
1008
|
|
|
449
|
-
//
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
1009
|
+
// git's skip_unnecessary_picks (allow_ff): the first picks whose parent is where the
|
|
1010
|
+
// rebase starts are done already, HEAD starts at the last of them; the noops and drops
|
|
1011
|
+
// among and before them go to `done` with them
|
|
1012
|
+
let start = onto;
|
|
1013
|
+
let skippedPicks = 0;
|
|
1014
|
+
if (!forced) {
|
|
1015
|
+
for (; skippedPicks < todo.length; skippedPicks++) {
|
|
1016
|
+
const item = todo[skippedPicks] as TodoItem;
|
|
1017
|
+
if (isNoop(item)) continue;
|
|
1018
|
+
if (item.action !== RebaseAction.PICK) break;
|
|
1019
|
+
const parents = (await this.loadCommit(item.id))?.parents ?? [];
|
|
1020
|
+
if (parents.length !== 1 || parents[0] !== start) break;
|
|
1021
|
+
start = item.id;
|
|
1022
|
+
}
|
|
456
1023
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
if (
|
|
461
|
-
|
|
462
|
-
|
|
1024
|
+
await state.write(`${DIR}/done`, formatTodo(todo.slice(0, skippedPicks)));
|
|
1025
|
+
todo = todo.slice(skippedPicks);
|
|
1026
|
+
// a fixup after the picks skipped: the base waits in rewritten-pending
|
|
1027
|
+
if (skippedPicks > 0 && isFixup(peekCommand(todo, 0))) {
|
|
1028
|
+
await state.append(`${DIR}/rewritten-pending`, `${start}\n`);
|
|
1029
|
+
}
|
|
1030
|
+
await state.write(`${DIR}/git-rebase-todo`, formatTodo(todo, { abbreviate }));
|
|
1031
|
+
const startTreeId = start === onto ? ontoTreeId : await this.commits.getTree(start);
|
|
1032
|
+
const startTree = start === onto ? ontoTree : await flatTree(this.trees, startTreeId);
|
|
1033
|
+
// detach HEAD at upstream, the worktree switched to it (as git's checkout of onto)
|
|
1034
|
+
const refused = await switchTrees(this.context(worktree), headTree, startTree, {
|
|
1035
|
+
action: "checkout",
|
|
1036
|
+
operation: "rebase",
|
|
1037
|
+
targetTree: startTreeId,
|
|
1038
|
+
});
|
|
1039
|
+
if (refused) {
|
|
1040
|
+
await state.remove(DIR);
|
|
1041
|
+
await unstash();
|
|
463
1042
|
return {
|
|
464
|
-
status: RebaseStatus.
|
|
465
|
-
|
|
1043
|
+
status: RebaseStatus.FAILED,
|
|
1044
|
+
message: `${refused.message}\nerror: could not detach HEAD`,
|
|
1045
|
+
output,
|
|
466
1046
|
};
|
|
467
1047
|
}
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
ontoCommitObj.tree,
|
|
1048
|
+
const commands = countTodoCommands(parsed.items);
|
|
1049
|
+
await state.write(`${DIR}/end`, `${commands}\n`);
|
|
1050
|
+
await state.write(`${DIR}/msgnum`, "0\n");
|
|
1051
|
+
// checkout_onto's reset_head: ORIG_HEAD expecting its old value, then HEAD detached
|
|
1052
|
+
// expecting the commit it left (as the reference-transaction hook sees both); an
|
|
1053
|
+
// ORIG_HEAD it cannot write is reported (as reset reports it) and HEAD moves all the same
|
|
1054
|
+
output.push(
|
|
1055
|
+
...(await moveHeadAsReset(
|
|
1056
|
+
this.refsStore,
|
|
1057
|
+
state,
|
|
1058
|
+
headId,
|
|
1059
|
+
start,
|
|
1060
|
+
this.startLog(),
|
|
1061
|
+
"rebase: updating ORIG_HEAD",
|
|
1062
|
+
{ detach: true },
|
|
1063
|
+
)),
|
|
485
1064
|
);
|
|
1065
|
+
// checkout_onto's post-checkout hook: the HEAD left, the commit the picks start from
|
|
1066
|
+
await runPostCheckoutHook(this.hooks, headId, start, true);
|
|
1067
|
+
// pick_commits runs the list as expanded (full command names), not as written
|
|
1068
|
+
return this.processTodo(state, worktree, output, todo);
|
|
1069
|
+
}
|
|
486
1070
|
|
|
487
|
-
|
|
1071
|
+
/** Picks what is left in `git-rebase-todo`, then finishes the rebase (git's pick_commits). */
|
|
1072
|
+
private async processTodo(
|
|
1073
|
+
state: OperationState,
|
|
1074
|
+
worktree: Worktree | undefined,
|
|
1075
|
+
output: string[] = [],
|
|
1076
|
+
/** The list in memory (complete_action's), else read from `git-rebase-todo` */
|
|
1077
|
+
items?: TodoItem[],
|
|
1078
|
+
): Promise<RebaseResult> {
|
|
1079
|
+
// git's read_populate_todo: a todo that cannot be read is an error, not an empty list
|
|
1080
|
+
if (state.supported && (await state.read(`${DIR}/git-rebase-todo`)) === undefined) {
|
|
1081
|
+
throw new RebaseTodoMissingError(`${DIR}/git-rebase-todo`);
|
|
1082
|
+
}
|
|
1083
|
+
const loaded = items ? { items, errors: [] } : await this.loadTodo(state);
|
|
1084
|
+
if (loaded.errors.length > 0) {
|
|
488
1085
|
return {
|
|
489
|
-
status: RebaseStatus.
|
|
490
|
-
|
|
491
|
-
|
|
1086
|
+
status: RebaseStatus.FAILED,
|
|
1087
|
+
message: [...loaded.errors, "error: please fix this using 'git rebase --edit-todo'."].join(
|
|
1088
|
+
"\n",
|
|
1089
|
+
),
|
|
1090
|
+
output,
|
|
492
1091
|
};
|
|
493
1092
|
}
|
|
1093
|
+
let todo = loaded.items;
|
|
1094
|
+
// pick_commits: what a stop left
|
|
1095
|
+
await state.remove(`${DIR}/message`, `${DIR}/stopped-sha`, `${DIR}/amend`, `${DIR}/patch`);
|
|
1096
|
+
// the commands done so far (`end`, the total, was written as the list was read)
|
|
1097
|
+
let doneCount = countCommands((await state.read(`${DIR}/done`)) ?? "");
|
|
1098
|
+
while (todo.length > 0) {
|
|
1099
|
+
const item = todo[0] as TodoItem;
|
|
1100
|
+
const rest = todo.slice(1);
|
|
1101
|
+
const line = formatTodo([item], { verbatim: true });
|
|
1102
|
+
// git's save_todo: the rest written to `git-rebase-todo` first (a lock file renamed
|
|
1103
|
+
// into place), then the command appended to `done` (O_APPEND); a crash between the
|
|
1104
|
+
// two loses the `done` line, never the todo
|
|
1105
|
+
const restText = formatTodo(rest, { verbatim: true });
|
|
1106
|
+
await state.write(`${DIR}/git-rebase-todo`, restText);
|
|
1107
|
+
await state.append(`${DIR}/done`, line);
|
|
1108
|
+
if (item.action !== RebaseAction.COMMENT || item.invalid) {
|
|
1109
|
+
doneCount++;
|
|
1110
|
+
await state.write(`${DIR}/msgnum`, `${doneCount}\n`);
|
|
1111
|
+
}
|
|
1112
|
+
// what the previous command left (pick_commits unlinks it before each one)
|
|
1113
|
+
await state.remove(`${DIR}/message`, `${DIR}/stopped-sha`);
|
|
1114
|
+
await state.remove(`${DIR}/author-script`, "MERGE_HEAD");
|
|
1115
|
+
// deleted whether they exist or not (git 2.5x's reference-transaction hook hears both)
|
|
1116
|
+
await state.deleteRef("AUTO_MERGE", "REBASE_HEAD");
|
|
1117
|
+
if (item.action === RebaseAction.BREAK) {
|
|
1118
|
+
// stopped_at_head
|
|
1119
|
+
return { status: RebaseStatus.STOPPED, message: await this.stoppedAtHead(), output };
|
|
1120
|
+
}
|
|
1121
|
+
this.nextCommand = peekCommand(rest, 0);
|
|
1122
|
+
const outcome = await this.runTodoCommand(state, worktree, todo, output);
|
|
1123
|
+
if (outcome?.reschedule) {
|
|
1124
|
+
// git's "It has been rescheduled": the command goes back to the todo's head (it
|
|
1125
|
+
// stays in `done`), so `--continue` runs it again
|
|
1126
|
+
await state.write(`${DIR}/git-rebase-todo`, formatTodo(todo, { verbatim: true }));
|
|
1127
|
+
if (item.id && takesCommit(item)) await state.setRef("REBASE_HEAD", item.id);
|
|
1128
|
+
const { reschedule: _, checkTodo: __, ...result } = outcome;
|
|
1129
|
+
return {
|
|
1130
|
+
...result,
|
|
1131
|
+
status: result.status ?? RebaseStatus.FAILED,
|
|
1132
|
+
message: `${result.message ?? ""}\n${rescheduledAdvice(line.replace(/\n$/, ""))}`,
|
|
1133
|
+
output,
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
if (outcome && outcome.status !== undefined) {
|
|
1137
|
+
const { reschedule: _, checkTodo: __, ...result } = outcome;
|
|
1138
|
+
return { ...result, status: outcome.status, output };
|
|
1139
|
+
}
|
|
1140
|
+
todo = rest;
|
|
1141
|
+
// reread_todo_if_changed: an exec (or an edited message) may have rewritten the list
|
|
1142
|
+
if (outcome?.checkTodo) {
|
|
1143
|
+
const now = (await state.read(`${DIR}/git-rebase-todo`)) ?? "";
|
|
1144
|
+
if (now !== restText) {
|
|
1145
|
+
const reread = await this.loadTodo(state);
|
|
1146
|
+
if (reread.errors.length > 0) {
|
|
1147
|
+
return {
|
|
1148
|
+
status: RebaseStatus.FAILED,
|
|
1149
|
+
message: [
|
|
1150
|
+
...reread.errors,
|
|
1151
|
+
"error: please fix this using 'git rebase --edit-todo'.",
|
|
1152
|
+
].join("\n"),
|
|
1153
|
+
output,
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
todo = reread.items;
|
|
1157
|
+
await this.writeTotal(state, todo);
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
// done: the branch takes the new commits, HEAD attached to it again
|
|
1162
|
+
const headName = (await state.read(`${DIR}/head-name`))?.trim() ?? "detached HEAD";
|
|
1163
|
+
const newHead = await this.resolveHead();
|
|
1164
|
+
const onto = (await state.read(`${DIR}/onto`))?.trim() ?? newHead;
|
|
1165
|
+
const origHead = (await state.read(`${DIR}/orig-head`))?.trim();
|
|
1166
|
+
await this.finishBranch(headName, newHead, onto, false, origHead || undefined);
|
|
1167
|
+
await this.flushRewrittenPending(state);
|
|
1168
|
+
// `git notes copy --for-rewrite=rebase` fed rewritten-list
|
|
1169
|
+
const rewritten = await state.read(`${DIR}/rewritten-list`);
|
|
1170
|
+
if (rewritten) {
|
|
1171
|
+
await copyNotesForRebase(this.notesContext(), rewritten);
|
|
1172
|
+
// the post-rewrite hook told "rebase", rewritten-list on its stdin
|
|
1173
|
+
await this.hooks.run("post-rewrite", { args: ["rebase"], stdin: rewritten });
|
|
1174
|
+
}
|
|
1175
|
+
const autostash = (await state.read(`${DIR}/autostash`))?.trim();
|
|
1176
|
+
if (autostash) output.push(...(await applyAutostash(this.context(worktree), autostash)));
|
|
1177
|
+
await this.doUpdateRefs(state, output);
|
|
1178
|
+
await this.deleteRewrittenRefs(state);
|
|
1179
|
+
await state.remove(DIR);
|
|
1180
|
+
return { status: RebaseStatus.OK, newHead, output };
|
|
1181
|
+
}
|
|
494
1182
|
|
|
495
|
-
|
|
496
|
-
|
|
1183
|
+
/** git's commit_tree of the empty tree with no parent and no message (squash_onto). */
|
|
1184
|
+
private async createSquashOnto(): Promise<ObjectId> {
|
|
1185
|
+
this.resetClock();
|
|
1186
|
+
const tree = await this.trees.storeTree([]);
|
|
1187
|
+
return this.commits.store({
|
|
1188
|
+
tree,
|
|
1189
|
+
parents: [],
|
|
1190
|
+
author: this.resolveAuthor(),
|
|
1191
|
+
committer: this.committer ?? this.resolveCommitter(),
|
|
1192
|
+
message: "",
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
497
1195
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
1196
|
+
/** stopped_at_head: `Stopped at <abbrev> (<subject>)` (a `break`; get_message's label). */
|
|
1197
|
+
private async stoppedAtHead(): Promise<string> {
|
|
1198
|
+
const head = await this.resolveHead().catch(() => undefined);
|
|
1199
|
+
const commit = head ? await this.commits.load(head).catch(() => undefined) : undefined;
|
|
1200
|
+
if (!head || !commit) return "Stopped at HEAD";
|
|
1201
|
+
return `Stopped at ${await this.abbreviateObjectId(head)} (${subjectOf(commit.message)})`;
|
|
502
1202
|
}
|
|
503
1203
|
|
|
504
1204
|
/**
|
|
505
|
-
*
|
|
1205
|
+
* git's record_in_rewritten: the commit waits in `rewritten-pending` while a fixup or
|
|
1206
|
+
* squash follows; otherwise the pending commits map to HEAD (`rewritten-list`).
|
|
506
1207
|
*/
|
|
507
|
-
private async
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
const committer = await this.getCommitter();
|
|
1208
|
+
private async recordInRewritten(
|
|
1209
|
+
state: OperationState,
|
|
1210
|
+
id: ObjectId,
|
|
1211
|
+
next: RebaseAction | undefined,
|
|
1212
|
+
): Promise<void> {
|
|
1213
|
+
await state.append(`${DIR}/rewritten-pending`, `${id}\n`);
|
|
1214
|
+
if (!isFixup(next)) await this.flushRewrittenPending(state);
|
|
1215
|
+
}
|
|
516
1216
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
1217
|
+
/** git's flush_rewritten_pending: each pending commit mapped to HEAD. */
|
|
1218
|
+
private async flushRewrittenPending(state: OperationState): Promise<void> {
|
|
1219
|
+
const pending = await state.read(`${DIR}/rewritten-pending`);
|
|
1220
|
+
if (!pending) return;
|
|
1221
|
+
const head = await this.resolveHead();
|
|
1222
|
+
let out = "";
|
|
1223
|
+
for (const line of pending.split("\n")) {
|
|
1224
|
+
if (line !== "") out += `${line} ${head}\n`;
|
|
1225
|
+
}
|
|
1226
|
+
await state.append(`${DIR}/rewritten-list`, out);
|
|
1227
|
+
await state.remove(`${DIR}/rewritten-pending`);
|
|
524
1228
|
}
|
|
525
1229
|
|
|
526
1230
|
/**
|
|
527
|
-
*
|
|
1231
|
+
* pick_one_commit: a pick-like command run (do_pick_commit) and what follows from its
|
|
1232
|
+
* outcome: `edit` stops, the commit recorded as rewritten, a conflict stops.
|
|
528
1233
|
*/
|
|
529
|
-
private async
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
1234
|
+
private async pickOne(
|
|
1235
|
+
state: OperationState,
|
|
1236
|
+
worktree: Worktree | undefined,
|
|
1237
|
+
todo: TodoItem[],
|
|
1238
|
+
output: string[],
|
|
1239
|
+
): Promise<StepOutcome> {
|
|
1240
|
+
const item = todo[0] as TodoItem;
|
|
1241
|
+
const finalFixup = isFinalFixup(todo);
|
|
1242
|
+
const outcome = await this.pick(state, worktree, item, output, finalFixup);
|
|
1243
|
+
if (outcome.kind === "reschedule") return outcome.result;
|
|
1244
|
+
const rest = item.rest ?? (await this.instructionText(item.id));
|
|
1245
|
+
if (item.action === RebaseAction.EDIT) {
|
|
1246
|
+
const conflicts = outcome.kind === "conflicts";
|
|
1247
|
+
const lines: string[] = conflicts ? [...outcome.lines] : [];
|
|
1248
|
+
if (!conflicts) {
|
|
1249
|
+
lines.push(`Stopped at ${await this.abbreviateObjectId(item.id)}... ${rest}`);
|
|
1250
|
+
}
|
|
1251
|
+
lines.push(
|
|
1252
|
+
...(await this.errorWithPatch(state, item.id, rest, {
|
|
1253
|
+
exitCode: conflicts ? 1 : 0,
|
|
1254
|
+
toAmend: outcome.kind === "ok",
|
|
1255
|
+
...(conflicts ? { message: outcome.message } : {}),
|
|
1256
|
+
// the picked message (do_pick_commit's, --signoff's trailer appended)
|
|
1257
|
+
signoff: !conflicts && (await state.exists(`${DIR}/signoff`)),
|
|
1258
|
+
})),
|
|
1259
|
+
);
|
|
1260
|
+
return {
|
|
1261
|
+
status: conflicts ? RebaseStatus.STOPPED : RebaseStatus.EDIT,
|
|
1262
|
+
newHead: await this.resolveHead(),
|
|
1263
|
+
currentCommit: item.id,
|
|
1264
|
+
...(conflicts ? { conflicts: outcome.conflicts } : {}),
|
|
1265
|
+
message: lines.join("\n"),
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
if (outcome.kind === "ok") {
|
|
1269
|
+
await this.recordInRewritten(state, item.id, this.nextCommand);
|
|
1270
|
+
return undefined;
|
|
1271
|
+
}
|
|
1272
|
+
if (outcome.kind === "dropped") {
|
|
1273
|
+
if (finalFixup) await this.flushRewrittenPending(state);
|
|
1274
|
+
return undefined;
|
|
1275
|
+
}
|
|
1276
|
+
if (isFixup(item.action)) {
|
|
1277
|
+
// error_failed_squash: the chain's message for --continue, the commit to amend
|
|
1278
|
+
const squash = (await state.read(`${DIR}/message-squash`)) ?? "";
|
|
1279
|
+
await state.write(`${DIR}/message`, squash);
|
|
1280
|
+
await state.write("MERGE_MSG", squash);
|
|
1281
|
+
return {
|
|
1282
|
+
status: RebaseStatus.STOPPED,
|
|
1283
|
+
currentCommit: item.id,
|
|
1284
|
+
conflicts: outcome.conflicts,
|
|
1285
|
+
message: [
|
|
1286
|
+
...outcome.lines,
|
|
1287
|
+
...(await this.errorWithPatch(state, item.id, rest, { exitCode: 1, toAmend: true })),
|
|
1288
|
+
].join("\n"),
|
|
1289
|
+
};
|
|
1290
|
+
}
|
|
1291
|
+
// a reword already fast-forwarded, or about to make a root commit (HEAD on git's
|
|
1292
|
+
// squash_onto), amends
|
|
1293
|
+
const head = await this.resolveHead();
|
|
1294
|
+
const squashOnto = (await state.read(`${DIR}/squash-onto`))?.trim();
|
|
1295
|
+
const toAmend =
|
|
1296
|
+
item.action === RebaseAction.REWORD &&
|
|
1297
|
+
(head === item.id || (squashOnto !== undefined && head === squashOnto));
|
|
1298
|
+
return {
|
|
1299
|
+
status: RebaseStatus.STOPPED,
|
|
1300
|
+
currentCommit: item.id,
|
|
1301
|
+
conflicts: outcome.conflicts,
|
|
1302
|
+
message: [
|
|
1303
|
+
...outcome.lines,
|
|
1304
|
+
...(await this.errorWithPatch(state, item.id, rest, {
|
|
1305
|
+
exitCode: 1,
|
|
1306
|
+
toAmend,
|
|
1307
|
+
...(outcome.message !== undefined ? { message: outcome.message } : {}),
|
|
1308
|
+
})),
|
|
1309
|
+
].join("\n"),
|
|
535
1310
|
};
|
|
536
1311
|
}
|
|
537
1312
|
|
|
538
1313
|
/**
|
|
539
|
-
*
|
|
1314
|
+
* git's error_with_patch: the message for `--continue`, make_patch's `stopped-sha`,
|
|
1315
|
+
* `REBASE_HEAD` and `patch`, and either the commit to amend (`amend`, "You can amend the
|
|
1316
|
+
* commit now") or `Could not apply <abbrev>... <rest>`; the lines printed.
|
|
540
1317
|
*/
|
|
541
|
-
private async
|
|
542
|
-
|
|
543
|
-
|
|
1318
|
+
private async errorWithPatch(
|
|
1319
|
+
state: OperationState,
|
|
1320
|
+
id: ObjectId,
|
|
1321
|
+
rest: string,
|
|
1322
|
+
options: { exitCode: number; toAmend: boolean; message?: string; signoff?: boolean },
|
|
1323
|
+
): Promise<string[]> {
|
|
1324
|
+
if (options.message !== undefined && !(await state.exists(`${DIR}/message`))) {
|
|
1325
|
+
await state.write(`${DIR}/message`, options.message);
|
|
1326
|
+
}
|
|
1327
|
+
// make_patch
|
|
1328
|
+
await state.write(`${DIR}/stopped-sha`, `${id}\n`);
|
|
1329
|
+
await state.setRef("REBASE_HEAD", id);
|
|
1330
|
+
await state.write(`${DIR}/patch`, "");
|
|
1331
|
+
if (!(await state.exists(`${DIR}/message`))) {
|
|
1332
|
+
const commit = await this.commits.load(id);
|
|
1333
|
+
const body = messageBody(commit?.message ?? "");
|
|
1334
|
+
await state.write(
|
|
1335
|
+
`${DIR}/message`,
|
|
1336
|
+
options.signoff
|
|
1337
|
+
? appendSignoff(body, this.signoffLine(), trailerConfigOf(this.workingCopy))
|
|
1338
|
+
: `${body}\n`,
|
|
1339
|
+
);
|
|
1340
|
+
}
|
|
1341
|
+
if (options.toAmend) {
|
|
1342
|
+
await state.write(`${DIR}/amend`, `${await this.resolveHead()}\n`);
|
|
1343
|
+
return [
|
|
1344
|
+
"You can amend the commit now, with",
|
|
1345
|
+
"",
|
|
1346
|
+
" git commit --amend ",
|
|
1347
|
+
"",
|
|
1348
|
+
"Once you are satisfied with your changes, run",
|
|
1349
|
+
"",
|
|
1350
|
+
" git rebase --continue",
|
|
1351
|
+
];
|
|
1352
|
+
}
|
|
1353
|
+
if (options.exitCode)
|
|
1354
|
+
return [`Could not apply ${await this.abbreviateObjectId(id)}... ${rest}`];
|
|
1355
|
+
return [];
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* do_pick_commit: one commit picked onto HEAD (fast-forwarded where its parent is HEAD),
|
|
1360
|
+
* a fixup or squash melded into HEAD, a reword amended; what came of it.
|
|
1361
|
+
*/
|
|
1362
|
+
private async pick(
|
|
1363
|
+
state: OperationState,
|
|
1364
|
+
worktree: Worktree | undefined,
|
|
1365
|
+
item: TodoItem,
|
|
1366
|
+
output: string[],
|
|
1367
|
+
finalFixup: boolean,
|
|
1368
|
+
): Promise<PickOutcome> {
|
|
1369
|
+
const commit = await this.commits.load(item.id);
|
|
1370
|
+
if (!commit) throw new CommitNotFoundError(item.id);
|
|
1371
|
+
const headId = await this.resolveHead();
|
|
1372
|
+
const signoff = await state.exists(`${DIR}/signoff`);
|
|
1373
|
+
const fixup = isFixup(item.action);
|
|
1374
|
+
// on git's squash_onto (--root, `reset [new root]`) the pick makes a root commit
|
|
1375
|
+
const squashOnto = (await state.read(`${DIR}/squash-onto`))?.trim();
|
|
1376
|
+
const unborn = squashOnto !== undefined && headId === squashOnto;
|
|
1377
|
+
if (unborn && fixup) {
|
|
1378
|
+
return {
|
|
1379
|
+
kind: "reschedule",
|
|
1380
|
+
result: {
|
|
1381
|
+
status: RebaseStatus.FAILED,
|
|
1382
|
+
message: "error: cannot fixup root commit",
|
|
1383
|
+
reschedule: true,
|
|
1384
|
+
},
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
// git's allow_ff (no --force-rebase): a commit whose parent is HEAD is kept as it is
|
|
1388
|
+
if (
|
|
1389
|
+
!fixup &&
|
|
1390
|
+
(commit.parents.length === 0 ? unborn : commit.parents[0] === headId) &&
|
|
1391
|
+
!signoff &&
|
|
1392
|
+
(await this.allowFastForward(state))
|
|
1393
|
+
) {
|
|
1394
|
+
await this.writeAuthorScript(state, commit.author);
|
|
1395
|
+
const refused = await this.fastForward(state, worktree, headId, item.id);
|
|
1396
|
+
if (refused) return { kind: "reschedule", result: refused };
|
|
1397
|
+
if (item.action === RebaseAction.REWORD) {
|
|
1398
|
+
try {
|
|
1399
|
+
await this.rewordHead(state);
|
|
1400
|
+
} catch (error) {
|
|
1401
|
+
// fast_forward_edit's git commit failed: a stop with HEAD to amend
|
|
1402
|
+
return editorFailure(error);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
return { kind: "ok" };
|
|
1406
|
+
}
|
|
1407
|
+
// the trees read once: a pick's result is the next one's HEAD, a commit the next one's
|
|
1408
|
+
// parent; what is written and staged is only what the pick changes
|
|
1409
|
+
this.flatTrees ??= new FlatTreeCache(this.trees);
|
|
1410
|
+
const flats = this.flatTrees;
|
|
1411
|
+
const headTreeId = await this.commits.getTree(headId);
|
|
1412
|
+
const parentId = commit.parents[0];
|
|
1413
|
+
const parentTreeId = parentId ? await this.commits.getTree(parentId) : undefined;
|
|
1414
|
+
const subject = subjectOf(commit.message);
|
|
1415
|
+
// do_pick_commit: the message from its subject on; --signoff appends the committer's
|
|
1416
|
+
// sign-off (not for a fixup or squash), before the merge (MERGE_MSG has it)
|
|
1417
|
+
const body = messageBody(commit.message);
|
|
1418
|
+
const pickedMessage =
|
|
1419
|
+
signoff && !fixup
|
|
1420
|
+
? appendSignoff(body, this.signoffLine(), trailerConfigOf(this.workingCopy))
|
|
1421
|
+
: body;
|
|
1422
|
+
let messageFile: "squash" | "fixup" | "edit" | undefined;
|
|
1423
|
+
if (fixup) {
|
|
1424
|
+
await this.updateSquashMessages(state, item, commit.message);
|
|
1425
|
+
if (!finalFixup) messageFile = "squash";
|
|
1426
|
+
else if (await state.exists(`${DIR}/message-fixup`)) messageFile = "fixup";
|
|
1427
|
+
else {
|
|
1428
|
+
await state.write("SQUASH_MSG", (await state.read(`${DIR}/message-squash`)) ?? "");
|
|
1429
|
+
await state.remove("MERGE_MSG");
|
|
1430
|
+
messageFile = "edit";
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
await this.writeAuthorScript(state, commit.author);
|
|
1434
|
+
// short_commit_name: repo_find_unique_abbrev with DEFAULT_ABBREV
|
|
1435
|
+
const short = await this.abbreviateObjectId(item.id);
|
|
1436
|
+
const label = `${short} (${subject})`;
|
|
1437
|
+
const baseLabel = parentId ? `parent of ${label}` : "(empty tree)";
|
|
1438
|
+
// the trees as merge-ort reads them: what all three hold alike is never read
|
|
1439
|
+
const merge = await mergeStoredTrees(
|
|
1440
|
+
this.context(worktree),
|
|
1441
|
+
storedBase(parentTreeId, baseLabel, () => flats.get(parentTreeId)),
|
|
1442
|
+
headTreeId,
|
|
1443
|
+
commit.tree,
|
|
1444
|
+
{
|
|
1445
|
+
oursLabel: "HEAD",
|
|
1446
|
+
theirsLabel: label,
|
|
1447
|
+
baseLabel,
|
|
1448
|
+
style: conflictStyleOf(this.workingCopy),
|
|
1449
|
+
favor: favorOf(this.contentStrategy),
|
|
1450
|
+
ignoreCase: coreBool(this.workingCopy, "ignorecase", false),
|
|
1451
|
+
...ortConfigOptions(this.workingCopy, worktree),
|
|
1452
|
+
},
|
|
1453
|
+
(id) => flats.get(id),
|
|
1454
|
+
);
|
|
1455
|
+
const target = await ortResultEntries(this.blobs, merge);
|
|
1456
|
+
// only the directories the pick changes are stored again
|
|
1457
|
+
const treeId = await storeFlatTreeOver(this.trees, headTreeId, merge.ours, target);
|
|
1458
|
+
const conflicts = [...merge.conflicts.values()];
|
|
1459
|
+
// only the paths HEAD and the result differ on are switched
|
|
1460
|
+
const delta = await treeDelta(this.trees, headTreeId, treeId);
|
|
1461
|
+
const refused = await switchTrees(this.context(worktree), delta.from, delta.to, {
|
|
1462
|
+
action: "merge",
|
|
1463
|
+
operation: "rebase",
|
|
1464
|
+
conflicts,
|
|
1465
|
+
targetTree: treeId,
|
|
1466
|
+
});
|
|
1467
|
+
if (refused) {
|
|
1468
|
+
// nothing written: the pick is rescheduled (git keeps the author and REBASE_HEAD)
|
|
1469
|
+
await state.setRef("REBASE_HEAD", item.id);
|
|
544
1470
|
return {
|
|
545
|
-
|
|
1471
|
+
kind: "reschedule",
|
|
1472
|
+
result: {
|
|
1473
|
+
status: RebaseStatus.FAILED,
|
|
1474
|
+
currentCommit: item.id,
|
|
1475
|
+
message: refused.message,
|
|
1476
|
+
reschedule: true,
|
|
1477
|
+
},
|
|
546
1478
|
};
|
|
547
1479
|
}
|
|
1480
|
+
// the sequencer shows merge-ort's messages of a rebase only when the pick conflicts
|
|
1481
|
+
if (conflicts.length > 0 || !merge.clean) output.push(...merge.messages);
|
|
1482
|
+
await state.setRef("AUTO_MERGE", treeId);
|
|
1483
|
+
const merged = pickedMessage.endsWith("\n") ? pickedMessage : `${pickedMessage}\n`;
|
|
1484
|
+
if (conflicts.length > 0 || !merge.clean) {
|
|
1485
|
+
const paths = [...new Set(conflicts.map((c) => c.stagePath))].sort(compareGitPaths);
|
|
1486
|
+
await state.write("MERGE_MSG", `${merged}${conflictsComment(paths, true)}`);
|
|
1487
|
+
if (!fixup) {
|
|
1488
|
+
// do_pick_commit writes CHERRY_PICK_HEAD after a conflicted merge too; the rebase
|
|
1489
|
+
// stopping there deletes it again (REBASE_HEAD names the commit instead)
|
|
1490
|
+
await state.writeAndDeleteRef("CHERRY_PICK_HEAD", item.id);
|
|
1491
|
+
}
|
|
1492
|
+
return {
|
|
1493
|
+
kind: "conflicts",
|
|
1494
|
+
conflicts: [...new Set(merge.conflicts.keys())].sort(),
|
|
1495
|
+
// git 2.43 writes the message and a blank line (2.5x the conflict list after it)
|
|
1496
|
+
message: `${merged}\n`,
|
|
1497
|
+
lines: [
|
|
1498
|
+
`error: could not apply ${short}... ${subject}`,
|
|
1499
|
+
"hint: Resolve all conflicts manually, mark them as resolved with",
|
|
1500
|
+
'hint: "git add/rm <conflicted_files>", then run "git rebase --continue".',
|
|
1501
|
+
'hint: You can instead skip this commit: run "git rebase --skip".',
|
|
1502
|
+
'hint: To abort and get back to the state before "git rebase", run "git rebase --abort".',
|
|
1503
|
+
],
|
|
1504
|
+
};
|
|
1505
|
+
}
|
|
1506
|
+
await state.write("MERGE_MSG", pickedMessage);
|
|
1507
|
+
// allow_empty: a commit that was empty from the start is kept; one that became empty is
|
|
1508
|
+
// dropped (drop_redundant_commits), kept (keep_redundant_commits) or stops the rebase
|
|
1509
|
+
let allowEmpty = false;
|
|
1510
|
+
if (treeId === headTreeId) {
|
|
1511
|
+
if (commit.tree === (parentTreeId ?? this.objectFormat.emptyTreeId)) allowEmpty = true;
|
|
1512
|
+
else if (await state.exists(`${DIR}/keep_redundant_commits`)) allowEmpty = true;
|
|
1513
|
+
else if (await state.exists(`${DIR}/drop_redundant_commits`)) {
|
|
1514
|
+
await state.removeRef("AUTO_MERGE");
|
|
1515
|
+
await state.remove("MERGE_MSG");
|
|
1516
|
+
output.push(`dropping ${item.id} ${subject} -- patch contents already upstream`);
|
|
1517
|
+
// do_pick_commit: a dropped final fixup still ends the chain (its files removed)
|
|
1518
|
+
if (finalFixup) await this.endFixupChain(state);
|
|
1519
|
+
return { kind: "dropped" };
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
if (fixup) {
|
|
1523
|
+
// try_to_commit with AMEND_MSG: HEAD's parents and author, the chain's message
|
|
1524
|
+
// verbatim; the last of a chain with a squash is edited (git commit -e)
|
|
1525
|
+
const head = await this.commits.load(headId);
|
|
1526
|
+
if (!head) throw new NoHeadError("HEAD cannot be resolved");
|
|
1527
|
+
// without ALLOW_EMPTY an amend whose tree is that of HEAD's first parent (the empty
|
|
1528
|
+
// tree for a root) is left to git commit, which refuses it: the chain stops
|
|
1529
|
+
// (error_failed_squash), HEAD to amend
|
|
1530
|
+
if (!allowEmpty && head.parents.length <= 1) {
|
|
1531
|
+
const parentTree = head.parents[0]
|
|
1532
|
+
? await this.commits.getTree(head.parents[0])
|
|
1533
|
+
: this.objectFormat.emptyTreeId;
|
|
1534
|
+
if (parentTree === treeId) {
|
|
1535
|
+
// do_commit: REBASE_HEAD written before git commit runs
|
|
1536
|
+
await state.setRef("REBASE_HEAD", item.id);
|
|
1537
|
+
return {
|
|
1538
|
+
kind: "conflicts",
|
|
1539
|
+
conflicts: [],
|
|
1540
|
+
lines: [
|
|
1541
|
+
"You asked to amend the most recent commit, but doing so would make",
|
|
1542
|
+
"it empty. You can repeat your command with --allow-empty, or you can",
|
|
1543
|
+
'remove the commit entirely with "git reset HEAD^".',
|
|
1544
|
+
],
|
|
1545
|
+
};
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
let message: string;
|
|
1549
|
+
if (messageFile === "edit") {
|
|
1550
|
+
// do_commit hands it to git commit: REBASE_HEAD first, git commit drops AUTO_MERGE
|
|
1551
|
+
// once it has committed (an editor that fails stops the chain as a conflict does)
|
|
1552
|
+
await state.setRef("REBASE_HEAD", item.id);
|
|
1553
|
+
try {
|
|
1554
|
+
message = this.editMessage((await state.read("SQUASH_MSG")) ?? "");
|
|
1555
|
+
} catch (error) {
|
|
1556
|
+
return editorFailure(error);
|
|
1557
|
+
}
|
|
1558
|
+
await state.removeRef("AUTO_MERGE");
|
|
1559
|
+
} else {
|
|
1560
|
+
message =
|
|
1561
|
+
(await state.read(
|
|
1562
|
+
messageFile === "fixup" ? `${DIR}/message-fixup` : `${DIR}/message-squash`,
|
|
1563
|
+
)) ?? "";
|
|
1564
|
+
if (signoff) message = stripspace(message);
|
|
1565
|
+
}
|
|
1566
|
+
await this.amendHead(state, treeId, message, item.action, messageFile === "edit");
|
|
1567
|
+
// try_to_commit removes MERGE_MSG; git commit (the edited end of a chain) SQUASH_MSG too
|
|
1568
|
+
await state.remove("MERGE_MSG");
|
|
1569
|
+
if (messageFile === "edit") await state.remove("SQUASH_MSG");
|
|
1570
|
+
} else {
|
|
1571
|
+
if (treeId === headTreeId && !allowEmpty) {
|
|
1572
|
+
// try_to_commit refuses the empty commit; git commit says why
|
|
1573
|
+
await state.setRef("CHERRY_PICK_HEAD", item.id);
|
|
1574
|
+
return {
|
|
1575
|
+
kind: "conflicts",
|
|
1576
|
+
conflicts: [],
|
|
1577
|
+
message: pickedMessage,
|
|
1578
|
+
lines: [
|
|
1579
|
+
"The previous cherry-pick is now empty, possibly due to conflict resolution.",
|
|
1580
|
+
"If you wish to commit it anyway, use:",
|
|
1581
|
+
"",
|
|
1582
|
+
" git commit --allow-empty",
|
|
1583
|
+
"",
|
|
1584
|
+
"Otherwise, please use 'git rebase --skip'",
|
|
1585
|
+
],
|
|
1586
|
+
};
|
|
1587
|
+
}
|
|
1588
|
+
// try_to_commit: a signed-off message is cleaned up as whitespace (else verbatim)
|
|
1589
|
+
const message = signoff ? stripspace(pickedMessage) : pickedMessage;
|
|
1590
|
+
// do_pick_commit writes CHERRY_PICK_HEAD for the commit to come; do_commit deletes it
|
|
1591
|
+
// once try_to_commit made it
|
|
1592
|
+
await state.withRefDuring("CHERRY_PICK_HEAD", item.id, () =>
|
|
1593
|
+
this.commitOnto(state, unborn ? [] : [headId], treeId, commit.author, message, item.action),
|
|
1594
|
+
);
|
|
1595
|
+
// do_commit: the commit made, MERGE_MSG removed
|
|
1596
|
+
await state.remove("MERGE_MSG");
|
|
1597
|
+
if (item.action === RebaseAction.REWORD) {
|
|
1598
|
+
try {
|
|
1599
|
+
await this.rewordHead(state);
|
|
1600
|
+
} catch (error) {
|
|
1601
|
+
// git commit --amend -e failed (the editor): the pick stays, the rebase stops
|
|
1602
|
+
return { ...editorFailure(error), message: pickedMessage };
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
if (finalFixup) await this.endFixupChain(state);
|
|
1607
|
+
return { kind: "ok" };
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
/** do_pick_commit after a final fixup: the chain's files removed. */
|
|
1611
|
+
private async endFixupChain(state: OperationState): Promise<void> {
|
|
1612
|
+
await state.remove(`${DIR}/message-fixup`, `${DIR}/message-squash`, `${DIR}/current-fixups`);
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
/** The commit message editor: the interactive handler, else the text as it is. */
|
|
1616
|
+
private editMessage(text: string): string {
|
|
1617
|
+
const edited = this.interactiveHandler?.modifyCommitMessage
|
|
1618
|
+
? this.interactiveHandler.modifyCommitMessage(text)
|
|
1619
|
+
: text;
|
|
1620
|
+
// git commit -e: the comments stripped (commit.cleanup aside)
|
|
1621
|
+
return cleanupMessage(edited, commentPrefixOf(this.workingCopy));
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
/**
|
|
1625
|
+
* A reword (git commit --amend -e after the pick): HEAD committed again with its message
|
|
1626
|
+
* edited, logged `rebase (reword): <subject>`.
|
|
1627
|
+
*/
|
|
1628
|
+
private async rewordHead(state: OperationState): Promise<void> {
|
|
1629
|
+
const head = await this.commits.load(await this.resolveHead());
|
|
1630
|
+
if (!head) return;
|
|
1631
|
+
if (this.hooks.enabled) {
|
|
1632
|
+
// run_git_commit(EDIT_MSG | VERIFY_MSG | AMEND_MSG): git commit --amend -e, its hooks
|
|
1633
|
+
await state.removeRef("AUTO_MERGE");
|
|
1634
|
+
await this.amendHead(state, head.tree, head.message, RebaseAction.REWORD, true, {
|
|
1635
|
+
verify: true,
|
|
1636
|
+
editor: true,
|
|
1637
|
+
hookArgs: ["commit", "HEAD"],
|
|
1638
|
+
env: { GIT_REFLOG_ACTION: this.stepAction(RebaseAction.REWORD) },
|
|
1639
|
+
});
|
|
1640
|
+
await state.remove("SQUASH_MSG");
|
|
1641
|
+
return;
|
|
1642
|
+
}
|
|
1643
|
+
const message = this.editMessage(head.message);
|
|
1644
|
+
// git commit --amend: the merge state it removes
|
|
1645
|
+
await state.removeRef("AUTO_MERGE");
|
|
1646
|
+
await this.amendHead(state, head.tree, message, RebaseAction.REWORD, true);
|
|
1647
|
+
await state.remove("SQUASH_MSG");
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/** The reflog action of a rebase step (`rebase (reword)`), git's GIT_REFLOG_ACTION for it. */
|
|
1651
|
+
private stepAction(step: string): string {
|
|
1652
|
+
return reflogMessages.rebaseStep(this.reflogAction("rebase"), step);
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
/** The editor of a `git commit -e` run (the interactive handler; comments kept). */
|
|
1656
|
+
private modifyMessage(text: string): string {
|
|
1657
|
+
return this.interactiveHandler?.modifyCommitMessage
|
|
1658
|
+
? this.interactiveHandler.modifyCommitMessage(text)
|
|
1659
|
+
: text;
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
/**
|
|
1663
|
+
* HEAD replaced by a commit of `tree` with HEAD's parents and author (an amend), its notes
|
|
1664
|
+
* carried over (`notes.rewriteRef`). `byGitCommit`: git runs `git commit --amend` for it,
|
|
1665
|
+
* whose notes commit takes the amended author (git exports it), not the environment's.
|
|
1666
|
+
*/
|
|
1667
|
+
private async amendHead(
|
|
1668
|
+
state: OperationState,
|
|
1669
|
+
tree: ObjectId,
|
|
1670
|
+
message: string,
|
|
1671
|
+
step: string,
|
|
1672
|
+
byGitCommit = false,
|
|
1673
|
+
/** `git commit` run with hooks: `message` is its template, edited and checked by them */
|
|
1674
|
+
gitCommit?: GitCommitHookRun,
|
|
1675
|
+
): Promise<ObjectId> {
|
|
1676
|
+
const headId = await this.resolveHead();
|
|
1677
|
+
const head = await this.commits.load(headId);
|
|
1678
|
+
if (!head) throw new NoHeadError("HEAD cannot be resolved");
|
|
1679
|
+
const id = await this.commitOnto(
|
|
1680
|
+
state,
|
|
1681
|
+
head.parents,
|
|
1682
|
+
tree,
|
|
1683
|
+
head.author,
|
|
1684
|
+
message,
|
|
1685
|
+
step,
|
|
1686
|
+
gitCommit,
|
|
1687
|
+
);
|
|
1688
|
+
// commit_post_rewrite of the amend (git commit --amend, try_to_commit's AMEND_MSG)
|
|
1689
|
+
await copyNotesForAmend(this.notesContext(), headId, id, byGitCommit ? head.author : undefined);
|
|
1690
|
+
// ... and git commit's post-rewrite hook, told "amend"
|
|
1691
|
+
if (gitCommit) await gitCommitAfterHooks(this.hooks, gitCommit, { from: headId, to: id });
|
|
1692
|
+
return id;
|
|
1693
|
+
}
|
|
548
1694
|
|
|
549
|
-
|
|
550
|
-
|
|
1695
|
+
/**
|
|
1696
|
+
* sequencer.c's update_squash_messages: the chain's message in `message-squash` (`# This
|
|
1697
|
+
* is a combination of N commits.`, each message after its header, a fixup's commented
|
|
1698
|
+
* out), `message-fixup` for a chain of fixups only (the first commit's message, or a
|
|
1699
|
+
* `fixup -C` commit's), the chain in `current-fixups`.
|
|
1700
|
+
*/
|
|
1701
|
+
private async updateSquashMessages(
|
|
1702
|
+
state: OperationState,
|
|
1703
|
+
item: TodoItem,
|
|
1704
|
+
message: string,
|
|
1705
|
+
): Promise<void> {
|
|
1706
|
+
const comment = commentPrefixOf(this.workingCopy);
|
|
1707
|
+
const currentFixups = (await state.read(`${DIR}/current-fixups`)) ?? "";
|
|
1708
|
+
let count = currentFixups === "" ? 0 : currentFixups.split("\n").length;
|
|
1709
|
+
const seenSquash = currentFixups.startsWith("squash") || currentFixups.includes("\nsquash");
|
|
1710
|
+
const fixupFlag =
|
|
1711
|
+
item.action === RebaseAction.FIXUP &&
|
|
1712
|
+
(item.replaceMessage === true || item.editMessage === true);
|
|
1713
|
+
let buf: string;
|
|
1714
|
+
if (count > 0) {
|
|
1715
|
+
const squash = await state.read(`${DIR}/message-squash`);
|
|
1716
|
+
if (!squash)
|
|
1717
|
+
throw new InvalidArgumentError("todo", item.id, `could not read '${DIR}/message-squash'`);
|
|
1718
|
+
const eol = !squash.startsWith(comment)
|
|
1719
|
+
? 0
|
|
1720
|
+
: squash.indexOf("\n") < 0
|
|
1721
|
+
? squash.length
|
|
1722
|
+
: squash.indexOf("\n");
|
|
1723
|
+
buf = `${comment} ${combinedMessage(count + 2)}${squash.slice(eol)}`;
|
|
1724
|
+
if (fixupFlag && !seenSquash) buf = squashMessageForFixup(buf, comment);
|
|
1725
|
+
} else {
|
|
1726
|
+
const head = await this.commits.load(await this.resolveHead());
|
|
1727
|
+
const headBody = messageBody(head?.message ?? "");
|
|
1728
|
+
if (item.action === RebaseAction.FIXUP && !fixupFlag) {
|
|
1729
|
+
await state.write(`${DIR}/message-fixup`, headBody);
|
|
1730
|
+
}
|
|
1731
|
+
buf = `${comment} ${combinedMessage(2)}\n${comment} ${fixupFlag ? squashTexts.skipFirstMessage : squashTexts.firstMessage}\n\n`;
|
|
1732
|
+
buf = fixupFlag ? commentLines(buf, headBody, comment) : buf + headBody;
|
|
1733
|
+
}
|
|
1734
|
+
const body = messageBody(message);
|
|
1735
|
+
if (item.action === RebaseAction.SQUASH || fixupFlag) {
|
|
1736
|
+
// append_squash_message
|
|
1737
|
+
let commented = 0;
|
|
1738
|
+
if (
|
|
1739
|
+
body.startsWith("amend!") ||
|
|
1740
|
+
((item.action === RebaseAction.SQUASH || seenSquash) &&
|
|
1741
|
+
(body.startsWith("squash!") || body.startsWith("fixup!")))
|
|
1742
|
+
) {
|
|
1743
|
+
commented = subjectLength(body);
|
|
1744
|
+
}
|
|
1745
|
+
buf += `\n${comment} ${squashTexts.nthMessage(++count + 1)}\n\n`;
|
|
1746
|
+
buf = commentLines(buf, body.slice(0, commented), comment);
|
|
1747
|
+
const fixupOff = buf.length;
|
|
1748
|
+
buf += body.slice(commented);
|
|
1749
|
+
if (fixupFlag && !seenSquash) {
|
|
1750
|
+
// the message replaced: --signoff's trailer appended (append_squash_message)
|
|
1751
|
+
if (await state.exists(`${DIR}/signoff`)) {
|
|
1752
|
+
buf = appendSignoff(buf, this.signoffLine(), trailerConfigOf(this.workingCopy));
|
|
1753
|
+
}
|
|
1754
|
+
if (
|
|
1755
|
+
item.replaceMessage &&
|
|
1756
|
+
((await state.exists(`${DIR}/message-fixup`)) ||
|
|
1757
|
+
!(await state.exists(`${DIR}/message-squash`)))
|
|
1758
|
+
) {
|
|
1759
|
+
await state.write(
|
|
1760
|
+
`${DIR}/message-fixup`,
|
|
1761
|
+
buf.slice(fixupOff).replace(/^([ \t\r]*\n)+/, ""),
|
|
1762
|
+
);
|
|
1763
|
+
} else await state.remove(`${DIR}/message-fixup`);
|
|
1764
|
+
} else await state.remove(`${DIR}/message-fixup`);
|
|
1765
|
+
} else {
|
|
1766
|
+
buf += `\n${comment} ${squashTexts.skipNthMessage(++count + 1)}\n\n`;
|
|
1767
|
+
buf = commentLines(buf, body, comment);
|
|
1768
|
+
}
|
|
1769
|
+
await state.write(`${DIR}/message-squash`, buf);
|
|
1770
|
+
const flag = fixupFlag && item.editMessage ? " -c" : "";
|
|
1771
|
+
await state.write(
|
|
1772
|
+
`${DIR}/current-fixups`,
|
|
1773
|
+
`${currentFixups}${currentFixups ? "\n" : ""}${item.action}${flag} ${item.id}`,
|
|
1774
|
+
);
|
|
1775
|
+
}
|
|
551
1776
|
|
|
552
|
-
|
|
553
|
-
|
|
1777
|
+
/** One todo command run: a pick, or a `label`, `reset`, `merge`; a result when it stops. */
|
|
1778
|
+
private async runTodoCommand(
|
|
1779
|
+
state: OperationState,
|
|
1780
|
+
worktree: Worktree | undefined,
|
|
1781
|
+
todo: TodoItem[],
|
|
1782
|
+
output: string[],
|
|
1783
|
+
): Promise<StepOutcome> {
|
|
1784
|
+
const item = todo[0] as TodoItem;
|
|
1785
|
+
if (isNoop(item)) return undefined;
|
|
1786
|
+
switch (item.action) {
|
|
1787
|
+
case RebaseAction.LABEL:
|
|
1788
|
+
return this.label(state, item.rest ?? "");
|
|
1789
|
+
case RebaseAction.RESET:
|
|
1790
|
+
return this.reset(state, worktree, item.rest ?? "");
|
|
1791
|
+
case RebaseAction.MERGE: {
|
|
1792
|
+
const outcome = await this.merge(state, worktree, item, output);
|
|
1793
|
+
if (outcome?.reschedule) return outcome;
|
|
1794
|
+
// record_in_rewritten after do_merge (conflicted or not)
|
|
1795
|
+
if (item.id) await this.recordInRewritten(state, item.id, this.nextCommand);
|
|
1796
|
+
return outcome;
|
|
1797
|
+
}
|
|
1798
|
+
case RebaseAction.EXEC:
|
|
1799
|
+
return this.exec(state, worktree, item.rest ?? "", output);
|
|
1800
|
+
case RebaseAction.UPDATE_REF:
|
|
1801
|
+
await this.updateRefRecord(state, item.rest ?? "");
|
|
1802
|
+
return undefined;
|
|
1803
|
+
default:
|
|
1804
|
+
return this.pickOne(state, worktree, todo, output);
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
554
1807
|
|
|
1808
|
+
/**
|
|
1809
|
+
* git's do_exec: `Executing: <command>`, the command run by the exec handler, the index
|
|
1810
|
+
* read again; a failure, or changes left in the index or worktree, stop the rebase (with
|
|
1811
|
+
* `--reschedule-failed-exec` the exec goes back to the todo list).
|
|
1812
|
+
*/
|
|
1813
|
+
private async exec(
|
|
1814
|
+
state: OperationState,
|
|
1815
|
+
worktree: Worktree | undefined,
|
|
1816
|
+
command: string,
|
|
1817
|
+
output: string[],
|
|
1818
|
+
): Promise<StepOutcome> {
|
|
1819
|
+
output.push(`Executing: ${command}`);
|
|
1820
|
+
let status: number;
|
|
1821
|
+
if (this.execHandler) {
|
|
1822
|
+
const result = await this.execHandler(command);
|
|
1823
|
+
status = result.exitCode;
|
|
1824
|
+
output.push(...(result.output ?? []));
|
|
1825
|
+
} else {
|
|
1826
|
+
output.push(`${command}: no exec handler to run it`);
|
|
1827
|
+
status = 127;
|
|
1828
|
+
}
|
|
1829
|
+
// force re-reading of the index
|
|
1830
|
+
await this.staging.read?.();
|
|
1831
|
+
const head = await this.resolveHead();
|
|
1832
|
+
const dirty = await this.uncleanWorktree(head, worktree, "rebase", false);
|
|
1833
|
+
const lines: string[] = dirty ? (dirty.message ?? "").split("\n") : [];
|
|
1834
|
+
if (status !== 0) {
|
|
1835
|
+
lines.push(
|
|
1836
|
+
`warning: execution failed: ${command}`,
|
|
1837
|
+
...(dirty ? ["and made changes to the index and/or the working tree."] : []),
|
|
1838
|
+
"You can fix the problem, and then run",
|
|
1839
|
+
"",
|
|
1840
|
+
" git rebase --continue",
|
|
1841
|
+
"",
|
|
1842
|
+
);
|
|
1843
|
+
} else if (dirty) {
|
|
1844
|
+
lines.push(
|
|
1845
|
+
`warning: execution succeeded: ${command}`,
|
|
1846
|
+
"but left changes to the index and/or the working tree.",
|
|
1847
|
+
"Commit or stash your changes, and then run",
|
|
1848
|
+
"",
|
|
1849
|
+
" git rebase --continue",
|
|
1850
|
+
"",
|
|
1851
|
+
);
|
|
1852
|
+
status = 1;
|
|
1853
|
+
}
|
|
1854
|
+
if (status === 0) return { checkTodo: true };
|
|
555
1855
|
return {
|
|
556
|
-
status: RebaseStatus.
|
|
557
|
-
|
|
1856
|
+
status: RebaseStatus.STOPPED,
|
|
1857
|
+
message: lines.join("\n"),
|
|
1858
|
+
...((await state.exists(`${DIR}/reschedule-failed-exec`)) ? { reschedule: true } : {}),
|
|
558
1859
|
};
|
|
559
1860
|
}
|
|
560
1861
|
|
|
1862
|
+
/** git's do_label: `refs/rewritten/<name>` at HEAD, listed in `refs-to-delete`. */
|
|
1863
|
+
private async label(
|
|
1864
|
+
state: OperationState,
|
|
1865
|
+
name: string,
|
|
1866
|
+
): Promise<(RebaseResult & { reschedule?: boolean }) | undefined> {
|
|
1867
|
+
if (name === "#") {
|
|
1868
|
+
return {
|
|
1869
|
+
status: RebaseStatus.FAILED,
|
|
1870
|
+
message: `error: illegal label name: '${name}'`,
|
|
1871
|
+
reschedule: true,
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1874
|
+
const head = await this.resolveHead();
|
|
1875
|
+
await this.refsStore.set(`refs/rewritten/${name}`, head);
|
|
1876
|
+
await state.append(`${DIR}/refs-to-delete`, `refs/rewritten/${name}\n`);
|
|
1877
|
+
return undefined;
|
|
1878
|
+
}
|
|
1879
|
+
|
|
561
1880
|
/**
|
|
562
|
-
*
|
|
1881
|
+
* sequencer.c's lookup_label: `refs/rewritten/<label>`, else the label as a commit-ish;
|
|
1882
|
+
* the name the merge's conflict markers show (the ref, or the name as written).
|
|
563
1883
|
*/
|
|
564
|
-
private async
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
1884
|
+
private async lookupLabel(
|
|
1885
|
+
state: OperationState,
|
|
1886
|
+
label: string,
|
|
1887
|
+
): Promise<{ id: ObjectId; name: string } | undefined> {
|
|
1888
|
+
const rewritten = `refs/rewritten/${label}`;
|
|
1889
|
+
const ref = await this.refsStore.resolve(rewritten).catch(() => undefined);
|
|
1890
|
+
if (ref?.objectId) return { id: ref.objectId, name: rewritten };
|
|
1891
|
+
const named = await this.refsStore.resolve(label).catch(() => undefined);
|
|
1892
|
+
if (named?.objectId) return { id: await this.peelToCommit(named.objectId), name: label };
|
|
1893
|
+
for (const prefix of ["refs/tags/", "refs/heads/", "refs/remotes/"]) {
|
|
1894
|
+
const r = await this.refsStore.resolve(`${prefix}${label}`).catch(() => undefined);
|
|
1895
|
+
if (r?.objectId) return { id: await this.peelToCommit(r.objectId), name: label };
|
|
568
1896
|
}
|
|
1897
|
+
if (/^[0-9a-f]{4,64}$/i.test(label)) {
|
|
1898
|
+
try {
|
|
1899
|
+
const id = await expandCommitId(
|
|
1900
|
+
{ commits: this.commits, refs: this.refsStore, state },
|
|
1901
|
+
label.toLowerCase(),
|
|
1902
|
+
);
|
|
1903
|
+
if (id) return { id, name: label };
|
|
1904
|
+
} catch {
|
|
1905
|
+
// not an abbreviation of one commit
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
return undefined;
|
|
1909
|
+
}
|
|
569
1910
|
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
1911
|
+
/** An annotated tag peeled to what it tags. */
|
|
1912
|
+
private async peelToCommit(id: ObjectId): Promise<ObjectId> {
|
|
1913
|
+
let current = id;
|
|
1914
|
+
for (
|
|
1915
|
+
let depth = 0;
|
|
1916
|
+
depth < 32 && this.tagsStore && (await this.tagsStore.has(current));
|
|
1917
|
+
depth++
|
|
1918
|
+
) {
|
|
1919
|
+
const tag = await this.tagsStore.load(current);
|
|
1920
|
+
if (!tag) break;
|
|
1921
|
+
current = tag.object;
|
|
574
1922
|
}
|
|
1923
|
+
return current;
|
|
1924
|
+
}
|
|
575
1925
|
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
1926
|
+
/** git's do_reset: HEAD, the index and the worktree moved to a label's commit. */
|
|
1927
|
+
private async reset(
|
|
1928
|
+
state: OperationState,
|
|
1929
|
+
worktree: Worktree | undefined,
|
|
1930
|
+
arg: string,
|
|
1931
|
+
): Promise<(RebaseResult & { reschedule?: boolean }) | undefined> {
|
|
1932
|
+
// the label ends at the first blank (a `reset <label> # <oneline>` line); `[new root]`
|
|
1933
|
+
// is git's squash_onto (made when the rebase has none yet)
|
|
1934
|
+
const newRoot = arg.startsWith("[new root]");
|
|
1935
|
+
const end = arg.search(/[ \t\n\r]/);
|
|
1936
|
+
const name = newRoot ? "[new root]" : end < 0 ? arg : arg.slice(0, end);
|
|
1937
|
+
let found: { id: ObjectId; name: string } | undefined;
|
|
1938
|
+
if (newRoot) {
|
|
1939
|
+
let id = (await state.read(`${DIR}/squash-onto`))?.trim();
|
|
1940
|
+
if (!id) {
|
|
1941
|
+
id = await this.createSquashOnto();
|
|
1942
|
+
await state.write(`${DIR}/squash-onto`, id);
|
|
1943
|
+
}
|
|
1944
|
+
found = { id, name };
|
|
1945
|
+
} else found = await this.lookupLabel(state, name);
|
|
1946
|
+
if (!found) {
|
|
1947
|
+
return {
|
|
1948
|
+
status: RebaseStatus.FAILED,
|
|
1949
|
+
message: `error: could not resolve '${name}'`,
|
|
1950
|
+
reschedule: true,
|
|
1951
|
+
};
|
|
1952
|
+
}
|
|
1953
|
+
const headId = await this.resolveHead();
|
|
1954
|
+
const headTree = await this.commits.getTree(headId);
|
|
1955
|
+
const targetTree = await this.commits.getTree(found.id);
|
|
1956
|
+
const delta = await treeDelta(this.trees, headTree, targetTree);
|
|
1957
|
+
const refused = await switchTrees(this.context(worktree), delta.from, delta.to, {
|
|
1958
|
+
action: "checkout",
|
|
1959
|
+
operation: "reset",
|
|
1960
|
+
targetTree,
|
|
1961
|
+
});
|
|
1962
|
+
if (refused) {
|
|
1963
|
+
return { status: RebaseStatus.FAILED, message: refused.message, reschedule: true };
|
|
583
1964
|
}
|
|
1965
|
+
await this.refsStore.set(
|
|
1966
|
+
"HEAD",
|
|
1967
|
+
found.id,
|
|
1968
|
+
this.reflogLine(
|
|
1969
|
+
`${reflogMessages.rebaseStep(this.reflogAction("rebase"), "reset")}: '${name}'`,
|
|
1970
|
+
this.committer,
|
|
1971
|
+
),
|
|
1972
|
+
);
|
|
1973
|
+
return undefined;
|
|
1974
|
+
}
|
|
584
1975
|
|
|
585
|
-
|
|
1976
|
+
/**
|
|
1977
|
+
* git's do_merge for one merge head: the original merge (`-C <commit>`) recreated on HEAD
|
|
1978
|
+
* with its message and author, merge-ort merging the label's commit; a fast-forward to the
|
|
1979
|
+
* original when HEAD and the heads are its parents; nothing when the head is already in
|
|
1980
|
+
* HEAD. A conflict stops as a pick does (`Could not apply <abbrev>... <label> # <oneline>`).
|
|
1981
|
+
*/
|
|
1982
|
+
private async merge(
|
|
1983
|
+
state: OperationState,
|
|
1984
|
+
worktree: Worktree | undefined,
|
|
1985
|
+
item: TodoItem,
|
|
1986
|
+
output: string[],
|
|
1987
|
+
): Promise<(RebaseResult & { reschedule?: boolean }) | undefined> {
|
|
1988
|
+
const arg = item.rest ?? "";
|
|
1989
|
+
// the merge heads, up to a `#` that starts the oneline
|
|
1990
|
+
const heads: { id: ObjectId; name: string }[] = [];
|
|
1991
|
+
let mergeArgEnd = arg.length;
|
|
1992
|
+
let oneline: string | undefined;
|
|
1993
|
+
let p = 0;
|
|
1994
|
+
for (;;) {
|
|
1995
|
+
while (p < arg.length && /[ \t\n]/.test(arg[p] as string)) p++;
|
|
1996
|
+
if (p >= arg.length) break;
|
|
1997
|
+
if (arg[p] === "#" && (p + 1 >= arg.length || /[ \t\n\r]/.test(arg[p + 1] as string))) {
|
|
1998
|
+
p++;
|
|
1999
|
+
while (p < arg.length && /[ \t\n]/.test(arg[p] as string)) p++;
|
|
2000
|
+
oneline = arg.slice(p);
|
|
2001
|
+
break;
|
|
2002
|
+
}
|
|
2003
|
+
let k = p;
|
|
2004
|
+
while (k < arg.length && !/[ \t\n]/.test(arg[k] as string)) k++;
|
|
2005
|
+
const label = arg.slice(p, k);
|
|
2006
|
+
const found = await this.lookupLabel(state, label);
|
|
2007
|
+
if (!found) {
|
|
2008
|
+
return {
|
|
2009
|
+
status: RebaseStatus.FAILED,
|
|
2010
|
+
message: `error: could not resolve '${label}'\nerror: unable to parse '${label}'`,
|
|
2011
|
+
reschedule: true,
|
|
2012
|
+
};
|
|
2013
|
+
}
|
|
2014
|
+
heads.push(found);
|
|
2015
|
+
p = k;
|
|
2016
|
+
mergeArgEnd = k;
|
|
2017
|
+
}
|
|
2018
|
+
if (heads.length === 0) {
|
|
2019
|
+
return { status: RebaseStatus.FAILED, message: `error: nothing to merge: '${arg}'` };
|
|
2020
|
+
}
|
|
2021
|
+
const original = item.id ? await this.commits.load(item.id) : undefined;
|
|
2022
|
+
const headId = await this.resolveHead();
|
|
2023
|
+
// a merge onto a `[new root]`: a fast-forward to the merge head
|
|
2024
|
+
const squashOnto = (await state.read(`${DIR}/squash-onto`))?.trim();
|
|
2025
|
+
if (squashOnto && headId === squashOnto) {
|
|
2026
|
+
if (heads.length > 1) {
|
|
2027
|
+
return {
|
|
2028
|
+
status: RebaseStatus.FAILED,
|
|
2029
|
+
message: "error: octopus merge cannot be executed on top of a [new root]",
|
|
2030
|
+
reschedule: true,
|
|
2031
|
+
};
|
|
2032
|
+
}
|
|
2033
|
+
return this.fastForward(state, worktree, headId, (heads[0] as { id: ObjectId }).id);
|
|
2034
|
+
}
|
|
2035
|
+
const forced = !(await this.allowFastForward(state));
|
|
2036
|
+
// HEAD the original's first parent, the heads its other parents: the original is kept
|
|
2037
|
+
if (
|
|
2038
|
+
!forced &&
|
|
2039
|
+
original &&
|
|
2040
|
+
original.parents[0] === headId &&
|
|
2041
|
+
original.parents.length === heads.length + 1 &&
|
|
2042
|
+
heads.every((h, k) => original.parents[k + 1] === h.id)
|
|
2043
|
+
) {
|
|
2044
|
+
const refused = await this.fastForward(state, worktree, headId, item.id);
|
|
2045
|
+
if (refused) return refused;
|
|
2046
|
+
if (item.editMessage) await this.amendMergeMessage(state, item.id);
|
|
2047
|
+
return undefined;
|
|
2048
|
+
}
|
|
2049
|
+
let message: string;
|
|
2050
|
+
let author: PersonIdent;
|
|
2051
|
+
if (original) {
|
|
2052
|
+
// find_commit_subject: the message from its first non-blank line
|
|
2053
|
+
message = original.message.replace(/^([ \t\r]*\n)+/, "");
|
|
2054
|
+
author = original.author;
|
|
2055
|
+
} else {
|
|
2056
|
+
author = this.resolveAuthor();
|
|
2057
|
+
message =
|
|
2058
|
+
oneline !== undefined
|
|
2059
|
+
? oneline
|
|
2060
|
+
: `Merge ${heads.length > 1 ? "branches" : "branch"} '${arg.slice(0, mergeArgEnd).replace(/^[ \t\n]+/, "")}'`;
|
|
2061
|
+
}
|
|
2062
|
+
await this.writeAuthorScript(state, author);
|
|
2063
|
+
await state.write("MERGE_MSG", message);
|
|
2064
|
+
if (heads.length > 1) {
|
|
2065
|
+
return this.octopus(state, item, heads, author, message, output);
|
|
2066
|
+
}
|
|
2067
|
+
const head = heads[0] as { id: ObjectId; name: string };
|
|
2068
|
+
const bases = await revisionMergeBases(this.commits, headId, head.id);
|
|
2069
|
+
if (bases[0] === head.id) {
|
|
2070
|
+
// the head is already in HEAD: nothing to merge
|
|
2071
|
+
return undefined;
|
|
2072
|
+
}
|
|
2073
|
+
await state.write("MERGE_HEAD", head.id);
|
|
2074
|
+
await state.write("MERGE_MODE", "no-ff");
|
|
2075
|
+
const headCommitTree = await this.commits.getTree(headId);
|
|
2076
|
+
const headTree = await this.commits.getTree(head.id);
|
|
2077
|
+
const common = {
|
|
2078
|
+
style: conflictStyleOf(this.workingCopy),
|
|
2079
|
+
ignoreCase: coreBool(this.workingCopy, "ignorecase", false),
|
|
2080
|
+
...ortConfigOptions(this.workingCopy, worktree),
|
|
2081
|
+
};
|
|
2082
|
+
const base = await mergeBaseTree(
|
|
2083
|
+
{
|
|
2084
|
+
commits: this.commits,
|
|
2085
|
+
trees: this.trees,
|
|
2086
|
+
blobs: this.blobs,
|
|
2087
|
+
abbreviate: (id) => this.abbreviateObjectId(id),
|
|
2088
|
+
},
|
|
2089
|
+
headId,
|
|
2090
|
+
head.id,
|
|
2091
|
+
bases,
|
|
2092
|
+
common,
|
|
2093
|
+
);
|
|
2094
|
+
const merge = await mergeStoredTrees(
|
|
2095
|
+
{ trees: this.trees, blobs: this.blobs },
|
|
2096
|
+
base,
|
|
2097
|
+
headCommitTree,
|
|
2098
|
+
headTree,
|
|
2099
|
+
{
|
|
2100
|
+
...common,
|
|
2101
|
+
oursLabel: "HEAD",
|
|
2102
|
+
theirsLabel: head.name,
|
|
2103
|
+
baseLabel: base.label,
|
|
2104
|
+
favor: favorOf(this.contentStrategy),
|
|
2105
|
+
},
|
|
2106
|
+
);
|
|
2107
|
+
const target = await ortResultEntries(this.blobs, merge);
|
|
2108
|
+
const treeId = await storeFlatTreeOver(this.trees, headCommitTree, merge.ours, target);
|
|
2109
|
+
const conflicts = [...merge.conflicts.values()];
|
|
2110
|
+
const delta = await treeDelta(this.trees, headCommitTree, treeId);
|
|
2111
|
+
const refused = await switchTrees(this.context(worktree), delta.from, delta.to, {
|
|
2112
|
+
action: "merge",
|
|
2113
|
+
operation: "merge",
|
|
2114
|
+
conflicts,
|
|
2115
|
+
targetTree: treeId,
|
|
2116
|
+
});
|
|
2117
|
+
if (refused) {
|
|
2118
|
+
// could not even attempt to merge: rescheduled
|
|
2119
|
+
await state.remove("MERGE_MSG", "MERGE_HEAD", "MERGE_MODE");
|
|
2120
|
+
if (item.id) await state.setRef("REBASE_HEAD", item.id);
|
|
2121
|
+
return {
|
|
2122
|
+
status: RebaseStatus.FAILED,
|
|
2123
|
+
currentCommit: item.id || undefined,
|
|
2124
|
+
message: `${refused.message}\nerror: could not even attempt to merge '${arg.slice(0, mergeArgEnd)}'`,
|
|
2125
|
+
reschedule: true,
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
if (conflicts.length > 0 || !merge.clean) {
|
|
2129
|
+
output.push(...merge.messages);
|
|
2130
|
+
await state.setRef("AUTO_MERGE", treeId);
|
|
2131
|
+
// the message as it is (do_merge appends no conflict list), also for --continue
|
|
2132
|
+
await state.write(`${DIR}/message`, message);
|
|
2133
|
+
// record_in_rewritten (pick_commits) runs before the stop: the merge maps to HEAD
|
|
2134
|
+
if (item.id) {
|
|
2135
|
+
await state.write(`${DIR}/stopped-sha`, `${item.id}\n`);
|
|
2136
|
+
await state.setRef("REBASE_HEAD", item.id);
|
|
2137
|
+
await state.write(`${DIR}/patch`, "");
|
|
2138
|
+
}
|
|
2139
|
+
const short = item.id ? await this.abbreviateObjectId(item.id) : "";
|
|
2140
|
+
return {
|
|
2141
|
+
status: RebaseStatus.STOPPED,
|
|
2142
|
+
...(item.id ? { currentCommit: item.id } : {}),
|
|
2143
|
+
conflicts: [...new Set(merge.conflicts.keys())].sort(),
|
|
2144
|
+
message: item.id ? `Could not apply ${short}... ${arg}` : `Could not merge ${arg}`,
|
|
2145
|
+
};
|
|
2146
|
+
}
|
|
2147
|
+
// git commit -n -F MERGE_MSG --cleanup=verbatim (whitespace with --signoff, which the
|
|
2148
|
+
// merge does not get): the merge committed, its state removed
|
|
2149
|
+
const committed = (await state.exists(`${DIR}/signoff`)) ? stripspace(message) : message;
|
|
2150
|
+
const newId = await this.commitOnto(
|
|
2151
|
+
state,
|
|
2152
|
+
[headId, head.id],
|
|
2153
|
+
treeId,
|
|
2154
|
+
author,
|
|
2155
|
+
committed,
|
|
2156
|
+
"merge",
|
|
2157
|
+
);
|
|
2158
|
+
// (git commit removes SQUASH_MSG too; the octopus' git merge leaves it)
|
|
2159
|
+
await state.remove("MERGE_MSG", "MERGE_HEAD", "MERGE_MODE", "SQUASH_MSG");
|
|
2160
|
+
await state.removeRef("AUTO_MERGE");
|
|
2161
|
+
if (item.editMessage) await this.amendMergeMessage(state, newId);
|
|
2162
|
+
return undefined;
|
|
586
2163
|
}
|
|
587
2164
|
|
|
588
2165
|
/**
|
|
589
|
-
*
|
|
2166
|
+
* do_merge of several heads: git runs `git merge -s octopus --no-ff --no-edit -F MERGE_MSG
|
|
2167
|
+
* <heads>` with the author of `author-script`; our merge runs it (git-merge-octopus, its
|
|
2168
|
+
* messages, ORIG_HEAD, the reflog line `merge <heads>: Merge made by the 'octopus'
|
|
2169
|
+
* strategy.`). Merged, the merge maps to its commit; already up to date, to HEAD; with
|
|
2170
|
+
* conflicts left (the last head's) or given up ("Should not be doing an octopus."), the
|
|
2171
|
+
* rebase stops there as at a conflicting merge (never rescheduled), the merge mapped to
|
|
2172
|
+
* HEAD.
|
|
590
2173
|
*/
|
|
591
|
-
private async
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
2174
|
+
private async octopus(
|
|
2175
|
+
state: OperationState,
|
|
2176
|
+
item: TodoItem,
|
|
2177
|
+
heads: { id: ObjectId; name: string }[],
|
|
2178
|
+
picked: PersonIdent,
|
|
2179
|
+
message: string,
|
|
2180
|
+
output: string[],
|
|
2181
|
+
): Promise<(RebaseResult & { reschedule?: boolean }) | undefined> {
|
|
2182
|
+
// the dates as the sequencer passes them to the merge (GIT_AUTHOR_DATE=, and
|
|
2183
|
+
// GIT_COMMITTER_DATE= the author's date or the clock)
|
|
2184
|
+
this.resetClock();
|
|
2185
|
+
let author = picked;
|
|
2186
|
+
let committer = this.committer ?? this.resolveCommitter();
|
|
2187
|
+
if (await state.exists(`${DIR}/ignore_date`)) {
|
|
2188
|
+
const now = this.clock();
|
|
2189
|
+
author = { ...author, timestamp: now, tzOffset: localTimezoneOffset(now) };
|
|
2190
|
+
}
|
|
2191
|
+
if (await state.exists(`${DIR}/cdate_is_adate`)) {
|
|
2192
|
+
committer = { ...committer, timestamp: author.timestamp, tzOffset: author.tzOffset };
|
|
595
2193
|
}
|
|
2194
|
+
const command = new MergeCommand(this._workingCopy)
|
|
2195
|
+
.setInProcess(true)
|
|
2196
|
+
.setFastForwardMode(FastForwardMode.NO_FF)
|
|
2197
|
+
.setMessage(message)
|
|
2198
|
+
.setAuthor(author)
|
|
2199
|
+
.setCommitter(committer);
|
|
2200
|
+
// `merge -c`: --edit, the message cleaned up as an edited one
|
|
2201
|
+
if (item.editMessage) command.setCleanupMode(CleanupMode.STRIP);
|
|
2202
|
+
// the merge inherits pull's GIT_REFLOG_ACTION, not the rebase's own action
|
|
2203
|
+
if (this.hasReflogAction()) command.setReflogAction(this.reflogAction("rebase"));
|
|
2204
|
+
for (const head of heads) command.include(head.id);
|
|
2205
|
+
let result: MergeResult;
|
|
2206
|
+
try {
|
|
2207
|
+
result = await command.call();
|
|
2208
|
+
} catch (error) {
|
|
2209
|
+
result = {
|
|
2210
|
+
status: MergeStatus.FAILED,
|
|
2211
|
+
mergedCommits: [],
|
|
2212
|
+
message: error instanceof Error ? error.message : String(error),
|
|
2213
|
+
};
|
|
2214
|
+
}
|
|
2215
|
+
output.push(...(result.output ?? []));
|
|
2216
|
+
if (result.status === MergeStatus.MERGED || result.status === MergeStatus.ALREADY_UP_TO_DATE) {
|
|
2217
|
+
// remove_merge_branch_state
|
|
2218
|
+
await state.remove("MERGE_MSG", "MERGE_HEAD", "MERGE_MODE");
|
|
2219
|
+
await state.removeRef("AUTO_MERGE");
|
|
2220
|
+
return undefined;
|
|
2221
|
+
}
|
|
2222
|
+
// error_with_patch, after record_in_rewritten
|
|
2223
|
+
await state.write(`${DIR}/message`, message);
|
|
2224
|
+
if (item.id) {
|
|
2225
|
+
await state.write(`${DIR}/stopped-sha`, `${item.id}\n`);
|
|
2226
|
+
await state.setRef("REBASE_HEAD", item.id);
|
|
2227
|
+
await state.write(`${DIR}/patch`, "");
|
|
2228
|
+
}
|
|
2229
|
+
const arg = item.rest ?? "";
|
|
2230
|
+
const stop = item.id
|
|
2231
|
+
? `Could not apply ${await this.abbreviateObjectId(item.id)}... ${arg}`
|
|
2232
|
+
: `Could not merge ${arg}`;
|
|
2233
|
+
return {
|
|
2234
|
+
status: RebaseStatus.STOPPED,
|
|
2235
|
+
...(item.id ? { currentCommit: item.id } : {}),
|
|
2236
|
+
conflicts: result.conflicts ?? [],
|
|
2237
|
+
message: result.message ? `${result.message}\n${stop}` : stop,
|
|
2238
|
+
};
|
|
2239
|
+
}
|
|
596
2240
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
2241
|
+
/**
|
|
2242
|
+
* `merge -c` without an editor to run (git's `commit --amend -e` with a no-op editor): the
|
|
2243
|
+
* merge committed again with its message cleaned up as an edited one (`strip`).
|
|
2244
|
+
*/
|
|
2245
|
+
private async amendMergeMessage(state: OperationState, id: ObjectId): Promise<void> {
|
|
2246
|
+
const commit = await this.commits.load(id);
|
|
2247
|
+
if (!commit) return;
|
|
2248
|
+
const message = cleanupMessage(commit.message, commentPrefixOf(this.workingCopy));
|
|
2249
|
+
this.resetClock();
|
|
2250
|
+
let committer = this.committer ?? this.resolveCommitter();
|
|
2251
|
+
if (await state.exists(`${DIR}/cdate_is_adate`)) {
|
|
2252
|
+
committer = {
|
|
2253
|
+
...committer,
|
|
2254
|
+
timestamp: commit.author.timestamp,
|
|
2255
|
+
tzOffset: commit.author.tzOffset,
|
|
2256
|
+
};
|
|
2257
|
+
}
|
|
2258
|
+
const newId = await this.commits.store({
|
|
2259
|
+
tree: commit.tree,
|
|
2260
|
+
parents: commit.parents,
|
|
2261
|
+
author: commit.author,
|
|
2262
|
+
committer,
|
|
2263
|
+
message,
|
|
2264
|
+
});
|
|
2265
|
+
const action = reflogMessages.rebaseStep(this.reflogAction("rebase"), "merge");
|
|
2266
|
+
await this.refsStore.set(
|
|
2267
|
+
"HEAD",
|
|
2268
|
+
newId,
|
|
2269
|
+
this.reflogLine(reflogMessages.pick(action, message), this.committer),
|
|
2270
|
+
);
|
|
2271
|
+
// git commit --amend: SQUASH_MSG removed with the merge state, the notes carried over
|
|
2272
|
+
await state.remove("SQUASH_MSG");
|
|
2273
|
+
await copyNotesForAmend(this.notesContext(), id, newId, commit.author);
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
/**
|
|
2277
|
+
* git's fast_forward_to: the worktree switched to `to` (refused as a merge would be) and
|
|
2278
|
+
* HEAD moved there, logged `rebase: fast-forward`; the refusal reschedules the command.
|
|
2279
|
+
*/
|
|
2280
|
+
private async fastForward(
|
|
2281
|
+
state: OperationState,
|
|
2282
|
+
worktree: Worktree | undefined,
|
|
2283
|
+
from: ObjectId,
|
|
2284
|
+
to: ObjectId,
|
|
2285
|
+
): Promise<(RebaseResult & { reschedule?: boolean }) | undefined> {
|
|
2286
|
+
const delta = await treeDelta(
|
|
2287
|
+
this.trees,
|
|
2288
|
+
await this.commits.getTree(from),
|
|
2289
|
+
await this.commits.getTree(to),
|
|
2290
|
+
);
|
|
2291
|
+
const refused = await switchTrees(this.context(worktree), delta.from, delta.to, {
|
|
2292
|
+
action: "merge",
|
|
2293
|
+
operation: "rebase",
|
|
2294
|
+
targetTree: await this.commits.getTree(to),
|
|
2295
|
+
});
|
|
2296
|
+
if (refused) {
|
|
2297
|
+
await state.setRef("REBASE_HEAD", to);
|
|
2298
|
+
return {
|
|
2299
|
+
status: RebaseStatus.FAILED,
|
|
2300
|
+
currentCommit: to,
|
|
2301
|
+
message: refused.message,
|
|
2302
|
+
reschedule: true,
|
|
2303
|
+
};
|
|
2304
|
+
}
|
|
2305
|
+
await this.refsStore.set(
|
|
2306
|
+
"HEAD",
|
|
2307
|
+
to,
|
|
2308
|
+
this.reflogLine(`${this.reflogAction("rebase")}: fast-forward`, this.committer),
|
|
2309
|
+
);
|
|
2310
|
+
return undefined;
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
/**
|
|
2314
|
+
* git's allow_ff as read_populate_opts leaves it: off with --force-rebase, which the date
|
|
2315
|
+
* options and --signoff imply (`cdate_is_adate`, `ignore_date`, `signoff` in the state).
|
|
2316
|
+
*/
|
|
2317
|
+
private async allowFastForward(state: OperationState): Promise<boolean> {
|
|
2318
|
+
for (const name of ["cdate_is_adate", "ignore_date", "signoff"]) {
|
|
2319
|
+
if (await state.exists(`${DIR}/${name}`)) return false;
|
|
2320
|
+
}
|
|
2321
|
+
return true;
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
/** append_signoff's `Signed-off-by: <committer name> <email>` line. */
|
|
2325
|
+
private signoffLine(): string {
|
|
2326
|
+
const committer = this.committer ?? this.resolveCommitter();
|
|
2327
|
+
return `Signed-off-by: ${committer.name} <${committer.email}>\n`;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
/** The labels' refs (`refs-to-delete`) removed, as git's sequencer_remove_state does. */
|
|
2331
|
+
private async deleteRewrittenRefs(state: OperationState): Promise<void> {
|
|
2332
|
+
const text = (await state.read(`${DIR}/refs-to-delete`)) ?? "";
|
|
2333
|
+
for (const name of text.split("\n")) {
|
|
2334
|
+
if (!name.startsWith("refs/rewritten/")) continue;
|
|
2335
|
+
if (await this.refsStore.has(name).catch(() => false)) await this.refsStore.delete(name);
|
|
2336
|
+
}
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
/**
|
|
2340
|
+
* git's make_script_with_merges: the todo list of `--rebase-merges` for `range` (the
|
|
2341
|
+
* commits to rebase, oldest first): each side branch picked from where it forked
|
|
2342
|
+
* (`reset`), its tip labeled, each merge recreated (`merge -C`), branch points labeled,
|
|
2343
|
+
* the branches in the order their merges name them, HEAD's last.
|
|
2344
|
+
*/
|
|
2345
|
+
private async makeScriptWithMerges(
|
|
2346
|
+
headId: ObjectId,
|
|
2347
|
+
upstream: ObjectId,
|
|
2348
|
+
range: ObjectId[],
|
|
2349
|
+
skipped: Set<ObjectId>,
|
|
2350
|
+
rebaseCousins: boolean,
|
|
2351
|
+
keepEmpty = true,
|
|
2352
|
+
): Promise<string> {
|
|
2353
|
+
const comment = commentPrefixOf(this.workingCopy);
|
|
2354
|
+
const maxLength =
|
|
2355
|
+
Number.parseInt(configValue(this.workingCopy, "rebase.maxLabelLength") ?? "", 10) ||
|
|
2356
|
+
MAX_LABEL_LENGTH;
|
|
2357
|
+
const commit2label = new Map<ObjectId, string>();
|
|
2358
|
+
const labels = new Set<string>();
|
|
2359
|
+
// the first merge base is the bottom of `upstream...HEAD`: labeled "onto"
|
|
2360
|
+
const bases = await revisionMergeBases(this.commits, upstream, headId);
|
|
2361
|
+
if (bases[0]) {
|
|
2362
|
+
commit2label.set(bases[0], "onto");
|
|
2363
|
+
labels.add("onto");
|
|
2364
|
+
}
|
|
2365
|
+
const encoder = new TextEncoder();
|
|
2366
|
+
const labelOid = async (oid: ObjectId, wanted?: string): Promise<string> => {
|
|
2367
|
+
const known = commit2label.get(oid);
|
|
2368
|
+
if (known !== undefined) return known;
|
|
2369
|
+
let label: string;
|
|
2370
|
+
if (wanted === undefined) {
|
|
2371
|
+
label = await this.abbreviateObjectId(oid);
|
|
2372
|
+
if (labels.has(label)) {
|
|
2373
|
+
// lengthened until no label has it, else the whole id
|
|
2374
|
+
const start = label.length + 1;
|
|
2375
|
+
label = oid;
|
|
2376
|
+
for (let i = start; i < oid.length; i++) {
|
|
2377
|
+
if (!labels.has(oid.slice(0, i))) {
|
|
2378
|
+
label = oid.slice(0, i);
|
|
2379
|
+
break;
|
|
2380
|
+
}
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
} else {
|
|
2384
|
+
// alphanumerics and other characters than ASCII kept, the rest a single dash
|
|
2385
|
+
let buf = "";
|
|
2386
|
+
let bytes = 0;
|
|
2387
|
+
for (const ch of wanted) {
|
|
2388
|
+
if (bytes + 1 >= maxLength) break;
|
|
2389
|
+
if (/^[A-Za-z0-9]$/.test(ch)) {
|
|
2390
|
+
buf += ch;
|
|
2391
|
+
bytes++;
|
|
2392
|
+
} else if ((ch.codePointAt(0) as number) >= 0x80) {
|
|
2393
|
+
const n = encoder.encode(ch).length;
|
|
2394
|
+
if (bytes + n > maxLength) break;
|
|
2395
|
+
buf += ch;
|
|
2396
|
+
bytes += n;
|
|
2397
|
+
} else if (buf.length > 0 && !buf.endsWith("-")) {
|
|
2398
|
+
buf += "-";
|
|
2399
|
+
bytes++;
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
if (buf === "") buf = `rev-${await this.abbreviateObjectId(oid)}`;
|
|
2403
|
+
if (
|
|
2404
|
+
(buf.length === this.objectFormat.hexLength && /^[0-9a-fA-F]+$/.test(buf)) ||
|
|
2405
|
+
buf === "#" ||
|
|
2406
|
+
labels.has(buf)
|
|
2407
|
+
) {
|
|
2408
|
+
let i = 2;
|
|
2409
|
+
while (labels.has(`${buf}-${i}`)) i++;
|
|
2410
|
+
buf = `${buf}-${i}`;
|
|
2411
|
+
}
|
|
2412
|
+
label = buf;
|
|
602
2413
|
}
|
|
2414
|
+
labels.add(label);
|
|
2415
|
+
commit2label.set(oid, label);
|
|
2416
|
+
return label;
|
|
2417
|
+
};
|
|
2418
|
+
// the branch each commit is decorated by (load_branch_decorations: refs/heads/ only,
|
|
2419
|
+
// the last name in order is the one get_name_decoration gives)
|
|
2420
|
+
const branchOf = new Map<ObjectId, string>();
|
|
2421
|
+
const heads: { name: string; objectId?: ObjectId; target?: string }[] = [];
|
|
2422
|
+
for await (const ref of this.refsStore.list("refs/heads/")) {
|
|
2423
|
+
if (ref.name.startsWith("refs/heads/")) heads.push(ref);
|
|
2424
|
+
}
|
|
2425
|
+
heads.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
2426
|
+
for (const ref of heads) {
|
|
2427
|
+
const value =
|
|
2428
|
+
ref.objectId && ref.target === undefined
|
|
2429
|
+
? ref.objectId
|
|
2430
|
+
: (await this.refsStore.resolve(ref.name).catch(() => undefined))?.objectId;
|
|
2431
|
+
if (value) branchOf.set(await this.peelToCommit(value), ref.name.slice("refs/heads/".length));
|
|
603
2432
|
}
|
|
604
2433
|
|
|
605
|
-
|
|
2434
|
+
const interesting = new Set(range);
|
|
2435
|
+
const commit2todo = new Map<ObjectId, string>();
|
|
2436
|
+
const tips: ObjectId[] = [];
|
|
2437
|
+
// first phase: the onelines, the tips of merged branches labeled
|
|
2438
|
+
for (const id of range) {
|
|
2439
|
+
const commit = await this.loadCommit(id);
|
|
2440
|
+
if (!commit) throw new CommitNotFoundError(id);
|
|
2441
|
+
const parentTree = commit.parents[0]
|
|
2442
|
+
? (await this.loadCommit(commit.parents[0]))?.tree
|
|
2443
|
+
: this.objectFormat.emptyTreeId;
|
|
2444
|
+
const empty = commit.tree === parentTree;
|
|
2445
|
+
if (!empty && skipped.has(id)) continue;
|
|
2446
|
+
if (empty && !keepEmpty) continue;
|
|
2447
|
+
const oneline = await this.instructionText(id);
|
|
2448
|
+
const toMerge = commit.parents.slice(1);
|
|
2449
|
+
if (toMerge.length === 0) {
|
|
2450
|
+
commit2todo.set(id, `pick ${id} ${oneline}${empty ? ` ${comment} empty` : ""}`);
|
|
2451
|
+
continue;
|
|
2452
|
+
}
|
|
2453
|
+
// a label from the message: `Merge branch 'x'`, `Merge pull request #1 from x`
|
|
2454
|
+
let fromMessage = oneline;
|
|
2455
|
+
const quoted = /^# Merge [^']*'([^']*)'/.exec(oneline);
|
|
2456
|
+
if (quoted) fromMessage = quoted[1] as string;
|
|
2457
|
+
else if (oneline.startsWith("# Merge pull request ")) {
|
|
2458
|
+
const at = oneline.indexOf(" from ", "# Merge pull request ".length);
|
|
2459
|
+
if (at >= 0) fromMessage = oneline.slice(at + " from ".length);
|
|
2460
|
+
}
|
|
2461
|
+
let line = `merge -C ${id}`;
|
|
2462
|
+
for (const parent of toMerge) {
|
|
2463
|
+
const label = branchOf.get(parent) ?? fromMessage;
|
|
2464
|
+
if (!interesting.has(parent)) {
|
|
2465
|
+
line += ` ${await labelOid(parent)}`;
|
|
2466
|
+
continue;
|
|
2467
|
+
}
|
|
2468
|
+
tips.push(parent);
|
|
2469
|
+
line += ` ${await labelOid(parent, label)}`;
|
|
2470
|
+
}
|
|
2471
|
+
commit2todo.set(id, `${line} ${oneline}`);
|
|
2472
|
+
}
|
|
2473
|
+
// second phase: branch points (commits with several children) labeled, HEAD a tip
|
|
2474
|
+
const childSeen = new Set<ObjectId>();
|
|
2475
|
+
for (const [k, id] of range.entries()) {
|
|
2476
|
+
for (const parent of (await this.loadCommit(id))?.parents ?? []) {
|
|
2477
|
+
if (!interesting.has(parent)) continue;
|
|
2478
|
+
if (childSeen.has(parent)) await labelOid(parent, "branch-point");
|
|
2479
|
+
else childSeen.add(parent);
|
|
2480
|
+
}
|
|
2481
|
+
if (k === range.length - 1) tips.push(id);
|
|
2482
|
+
}
|
|
2483
|
+
// third phase: each tip's branch, walking first parents down to what is shown
|
|
2484
|
+
let out = "label onto\n";
|
|
2485
|
+
const shown = new Set<ObjectId>();
|
|
2486
|
+
for (const tip of tips) {
|
|
2487
|
+
if (shown.has(tip)) continue;
|
|
2488
|
+
const tipLabel = commit2label.get(tip);
|
|
2489
|
+
out += tipLabel !== undefined ? `\n${comment} Branch ${tipLabel}\n` : "\n";
|
|
2490
|
+
const list: ObjectId[] = [];
|
|
2491
|
+
let commit: ObjectId | undefined = tip;
|
|
2492
|
+
while (commit !== undefined && interesting.has(commit) && !shown.has(commit)) {
|
|
2493
|
+
list.unshift(commit);
|
|
2494
|
+
const parents: ObjectId[] = (await this.loadCommit(commit))?.parents ?? [];
|
|
2495
|
+
commit = parents[0];
|
|
2496
|
+
}
|
|
2497
|
+
if (commit === undefined) out += `reset ${rebaseCousins ? "onto" : "[new root]"}\n`;
|
|
2498
|
+
else {
|
|
2499
|
+
let to = commit2label.get(commit);
|
|
2500
|
+
if (to === undefined && !rebaseCousins) to = await labelOid(commit);
|
|
2501
|
+
if (to === undefined || to === "onto") out += "reset onto\n";
|
|
2502
|
+
else out += `reset ${to} ${await this.instructionText(commit)}\n`;
|
|
2503
|
+
}
|
|
2504
|
+
for (const id of list) {
|
|
2505
|
+
const todo = commit2todo.get(id);
|
|
2506
|
+
if (todo !== undefined) out += `${todo}\n`;
|
|
2507
|
+
const label = commit2label.get(id);
|
|
2508
|
+
if (label !== undefined) out += `label ${label}\n`;
|
|
2509
|
+
shown.add(id);
|
|
2510
|
+
}
|
|
2511
|
+
}
|
|
2512
|
+
return out;
|
|
2513
|
+
}
|
|
2514
|
+
|
|
2515
|
+
/** git's `author-script` of the commit being picked. */
|
|
2516
|
+
private async writeAuthorScript(state: OperationState, author: PersonIdent): Promise<void> {
|
|
2517
|
+
await state.write(
|
|
2518
|
+
`${DIR}/author-script`,
|
|
2519
|
+
`GIT_AUTHOR_NAME=${sq(author.name)}\nGIT_AUTHOR_EMAIL=${sq(author.email)}\nGIT_AUTHOR_DATE=${sq(`@${author.timestamp} ${author.tzOffset}`)}\n`,
|
|
2520
|
+
);
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
/** read_populate_todo's todo_list_write_total_nr: `end`, the commands done and to do. */
|
|
2524
|
+
private async writeTotal(state: OperationState, todo: TodoItem[]): Promise<void> {
|
|
2525
|
+
const done = countCommands((await state.read(`${DIR}/done`)) ?? "");
|
|
2526
|
+
await state.write(`${DIR}/end`, `${done + countTodoCommands(todo)}\n`);
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
/** `git rebase --continue`: what is staged committed (commit_staged_changes), the rest picked. */
|
|
2530
|
+
private async continueRebase(
|
|
2531
|
+
state: OperationState,
|
|
2532
|
+
worktree: Worktree | undefined,
|
|
2533
|
+
): Promise<RebaseResult> {
|
|
2534
|
+
if (!(await this.inProgress(state))) throw new NoRebaseInProgressError();
|
|
2535
|
+
const { unmerged } = await readIndex(this.staging);
|
|
2536
|
+
if (unmerged.length > 0) {
|
|
2537
|
+
// builtin/rebase.c: refresh_index's `<path>: needs merge`, then has_unstaged_changes
|
|
2538
|
+
return {
|
|
2539
|
+
status: RebaseStatus.STOPPED,
|
|
2540
|
+
conflicts: unmerged,
|
|
2541
|
+
message: [
|
|
2542
|
+
...[...new Set(unmerged)].map((path) => `${path}: needs merge`),
|
|
2543
|
+
"You must edit all merge conflicts and then",
|
|
2544
|
+
"mark them as resolved using git add",
|
|
2545
|
+
].join("\n"),
|
|
2546
|
+
};
|
|
2547
|
+
}
|
|
2548
|
+
// builtin/rebase.c: unstaged changes refused before the sequencer reads anything
|
|
2549
|
+
if (worktree) {
|
|
2550
|
+
const unclean = await this.uncleanWorktree(
|
|
2551
|
+
await this.resolveHead(),
|
|
2552
|
+
worktree,
|
|
2553
|
+
"rebase",
|
|
2554
|
+
false,
|
|
2555
|
+
true,
|
|
2556
|
+
);
|
|
2557
|
+
if (unclean) {
|
|
2558
|
+
return {
|
|
2559
|
+
status: RebaseStatus.FAILED,
|
|
2560
|
+
message:
|
|
2561
|
+
"You must edit all merge conflicts and then\nmark them as resolved using git add",
|
|
2562
|
+
};
|
|
2563
|
+
}
|
|
2564
|
+
}
|
|
2565
|
+
return this.resume(state, worktree);
|
|
2566
|
+
}
|
|
2567
|
+
|
|
2568
|
+
/** sequencer_continue for `--continue` and `--skip`: the stop settled, the rest picked. */
|
|
2569
|
+
private async resume(
|
|
2570
|
+
state: OperationState,
|
|
2571
|
+
worktree: Worktree | undefined,
|
|
2572
|
+
output: string[] = [],
|
|
2573
|
+
): Promise<RebaseResult> {
|
|
2574
|
+
const loaded = await this.loadTodo(state);
|
|
2575
|
+
if (loaded.errors.length > 0) {
|
|
2576
|
+
return {
|
|
2577
|
+
status: RebaseStatus.FAILED,
|
|
2578
|
+
message: [...loaded.errors, "error: please fix this using 'git rebase --edit-todo'."].join(
|
|
2579
|
+
"\n",
|
|
2580
|
+
),
|
|
2581
|
+
};
|
|
2582
|
+
}
|
|
2583
|
+
// read_populate_todo writes the total before anything may refuse to go on
|
|
2584
|
+
await this.writeTotal(state, loaded.items);
|
|
2585
|
+
if (await state.exists(`${DIR}/dropped`)) {
|
|
2586
|
+
// todo_list_check_against_backup: commits still dropped by mistake
|
|
2587
|
+
const backup = await this.resolveTodo(
|
|
2588
|
+
parseTodo(
|
|
2589
|
+
(await state.read(`${DIR}/git-rebase-todo.backup`)) ?? "",
|
|
2590
|
+
commentPrefixOf(this.workingCopy),
|
|
2591
|
+
).items,
|
|
2592
|
+
);
|
|
2593
|
+
const level = this.missingCommitsCheckLevel(output);
|
|
2594
|
+
const missing = await this.missingCommits(backup.items, loaded.items, level);
|
|
2595
|
+
if (missing.length > 0 && level === "error") {
|
|
2596
|
+
return { status: RebaseStatus.FAILED, message: missing.join("\n"), output };
|
|
2597
|
+
}
|
|
2598
|
+
output.push(...missing);
|
|
2599
|
+
await state.remove(`${DIR}/dropped`);
|
|
2600
|
+
}
|
|
2601
|
+
const refusal = await this.commitStagedChanges(state, loaded.items);
|
|
2602
|
+
if (refusal) return { ...refusal, output };
|
|
2603
|
+
// sequencer_continue: the stopped commit maps to HEAD, committed or not
|
|
2604
|
+
const stopped = (await state.read(`${DIR}/stopped-sha`))?.trim();
|
|
2605
|
+
if (stopped && /^[0-9a-f]{40,64}$/.test(stopped)) {
|
|
2606
|
+
await this.recordInRewritten(state, stopped, peekCommand(loaded.items, 0));
|
|
2607
|
+
}
|
|
2608
|
+
return this.processTodo(state, worktree, output);
|
|
2609
|
+
}
|
|
2610
|
+
|
|
2611
|
+
/**
|
|
2612
|
+
* sequencer.c's commit_staged_changes: unstaged changes refuse; staged ones are committed
|
|
2613
|
+
* with `rebase-merge/message` (a new commit, or HEAD amended where the stop asked for it:
|
|
2614
|
+
* `amend`), after a failed fixup or squash the chain's bookkeeping put right.
|
|
2615
|
+
*/
|
|
2616
|
+
private async commitStagedChanges(
|
|
2617
|
+
state: OperationState,
|
|
2618
|
+
todo: TodoItem[],
|
|
2619
|
+
): Promise<RebaseResult | undefined> {
|
|
2620
|
+
// (its has_unstaged_changes refusal cannot happen: `--continue` refused them before the
|
|
2621
|
+
// list was read, `--skip` reset them)
|
|
2622
|
+
const headId = await this.resolveHead();
|
|
2623
|
+
const headCommit = await this.commits.load(headId);
|
|
2624
|
+
const treeId = await this.staging.writeTree(this.trees);
|
|
2625
|
+
const isClean = headCommit?.tree === treeId;
|
|
2626
|
+
if (!isClean && !(await state.exists(`${DIR}/message`))) {
|
|
2627
|
+
return { status: RebaseStatus.FAILED, message: STAGED_CHANGES_ADVICE };
|
|
2628
|
+
}
|
|
2629
|
+
let amend = false;
|
|
2630
|
+
let finalFixup = false;
|
|
2631
|
+
let edit = true;
|
|
2632
|
+
let currentFixups = (await state.read(`${DIR}/current-fixups`)) ?? "";
|
|
2633
|
+
let fixupCount = currentFixups === "" ? 0 : currentFixups.split("\n").length;
|
|
2634
|
+
const amendFile = (await state.read(`${DIR}/amend`))?.trim();
|
|
2635
|
+
if (amendFile !== undefined) {
|
|
2636
|
+
if (!/^[0-9a-f]{40,64}$/.test(amendFile)) {
|
|
2637
|
+
return { status: RebaseStatus.FAILED, message: `error: invalid contents: '${DIR}/amend'` };
|
|
2638
|
+
}
|
|
2639
|
+
if (!isClean && headId !== amendFile) {
|
|
2640
|
+
return {
|
|
2641
|
+
status: RebaseStatus.FAILED,
|
|
2642
|
+
message:
|
|
2643
|
+
"error: \nYou have uncommitted changes in your working tree. Please, commit them\nfirst and then run 'git rebase --continue' again.",
|
|
2644
|
+
};
|
|
2645
|
+
}
|
|
2646
|
+
const next = peekCommand(todo, 0);
|
|
2647
|
+
if (!isClean || fixupCount === 0) {
|
|
2648
|
+
// not the final fixup
|
|
2649
|
+
} else if (headId !== amendFile || !(await state.exists(`${DIR}/stopped-sha`))) {
|
|
2650
|
+
// a final fixup or squash done by hand
|
|
2651
|
+
if (!isFixup(next)) {
|
|
2652
|
+
await state.remove(
|
|
2653
|
+
`${DIR}/message-fixup`,
|
|
2654
|
+
`${DIR}/message-squash`,
|
|
2655
|
+
`${DIR}/current-fixups`,
|
|
2656
|
+
);
|
|
2657
|
+
currentFixups = "";
|
|
2658
|
+
fixupCount = 0;
|
|
2659
|
+
}
|
|
2660
|
+
} else {
|
|
2661
|
+
// a fixup/squash of a chain skipped: it is taken off the chain
|
|
2662
|
+
fixupCount--;
|
|
2663
|
+
const cut = currentFixups.lastIndexOf("\n");
|
|
2664
|
+
currentFixups = cut < 0 ? "" : currentFixups.slice(0, cut);
|
|
2665
|
+
await state.write(`${DIR}/current-fixups`, currentFixups);
|
|
2666
|
+
const seenSquash = currentFixups.startsWith("squash") || currentFixups.includes("\nsquash");
|
|
2667
|
+
const seenEdit =
|
|
2668
|
+
currentFixups.startsWith("fixup -c") || currentFixups.includes("\nfixup -c");
|
|
2669
|
+
if (fixupCount > 0 && !isFixup(next)) {
|
|
2670
|
+
finalFixup = true;
|
|
2671
|
+
// only fixups: the message cleaned up, no editor
|
|
2672
|
+
if (!seenSquash && !seenEdit) edit = false;
|
|
2673
|
+
} else if (isFixup(next)) {
|
|
2674
|
+
await state.write(`${DIR}/message-squash`, messageBody(headCommit?.message ?? ""));
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
amend = true;
|
|
2678
|
+
}
|
|
2679
|
+
if (isClean) {
|
|
2680
|
+
await state.removeRef("CHERRY_PICK_HEAD");
|
|
2681
|
+
await state.remove("MERGE_MSG");
|
|
2682
|
+
if (!finalFixup) return undefined;
|
|
2683
|
+
}
|
|
2684
|
+
// run_git_commit: `rebase-merge/message` (HEAD's own message for a final fixup), edited
|
|
2685
|
+
// (git commit -e: comments stripped) or cleaned up (--cleanup=strip)
|
|
2686
|
+
const comment = commentPrefixOf(this.workingCopy);
|
|
2687
|
+
let message: string;
|
|
2688
|
+
// with hooks: git commit -n (-F rebase-merge/message) -e, the template edited between
|
|
2689
|
+
// its hooks (commit_staged_changes: EDIT_MSG without VERIFY_MSG)
|
|
2690
|
+
let gitCommit: GitCommitHookRun | undefined;
|
|
2691
|
+
try {
|
|
2692
|
+
if (this.hooks.enabled) {
|
|
2693
|
+
gitCommit = {
|
|
2694
|
+
verify: false,
|
|
2695
|
+
editor: !finalFixup || edit,
|
|
2696
|
+
hookArgs: finalFixup ? (amend ? ["commit", "HEAD"] : []) : ["message"],
|
|
2697
|
+
env: { GIT_REFLOG_ACTION: this.stepAction("continue") },
|
|
2698
|
+
};
|
|
2699
|
+
message = finalFixup
|
|
2700
|
+
? (headCommit?.message ?? "")
|
|
2701
|
+
: ((await state.read(`${DIR}/message`)) ?? (await state.read("MERGE_MSG")) ?? "");
|
|
2702
|
+
} else if (finalFixup) {
|
|
2703
|
+
const own = headCommit?.message ?? "";
|
|
2704
|
+
message = edit ? this.editMessage(own) : cleanupMessage(own, comment);
|
|
2705
|
+
} else {
|
|
2706
|
+
message = this.editMessage(
|
|
2707
|
+
(await state.read(`${DIR}/message`)) ?? (await state.read("MERGE_MSG")) ?? "",
|
|
2708
|
+
);
|
|
2709
|
+
}
|
|
2710
|
+
if (gitCommit) {
|
|
2711
|
+
const run = gitCommit;
|
|
2712
|
+
if (amend) await this.amendHead(state, treeId, message, "continue", true, run);
|
|
2713
|
+
else {
|
|
2714
|
+
const mergeHeads = ((await state.read("MERGE_HEAD")) ?? "")
|
|
2715
|
+
.split("\n")
|
|
2716
|
+
.map((line) => line.trim())
|
|
2717
|
+
.filter((line) => /^[0-9a-f]{40,64}$/.test(line));
|
|
2718
|
+
const author =
|
|
2719
|
+
parseAuthorScript((await state.read(`${DIR}/author-script`)) ?? "") ??
|
|
2720
|
+
this.resolveAuthor();
|
|
2721
|
+
// git commit, a child with GIT_REFLOG_ACTION: its HEAD update and its cleanup
|
|
2722
|
+
// (sequencer_post_commit_cleanup: CHERRY_PICK_HEAD when there, AUTO_MERGE)
|
|
2723
|
+
await this.withRefTransactionEnv(run.env ?? {}, async () => {
|
|
2724
|
+
await this.commitOnto(
|
|
2725
|
+
state,
|
|
2726
|
+
[headId, ...mergeHeads],
|
|
2727
|
+
treeId,
|
|
2728
|
+
author,
|
|
2729
|
+
message,
|
|
2730
|
+
"continue",
|
|
2731
|
+
run,
|
|
2732
|
+
);
|
|
2733
|
+
await state.removeRef("CHERRY_PICK_HEAD", "REVERT_HEAD");
|
|
2734
|
+
await state.deleteRef("AUTO_MERGE");
|
|
2735
|
+
});
|
|
2736
|
+
await gitCommitAfterHooks(this.hooks, run);
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
} catch (error) {
|
|
2740
|
+
// git commit failed on its editor: nothing committed, the stop stays as it is
|
|
2741
|
+
return {
|
|
2742
|
+
status: RebaseStatus.FAILED,
|
|
2743
|
+
message: [...editorFailure(error).lines, "error: could not commit staged changes."].join(
|
|
2744
|
+
"\n",
|
|
2745
|
+
),
|
|
2746
|
+
};
|
|
2747
|
+
}
|
|
2748
|
+
if (gitCommit) {
|
|
2749
|
+
// committed above, with git commit's hooks
|
|
2750
|
+
} else if (amend) await this.amendHead(state, treeId, message, "continue", true);
|
|
2751
|
+
else {
|
|
2752
|
+
// a stopped merge: its other parents (git commit reads MERGE_HEAD, one per line: an
|
|
2753
|
+
// octopus has several)
|
|
2754
|
+
const mergeHeads = ((await state.read("MERGE_HEAD")) ?? "")
|
|
2755
|
+
.split("\n")
|
|
2756
|
+
.map((line) => line.trim())
|
|
2757
|
+
.filter((line) => /^[0-9a-f]{40,64}$/.test(line));
|
|
2758
|
+
const author =
|
|
2759
|
+
parseAuthorScript((await state.read(`${DIR}/author-script`)) ?? "") ?? this.resolveAuthor();
|
|
2760
|
+
await this.commitOnto(state, [headId, ...mergeHeads], treeId, author, message, "continue");
|
|
2761
|
+
}
|
|
2762
|
+
// git commit's cleanup (SQUASH_MSG of a final squash too), then commit_staged_changes'
|
|
2763
|
+
await state.remove(`${DIR}/amend`, "MERGE_HEAD", "MERGE_MODE", "MERGE_MSG", "SQUASH_MSG");
|
|
2764
|
+
await state.removeRef("CHERRY_PICK_HEAD");
|
|
2765
|
+
// commit_staged_changes deletes AUTO_MERGE once git commit is done, whether it exists
|
|
2766
|
+
await state.deleteRef("AUTO_MERGE");
|
|
2767
|
+
if (finalFixup) await state.remove(`${DIR}/message-fixup`, `${DIR}/message-squash`);
|
|
2768
|
+
if (fixupCount > 0) await state.remove(`${DIR}/current-fixups`);
|
|
2769
|
+
return undefined;
|
|
2770
|
+
}
|
|
606
2771
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
2772
|
+
/** `git rebase --skip`: the stopped pick dropped (`reset --hard`), then as `--continue`. */
|
|
2773
|
+
private async skip(state: OperationState, worktree: Worktree | undefined): Promise<RebaseResult> {
|
|
2774
|
+
if (!(await this.inProgress(state))) throw new NoRebaseInProgressError();
|
|
2775
|
+
const headId = await this.resolveHead();
|
|
2776
|
+
const refused = await resetMerge(this.context(worktree), headId, "HEAD", { hard: true });
|
|
2777
|
+
if (refused) {
|
|
2778
|
+
return {
|
|
2779
|
+
status: RebaseStatus.FAILED,
|
|
2780
|
+
message: `${refused.message.replace(/\nfatal: .*$/, "")}\nfatal: could not discard worktree changes`,
|
|
2781
|
+
};
|
|
2782
|
+
}
|
|
2783
|
+
// remove_branch_state (REBASE_HEAD stays: the next command removes it, so a skip of the
|
|
2784
|
+
// last one leaves it, as git does); AUTO_MERGE deleted twice, as git's hook hears it
|
|
2785
|
+
await removeBranchState(this.context(worktree), state);
|
|
2786
|
+
return this.resume(state, worktree);
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
/** `git rebase --abort`: back to the original branch and commit (`reset --hard`). */
|
|
2790
|
+
private async abort(
|
|
2791
|
+
state: OperationState,
|
|
2792
|
+
worktree: Worktree | undefined,
|
|
2793
|
+
): Promise<RebaseResult> {
|
|
2794
|
+
if (!(await this.inProgress(state))) return { status: RebaseStatus.ABORTED };
|
|
2795
|
+
const origHead = (await state.read(`${DIR}/orig-head`))?.trim();
|
|
2796
|
+
const headName = (await state.read(`${DIR}/head-name`))?.trim() ?? "detached HEAD";
|
|
2797
|
+
if (!origHead)
|
|
2798
|
+
return { status: RebaseStatus.FAILED, message: "fatal: could not read orig-head" };
|
|
2799
|
+
const refused = await resetMerge(this.context(worktree), origHead, origHead, { hard: true });
|
|
2800
|
+
if (refused) {
|
|
2801
|
+
return {
|
|
2802
|
+
status: RebaseStatus.FAILED,
|
|
2803
|
+
message: `${refused.message.replace(/\nfatal: .*$/, "")}\nfatal: could not move back to ${origHead}`,
|
|
2804
|
+
};
|
|
2805
|
+
}
|
|
2806
|
+
await this.finishBranch(headName, origHead, undefined, true);
|
|
2807
|
+
const autostash = (await state.read(`${DIR}/autostash`))?.trim();
|
|
2808
|
+
// remove_branch_state, then finish_rebase's unconditional deletions of REBASE_HEAD and
|
|
2809
|
+
// AUTO_MERGE (git 2.5x's reference-transaction hook hears each)
|
|
2810
|
+
await removeBranchState(this.context(worktree), state);
|
|
2811
|
+
await state.deleteRef("REBASE_HEAD", "AUTO_MERGE");
|
|
2812
|
+
await this.deleteRewrittenRefs(state);
|
|
2813
|
+
await state.remove("MERGE_HEAD", "MERGE_MODE", "MERGE_MSG", "SQUASH_MSG", DIR);
|
|
2814
|
+
const output = autostash ? await applyAutostash(this.context(worktree), autostash) : [];
|
|
2815
|
+
return { status: RebaseStatus.ABORTED, newHead: origHead, output };
|
|
610
2816
|
}
|
|
611
2817
|
|
|
612
2818
|
/**
|
|
613
|
-
*
|
|
2819
|
+
* git's `require_clean_work_tree(<action>)` on this command's working copy, without
|
|
2820
|
+
* rebasing: the refusal (`cannot <action>: You have unstaged changes.` ...), or undefined
|
|
2821
|
+
* when the tree is clean or HEAD is unborn. `git pull --rebase` checks it before fetching,
|
|
2822
|
+
* as `pull with rebase`.
|
|
614
2823
|
*/
|
|
615
|
-
|
|
616
|
-
const
|
|
2824
|
+
async checkCleanWorkTree(action = "rebase"): Promise<RebaseResult | undefined> {
|
|
2825
|
+
const headId = (await this.refsStore.resolve("HEAD").catch(() => undefined))?.objectId;
|
|
2826
|
+
if (!headId) return undefined;
|
|
2827
|
+
return this.uncleanWorktree(headId, this.worktreeAccess, action);
|
|
2828
|
+
}
|
|
2829
|
+
|
|
2830
|
+
/** Refuses as git's require_clean_work_tree: unstaged, then staged changes. */
|
|
2831
|
+
private async uncleanWorktree(
|
|
2832
|
+
headId: ObjectId,
|
|
2833
|
+
worktree: Worktree | undefined,
|
|
2834
|
+
action = "rebase",
|
|
2835
|
+
/** "Please commit or stash them." after the refusal */
|
|
2836
|
+
hint = true,
|
|
2837
|
+
/** git's has_unstaged_changes: staged changes do not count */
|
|
2838
|
+
unstagedOnly = false,
|
|
2839
|
+
): Promise<RebaseResult | undefined> {
|
|
2840
|
+
const { index, unmerged } = await readIndex(this.staging);
|
|
2841
|
+
const headTree = await flatTree(this.trees, await this.commits.getTree(headId));
|
|
2842
|
+
// has_uncommitted_changes: an unborn index (no index file) has none
|
|
2843
|
+
const staged = (await isIndexUnborn(this.staging)) ? [] : indexChanges(index, headTree);
|
|
2844
|
+
const unstaged: string[] = [...unmerged];
|
|
2845
|
+
if (worktree) {
|
|
2846
|
+
const trust = worktreeModeTrust(this.workingCopy, worktree);
|
|
2847
|
+
const indexTime = this.staging.getUpdateTime();
|
|
2848
|
+
// every tracked file is checked (git's refresh_index); several at a time, as git's
|
|
2849
|
+
// preload-index stats in parallel, the results kept in index order
|
|
2850
|
+
// diff-files passes over a skip-worktree entry, and an assume-unchanged one matches
|
|
2851
|
+
// whatever its file holds (ie_match_stat honours CE_VALID)
|
|
2852
|
+
const entries = [...index].filter(([, e]) => {
|
|
2853
|
+
const flags = e as { assumeValid?: boolean; skipWorktree?: boolean };
|
|
2854
|
+
return !flags.assumeValid && !flags.skipWorktree;
|
|
2855
|
+
});
|
|
2856
|
+
for (let k = 0; k < entries.length; k += WORKTREE_CHECK_BATCH) {
|
|
2857
|
+
const batch = entries.slice(k, k + WORKTREE_CHECK_BATCH);
|
|
2858
|
+
const modified = await Promise.all(
|
|
2859
|
+
batch.map(([path, entry]) => worktreeModified(worktree, path, entry, trust, indexTime)),
|
|
2860
|
+
);
|
|
2861
|
+
// a deleted file is an unstaged change too
|
|
2862
|
+
batch.forEach(([path], j) => {
|
|
2863
|
+
if (modified[j]) unstaged.push(path);
|
|
2864
|
+
});
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
2867
|
+
if (unstagedOnly && unstaged.length === 0) return undefined;
|
|
2868
|
+
if (staged.length === 0 && unstaged.length === 0) return undefined;
|
|
2869
|
+
const lines = [`error: cannot ${action}: You have unstaged changes.`];
|
|
2870
|
+
if (unstaged.length === 0)
|
|
2871
|
+
lines[0] = `error: cannot ${action}: Your index contains uncommitted changes.`;
|
|
2872
|
+
else if (staged.length > 0)
|
|
2873
|
+
lines.push("error: additionally, your index contains uncommitted changes.");
|
|
2874
|
+
if (hint) lines.push("error: Please commit or stash them.");
|
|
2875
|
+
return {
|
|
2876
|
+
status: RebaseStatus.UNCOMMITTED_CHANGES,
|
|
2877
|
+
uncommittedChanges: [...new Set([...unstaged, ...staged])].sort(),
|
|
2878
|
+
message: lines.join("\n"),
|
|
2879
|
+
};
|
|
2880
|
+
}
|
|
2881
|
+
|
|
2882
|
+
/** A commit made on the detached HEAD, logged `rebase (<step>): <subject>`. */
|
|
2883
|
+
private async commitOnto(
|
|
2884
|
+
state: OperationState,
|
|
2885
|
+
parents: ObjectId[],
|
|
2886
|
+
tree: ObjectId,
|
|
2887
|
+
picked: PersonIdent,
|
|
2888
|
+
message: string,
|
|
2889
|
+
step: string,
|
|
2890
|
+
/**
|
|
2891
|
+
* A `git commit` run (sequencer.c `run_git_commit`) with its hooks, `message` its
|
|
2892
|
+
* template: post-commit is left to the caller (post-rewrite follows it for an amend)
|
|
2893
|
+
*/
|
|
2894
|
+
gitCommit?: GitCommitHookRun,
|
|
2895
|
+
): Promise<ObjectId> {
|
|
2896
|
+
// git's try_to_commit: reset_ident_date() first, so each pick is committed (and its
|
|
2897
|
+
// reflog line written) at its own time
|
|
2898
|
+
this.resetClock();
|
|
2899
|
+
// the pick keeps its author; the committer is resolved as git's git_committer_info
|
|
2900
|
+
let author = picked;
|
|
2901
|
+
let committer = this.committer ?? this.resolveCommitter();
|
|
2902
|
+
// git's try_to_commit: --ignore-date dates the author now (the clock, local offset),
|
|
2903
|
+
// --committer-date-is-author-date gives the committer the author's date
|
|
2904
|
+
if (await state.exists(`${DIR}/ignore_date`)) {
|
|
2905
|
+
const now = this.clock();
|
|
2906
|
+
author = { ...author, timestamp: now, tzOffset: localTimezoneOffset(now) };
|
|
2907
|
+
}
|
|
2908
|
+
if (await state.exists(`${DIR}/cdate_is_adate`)) {
|
|
2909
|
+
committer = { ...committer, timestamp: author.timestamp, tzOffset: author.tzOffset };
|
|
2910
|
+
}
|
|
2911
|
+
const hooked = gitCommit
|
|
2912
|
+
? cleanupMessage(
|
|
2913
|
+
await gitCommitMessageHooks(this.hooks, state, message, gitCommit, (text) =>
|
|
2914
|
+
gitCommit.editor ? this.modifyMessage(text) : text,
|
|
2915
|
+
),
|
|
2916
|
+
commentPrefixOf(this.workingCopy),
|
|
2917
|
+
)
|
|
2918
|
+
: await sequencerPrepareMessage(this.hooks, state, message);
|
|
2919
|
+
const id = await this.commits.store({
|
|
2920
|
+
tree,
|
|
2921
|
+
parents,
|
|
2922
|
+
author,
|
|
2923
|
+
committer,
|
|
2924
|
+
message: hooked,
|
|
2925
|
+
});
|
|
2926
|
+
const action = reflogMessages.rebaseStep(this.reflogAction("rebase"), step);
|
|
2927
|
+
const log = this.reflogLine(reflogMessages.pick(action, hooked), this.committer);
|
|
2928
|
+
const refs = this.refsStore;
|
|
2929
|
+
if (refsWatched(refs)) {
|
|
2930
|
+
// update_head_with_reflog: HEAD expecting the commit it is at (detached while
|
|
2931
|
+
// rebasing), as the reference-transaction hook hears it
|
|
2932
|
+
const current = (await refs.resolve("HEAD").catch(() => undefined))?.objectId;
|
|
2933
|
+
await updateHeadExpecting(refs, id, current, log);
|
|
2934
|
+
} else {
|
|
2935
|
+
// no hook to hear an old value: one batch through HEAD to its branch, unchecked
|
|
2936
|
+
await updateHeadExpecting(refs, id, undefined, log);
|
|
2937
|
+
}
|
|
2938
|
+
if (!gitCommit) await sequencerPostCommit(this.hooks);
|
|
2939
|
+
return id;
|
|
2940
|
+
}
|
|
2941
|
+
|
|
2942
|
+
/**
|
|
2943
|
+
* `name` set to `id` as git's `refs_update_ref(..., name, id, old, REF_NO_DEREF)`: one
|
|
2944
|
+
* transaction expecting `old` (unchecked when undefined), as the reference-transaction hook
|
|
2945
|
+
* sees it; a store that cannot batch the ref sets it plainly.
|
|
2946
|
+
*/
|
|
2947
|
+
private async setRefExpecting(
|
|
2948
|
+
name: string,
|
|
2949
|
+
id: ObjectId,
|
|
2950
|
+
old: ObjectId | undefined,
|
|
2951
|
+
log: RefLogOptions | undefined,
|
|
2952
|
+
): Promise<void> {
|
|
2953
|
+
if (old === undefined) {
|
|
2954
|
+
await this.refsStore.set(name, id, log);
|
|
2955
|
+
return;
|
|
2956
|
+
}
|
|
2957
|
+
const result = await this.refsStore.batch([{ name, old, new: id, noDeref: true }], log);
|
|
2958
|
+
if (result.ok) return;
|
|
2959
|
+
if (result.rejected.every((r) => r.reason === "unsupported" || r.reason === "invalid-name")) {
|
|
2960
|
+
await this.refsStore.set(name, id, log);
|
|
2961
|
+
return;
|
|
2962
|
+
}
|
|
2963
|
+
throw new Error(
|
|
2964
|
+
`cannot lock ref '${name}': ${result.rejected.map((r) => `${r.name}: ${r.reason}`).join(", ")}`,
|
|
2965
|
+
);
|
|
2966
|
+
}
|
|
2967
|
+
|
|
2968
|
+
/** HEAD detached at upstream: `rebase (start): checkout <upstream as given>`. */
|
|
2969
|
+
private startLog(): RefLogOptions {
|
|
2970
|
+
const onto = this.upstreamCommitName ?? this.upstreamCommit ?? "";
|
|
2971
|
+
return this.reflogLine(
|
|
2972
|
+
reflogMessages.rebaseStart(this.reflogAction("rebase"), onto),
|
|
2973
|
+
this.committer,
|
|
2974
|
+
);
|
|
2975
|
+
}
|
|
2976
|
+
|
|
2977
|
+
/**
|
|
2978
|
+
* The branch rebased moves to `id` and HEAD is attached to it again, logged as git's
|
|
2979
|
+
* move_to_original_branch: `rebase (finish): <branch> onto <onto>` for the branch,
|
|
2980
|
+
* `rebase (finish): returning to <branch>` for HEAD; an abort logs both
|
|
2981
|
+
* `rebase (abort): returning to <branch>`. A detached rebase leaves HEAD where it is.
|
|
2982
|
+
*/
|
|
2983
|
+
private async finishBranch(
|
|
2984
|
+
headName: string,
|
|
2985
|
+
id: ObjectId,
|
|
2986
|
+
onto: ObjectId | undefined,
|
|
2987
|
+
abort = false,
|
|
2988
|
+
/** The value the branch is expected at (pick_commits' `orig-head`), shown to the hook */
|
|
2989
|
+
old?: ObjectId,
|
|
2990
|
+
): Promise<void> {
|
|
2991
|
+
const action = this.reflogAction("rebase");
|
|
2992
|
+
if (headName.startsWith("refs/")) {
|
|
2993
|
+
const headLog = this.reflogLine(
|
|
2994
|
+
abort
|
|
2995
|
+
? reflogMessages.rebaseAbort(action, headName)
|
|
2996
|
+
: reflogMessages.rebaseFinishHead(action, headName),
|
|
2997
|
+
this.committer,
|
|
2998
|
+
);
|
|
2999
|
+
const branchLog = abort
|
|
3000
|
+
? headLog
|
|
3001
|
+
: this.reflogLine(
|
|
3002
|
+
reflogMessages.rebaseFinishBranch(action, headName, onto ?? id),
|
|
3003
|
+
this.committer,
|
|
3004
|
+
);
|
|
3005
|
+
await this.setRefExpecting(headName, id, old, branchLog);
|
|
3006
|
+
await this.refsStore.setSymbolic("HEAD", headName, headLog);
|
|
3007
|
+
} else {
|
|
3008
|
+
// git's abort of a detached rebase: HEAD back at orig-head ("rebase (abort): returning
|
|
3009
|
+
// to <id>"); a finished one stays where the picks left it
|
|
3010
|
+
const log = abort
|
|
3011
|
+
? this.reflogLine(reflogMessages.rebaseAbort(action, id), this.committer)
|
|
3012
|
+
: undefined;
|
|
3013
|
+
await this.refsStore.set("HEAD", id, log);
|
|
3014
|
+
}
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3017
|
+
/** `git-rebase-todo` read and its names resolved (read_populate_todo). */
|
|
3018
|
+
private async loadTodo(state: OperationState): Promise<{ items: TodoItem[]; errors: string[] }> {
|
|
3019
|
+
const fixupOkay = await state.exists(`${DIR}/done`);
|
|
3020
|
+
const parsed = parseTodo(
|
|
3021
|
+
(await state.read(`${DIR}/git-rebase-todo`)) ?? "",
|
|
3022
|
+
commentPrefixOf(this.workingCopy),
|
|
3023
|
+
fixupOkay,
|
|
3024
|
+
);
|
|
3025
|
+
return this.resolveTodo(parsed.items, { fixupOkay });
|
|
3026
|
+
}
|
|
3027
|
+
|
|
3028
|
+
/**
|
|
3029
|
+
* The commit names of parsed todo items resolved (git's repo_get_oid in parse_insn_line),
|
|
3030
|
+
* the lines git refuses made invalid: names that resolve to no commit, merges where a pick
|
|
3031
|
+
* cannot take one; then todo_list_parse_insn_buffer's check of a fixup with no commit
|
|
3032
|
+
* before it (a line that failed is none). The errors in line order.
|
|
3033
|
+
*/
|
|
3034
|
+
private async resolveTodo(
|
|
3035
|
+
items: TodoItem[],
|
|
3036
|
+
options: {
|
|
3037
|
+
/**
|
|
3038
|
+
* The abbreviations of the commits the list was made of (as written for the editor): a
|
|
3039
|
+
* name that starts with one of them is that commit, without a look at the store
|
|
3040
|
+
*/
|
|
3041
|
+
known?: Map<ObjectId, string>;
|
|
3042
|
+
/** `rebase-merge/done` exists: a fixup or squash may start the list */
|
|
3043
|
+
fixupOkay?: boolean;
|
|
3044
|
+
} = {},
|
|
3045
|
+
): Promise<{ items: TodoItem[]; errors: string[]; warnings: string[] }> {
|
|
3046
|
+
const errors: string[] = [];
|
|
3047
|
+
const { objects: resolved, warnings } = await this.resolveObjectNames(
|
|
3048
|
+
items.filter(takesCommit).map((item) => item.id),
|
|
3049
|
+
options.known,
|
|
3050
|
+
);
|
|
3051
|
+
let fixupOkay = options.fixupOkay ?? false;
|
|
3052
|
+
for (const [k, item] of items.entries()) {
|
|
3053
|
+
// parseTodo's check of a fixup made again below, once the names are resolved
|
|
3054
|
+
if (!item.invalid) delete item.errors;
|
|
3055
|
+
if (takesCommit(item)) {
|
|
3056
|
+
const name = item.id;
|
|
3057
|
+
const named = resolved.get(name);
|
|
3058
|
+
const fail = (lines: string[]) => {
|
|
3059
|
+
item.errors = [...lines, `error: invalid line ${k + 1}: ${item.line ?? ""}`];
|
|
3060
|
+
item.invalid = true;
|
|
3061
|
+
item.rest = item.line ?? "";
|
|
3062
|
+
item.action = RebaseAction.COMMENT;
|
|
3063
|
+
item.id = "";
|
|
3064
|
+
};
|
|
3065
|
+
// lookup_commit_reference: tags peeled; another object refused with git's line
|
|
3066
|
+
const peeled = named?.commit
|
|
3067
|
+
? { id: named.id, type: "commit" }
|
|
3068
|
+
: named
|
|
3069
|
+
? await this.revisionResolver().peelTags(named.id)
|
|
3070
|
+
: undefined;
|
|
3071
|
+
const id = peeled?.type === "commit" ? peeled.id : undefined;
|
|
3072
|
+
if (!named) fail([`error: could not parse '${name}'`]);
|
|
3073
|
+
else if (!id) {
|
|
3074
|
+
fail(peeled?.type ? [`error: object ${named.id} is a ${peeled.type}, not a commit`] : []);
|
|
3075
|
+
} else {
|
|
3076
|
+
item.id = id;
|
|
3077
|
+
const parents = (await this.loadCommit(id))?.parents ?? [];
|
|
3078
|
+
if (parents.length > 1 && isPickLike(item.action)) fail(mergeCommitErrors(item.action));
|
|
3079
|
+
}
|
|
3080
|
+
}
|
|
3081
|
+
if (fixupOkay) {
|
|
3082
|
+
// nothing to check
|
|
3083
|
+
} else if (isFixup(item.action) && !item.invalid) {
|
|
3084
|
+
item.errors = [`error: cannot '${item.action}' without a previous commit`];
|
|
3085
|
+
} else if (!isNoop(item)) fixupOkay = true;
|
|
3086
|
+
if (item.errors) errors.push(...item.errors);
|
|
3087
|
+
}
|
|
3088
|
+
return { items, errors, warnings };
|
|
3089
|
+
}
|
|
3090
|
+
|
|
3091
|
+
/** Commit names resolved at once (see {@link resolveObjectNames}), tags peeled. */
|
|
3092
|
+
private async resolveCommitNames(
|
|
3093
|
+
names: string[],
|
|
3094
|
+
known?: Map<ObjectId, string>,
|
|
3095
|
+
): Promise<Map<string, ObjectId | undefined>> {
|
|
3096
|
+
const { objects } = await this.resolveObjectNames(names, known);
|
|
3097
|
+
const out = new Map<string, ObjectId | undefined>();
|
|
3098
|
+
for (const [name, named] of objects) {
|
|
3099
|
+
out.set(name, !named ? undefined : named.commit ? named.id : await this.commitOf(named.id));
|
|
3100
|
+
}
|
|
3101
|
+
return out;
|
|
3102
|
+
}
|
|
3103
|
+
|
|
3104
|
+
/**
|
|
3105
|
+
* The objects todo names stand for, resolved at once as git's repo_get_oid (get_oid_1)
|
|
3106
|
+
* resolves each: a full id the store holds is that object; a ref by git's rules
|
|
3107
|
+
* (get_oid_basic) before an abbreviation, with git's `warning: refname '<name>' is
|
|
3108
|
+
* ambiguous.` when the abbreviation names one object as well (the refs listed once, a
|
|
3109
|
+
* ref looked up only for a name some ref could have); an abbreviation as long as one of
|
|
3110
|
+
* `known` that starts the same is that commit (unique in the store, as it was written);
|
|
3111
|
+
* the other abbreviations looked up in the store together (one listing of its ids where
|
|
3112
|
+
* it cannot list by prefix), a prefix of several objects refused as git's get_short_oid
|
|
3113
|
+
* refuses it (unless core.disambiguate tells them apart). `commit` marks the ids known to
|
|
3114
|
+
* be commits.
|
|
3115
|
+
*/
|
|
3116
|
+
private async resolveObjectNames(
|
|
3117
|
+
names: string[],
|
|
3118
|
+
known?: Map<ObjectId, string>,
|
|
3119
|
+
): Promise<{
|
|
3120
|
+
objects: Map<string, { id: ObjectId; commit?: boolean } | undefined>;
|
|
3121
|
+
warnings: string[];
|
|
3122
|
+
}> {
|
|
3123
|
+
const objects = new Map<string, { id: ObjectId; commit?: boolean } | undefined>();
|
|
3124
|
+
const warnings: string[] = [];
|
|
3125
|
+
const pending: string[] = [];
|
|
3126
|
+
const viaRef: string[] = [];
|
|
3127
|
+
let refNames: Set<string> | undefined | null = null;
|
|
3128
|
+
for (const name of new Set(names)) {
|
|
3129
|
+
if (!/^[0-9a-f]{4,39}$/i.test(name)) {
|
|
3130
|
+
objects.set(name, await this.objectNamed(name));
|
|
3131
|
+
continue;
|
|
3132
|
+
}
|
|
3133
|
+
if (refNames === null) refNames = await this.refShortNames();
|
|
3134
|
+
if (!refNames || refNames.has(name)) {
|
|
3135
|
+
const ids = await this.refsNamed(name, false);
|
|
3136
|
+
if (ids[0]) {
|
|
3137
|
+
objects.set(name, { id: ids[0] });
|
|
3138
|
+
if (ids.length > 1) warnings.push(`warning: refname '${name}' is ambiguous.`);
|
|
3139
|
+
else viaRef.push(name);
|
|
3140
|
+
continue;
|
|
3141
|
+
}
|
|
3142
|
+
}
|
|
3143
|
+
const fast = known ? knownPrefix(name.toLowerCase(), known) : undefined;
|
|
3144
|
+
if (fast) objects.set(name, { id: fast, commit: true });
|
|
3145
|
+
else pending.push(name);
|
|
3146
|
+
}
|
|
3147
|
+
if (pending.length === 0 && viaRef.length === 0) return { objects, warnings };
|
|
3148
|
+
const candidates = await this.objectsWithPrefixes(
|
|
3149
|
+
[...pending, ...viaRef].map((name) => name.toLowerCase()),
|
|
3150
|
+
);
|
|
3151
|
+
// get_oid_basic: a ref that an abbreviation of one object (get_short_oid, quietly, no
|
|
3152
|
+
// hint) shares
|
|
3153
|
+
for (const name of viaRef) {
|
|
3154
|
+
if (candidates?.get(name.toLowerCase())?.length === 1) {
|
|
3155
|
+
warnings.push(`warning: refname '${name}' is ambiguous.`);
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
for (const name of pending) {
|
|
3159
|
+
const found = candidates?.get(name.toLowerCase());
|
|
3160
|
+
if (!found) {
|
|
3161
|
+
const id = await this.commitNamed(name);
|
|
3162
|
+
objects.set(name, id ? { id, commit: true } : undefined);
|
|
3163
|
+
} else if (found.length === 0) objects.set(name, undefined);
|
|
3164
|
+
else if (found.length === 1) objects.set(name, { id: found[0] as ObjectId });
|
|
3165
|
+
else {
|
|
3166
|
+
// ambiguous: only core.disambiguate can pick one (the revision resolver applies it)
|
|
3167
|
+
const id = await this.resolveRevision(name).catch(() => undefined);
|
|
3168
|
+
objects.set(name, id ? { id } : undefined);
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
return { objects, warnings };
|
|
3172
|
+
}
|
|
3173
|
+
|
|
3174
|
+
/**
|
|
3175
|
+
* A name that is no abbreviation, as get_oid_basic resolves it: a full id the store holds
|
|
3176
|
+
* (of any object), else a ref by git's rules; undefined if neither.
|
|
3177
|
+
*/
|
|
3178
|
+
private async objectNamed(name: string): Promise<{ id: ObjectId; commit?: boolean } | undefined> {
|
|
3179
|
+
if (/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i.test(name)) {
|
|
3180
|
+
const id = name.toLowerCase();
|
|
3181
|
+
if (await this.commits.has(id).catch(() => false)) return { id, commit: true };
|
|
3182
|
+
if (await this.revisionResolver().objectType(id)) return { id };
|
|
3183
|
+
}
|
|
3184
|
+
const ids = await this.refsNamed(name, true);
|
|
3185
|
+
return ids[0] ? { id: ids[0] } : undefined;
|
|
3186
|
+
}
|
|
3187
|
+
|
|
3188
|
+
/**
|
|
3189
|
+
* The objects of the refs a name stands for by git's ref_rev_parse_rules (as written,
|
|
3190
|
+
* under refs/, a tag, a branch, a remote, a remote's HEAD), in that order; the name as
|
|
3191
|
+
* written only with `asWritten` (a lowercase abbreviation is no root ref).
|
|
3192
|
+
*/
|
|
3193
|
+
private async refsNamed(name: string, asWritten: boolean): Promise<ObjectId[]> {
|
|
3194
|
+
const out: ObjectId[] = [];
|
|
3195
|
+
const candidates = [
|
|
3196
|
+
...(asWritten ? [name] : []),
|
|
3197
|
+
`refs/${name}`,
|
|
3198
|
+
`refs/tags/${name}`,
|
|
3199
|
+
`refs/heads/${name}`,
|
|
3200
|
+
`refs/remotes/${name}`,
|
|
3201
|
+
`refs/remotes/${name}/HEAD`,
|
|
3202
|
+
];
|
|
3203
|
+
for (const candidate of candidates) {
|
|
3204
|
+
const ref = await this.refsStore.resolve(candidate).catch(() => undefined);
|
|
3205
|
+
if (ref?.objectId) out.push(ref.objectId);
|
|
3206
|
+
}
|
|
3207
|
+
return out;
|
|
3208
|
+
}
|
|
3209
|
+
|
|
3210
|
+
/**
|
|
3211
|
+
* The names refs/ refs answer to under git's rules (each without `refs/`, `refs/tags/`,
|
|
3212
|
+
* `refs/heads/`, `refs/remotes/`, and a remote's `HEAD`), from one listing; undefined
|
|
3213
|
+
* where the refs cannot be listed.
|
|
3214
|
+
*/
|
|
3215
|
+
private async refShortNames(): Promise<Set<string> | undefined> {
|
|
3216
|
+
const names = new Set<string>();
|
|
3217
|
+
try {
|
|
3218
|
+
for await (const entry of this.refsStore.list("refs/")) {
|
|
3219
|
+
const full = entry.name;
|
|
3220
|
+
names.add(full.slice("refs/".length));
|
|
3221
|
+
for (const prefix of ["refs/tags/", "refs/heads/", "refs/remotes/"]) {
|
|
3222
|
+
if (full.startsWith(prefix)) names.add(full.slice(prefix.length));
|
|
3223
|
+
}
|
|
3224
|
+
if (full.startsWith("refs/remotes/") && full.endsWith("/HEAD")) {
|
|
3225
|
+
names.add(full.slice("refs/remotes/".length, -"/HEAD".length));
|
|
3226
|
+
}
|
|
3227
|
+
}
|
|
3228
|
+
} catch {
|
|
3229
|
+
return undefined;
|
|
3230
|
+
}
|
|
3231
|
+
return names;
|
|
3232
|
+
}
|
|
3233
|
+
|
|
3234
|
+
/** The commit an object is (a tag peeled, as lookup_commit_reference), else undefined. */
|
|
3235
|
+
private async commitOf(id: ObjectId): Promise<ObjectId | undefined> {
|
|
3236
|
+
const peeled = await this.peelToCommit(id);
|
|
3237
|
+
return (await this.commits.has(peeled).catch(() => false)) ? peeled : undefined;
|
|
3238
|
+
}
|
|
3239
|
+
|
|
3240
|
+
/**
|
|
3241
|
+
* The stored ids starting with each lowercase prefix: by prefix where the store can list
|
|
3242
|
+
* so, else one listing of every id for all of them; undefined without an object store.
|
|
3243
|
+
*/
|
|
3244
|
+
private async objectsWithPrefixes(
|
|
3245
|
+
prefixes: string[],
|
|
3246
|
+
): Promise<Map<string, ObjectId[]> | undefined> {
|
|
3247
|
+
const objects = this.workingCopy.history?.objects;
|
|
3248
|
+
if (!objects) return undefined;
|
|
3249
|
+
const found = new Map(prefixes.map((prefix) => [prefix, new Set<ObjectId>()]));
|
|
3250
|
+
try {
|
|
3251
|
+
if (objects.listPrefix) {
|
|
3252
|
+
for (const [prefix, ids] of found) {
|
|
3253
|
+
for await (const id of listObjectsWithPrefix(objects, prefix)) ids.add(id);
|
|
3254
|
+
}
|
|
3255
|
+
} else {
|
|
3256
|
+
const lengths = [...new Set(prefixes.map((prefix) => prefix.length))];
|
|
3257
|
+
for await (const id of objects.list()) {
|
|
3258
|
+
for (const length of lengths) found.get(id.slice(0, length))?.add(id);
|
|
3259
|
+
}
|
|
3260
|
+
}
|
|
3261
|
+
} catch {
|
|
3262
|
+
return undefined;
|
|
3263
|
+
}
|
|
3264
|
+
return new Map([...found].map(([prefix, ids]) => [prefix, [...ids]]));
|
|
3265
|
+
}
|
|
3266
|
+
|
|
3267
|
+
/** A commit by name: an id (abbreviated), a ref, a branch, a tag; undefined if none. */
|
|
3268
|
+
private async commitNamed(name: string): Promise<ObjectId | undefined> {
|
|
3269
|
+
if (/^[0-9a-f]{4,64}$/i.test(name)) {
|
|
3270
|
+
try {
|
|
3271
|
+
return await expandCommitId(
|
|
3272
|
+
{
|
|
3273
|
+
commits: this.commits,
|
|
3274
|
+
refs: this.refsStore,
|
|
3275
|
+
state: new OperationState(this.workingCopy.checkout, this.refsStore),
|
|
3276
|
+
},
|
|
3277
|
+
name.toLowerCase(),
|
|
3278
|
+
);
|
|
3279
|
+
} catch {
|
|
3280
|
+
// not an abbreviation of one commit: maybe a ref
|
|
3281
|
+
}
|
|
3282
|
+
}
|
|
3283
|
+
return this.refNamed(name);
|
|
3284
|
+
}
|
|
3285
|
+
|
|
3286
|
+
/** A commit by ref name (as given, under refs/, a tag, a branch); undefined if none. */
|
|
3287
|
+
private async refNamed(name: string): Promise<ObjectId | undefined> {
|
|
3288
|
+
for (const candidate of [name, `refs/${name}`, `refs/tags/${name}`, `refs/heads/${name}`]) {
|
|
3289
|
+
const ref = await this.refsStore.resolve(candidate).catch(() => undefined);
|
|
3290
|
+
if (ref?.objectId) {
|
|
3291
|
+
const id = await this.peelToCommit(ref.objectId);
|
|
3292
|
+
if (await this.commits.has(id).catch(() => false)) return id;
|
|
3293
|
+
}
|
|
3294
|
+
}
|
|
3295
|
+
return undefined;
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
/**
|
|
3299
|
+
* An `--autosquash` target that is one word (todo_list_rearrange_squash's
|
|
3300
|
+
* lookup_commit_reference_by_name, whose commit counts only when it is in the list): the
|
|
3301
|
+
* list's own commits matched by id first; a hex word no commit of the list starts with can
|
|
3302
|
+
* only name one of them as a ref.
|
|
3303
|
+
*/
|
|
3304
|
+
private async autosquashTarget(
|
|
3305
|
+
name: string,
|
|
3306
|
+
known: Map<ObjectId, string>,
|
|
3307
|
+
): Promise<ObjectId | undefined> {
|
|
3308
|
+
if (!/^[0-9a-f]{4,64}$/i.test(name)) return this.commitNamed(name);
|
|
3309
|
+
const lower = name.toLowerCase();
|
|
3310
|
+
if (lower.length >= 40) return known.has(lower) ? lower : this.refNamed(name);
|
|
3311
|
+
const fast = knownPrefix(lower, known);
|
|
3312
|
+
if (fast) return fast;
|
|
3313
|
+
for (const id of known.keys()) {
|
|
3314
|
+
// lookup_commit_reference_by_name: an abbreviation read as a committish
|
|
3315
|
+
if (id.startsWith(lower)) return this.commitNamed(name);
|
|
3316
|
+
}
|
|
3317
|
+
return this.refNamed(name);
|
|
3318
|
+
}
|
|
3319
|
+
|
|
3320
|
+
/** is_original_commit_empty: the commit changes nothing against its parent (or none). */
|
|
3321
|
+
private async originallyEmpty(id: ObjectId): Promise<boolean> {
|
|
3322
|
+
const commit = await this.loadCommit(id);
|
|
3323
|
+
if (!commit) return false;
|
|
3324
|
+
const parentTree = commit.parents[0]
|
|
3325
|
+
? (await this.loadCommit(commit.parents[0]))?.tree
|
|
3326
|
+
: this.objectFormat.emptyTreeId;
|
|
3327
|
+
return commit.tree === parentTree;
|
|
3328
|
+
}
|
|
3329
|
+
|
|
3330
|
+
/** rebase.missingCommitsCheck (ignore, warn, error; anything else warned about). */
|
|
3331
|
+
private missingCommitsCheckLevel(output: string[]): "ignore" | "warn" | "error" {
|
|
3332
|
+
const value = configValue(this.workingCopy, "rebase.missingCommitsCheck");
|
|
3333
|
+
if (value === undefined || value.toLowerCase() === "ignore") return "ignore";
|
|
3334
|
+
if (value.toLowerCase() === "warn") return "warn";
|
|
3335
|
+
if (value.toLowerCase() === "error") return "error";
|
|
3336
|
+
output.push(
|
|
3337
|
+
`warning: unrecognized setting ${value} for option rebase.missingCommitsCheck. Ignoring.`,
|
|
3338
|
+
);
|
|
3339
|
+
return "ignore";
|
|
3340
|
+
}
|
|
3341
|
+
|
|
3342
|
+
/**
|
|
3343
|
+
* rebase-interactive.c's todo_list_check: the commits of the list before editing that the
|
|
3344
|
+
* edited one dropped (newer to older), with git's warning; nothing when none or ignored.
|
|
3345
|
+
*/
|
|
3346
|
+
private async missingCommits(
|
|
3347
|
+
before: TodoItem[],
|
|
3348
|
+
after: TodoItem[],
|
|
3349
|
+
level: "ignore" | "warn" | "error",
|
|
3350
|
+
): Promise<string[]> {
|
|
3351
|
+
if (level === "ignore") return [];
|
|
617
3352
|
const seen = new Set<ObjectId>();
|
|
3353
|
+
for (const item of after) if (takesCommit(item)) seen.add(item.id);
|
|
3354
|
+
const missing: string[] = [];
|
|
3355
|
+
for (let i = before.length - 1; i >= 0; i--) {
|
|
3356
|
+
const item = before[i] as TodoItem;
|
|
3357
|
+
if (!takesCommit(item) || seen.has(item.id)) continue;
|
|
3358
|
+
missing.push(` - ${await this.abbreviateObjectId(item.id)} ${item.rest ?? ""}`);
|
|
3359
|
+
seen.add(item.id);
|
|
3360
|
+
}
|
|
3361
|
+
if (missing.length === 0) return [];
|
|
3362
|
+
return [
|
|
3363
|
+
"Warning: some commits may have been dropped accidentally.",
|
|
3364
|
+
"Dropped commits (newer to older):",
|
|
3365
|
+
...missing,
|
|
3366
|
+
'To avoid this message, use "drop" to explicitly remove a commit.',
|
|
3367
|
+
"",
|
|
3368
|
+
"Use 'git config rebase.missingCommitsCheck' to change the level of warnings.",
|
|
3369
|
+
"The possible behaviours are: ignore, warn, error.",
|
|
3370
|
+
"",
|
|
3371
|
+
...EDIT_TODO_ADVICE,
|
|
3372
|
+
];
|
|
3373
|
+
}
|
|
618
3374
|
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
3375
|
+
/** The abbreviated ids of the list's commits (TODO_LIST_SHORTEN_IDS), in one batch. */
|
|
3376
|
+
private async shortIds(items: TodoItem[]): Promise<Map<ObjectId, string>> {
|
|
3377
|
+
const ids = [...new Set(items.filter(takesCommit).map((item) => item.id))];
|
|
3378
|
+
const lengths = await this.idAbbreviator().lengths(ids);
|
|
3379
|
+
return new Map(ids.map((id) => [id, id.slice(0, lengths.get(id) ?? 7)]));
|
|
3380
|
+
}
|
|
3381
|
+
|
|
3382
|
+
/**
|
|
3383
|
+
* launch_sequence_editor: the interactive handler edits the list (editTodo, then the
|
|
3384
|
+
* steps of prepareSteps); without one the list is kept.
|
|
3385
|
+
*/
|
|
3386
|
+
private async runSequenceEditor(
|
|
3387
|
+
text: string,
|
|
3388
|
+
/** The abbreviations the list was written with (see {@link resolveCommitNames}) */
|
|
3389
|
+
known?: Map<ObjectId, string>,
|
|
3390
|
+
): Promise<string> {
|
|
3391
|
+
const handler = this.interactiveHandler;
|
|
3392
|
+
let out = text;
|
|
3393
|
+
if (handler?.editTodo) out = await handler.editTodo(out);
|
|
3394
|
+
if (handler?.prepareSteps) {
|
|
3395
|
+
const comment = commentPrefixOf(this.workingCopy);
|
|
3396
|
+
const items = parseTodo(stripspace(out, comment), comment).items.filter(
|
|
3397
|
+
(item) => item.action !== RebaseAction.COMMENT,
|
|
3398
|
+
);
|
|
3399
|
+
const ids = await this.resolveCommitNames(
|
|
3400
|
+
items.filter(takesCommit).map((item) => item.id),
|
|
3401
|
+
known,
|
|
3402
|
+
);
|
|
3403
|
+
const steps: RebaseTodoLine[] = [];
|
|
3404
|
+
for (const item of items) {
|
|
3405
|
+
if (takesCommit(item)) {
|
|
3406
|
+
const id = ids.get(item.id);
|
|
3407
|
+
steps.push({
|
|
3408
|
+
action: item.action,
|
|
3409
|
+
commit: item.id,
|
|
3410
|
+
shortMessage: subjectOf((id ? await this.loadCommit(id) : undefined)?.message ?? ""),
|
|
3411
|
+
...(item.replaceMessage ? { fixupFlag: "C" as const } : {}),
|
|
3412
|
+
...(item.editMessage && item.action === RebaseAction.FIXUP
|
|
3413
|
+
? { fixupFlag: "c" as const }
|
|
3414
|
+
: {}),
|
|
3415
|
+
});
|
|
3416
|
+
} else
|
|
3417
|
+
steps.push({
|
|
3418
|
+
action: item.action,
|
|
3419
|
+
commit: "",
|
|
3420
|
+
shortMessage: "",
|
|
3421
|
+
argument: item.rest ?? "",
|
|
3422
|
+
});
|
|
3423
|
+
}
|
|
3424
|
+
try {
|
|
3425
|
+
handler.prepareSteps(steps);
|
|
3426
|
+
} catch (error) {
|
|
3427
|
+
throw new HandlerFailure("prepareSteps", error);
|
|
3428
|
+
}
|
|
3429
|
+
out = "";
|
|
3430
|
+
for (const step of steps) {
|
|
3431
|
+
if (step.action === RebaseAction.COMMENT) continue;
|
|
3432
|
+
if (!step.commit) {
|
|
3433
|
+
out += `${step.action}${step.argument ? ` ${step.argument}` : ""}\n`;
|
|
3434
|
+
continue;
|
|
3435
|
+
}
|
|
3436
|
+
const original = items.find(
|
|
3437
|
+
(item) =>
|
|
3438
|
+
takesCommit(item) &&
|
|
3439
|
+
(item.id.startsWith(step.commit) || step.commit.startsWith(item.id)),
|
|
3440
|
+
);
|
|
3441
|
+
const flag = step.fixupFlag ? ` -${step.fixupFlag}` : "";
|
|
3442
|
+
const rest = original?.rest ?? (step.shortMessage ? `# ${step.shortMessage}` : "");
|
|
3443
|
+
out += `${step.action}${flag} ${step.commit}${rest ? ` ${rest}` : ""}\n`;
|
|
3444
|
+
}
|
|
3445
|
+
}
|
|
3446
|
+
return out;
|
|
3447
|
+
}
|
|
3448
|
+
|
|
3449
|
+
/**
|
|
3450
|
+
* `git rebase --edit-todo` (edit_todo_file): the rest of the list edited by the
|
|
3451
|
+
* interactive handler (git's help for an ongoing rebase after it), checked as git checks
|
|
3452
|
+
* it, written back with the full ids.
|
|
3453
|
+
*/
|
|
3454
|
+
private async editTodo(state: OperationState): Promise<RebaseResult> {
|
|
3455
|
+
if (!(await this.inProgress(state))) throw new NoRebaseInProgressError();
|
|
3456
|
+
const comment = commentPrefixOf(this.workingCopy);
|
|
3457
|
+
const abbreviate = configBool(this.workingCopy, "rebase.abbreviateCommands", false);
|
|
3458
|
+
const output: string[] = [];
|
|
3459
|
+
const current = stripspace((await state.read(`${DIR}/git-rebase-todo`)) ?? "", comment);
|
|
3460
|
+
const fixupOkay = await state.exists(`${DIR}/done`);
|
|
3461
|
+
const old = await this.resolveTodo(parseTodo(current, comment, fixupOkay).items, {
|
|
3462
|
+
fixupOkay,
|
|
3463
|
+
});
|
|
3464
|
+
const incorrect = old.errors.length > 0 || (await state.exists(`${DIR}/dropped`));
|
|
3465
|
+
const level = this.missingCommitsCheckLevel(output);
|
|
3466
|
+
const help = todoHelp(countTodoCommands(old.items), comment, undefined, level === "error");
|
|
3467
|
+
const shorts = await this.shortIds(old.items);
|
|
3468
|
+
const text = `${formatTodo(old.items, { abbreviate, idOf: (id) => shorts.get(id) ?? id })}${help}`;
|
|
3469
|
+
await state.write(`${DIR}/git-rebase-todo`, text);
|
|
3470
|
+
if (!incorrect) {
|
|
3471
|
+
await state.write(
|
|
3472
|
+
`${DIR}/git-rebase-todo.backup`,
|
|
3473
|
+
`${formatTodo(old.items, { abbreviate })}${help}`,
|
|
3474
|
+
);
|
|
3475
|
+
}
|
|
3476
|
+
let edited: string;
|
|
3477
|
+
try {
|
|
3478
|
+
edited = await this.runSequenceEditor(text, shorts);
|
|
3479
|
+
} catch (error) {
|
|
3480
|
+
// edit_todo_list's -2: the list stays as written for the editor
|
|
3481
|
+
return { status: RebaseStatus.FAILED, message: sequenceEditorFailure(error), output };
|
|
623
3482
|
}
|
|
3483
|
+
await state.write(`${DIR}/git-rebase-todo`, edited);
|
|
3484
|
+
// the names read back against the list as it was before the edit
|
|
3485
|
+
const parsed = await this.resolveTodo(
|
|
3486
|
+
parseTodo(stripspace(edited, comment), comment, fixupOkay).items,
|
|
3487
|
+
{ known: shorts, fixupOkay },
|
|
3488
|
+
);
|
|
3489
|
+
output.push(...parsed.warnings);
|
|
3490
|
+
if (parsed.errors.length > 0) {
|
|
3491
|
+
return {
|
|
3492
|
+
status: RebaseStatus.FAILED,
|
|
3493
|
+
message: [...parsed.errors, ...EDIT_TODO_ADVICE].join("\n"),
|
|
3494
|
+
output,
|
|
3495
|
+
};
|
|
3496
|
+
}
|
|
3497
|
+
let reference = old.items;
|
|
3498
|
+
if (incorrect) {
|
|
3499
|
+
reference = (
|
|
3500
|
+
await this.resolveTodo(
|
|
3501
|
+
parseTodo((await state.read(`${DIR}/git-rebase-todo.backup`)) ?? "", comment).items,
|
|
3502
|
+
)
|
|
3503
|
+
).items;
|
|
3504
|
+
}
|
|
3505
|
+
const missing = await this.missingCommits(reference, parsed.items, level);
|
|
3506
|
+
if (missing.length > 0 && level === "error") {
|
|
3507
|
+
await state.write(`${DIR}/dropped`, "");
|
|
3508
|
+
return { status: RebaseStatus.FAILED, message: missing.join("\n"), output };
|
|
3509
|
+
}
|
|
3510
|
+
output.push(...missing);
|
|
3511
|
+
// edit_todo_list unlinks it only when `incorrect` is positive: the list read before the
|
|
3512
|
+
// edit parsed (todo_list_parse_insn_buffer's -1 or'ed with the file's 1 stays negative)
|
|
3513
|
+
if (incorrect && old.errors.length === 0) await state.remove(`${DIR}/dropped`);
|
|
3514
|
+
await this.filterUpdateRefs(state, parsed.items);
|
|
3515
|
+
await state.write(
|
|
3516
|
+
`${DIR}/git-rebase-todo`,
|
|
3517
|
+
formatTodo(
|
|
3518
|
+
parsed.items.map(({ line: _, ...item }) => item as TodoItem),
|
|
3519
|
+
{ abbreviate },
|
|
3520
|
+
),
|
|
3521
|
+
);
|
|
3522
|
+
return { status: RebaseStatus.OK, output };
|
|
3523
|
+
}
|
|
3524
|
+
|
|
3525
|
+
/** `rebase-merge/update-refs`: ref, its value at the start, the value to give it. */
|
|
3526
|
+
private async readUpdateRefs(state: OperationState): Promise<UpdateRefRecord[]> {
|
|
3527
|
+
const lines = ((await state.read(`${DIR}/update-refs`)) ?? "").split("\n");
|
|
3528
|
+
const out: UpdateRefRecord[] = [];
|
|
3529
|
+
for (let k = 0; k + 2 < lines.length; k += 3) {
|
|
3530
|
+
out.push({
|
|
3531
|
+
ref: lines[k] as string,
|
|
3532
|
+
before: lines[k + 1] as string,
|
|
3533
|
+
after: lines[k + 2] as string,
|
|
3534
|
+
});
|
|
3535
|
+
}
|
|
3536
|
+
return out;
|
|
3537
|
+
}
|
|
3538
|
+
|
|
3539
|
+
/** write_update_refs_state: the records sorted by ref; no file when there are none. */
|
|
3540
|
+
private async writeUpdateRefs(state: OperationState, records: UpdateRefRecord[]): Promise<void> {
|
|
3541
|
+
if (records.length === 0) {
|
|
3542
|
+
await state.remove(`${DIR}/update-refs`);
|
|
3543
|
+
return;
|
|
3544
|
+
}
|
|
3545
|
+
const sorted = [...records].sort((a, b) => (a.ref < b.ref ? -1 : a.ref > b.ref ? 1 : 0));
|
|
3546
|
+
await state.write(
|
|
3547
|
+
`${DIR}/update-refs`,
|
|
3548
|
+
sorted.map((r) => `${r.ref}\n${r.before}\n${r.after}\n`).join(""),
|
|
3549
|
+
);
|
|
3550
|
+
}
|
|
624
3551
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
3552
|
+
/**
|
|
3553
|
+
* todo_list_add_update_ref_commands (`--update-refs`): after each command with a commit,
|
|
3554
|
+
* an `update-ref <branch>` for each other branch at that commit (the last in name order
|
|
3555
|
+
* first, as git's decorations list them); a branch checked out in another worktree gets a
|
|
3556
|
+
* comment instead. The records written to `rebase-merge/update-refs`.
|
|
3557
|
+
*/
|
|
3558
|
+
private async addUpdateRefCommands(
|
|
3559
|
+
state: OperationState,
|
|
3560
|
+
items: TodoItem[],
|
|
3561
|
+
comment: string,
|
|
3562
|
+
): Promise<TodoItem[]> {
|
|
3563
|
+
const headRef = await this.refsStore.get("HEAD");
|
|
3564
|
+
const current = headRef && isSymbolicRef(headRef) ? headRef.target : undefined;
|
|
3565
|
+
const branches = new Map<ObjectId, string[]>();
|
|
3566
|
+
const names: string[] = [];
|
|
3567
|
+
for await (const ref of this.refsStore.list("refs/heads/")) {
|
|
3568
|
+
if (ref.name.startsWith("refs/heads/")) names.push(ref.name);
|
|
3569
|
+
}
|
|
3570
|
+
names.sort();
|
|
3571
|
+
const values = new Map<string, ObjectId>();
|
|
3572
|
+
for (const name of names) {
|
|
3573
|
+
const value = (await this.refsStore.resolve(name).catch(() => undefined))?.objectId;
|
|
3574
|
+
if (!value) continue;
|
|
3575
|
+
values.set(name, value);
|
|
3576
|
+
const id = await this.peelToCommit(value);
|
|
3577
|
+
branches.set(id, [name, ...(branches.get(id) ?? [])]);
|
|
3578
|
+
}
|
|
3579
|
+
const checkedOut = new Map<string, string>();
|
|
3580
|
+
const worktrees = this.workingCopy.worktrees;
|
|
3581
|
+
if (worktrees) {
|
|
3582
|
+
for (const wt of await worktrees.list().catch(() => [])) {
|
|
3583
|
+
for (const name of names) {
|
|
3584
|
+
if (!checkedOut.has(name) && usesBranch(wt, name)) checkedOut.set(name, wt.path);
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
}
|
|
3588
|
+
const records: UpdateRefRecord[] = [];
|
|
3589
|
+
const out: TodoItem[] = [];
|
|
3590
|
+
for (const item of items) {
|
|
3591
|
+
out.push(item);
|
|
3592
|
+
if (!item.id || item.action === RebaseAction.COMMENT) continue;
|
|
3593
|
+
for (const name of branches.get(item.id) ?? []) {
|
|
3594
|
+
if (name === current) continue;
|
|
3595
|
+
const path = checkedOut.get(name);
|
|
3596
|
+
if (path !== undefined) {
|
|
3597
|
+
out.push({
|
|
3598
|
+
action: RebaseAction.COMMENT,
|
|
3599
|
+
id: "",
|
|
3600
|
+
rest: `${comment} Ref ${name} checked out at '${path}'`,
|
|
3601
|
+
blankAfter: true,
|
|
3602
|
+
});
|
|
3603
|
+
} else {
|
|
3604
|
+
out.push({ action: RebaseAction.UPDATE_REF, id: "", rest: name, blankAfter: true });
|
|
3605
|
+
if (!records.some((r) => r.ref === name)) {
|
|
3606
|
+
records.push({
|
|
3607
|
+
ref: name,
|
|
3608
|
+
before: values.get(name) ?? this.objectFormat.nullId,
|
|
3609
|
+
after: this.objectFormat.nullId,
|
|
3610
|
+
});
|
|
3611
|
+
}
|
|
3612
|
+
}
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
await this.writeUpdateRefs(state, records);
|
|
3616
|
+
return out;
|
|
3617
|
+
}
|
|
3618
|
+
|
|
3619
|
+
/**
|
|
3620
|
+
* todo_list_filter_update_refs: the records of refs the edited list no longer updates (and
|
|
3621
|
+
* not updated yet) dropped, the refs it newly names added.
|
|
3622
|
+
*/
|
|
3623
|
+
private async filterUpdateRefs(state: OperationState, items: TodoItem[]): Promise<void> {
|
|
3624
|
+
const records = await this.readUpdateRefs(state);
|
|
3625
|
+
const named = items
|
|
3626
|
+
.filter((item) => item.action === RebaseAction.UPDATE_REF && !item.invalid)
|
|
3627
|
+
.map((item) => item.rest ?? "");
|
|
3628
|
+
let updated = false;
|
|
3629
|
+
const kept = records.filter((r) => {
|
|
3630
|
+
const keep = r.after !== this.objectFormat.nullId || named.includes(r.ref);
|
|
3631
|
+
if (!keep) updated = true;
|
|
3632
|
+
return keep;
|
|
3633
|
+
});
|
|
3634
|
+
for (const ref of named) {
|
|
3635
|
+
if (!kept.some((r) => r.ref === ref)) {
|
|
3636
|
+
const value = (await this.refsStore.resolve(ref).catch(() => undefined))?.objectId;
|
|
3637
|
+
kept.push({
|
|
3638
|
+
ref,
|
|
3639
|
+
before: value ?? this.objectFormat.nullId,
|
|
3640
|
+
after: this.objectFormat.nullId,
|
|
3641
|
+
});
|
|
3642
|
+
updated = true;
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
if (updated) await this.writeUpdateRefs(state, kept);
|
|
3646
|
+
}
|
|
3647
|
+
|
|
3648
|
+
/** do_update_ref: the ref's record takes HEAD. */
|
|
3649
|
+
private async updateRefRecord(state: OperationState, ref: string): Promise<void> {
|
|
3650
|
+
const records = await this.readUpdateRefs(state);
|
|
3651
|
+
const head = await this.resolveHead();
|
|
3652
|
+
for (const record of records) {
|
|
3653
|
+
if (record.ref === ref) {
|
|
3654
|
+
record.after = head;
|
|
628
3655
|
break;
|
|
629
3656
|
}
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
3657
|
+
}
|
|
3658
|
+
await this.writeUpdateRefs(state, records);
|
|
3659
|
+
}
|
|
3660
|
+
|
|
3661
|
+
/**
|
|
3662
|
+
* do_update_refs: each recorded ref moved to its new value if it still has its old one
|
|
3663
|
+
* (`rewritten during rebase`), with git's `Updated the following refs with --update-refs:`.
|
|
3664
|
+
*/
|
|
3665
|
+
private async doUpdateRefs(state: OperationState, output: string[]): Promise<void> {
|
|
3666
|
+
const records = await this.readUpdateRefs(state);
|
|
3667
|
+
const updated: string[] = [];
|
|
3668
|
+
const failed: string[] = [];
|
|
3669
|
+
for (const record of records) {
|
|
3670
|
+
const value = (await this.refsStore.resolve(record.ref).catch(() => undefined))?.objectId;
|
|
3671
|
+
if (
|
|
3672
|
+
(value ?? this.objectFormat.nullId) !== record.before ||
|
|
3673
|
+
record.after === this.objectFormat.nullId
|
|
3674
|
+
) {
|
|
3675
|
+
failed.push(record.ref);
|
|
3676
|
+
continue;
|
|
3677
|
+
}
|
|
3678
|
+
await this.refsStore.set(
|
|
3679
|
+
record.ref,
|
|
3680
|
+
record.after,
|
|
3681
|
+
this.reflogLine("rewritten during rebase", this.committer),
|
|
3682
|
+
);
|
|
3683
|
+
updated.push(record.ref);
|
|
3684
|
+
}
|
|
3685
|
+
if (updated.length + failed.length === 0) return;
|
|
3686
|
+
output.push("Updated the following refs with --update-refs:", ...updated.map((r) => `\t${r}`));
|
|
3687
|
+
if (failed.length > 0) {
|
|
3688
|
+
output.push(
|
|
3689
|
+
"Failed to update the following refs with --update-refs:",
|
|
3690
|
+
...failed.map((r) => `\t${r}`),
|
|
3691
|
+
);
|
|
3692
|
+
}
|
|
3693
|
+
}
|
|
3694
|
+
|
|
3695
|
+
/**
|
|
3696
|
+
* The text after a pick's id, as git 2.5x's sequencer_make_script writes it: the commit
|
|
3697
|
+
* through `rebase.instructionFormat` (default `# %s`; a format that does not start with
|
|
3698
|
+
* `#` gets `# ` before it), `%h` unabbreviated (its pretty context has abbrev 0), trailing
|
|
3699
|
+
* blanks dropped.
|
|
3700
|
+
*/
|
|
3701
|
+
private async instructionText(id: ObjectId): Promise<string> {
|
|
3702
|
+
let format = configValue(this.workingCopy, "rebase.instructionFormat") ?? "";
|
|
3703
|
+
if (format === "") format = "# %s";
|
|
3704
|
+
if (!format.startsWith("#")) format = `# ${format}`;
|
|
3705
|
+
if (format === "# %s") {
|
|
3706
|
+
let subject = this.subjects.get(id);
|
|
3707
|
+
if (subject === undefined) {
|
|
3708
|
+
subject = subjectOf((await this.commits.load(id))?.message ?? "");
|
|
3709
|
+
this.subjects.set(id, subject);
|
|
633
3710
|
}
|
|
3711
|
+
return `# ${subject}`;
|
|
3712
|
+
}
|
|
3713
|
+
const commit = await this.commits.load(id);
|
|
3714
|
+
if (!commit) return format;
|
|
3715
|
+
// %d and friends: the refs decorate the commits (load_ref_decorations, no filter)
|
|
3716
|
+
if (this.decorations === undefined && /%[-+ ]?(?:[dD]|\(decorate)/.test(format)) {
|
|
3717
|
+
this.decorations = await this.refDecorations();
|
|
634
3718
|
}
|
|
3719
|
+
// the list goes through the todo editing's stripspace: no trailing blanks
|
|
3720
|
+
const text = await formatCommitPretty(format, commit, id, {
|
|
3721
|
+
now: this.clock(),
|
|
3722
|
+
...(this.decorations ? { decorations: this.decorations } : {}),
|
|
3723
|
+
...(format.includes("%(trailers") ? { trailers: trailerConfigOf(this.workingCopy) } : {}),
|
|
3724
|
+
...(format.includes("%(describe") ? { describe: this.describer() } : {}),
|
|
3725
|
+
});
|
|
3726
|
+
return text.replace(/[ \t]+$/, "");
|
|
3727
|
+
}
|
|
635
3728
|
|
|
636
|
-
|
|
637
|
-
|
|
3729
|
+
/** `%(describe)` of rebase.instructionFormat: git describe over this repository's tags. */
|
|
3730
|
+
private describer(): (id: ObjectId, options: DescribeOptions) => Promise<string> {
|
|
3731
|
+
const abbreviator = createIdAbbreviator(this._workingCopy);
|
|
3732
|
+
const tags = this.tagsStore;
|
|
3733
|
+
return createDescriber({
|
|
3734
|
+
refs: this.refsStore,
|
|
3735
|
+
...(tags ? { tags } : {}),
|
|
3736
|
+
commits: this.commits,
|
|
3737
|
+
abbreviate: async (id, length) => {
|
|
3738
|
+
if (length === 0) return id;
|
|
3739
|
+
const lengths = await abbreviator.lengths([id], length);
|
|
3740
|
+
return id.slice(0, lengths.get(id) ?? 7);
|
|
3741
|
+
},
|
|
3742
|
+
});
|
|
638
3743
|
}
|
|
639
3744
|
|
|
640
3745
|
/**
|
|
641
|
-
*
|
|
3746
|
+
* The decorations git's sequencer shows for `%d` (load_ref_decorations without a filter):
|
|
3747
|
+
* every ref, HEAD, the grafted and shallow commits, the objects replace refs replace.
|
|
642
3748
|
*/
|
|
643
|
-
private async
|
|
644
|
-
|
|
3749
|
+
private async refDecorations(): Promise<RefDecorations> {
|
|
3750
|
+
const replacements = await this.objectReplacements();
|
|
3751
|
+
const shallow = (await this.refsStore.getShallow?.().catch(() => undefined)) ?? [];
|
|
3752
|
+
const environment = hostEnvironmentOf(this._workingCopy);
|
|
3753
|
+
const objects = this._workingCopy.history?.objects;
|
|
3754
|
+
const tags = this.tagsStore;
|
|
3755
|
+
return loadRefDecorations({
|
|
3756
|
+
refs: this.refsStore,
|
|
3757
|
+
...(tags ? { tags } : {}),
|
|
3758
|
+
...(environment?.replaceRefBase !== undefined
|
|
3759
|
+
? { replaceRefBase: environment.replaceRefBase }
|
|
3760
|
+
: {}),
|
|
3761
|
+
useReplaceRefs: this.useReplaceRefs(),
|
|
3762
|
+
grafted: new Set([...(replacements?.grafts.keys() ?? []), ...shallow]),
|
|
3763
|
+
...(objects ? { hasObject: (id: ObjectId) => objects.has(id) } : {}),
|
|
3764
|
+
onWarning: (message) => this.decorationWarnings.push(message),
|
|
3765
|
+
});
|
|
3766
|
+
}
|
|
645
3767
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
3768
|
+
/**
|
|
3769
|
+
* The commits of `head` not in `upstream`, merges dropped, oldest first: git's
|
|
3770
|
+
* `rev-list --reverse --topo-order --no-merges upstream..head` as its todo list is made
|
|
3771
|
+
* (`sort_in_topological_order` in graph order: a commit once all its children are out,
|
|
3772
|
+
* the last parent's line first, then reversed).
|
|
3773
|
+
*/
|
|
3774
|
+
private async commitsToRebase(
|
|
3775
|
+
head: ObjectId,
|
|
3776
|
+
upstream: ObjectId,
|
|
3777
|
+
keepMerges = false,
|
|
3778
|
+
): Promise<ObjectId[]> {
|
|
3779
|
+
const upstreamCommits = new Set<ObjectId>();
|
|
3780
|
+
const walk = [upstream];
|
|
3781
|
+
while (walk.length > 0) {
|
|
3782
|
+
const id = walk.pop() as ObjectId;
|
|
3783
|
+
if (upstreamCommits.has(id)) continue;
|
|
3784
|
+
upstreamCommits.add(id);
|
|
3785
|
+
walk.push(...((await this.loadCommit(id))?.parents ?? []));
|
|
3786
|
+
}
|
|
3787
|
+
const parents = new Map<ObjectId, ObjectId[]>();
|
|
3788
|
+
const stack = [head];
|
|
3789
|
+
while (stack.length > 0) {
|
|
3790
|
+
const id = stack.pop() as ObjectId;
|
|
3791
|
+
if (parents.has(id) || upstreamCommits.has(id)) continue;
|
|
3792
|
+
const commit = await this.loadCommit(id);
|
|
3793
|
+
if (!commit) continue;
|
|
3794
|
+
parents.set(id, commit.parents);
|
|
3795
|
+
stack.push(...commit.parents);
|
|
3796
|
+
}
|
|
3797
|
+
// the indegree within the range (1 + the children there), as git counts it
|
|
3798
|
+
const indegree = new Map<ObjectId, number>();
|
|
3799
|
+
for (const id of parents.keys()) indegree.set(id, 1);
|
|
3800
|
+
for (const list of parents.values()) {
|
|
3801
|
+
for (const p of list) {
|
|
3802
|
+
const d = indegree.get(p);
|
|
3803
|
+
if (d) indegree.set(p, d + 1);
|
|
3804
|
+
}
|
|
3805
|
+
}
|
|
3806
|
+
const order: ObjectId[] = [];
|
|
3807
|
+
const queue: ObjectId[] = [...parents.keys()].filter((id) => indegree.get(id) === 1);
|
|
3808
|
+
// graph order: the queue is a stack, the tips taken in the walk's order
|
|
3809
|
+
queue.reverse();
|
|
3810
|
+
while (queue.length > 0) {
|
|
3811
|
+
const id = queue.pop() as ObjectId;
|
|
3812
|
+
for (const p of parents.get(id) ?? []) {
|
|
3813
|
+
const d = indegree.get(p);
|
|
3814
|
+
if (!d) continue;
|
|
3815
|
+
indegree.set(p, d - 1);
|
|
3816
|
+
if (d - 1 === 1) queue.push(p);
|
|
649
3817
|
}
|
|
3818
|
+
indegree.set(id, 0);
|
|
3819
|
+
order.push(id);
|
|
650
3820
|
}
|
|
651
|
-
|
|
3821
|
+
order.reverse();
|
|
3822
|
+
return keepMerges ? order : order.filter((id) => (parents.get(id)?.length ?? 0) <= 1);
|
|
652
3823
|
}
|
|
653
3824
|
|
|
654
3825
|
/**
|
|
655
|
-
*
|
|
3826
|
+
* The commits of `range` (HEAD's side of `upstream...head`) whose patch-id one of
|
|
3827
|
+
* upstream's side has: git's `--cherry-mark` of the todo list (merges and commits that
|
|
3828
|
+
* change nothing never match). As git's cherry_pick_list and patch-ids.c: upstream's side
|
|
3829
|
+
* ends at the merge bases (not HEAD's whole history), every commit gets the cheap id of
|
|
3830
|
+
* its diff's headers, and the full patch-id is computed only where those collide.
|
|
656
3831
|
*/
|
|
657
|
-
private async
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
3832
|
+
private async appliedUpstream(
|
|
3833
|
+
head: ObjectId,
|
|
3834
|
+
upstream: ObjectId,
|
|
3835
|
+
range: ObjectId[],
|
|
3836
|
+
): Promise<Set<ObjectId>> {
|
|
3837
|
+
const skipped = new Set<ObjectId>();
|
|
3838
|
+
const left = await this.onlyReachableFrom(upstream, head);
|
|
3839
|
+
if (left.length === 0 || range.length === 0) return skipped;
|
|
3840
|
+
// each tree read once: a commit's tree is its child's parent tree
|
|
3841
|
+
const treeEntries = new Map<ObjectId, TreeEntry[]>();
|
|
3842
|
+
const trees: PatchIdTrees = {
|
|
3843
|
+
loadTree: async function* (this: void, id: ObjectId) {
|
|
3844
|
+
let entries = treeEntries.get(id);
|
|
3845
|
+
if (!entries) {
|
|
3846
|
+
entries = [];
|
|
3847
|
+
for await (const e of loadTree(id)) entries.push(e);
|
|
3848
|
+
treeEntries.set(id, entries);
|
|
3849
|
+
}
|
|
3850
|
+
yield* entries;
|
|
3851
|
+
},
|
|
3852
|
+
};
|
|
3853
|
+
const loadTree = (id: ObjectId) => this.trees.loadTree(id);
|
|
3854
|
+
const patchIdOf = async (id: ObjectId, headerOnly: boolean) => {
|
|
3855
|
+
const commit = await this.loadCommit(id);
|
|
3856
|
+
if (!commit || commit.parents.length > 1) return undefined;
|
|
3857
|
+
const parentTree = commit.parents[0]
|
|
3858
|
+
? (await this.loadCommit(commit.parents[0]))?.tree
|
|
3859
|
+
: undefined;
|
|
3860
|
+
return patchId(trees, this.blobs, parentTree, commit.tree, {
|
|
3861
|
+
headerOnly,
|
|
3862
|
+
objectFormat: this.objectFormat,
|
|
3863
|
+
});
|
|
3864
|
+
};
|
|
3865
|
+
const full = new Map<ObjectId, string | undefined>();
|
|
3866
|
+
const fullOf = async (id: ObjectId) => {
|
|
3867
|
+
if (!full.has(id)) full.set(id, await patchIdOf(id, false));
|
|
3868
|
+
return full.get(id);
|
|
3869
|
+
};
|
|
3870
|
+
// HEAD's side by header id
|
|
3871
|
+
const byHeader = new Map<string, ObjectId[]>();
|
|
3872
|
+
for (const id of range) {
|
|
3873
|
+
const header = await patchIdOf(id, true);
|
|
3874
|
+
if (!header) continue;
|
|
3875
|
+
const list = byHeader.get(header) ?? [];
|
|
3876
|
+
list.push(id);
|
|
3877
|
+
byHeader.set(header, list);
|
|
3878
|
+
}
|
|
3879
|
+
if (byHeader.size === 0) return skipped;
|
|
3880
|
+
for (const id of left) {
|
|
3881
|
+
const header = await patchIdOf(id, true);
|
|
3882
|
+
const candidates = header ? byHeader.get(header) : undefined;
|
|
3883
|
+
if (!candidates) continue;
|
|
3884
|
+
const mine = await fullOf(id);
|
|
3885
|
+
if (!mine) continue;
|
|
3886
|
+
for (const c of candidates) {
|
|
3887
|
+
if (!skipped.has(c) && (await fullOf(c)) === mine) skipped.add(c);
|
|
3888
|
+
}
|
|
3889
|
+
}
|
|
3890
|
+
return skipped;
|
|
3891
|
+
}
|
|
3892
|
+
|
|
3893
|
+
/**
|
|
3894
|
+
* The commits reachable from `tip` and not from `other` (`other..tip`), merges left out,
|
|
3895
|
+
* without walking `other`'s whole history: revision.c's limit_list, newest commit first,
|
|
3896
|
+
* what `other` reaches painted uninteresting, done once every commit still queued is
|
|
3897
|
+
* uninteresting (and a few more, git's slop against clock skew).
|
|
3898
|
+
*/
|
|
3899
|
+
private async onlyReachableFrom(tip: ObjectId, other: ObjectId): Promise<ObjectId[]> {
|
|
3900
|
+
interface Node {
|
|
3901
|
+
id: ObjectId;
|
|
3902
|
+
time: number;
|
|
3903
|
+
parents?: ObjectId[];
|
|
3904
|
+
uninteresting: boolean;
|
|
3905
|
+
queued: boolean;
|
|
661
3906
|
}
|
|
662
|
-
|
|
663
|
-
|
|
3907
|
+
const nodes = new Map<ObjectId, Node>();
|
|
3908
|
+
const queue: Node[] = [];
|
|
3909
|
+
/** The interesting commits still queued */
|
|
3910
|
+
let interesting = 0;
|
|
3911
|
+
const push = async (id: ObjectId, uninteresting: boolean) => {
|
|
3912
|
+
const known = nodes.get(id);
|
|
3913
|
+
if (known) {
|
|
3914
|
+
if (uninteresting) markUninteresting(known);
|
|
3915
|
+
return;
|
|
3916
|
+
}
|
|
3917
|
+
const commit = await this.loadCommit(id);
|
|
3918
|
+
if (!commit) return;
|
|
3919
|
+
const node: Node = { id, time: commit.committer.timestamp, uninteresting, queued: true };
|
|
3920
|
+
nodes.set(id, node);
|
|
3921
|
+
if (!uninteresting) interesting++;
|
|
3922
|
+
// newest first, ties in the order queued (commit_list_insert_by_date)
|
|
3923
|
+
let k = queue.length;
|
|
3924
|
+
while (k > 0 && queue[k - 1].time < node.time) k--;
|
|
3925
|
+
queue.splice(k, 0, node);
|
|
3926
|
+
};
|
|
3927
|
+
// mark_parents_uninteresting: through the commits already walked
|
|
3928
|
+
const markUninteresting = (start: Node) => {
|
|
3929
|
+
const stack = [start];
|
|
3930
|
+
while (stack.length > 0) {
|
|
3931
|
+
const n = stack.pop() as Node;
|
|
3932
|
+
if (n.uninteresting && n !== start) continue;
|
|
3933
|
+
if (!n.uninteresting && n.queued) interesting--;
|
|
3934
|
+
n.uninteresting = true;
|
|
3935
|
+
for (const p of n.parents ?? []) {
|
|
3936
|
+
const pn = nodes.get(p);
|
|
3937
|
+
if (pn && !pn.uninteresting) stack.push(pn);
|
|
3938
|
+
}
|
|
3939
|
+
}
|
|
3940
|
+
};
|
|
3941
|
+
await push(other, true);
|
|
3942
|
+
await push(tip, false);
|
|
3943
|
+
const out: Node[] = [];
|
|
3944
|
+
let slop = 5;
|
|
3945
|
+
while (queue.length > 0) {
|
|
3946
|
+
const node = queue.shift() as Node;
|
|
3947
|
+
node.queued = false;
|
|
3948
|
+
if (!node.uninteresting) interesting--;
|
|
3949
|
+
node.parents = (await this.loadCommit(node.id))?.parents ?? [];
|
|
3950
|
+
if (node.uninteresting) markUninteresting(node);
|
|
3951
|
+
else out.push(node);
|
|
3952
|
+
for (const p of node.parents) await push(p, node.uninteresting);
|
|
3953
|
+
if (interesting === 0) {
|
|
3954
|
+
if (--slop <= 0) break;
|
|
3955
|
+
} else slop = 5;
|
|
3956
|
+
}
|
|
3957
|
+
return out.filter((n) => !n.uninteresting && (n.parents?.length ?? 0) <= 1).map((n) => n.id);
|
|
3958
|
+
}
|
|
3959
|
+
|
|
3960
|
+
/** A commit, read once for the walks that pick the todo list. */
|
|
3961
|
+
private async loadCommit(id: ObjectId): Promise<Commit | undefined> {
|
|
3962
|
+
let commit = this.loadedCommits.get(id);
|
|
3963
|
+
if (!commit) {
|
|
3964
|
+
commit = await this.commits.load(id);
|
|
3965
|
+
if (commit) this.loadedCommits.set(id, commit);
|
|
3966
|
+
}
|
|
3967
|
+
return commit;
|
|
3968
|
+
}
|
|
3969
|
+
|
|
3970
|
+
/**
|
|
3971
|
+
* builtin/rebase.c's can_fast_forward for `git rebase <upstream>` (onto the upstream, no
|
|
3972
|
+
* fork point, no --keep-base): HEAD and upstream have one merge base, upstream itself, and
|
|
3973
|
+
* the first-parent line from HEAD down to it has no merge (is_linear_history). Upstream
|
|
3974
|
+
* in HEAD's history is that one merge base.
|
|
3975
|
+
*/
|
|
3976
|
+
private async canFastForward(onto: ObjectId, head: ObjectId): Promise<boolean> {
|
|
3977
|
+
if (!(await this.isAncestor(onto, head))) return false;
|
|
3978
|
+
let id: ObjectId | undefined = head;
|
|
3979
|
+
while (id !== undefined && id !== onto) {
|
|
3980
|
+
const parents: ObjectId[] = (await this.loadCommit(id))?.parents ?? [];
|
|
3981
|
+
if (parents.length > 1) return false;
|
|
3982
|
+
id = parents[0];
|
|
3983
|
+
}
|
|
3984
|
+
return true;
|
|
3985
|
+
}
|
|
3986
|
+
|
|
3987
|
+
private async isAncestor(a: ObjectId, b: ObjectId): Promise<boolean> {
|
|
3988
|
+
// the store's walk: commit-graph generations cut it short
|
|
3989
|
+
return this.commits.isAncestor(a, b);
|
|
3990
|
+
}
|
|
3991
|
+
}
|
|
3992
|
+
|
|
3993
|
+
/** The commands in a todo or done list (comments and blank lines not counted). */
|
|
3994
|
+
function countCommands(text: string): number {
|
|
3995
|
+
return text.split("\n").filter((l) => l.trim() !== "" && !l.trim().startsWith("#")).length;
|
|
3996
|
+
}
|
|
3997
|
+
|
|
3998
|
+
/** git's advice where a todo command could not run and was put back (`rescheduled_advice`). */
|
|
3999
|
+
function rescheduledAdvice(line: string): string {
|
|
4000
|
+
return [
|
|
4001
|
+
"hint: Could not execute the todo command",
|
|
4002
|
+
"hint:",
|
|
4003
|
+
`hint: ${line}`,
|
|
4004
|
+
"hint:",
|
|
4005
|
+
"hint: It has been rescheduled; To edit the command before continuing, please",
|
|
4006
|
+
"hint: edit the todo list first:",
|
|
4007
|
+
"hint:",
|
|
4008
|
+
"hint: git rebase --edit-todo",
|
|
4009
|
+
"hint: git rebase --continue",
|
|
4010
|
+
].join("\n");
|
|
4011
|
+
}
|
|
4012
|
+
|
|
4013
|
+
/** The author of git's `author-script` (`GIT_AUTHOR_NAME='...'`, ...). */
|
|
4014
|
+
function parseAuthorScript(text: string): PersonIdent | undefined {
|
|
4015
|
+
const values: Record<string, string> = {};
|
|
4016
|
+
for (const line of text.split("\n")) {
|
|
4017
|
+
const match = /^(GIT_AUTHOR_\w+)='(.*)'$/.exec(line);
|
|
4018
|
+
if (match) values[match[1]] = match[2].replaceAll("'\\''", "'");
|
|
4019
|
+
}
|
|
4020
|
+
const date = /^@?(\d+)\s+([+-]\d{4})$/.exec(values.GIT_AUTHOR_DATE ?? "");
|
|
4021
|
+
if (!values.GIT_AUTHOR_NAME || !date) return undefined;
|
|
4022
|
+
return {
|
|
4023
|
+
name: values.GIT_AUTHOR_NAME,
|
|
4024
|
+
email: values.GIT_AUTHOR_EMAIL ?? "",
|
|
4025
|
+
timestamp: Number(date[1]),
|
|
4026
|
+
tzOffset: date[2],
|
|
4027
|
+
};
|
|
4028
|
+
}
|
|
4029
|
+
|
|
4030
|
+
/** check_merge_commit_insn: a merge where a pick-like command cannot take one. */
|
|
4031
|
+
function mergeCommitErrors(action: RebaseAction): string[] {
|
|
4032
|
+
switch (action) {
|
|
4033
|
+
case RebaseAction.PICK:
|
|
4034
|
+
return [
|
|
4035
|
+
"error: 'pick' does not accept merge commits",
|
|
4036
|
+
"hint: 'pick' does not take a merge commit. If you wanted to",
|
|
4037
|
+
"hint: replay the merge, use 'merge -C' on the commit.",
|
|
4038
|
+
'hint: Disable this message with "git config set advice.rebaseTodoError false"',
|
|
4039
|
+
];
|
|
4040
|
+
case RebaseAction.REWORD:
|
|
4041
|
+
return [
|
|
4042
|
+
"error: 'reword' does not accept merge commits",
|
|
4043
|
+
"hint: 'reword' does not take a merge commit. If you wanted to",
|
|
4044
|
+
"hint: replay the merge and reword the commit message, use",
|
|
4045
|
+
"hint: 'merge -c' on the commit",
|
|
4046
|
+
'hint: Disable this message with "git config set advice.rebaseTodoError false"',
|
|
4047
|
+
];
|
|
4048
|
+
case RebaseAction.EDIT:
|
|
4049
|
+
return [
|
|
4050
|
+
"error: 'edit' does not accept merge commits",
|
|
4051
|
+
"hint: 'edit' does not take a merge commit. If you wanted to",
|
|
4052
|
+
"hint: replay the merge, use 'merge -C' on the commit, and then",
|
|
4053
|
+
"hint: 'break' to give the control back to you so that you can",
|
|
4054
|
+
"hint: do 'git commit --amend && git rebase --continue'.",
|
|
4055
|
+
'hint: Disable this message with "git config set advice.rebaseTodoError false"',
|
|
4056
|
+
];
|
|
4057
|
+
default:
|
|
4058
|
+
return ["error: cannot squash merge commit into another commit"];
|
|
4059
|
+
}
|
|
4060
|
+
}
|
|
4061
|
+
|
|
4062
|
+
/**
|
|
4063
|
+
* A message editor (the interactive handler's modifyCommitMessage) that threw, as git commit
|
|
4064
|
+
* failing on its editor: the pick counts as conflicted (do_pick_commit's `res = !!res`).
|
|
4065
|
+
*/
|
|
4066
|
+
function editorFailure(error: unknown): Extract<PickOutcome, { kind: "conflicts" }> {
|
|
4067
|
+
return {
|
|
4068
|
+
kind: "conflicts",
|
|
4069
|
+
conflicts: [],
|
|
4070
|
+
// git commit's line after the editor's
|
|
4071
|
+
lines: [
|
|
4072
|
+
editorProblem("modifyCommitMessage", error),
|
|
4073
|
+
"Please supply the message using either -m or -F option.",
|
|
4074
|
+
],
|
|
4075
|
+
};
|
|
4076
|
+
}
|
|
4077
|
+
|
|
4078
|
+
/** launch_sequence_editor failing (the interactive handler's editTodo or prepareSteps threw). */
|
|
4079
|
+
function sequenceEditorFailure(error: unknown): string {
|
|
4080
|
+
return error instanceof HandlerFailure
|
|
4081
|
+
? editorProblem(error.editor, error.reason)
|
|
4082
|
+
: editorProblem("editTodo", error);
|
|
4083
|
+
}
|
|
4084
|
+
|
|
4085
|
+
/**
|
|
4086
|
+
* editor.c's `there was a problem with the editor '<editor>'`: the editor named by the
|
|
4087
|
+
* interactive handler's method that threw, its reason after.
|
|
4088
|
+
*/
|
|
4089
|
+
function editorProblem(editor: string, error: unknown): string {
|
|
4090
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
4091
|
+
return `error: there was a problem with the editor '${editor}': ${reason}`;
|
|
4092
|
+
}
|
|
4093
|
+
|
|
4094
|
+
/** An interactive handler's method that threw, named. */
|
|
4095
|
+
class HandlerFailure extends Error {
|
|
4096
|
+
constructor(
|
|
4097
|
+
readonly editor: string,
|
|
4098
|
+
readonly reason: unknown,
|
|
4099
|
+
) {
|
|
4100
|
+
super(reason instanceof Error ? reason.message : String(reason));
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
|
|
4104
|
+
/**
|
|
4105
|
+
* The commit of `known` whose abbreviation `prefix` extends (at least as long as it, so no
|
|
4106
|
+
* other stored object starts with it), else undefined.
|
|
4107
|
+
*/
|
|
4108
|
+
function knownPrefix(prefix: string, known: Map<ObjectId, string>): ObjectId | undefined {
|
|
4109
|
+
for (const [id, short] of known) {
|
|
4110
|
+
if (prefix.length >= short.length && id.startsWith(prefix)) return id;
|
|
664
4111
|
}
|
|
4112
|
+
return undefined;
|
|
665
4113
|
}
|