@statewalker/vcs-commands 0.8.0 → 0.9.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/dist/commands/add-command.d.ts +32 -0
- package/dist/commands/add-command.d.ts.map +1 -1
- package/dist/commands/add-patch/add-patch.d.ts +7 -0
- package/dist/commands/add-patch/add-patch.d.ts.map +1 -1
- package/dist/commands/add-patch/patch-backend.d.ts.map +1 -1
- package/dist/commands/am/am-command.d.ts +47 -0
- package/dist/commands/am/am-command.d.ts.map +1 -1
- package/dist/commands/apply/apply-command.d.ts +2 -0
- package/dist/commands/apply/apply-command.d.ts.map +1 -1
- package/dist/commands/apply/apply-parse.d.ts +7 -0
- package/dist/commands/apply/apply-parse.d.ts.map +1 -1
- package/dist/commands/apply/index.d.ts +1 -1
- package/dist/commands/apply/index.d.ts.map +1 -1
- package/dist/commands/blame-command.d.ts +303 -8
- package/dist/commands/blame-command.d.ts.map +1 -1
- package/dist/commands/blame-ignore.d.ts +48 -0
- package/dist/commands/blame-ignore.d.ts.map +1 -0
- package/dist/commands/check-mailmap-command.d.ts +49 -0
- package/dist/commands/check-mailmap-command.d.ts.map +1 -0
- package/dist/commands/checkout-command.d.ts +7 -0
- package/dist/commands/checkout-command.d.ts.map +1 -1
- package/dist/commands/checkout-paths.d.ts +4 -0
- package/dist/commands/checkout-paths.d.ts.map +1 -1
- package/dist/commands/cherry-command.d.ts +61 -0
- package/dist/commands/cherry-command.d.ts.map +1 -0
- package/dist/commands/cherry-pick-command.d.ts +23 -1
- package/dist/commands/cherry-pick-command.d.ts.map +1 -1
- package/dist/commands/clean-command.d.ts +32 -0
- package/dist/commands/clean-command.d.ts.map +1 -1
- package/dist/commands/clean-interactive.d.ts +56 -0
- package/dist/commands/clean-interactive.d.ts.map +1 -0
- package/dist/commands/clone-command.d.ts +116 -2
- package/dist/commands/clone-command.d.ts.map +1 -1
- package/dist/commands/commit-command.d.ts +50 -2
- package/dist/commands/commit-command.d.ts.map +1 -1
- package/dist/commands/describe-command.d.ts +71 -64
- package/dist/commands/describe-command.d.ts.map +1 -1
- package/dist/commands/diff-command.d.ts +118 -6
- package/dist/commands/diff-command.d.ts.map +1 -1
- package/dist/commands/fetch-command.d.ts +60 -0
- package/dist/commands/fetch-command.d.ts.map +1 -1
- package/dist/commands/fetch-multiple-command.d.ts +193 -0
- package/dist/commands/fetch-multiple-command.d.ts.map +1 -0
- package/dist/commands/format-patch-command.d.ts +28 -2
- package/dist/commands/format-patch-command.d.ts.map +1 -1
- package/dist/commands/git-describe.d.ts +77 -4
- package/dist/commands/git-describe.d.ts.map +1 -1
- package/dist/commands/git-hooks.d.ts +15 -1
- package/dist/commands/git-hooks.d.ts.map +1 -1
- package/dist/commands/index.d.ts +12 -0
- package/dist/commands/index.d.ts.map +1 -1
- package/dist/commands/index.js +5 -3
- package/dist/commands/init-command.d.ts +89 -0
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/line-log.d.ts +161 -0
- package/dist/commands/line-log.d.ts.map +1 -0
- package/dist/commands/log-command.d.ts +235 -6
- package/dist/commands/log-command.d.ts.map +1 -1
- package/dist/commands/log-graph.d.ts +4 -2
- package/dist/commands/log-graph.d.ts.map +1 -1
- package/dist/commands/log-output.d.ts +66 -1
- package/dist/commands/log-output.d.ts.map +1 -1
- package/dist/commands/mailmap.d.ts +24 -1
- package/dist/commands/mailmap.d.ts.map +1 -1
- package/dist/commands/merge-command.d.ts +51 -1
- package/dist/commands/merge-command.d.ts.map +1 -1
- package/dist/commands/merge-drivers.d.ts +2 -0
- package/dist/commands/merge-drivers.d.ts.map +1 -1
- package/dist/commands/merge-strategy-options.d.ts +69 -0
- package/dist/commands/merge-strategy-options.d.ts.map +1 -0
- package/dist/commands/merge-support.d.ts +10 -0
- package/dist/commands/merge-support.d.ts.map +1 -1
- package/dist/commands/message-cleanup.d.ts +2 -0
- package/dist/commands/message-cleanup.d.ts.map +1 -1
- package/dist/commands/name-rev-command.d.ts +65 -0
- package/dist/commands/name-rev-command.d.ts.map +1 -0
- package/dist/commands/name-rev.d.ts +110 -22
- package/dist/commands/name-rev.d.ts.map +1 -1
- package/dist/commands/octopus-merge.d.ts +26 -1
- package/dist/commands/octopus-merge.d.ts.map +1 -1
- package/dist/commands/ort-merge.d.ts +16 -0
- package/dist/commands/ort-merge.d.ts.map +1 -1
- package/dist/commands/ort-recursive.d.ts.map +1 -1
- package/dist/commands/patch-id.d.ts +58 -0
- package/dist/commands/patch-id.d.ts.map +1 -1
- package/dist/commands/pull-command.d.ts +182 -38
- package/dist/commands/pull-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts +65 -0
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/commands/range-diff/index.d.ts +4 -0
- package/dist/commands/range-diff/index.d.ts.map +1 -0
- package/dist/commands/range-diff/linear-assignment.d.ts +27 -0
- package/dist/commands/range-diff/linear-assignment.d.ts.map +1 -0
- package/dist/commands/range-diff/range-diff-command.d.ts +121 -0
- package/dist/commands/range-diff/range-diff-command.d.ts.map +1 -0
- package/dist/commands/range-diff/range-diff.d.ts +106 -0
- package/dist/commands/range-diff/range-diff.d.ts.map +1 -0
- package/dist/commands/read-tree-command.d.ts +14 -0
- package/dist/commands/read-tree-command.d.ts.map +1 -1
- package/dist/commands/rebase-command.d.ts +21 -2
- package/dist/commands/rebase-command.d.ts.map +1 -1
- package/dist/commands/remerge-diff.d.ts +59 -0
- package/dist/commands/remerge-diff.d.ts.map +1 -0
- package/dist/commands/remote-command.d.ts +41 -0
- package/dist/commands/remote-command.d.ts.map +1 -1
- package/dist/commands/remote-show-command.d.ts +204 -0
- package/dist/commands/remote-show-command.d.ts.map +1 -0
- package/dist/commands/reset-command.d.ts +19 -1
- package/dist/commands/reset-command.d.ts.map +1 -1
- package/dist/commands/restore-command.d.ts +7 -0
- package/dist/commands/restore-command.d.ts.map +1 -1
- package/dist/commands/rev-list-command.d.ts +75 -3
- package/dist/commands/rev-list-command.d.ts.map +1 -1
- package/dist/commands/revert-command.d.ts +22 -1
- package/dist/commands/revert-command.d.ts.map +1 -1
- package/dist/commands/revision-range.d.ts +2 -0
- package/dist/commands/revision-range.d.ts.map +1 -1
- package/dist/commands/sequencer-pick.d.ts +102 -1
- package/dist/commands/sequencer-pick.d.ts.map +1 -1
- package/dist/commands/sequencer-support.d.ts +10 -1
- package/dist/commands/sequencer-support.d.ts.map +1 -1
- package/dist/commands/shortlog-command.d.ts +109 -0
- package/dist/commands/shortlog-command.d.ts.map +1 -0
- package/dist/commands/shortlog.d.ts +151 -0
- package/dist/commands/shortlog.d.ts.map +1 -0
- package/dist/commands/status-command.d.ts +31 -2
- package/dist/commands/status-command.d.ts.map +1 -1
- package/dist/commands/submodule-commands.d.ts +90 -2
- package/dist/commands/submodule-commands.d.ts.map +1 -1
- package/dist/commands/submodule-fetch.d.ts +149 -0
- package/dist/commands/submodule-fetch.d.ts.map +1 -0
- package/dist/commands/submodule-model.d.ts +8 -0
- package/dist/commands/submodule-model.d.ts.map +1 -1
- package/dist/commands/submodule-unpack.d.ts +120 -0
- package/dist/commands/submodule-unpack.d.ts.map +1 -0
- package/dist/commands/submodule-update-command.d.ts +8 -0
- package/dist/commands/submodule-update-command.d.ts.map +1 -1
- package/dist/commands/subtree-shift.d.ts +27 -0
- package/dist/commands/subtree-shift.d.ts.map +1 -0
- package/dist/commands/switch-command.d.ts +3 -0
- package/dist/commands/switch-command.d.ts.map +1 -1
- package/dist/commands/tree-restore.d.ts.map +1 -1
- package/dist/commands/worktree-overwrite-checks.d.ts +2 -0
- package/dist/commands/worktree-overwrite-checks.d.ts.map +1 -1
- package/dist/{commands-ISM52N0n.js → commands-CNvli5bl.js} +39426 -61169
- package/dist/errors/command-errors.d.ts +25 -0
- package/dist/errors/command-errors.d.ts.map +1 -1
- package/dist/errors/index.js +29 -1
- package/dist/fsck-CPeDh9fh.js +5138 -0
- package/dist/git-command.d.ts +31 -1
- package/dist/git-command.d.ts.map +1 -1
- package/dist/git.d.ts +97 -1
- package/dist/git.d.ts.map +1 -1
- package/dist/guarded-transport-command.d.ts +82 -0
- package/dist/guarded-transport-command.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -5
- package/dist/remote-transport-CCmBadm-.js +1606 -0
- package/dist/rename/detect-renames.d.ts +20 -0
- package/dist/rename/detect-renames.d.ts.map +1 -1
- package/dist/rename/diffcore-break.d.ts +51 -0
- package/dist/rename/diffcore-break.d.ts.map +1 -0
- package/dist/rename/index.d.ts +1 -0
- package/dist/rename/index.d.ts.map +1 -1
- package/dist/results/blame-format.d.ts +62 -1
- package/dist/results/blame-format.d.ts.map +1 -1
- package/dist/results/clone-result.d.ts +9 -0
- package/dist/results/clone-result.d.ts.map +1 -1
- package/dist/results/diff-dirstat.d.ts +36 -0
- package/dist/results/diff-dirstat.d.ts.map +1 -0
- package/dist/results/diff-entry.d.ts +10 -0
- package/dist/results/diff-entry.d.ts.map +1 -1
- package/dist/results/diff-formatter.d.ts.map +1 -1
- package/dist/results/diff-output.d.ts +18 -0
- package/dist/results/diff-output.d.ts.map +1 -1
- package/dist/results/fetch-result.d.ts +12 -1
- package/dist/results/fetch-result.d.ts.map +1 -1
- package/dist/results/index.d.ts +2 -0
- package/dist/results/index.d.ts.map +1 -1
- package/dist/results/index.js +2 -2
- package/dist/results/init-result.d.ts +8 -1
- package/dist/results/init-result.d.ts.map +1 -1
- package/dist/results/line-range-hunks.d.ts +27 -0
- package/dist/results/line-range-hunks.d.ts.map +1 -0
- package/dist/results/merge-result.d.ts +8 -1
- package/dist/results/merge-result.d.ts.map +1 -1
- package/dist/results/pull-result.d.ts +11 -0
- package/dist/results/pull-result.d.ts.map +1 -1
- package/dist/results/push-result.d.ts +5 -0
- package/dist/results/push-result.d.ts.map +1 -1
- package/dist/results/status-result.d.ts +1 -1
- package/dist/results/status-result.d.ts.map +1 -1
- package/dist/results-BDuEgdLU.js +46847 -0
- package/dist/transport-command.d.ts +14 -0
- package/dist/transport-command.d.ts.map +1 -1
- package/package.json +12 -12
- package/src/commands/add-command.ts +115 -0
- package/src/commands/add-patch/add-patch.ts +8 -0
- package/src/commands/add-patch/patch-backend.ts +20 -2
- package/src/commands/am/am-command.ts +138 -4
- package/src/commands/apply/apply-command.ts +52 -9
- package/src/commands/apply/apply-parse.ts +10 -0
- package/src/commands/apply/index.ts +8 -1
- package/src/commands/blame-command.ts +1450 -51
- package/src/commands/blame-ignore.ts +307 -0
- package/src/commands/check-mailmap-command.ts +129 -0
- package/src/commands/checkout-command.ts +97 -12
- package/src/commands/checkout-paths.ts +28 -0
- package/src/commands/cherry-command.ts +187 -0
- package/src/commands/cherry-pick-command.ts +42 -1
- package/src/commands/clean-command.ts +92 -12
- package/src/commands/clean-interactive.ts +522 -0
- package/src/commands/clone-command.ts +596 -44
- package/src/commands/commit-command.ts +180 -47
- package/src/commands/describe-command.ts +341 -254
- package/src/commands/diff-command.ts +616 -56
- package/src/commands/fetch-command.ts +353 -6
- package/src/commands/fetch-multiple-command.ts +665 -0
- package/src/commands/format-patch-command.ts +189 -77
- package/src/commands/git-describe.ts +456 -217
- package/src/commands/git-hooks.ts +42 -3
- package/src/commands/index.ts +17 -0
- package/src/commands/init-command.ts +603 -49
- package/src/commands/line-log.ts +931 -0
- package/src/commands/log-command.ts +1414 -177
- package/src/commands/log-graph.ts +6 -3
- package/src/commands/log-output.ts +173 -10
- package/src/commands/mailmap.ts +41 -5
- package/src/commands/merge-command.ts +274 -16
- package/src/commands/merge-drivers.ts +3 -0
- package/src/commands/merge-strategy-options.ts +224 -0
- package/src/commands/merge-support.ts +101 -21
- package/src/commands/message-cleanup.ts +1 -1
- package/src/commands/name-rev-command.ts +184 -0
- package/src/commands/name-rev.ts +700 -109
- package/src/commands/octopus-merge.ts +35 -11
- package/src/commands/ort-merge.ts +41 -7
- package/src/commands/ort-recursive.ts +40 -2
- package/src/commands/patch-id.ts +109 -0
- package/src/commands/pull-command.ts +718 -224
- package/src/commands/push-command.ts +353 -3
- package/src/commands/range-diff/index.ts +3 -0
- package/src/commands/range-diff/linear-assignment.ts +203 -0
- package/src/commands/range-diff/range-diff-command.ts +433 -0
- package/src/commands/range-diff/range-diff.ts +617 -0
- package/src/commands/read-tree-command.ts +74 -3
- package/src/commands/rebase-command.ts +141 -65
- package/src/commands/remerge-diff.ts +241 -0
- package/src/commands/remote-command.ts +98 -10
- package/src/commands/remote-show-command.ts +836 -0
- package/src/commands/reset-command.ts +78 -3
- package/src/commands/restore-command.ts +15 -0
- package/src/commands/rev-list-command.ts +187 -11
- package/src/commands/revert-command.ts +41 -1
- package/src/commands/revision-range.ts +3 -1
- package/src/commands/sequencer-pick.ts +323 -29
- package/src/commands/sequencer-support.ts +164 -2
- package/src/commands/shortlog-command.ts +354 -0
- package/src/commands/shortlog.ts +361 -0
- package/src/commands/show-command.ts +2 -1
- package/src/commands/status-command.ts +115 -25
- package/src/commands/status-format.ts +1 -1
- package/src/commands/submodule-commands.ts +321 -3
- package/src/commands/submodule-fetch.ts +789 -0
- package/src/commands/submodule-model.ts +9 -1
- package/src/commands/submodule-unpack.ts +265 -0
- package/src/commands/submodule-update-command.ts +39 -5
- package/src/commands/subtree-shift.ts +215 -0
- package/src/commands/switch-command.ts +9 -0
- package/src/commands/tree-restore.ts +6 -1
- package/src/commands/worktree-overwrite-checks.ts +5 -0
- package/src/errors/command-errors.ts +35 -0
- package/src/git-command.ts +87 -1
- package/src/git.ts +146 -0
- package/src/guarded-transport-command.ts +171 -0
- package/src/index.ts +1 -0
- package/src/rename/detect-renames.ts +42 -8
- package/src/rename/diffcore-break.ts +166 -0
- package/src/rename/index.ts +1 -0
- package/src/results/blame-format.ts +268 -43
- package/src/results/clone-result.ts +6 -0
- package/src/results/diff-dirstat.ts +117 -0
- package/src/results/diff-entry.ts +12 -0
- package/src/results/diff-formatter.ts +1 -0
- package/src/results/diff-output.ts +127 -2
- package/src/results/fetch-result.ts +12 -1
- package/src/results/index.ts +2 -0
- package/src/results/init-result.ts +10 -1
- package/src/results/line-range-hunks.ts +126 -0
- package/src/results/merge-result.ts +9 -0
- package/src/results/pull-result.ts +7 -0
- package/src/results/push-result.ts +5 -0
- package/src/results/status-result.ts +2 -2
- package/src/transport-command.ts +34 -3
- package/dist/fsck-DLRpbwTj.js +0 -13405
- package/dist/results-TB2IJ64h.js +0 -4700
|
@@ -4,15 +4,25 @@ import {
|
|
|
4
4
|
type FilesApi,
|
|
5
5
|
type History,
|
|
6
6
|
type PosixFilesApi,
|
|
7
|
+
readFile,
|
|
7
8
|
} from "@statewalker/vcs-core";
|
|
8
9
|
import {
|
|
10
|
+
configFilesApiOf,
|
|
9
11
|
createFileWorktree,
|
|
10
12
|
createGitFilesHistoryFromFiles,
|
|
13
|
+
createWorkingCopyConfig,
|
|
11
14
|
FileStagingStore,
|
|
15
|
+
HostPaths,
|
|
16
|
+
hostPathOf,
|
|
17
|
+
hostRootOf,
|
|
18
|
+
isAbsoluteGitPath,
|
|
19
|
+
openGitLinkedWorktrees,
|
|
20
|
+
parseGitfile,
|
|
12
21
|
probeCaseFolding,
|
|
13
22
|
readProtectedConfig,
|
|
14
23
|
readRepositoryFormat,
|
|
15
24
|
resolveLinkedGitDir,
|
|
25
|
+
setConfigMultivarText,
|
|
16
26
|
} from "@statewalker/vcs-store-files";
|
|
17
27
|
import { MemoryStagingStore } from "@statewalker/vcs-store-mem";
|
|
18
28
|
import {
|
|
@@ -22,6 +32,17 @@ import {
|
|
|
22
32
|
objectFormatByName,
|
|
23
33
|
SHA1_OBJECT_FORMAT,
|
|
24
34
|
} from "@statewalker/vcs-utils";
|
|
35
|
+
import {
|
|
36
|
+
adjustSharedPerm,
|
|
37
|
+
dirname,
|
|
38
|
+
joinPath,
|
|
39
|
+
normalizePath,
|
|
40
|
+
PERM_GROUP,
|
|
41
|
+
parseSharedRepository,
|
|
42
|
+
sharedRepositoryConfigValue,
|
|
43
|
+
sharedRepositoryOf,
|
|
44
|
+
withSharedPerm,
|
|
45
|
+
} from "@statewalker/vcs-utils/files";
|
|
25
46
|
import {
|
|
26
47
|
createMemoryCheckout,
|
|
27
48
|
type GitHostEnvironment,
|
|
@@ -91,8 +112,73 @@ export class InitCommand {
|
|
|
91
112
|
private objectFormatName?: string;
|
|
92
113
|
/** `--ref-format`; undefined: git's default (see {@link defaultRefFormat}) */
|
|
93
114
|
private refFormat?: string;
|
|
115
|
+
/** `--separate-git-dir`: the repository's place, `.git` then a gitfile naming it */
|
|
116
|
+
private separateGitDir?: string;
|
|
117
|
+
/** `--template`: the directory copied into the repository ("" copies none) */
|
|
118
|
+
private template?: { path: string; files?: FilesApi };
|
|
119
|
+
/** `--shared` as git_config_perm reads it; undefined: core.sharedRepository decides */
|
|
120
|
+
private shared?: number;
|
|
94
121
|
private callable = true;
|
|
95
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Put the repository at `path` (`git init --separate-git-dir <path>`, a path of the
|
|
125
|
+
* FilesApi) and write a gitfile in its place (`<directory>/.git`: `gitdir: <path>`, the
|
|
126
|
+
* path as the host names it: under the FilesApi's `rootDirectory`, its links resolved
|
|
127
|
+
* through `GitHostEnvironment.realPath`). An existing repository there (a `.git`
|
|
128
|
+
* directory, or the one a `.git` gitfile names) is moved to `path` first, as git's
|
|
129
|
+
* re-init moves it, and its linked worktrees are pointed at the new place. Refused for a
|
|
130
|
+
* bare repository (`InitOptionsError`).
|
|
131
|
+
*
|
|
132
|
+
* @param path The git directory's place
|
|
133
|
+
* @returns this for method chaining
|
|
134
|
+
*/
|
|
135
|
+
setSeparateGitDir(path: string): this {
|
|
136
|
+
this.checkCallable();
|
|
137
|
+
this.separateGitDir = path;
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Copy the templates of `path` into the repository (`git init --template=<path>`): every
|
|
143
|
+
* file, directory and symbolic link not starting with `.`, an existing file never
|
|
144
|
+
* replaced (a template `config` included), executables made executable (hooks). `path`
|
|
145
|
+
* is a directory of `files` (default: the command's FilesApi); "" copies nothing. Without
|
|
146
|
+
* it, `$GIT_TEMPLATE_DIR` (`GitHostEnvironment.templateDir`) then `init.templateDir`
|
|
147
|
+
* from the system or global configuration (or `git -c`), read through
|
|
148
|
+
* `GitHostEnvironment.hostFilesOf`; ours has no built-in templates.
|
|
149
|
+
*
|
|
150
|
+
* @param path The template directory, or "" for none
|
|
151
|
+
* @param files The storage holding it
|
|
152
|
+
* @returns this for method chaining
|
|
153
|
+
*/
|
|
154
|
+
setTemplate(path: string, files?: FilesApi): this {
|
|
155
|
+
this.checkCallable();
|
|
156
|
+
this.template = files ? { path, files } : { path };
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Make the repository shared (`git init --shared[=<permissions>]`): true or no value is
|
|
162
|
+
* `group`; a string is what git accepts (`false`/`umask`, `true`/`group`, `all`/`world`/
|
|
163
|
+
* `everybody`, an octal mode such as `0640`). Written as `core.sharedRepository` (with
|
|
164
|
+
* `receive.denyNonFastforwards`), and the files and directories created get git's modes
|
|
165
|
+
* (`adjust_shared_perm`) where the FilesApi keeps modes. Without it,
|
|
166
|
+
* `core.sharedRepository` from the configuration (the template's included) decides.
|
|
167
|
+
*
|
|
168
|
+
* @throws SharedRepositoryModeError for an octal mode without owner read and write
|
|
169
|
+
* @returns this for method chaining
|
|
170
|
+
*/
|
|
171
|
+
setShared(permissions: string | boolean = true): this {
|
|
172
|
+
this.checkCallable();
|
|
173
|
+
this.shared =
|
|
174
|
+
permissions === true
|
|
175
|
+
? PERM_GROUP
|
|
176
|
+
: permissions === false
|
|
177
|
+
? 0
|
|
178
|
+
: parseSharedRepository(permissions);
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
|
|
96
182
|
/**
|
|
97
183
|
* Set the object format of the new repository (`git init --object-format=<format>`):
|
|
98
184
|
* `sha1` (git's default) or `sha256`. Without it, `$GIT_DEFAULT_HASH`
|
|
@@ -330,15 +416,57 @@ export class InitCommand {
|
|
|
330
416
|
// one it stays in memory.
|
|
331
417
|
let history: History;
|
|
332
418
|
let staging: Staging;
|
|
419
|
+
let resultGitDir = gitDir;
|
|
420
|
+
// re-init from a linked worktree: its administrative directory (the common one gets
|
|
421
|
+
// the templates, config, objects and refs)
|
|
422
|
+
let linkedAdmin: string | undefined;
|
|
423
|
+
// the worktree's top: the directory asked for, or the main worktree when
|
|
424
|
+
// --separate-git-dir was given inside a linked one
|
|
425
|
+
let worktreeRoot = (this.directory ?? "").replace(/\/+$/, "");
|
|
426
|
+
let reinitialized = false;
|
|
427
|
+
let shared = 0;
|
|
333
428
|
if (this.files) {
|
|
334
|
-
|
|
429
|
+
if (this.separateGitDir !== undefined && this.bare) {
|
|
430
|
+
throw new InitOptionsError(
|
|
431
|
+
"fatal: options '--separate-git-dir' and '--bare' cannot be used together",
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
// cmd_init_db's paths: the git directory, or with --separate-git-dir the gitfile's place
|
|
435
|
+
// and the directory it names (an existing repository moved there first, as
|
|
436
|
+
// separate_git_dir renames it); a `.git` gitfile names the repository re-initialised
|
|
437
|
+
let dir = gitDir.replace(/\/+$/, "") || ".";
|
|
438
|
+
if (this.separateGitDir !== undefined) {
|
|
439
|
+
const real = this.separateGitDir.replace(/\/+$/, "") || ".";
|
|
440
|
+
const { worktreeTop } = await this.separateGitDirectory(files, dir, real, warnings);
|
|
441
|
+
if (worktreeTop !== undefined) worktreeRoot = worktreeTop;
|
|
442
|
+
dir = real;
|
|
443
|
+
} else if (!this.bare) {
|
|
444
|
+
// re-init through a gitfile: the repository it names, a linked worktree's common
|
|
445
|
+
// directory (templates, config, objects and refs live there)
|
|
446
|
+
const linked = await gitfileTargetOf(files, dir);
|
|
447
|
+
if (linked !== undefined) {
|
|
448
|
+
if (!linked.reachable) {
|
|
449
|
+
throw new InitOptionsError(
|
|
450
|
+
`fatal: the repository '${linked.path}' the gitfile '${dir}' names lies outside the storage`,
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
const common = await commonDirectoryOf(files, linked.path);
|
|
454
|
+
// a linked worktree's own git directory: HEAD, index and the working copy are its
|
|
455
|
+
if (common !== undefined) linkedAdmin = linked.path;
|
|
456
|
+
dir = common ?? linked.path;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
// a relative path stays relative (gitfile targets come back rooted)
|
|
460
|
+
resultGitDir = gitDir.startsWith("/") ? dir : dir.replace(/^\/+/, "") || ".";
|
|
335
461
|
// a `.git` the user linked elsewhere is followed (resolved through the FilesApi)
|
|
336
462
|
const repoFiles = await resolveLinkedGitDir(files, dir);
|
|
337
463
|
const isFile = async (path: string) => (await repoFiles.stats(path))?.kind === "file";
|
|
338
464
|
const hasHead = await isFile(`${dir}/HEAD`);
|
|
339
465
|
// A repository with a config but no HEAD (damaged or half-made) is opened, not
|
|
340
466
|
// re-created: creating rewrites the config, and its remotes and settings with it.
|
|
341
|
-
const
|
|
467
|
+
const hadConfig = await isFile(`${dir}/config`);
|
|
468
|
+
const fresh = !hasHead && !hadConfig;
|
|
469
|
+
reinitialized = hasHead;
|
|
342
470
|
if (fresh) {
|
|
343
471
|
objectFormat = explicitFormat ?? (await this.defaultObjectFormat(warnings));
|
|
344
472
|
} else {
|
|
@@ -358,26 +486,92 @@ export class InitCommand {
|
|
|
358
486
|
? await defaultRefFormat(this.environment, this.refFormat)
|
|
359
487
|
: undefined;
|
|
360
488
|
if (refFormat) warnings.push(...refFormat.warnings);
|
|
489
|
+
|
|
490
|
+
// core.sharedRepository: --shared, else the configuration (the repository's own when it
|
|
491
|
+
// exists); what init creates gets git's modes (adjust_shared_perm)
|
|
492
|
+
shared = this.shared ?? (await this.configuredShared(repoFiles, dir));
|
|
493
|
+
const sharedFiles = withSharedPerm(repoFiles, () => shared, dir);
|
|
494
|
+
// safe_create_dir(git_dir, 0): made under the umask, adjusted below
|
|
495
|
+
await repoFiles.mkdir(dir);
|
|
496
|
+
// copy_templates: the template's config may set core.sharedRepository, read after it
|
|
497
|
+
await this.copyTemplates(sharedFiles, dir, warnings);
|
|
498
|
+
shared = this.shared ?? (await this.configuredShared(repoFiles, dir));
|
|
499
|
+
if (shared) await adjustSharedPerm(repoFiles, dir, shared);
|
|
500
|
+
|
|
501
|
+
// the config, set as create_default_files and init_db set it
|
|
502
|
+
const configPath = `${dir}/config`;
|
|
503
|
+
const configExisted = await isFile(configPath);
|
|
504
|
+
const readText = async () =>
|
|
505
|
+
configExisted || (await isFile(configPath))
|
|
506
|
+
? decodeGitPath(await readFile(repoFiles, configPath))
|
|
507
|
+
: undefined;
|
|
508
|
+
const fullConfig = await createWorkingCopyConfig(
|
|
509
|
+
configFilesApiOf(repoFiles, { readOnly: "init reads the configuration only" }),
|
|
510
|
+
dir,
|
|
511
|
+
undefined,
|
|
512
|
+
this.environment ? { environment: this.environment } : {},
|
|
513
|
+
);
|
|
514
|
+
let text = await readText();
|
|
515
|
+
const set = (key: string, value: string) => {
|
|
516
|
+
const result = setConfigMultivarText(text, configPath, key, value);
|
|
517
|
+
if (result.text !== undefined) text = result.text;
|
|
518
|
+
};
|
|
519
|
+
const refStorage = fresh ? (refFormat?.format ?? "files") : undefined;
|
|
520
|
+
if (fresh && objectFormat.name !== "sha1") set("extensions.objectformat", objectFormat.name);
|
|
521
|
+
if (refStorage === "reftable") set("extensions.refstorage", "reftable");
|
|
522
|
+
const v1 =
|
|
523
|
+
(fresh && (objectFormat.name !== "sha1" || refStorage === "reftable")) ||
|
|
524
|
+
(!fresh && /^\s*\[\s*extensions\s*\]/im.test(text ?? ""));
|
|
525
|
+
set("core.repositoryformatversion", v1 ? "1" : "0");
|
|
526
|
+
set("core.filemode", "true");
|
|
527
|
+
set("core.bare", this.bare ? "true" : "false");
|
|
528
|
+
// a template's (or the user's) core.logAllRefUpdates is kept
|
|
529
|
+
if (!this.bare && fullConfig.get("core.logAllRefUpdates") === undefined) {
|
|
530
|
+
set("core.logallrefupdates", "true");
|
|
531
|
+
}
|
|
532
|
+
const writeConfig = async () => {
|
|
533
|
+
const bytes = encodeGitPath(text ?? "");
|
|
534
|
+
// a new config is created as a tempfile (adjusted); a rewrite keeps the file's mode
|
|
535
|
+
if (configExisted) await repoFiles.write(configPath, [bytes]);
|
|
536
|
+
else await sharedFiles.write(configPath, [bytes]);
|
|
537
|
+
};
|
|
538
|
+
await writeConfig();
|
|
539
|
+
if (fresh) {
|
|
540
|
+
// git init's probe: a filesystem that folds case or Unicode composition
|
|
541
|
+
const folding = await probeCaseFolding(repoFiles as PosixFilesApi, dir);
|
|
542
|
+
if (folding.ignoreCase) set("core.ignorecase", "true");
|
|
543
|
+
if (folding.precomposeUnicode) set("core.precomposeunicode", "true");
|
|
544
|
+
}
|
|
545
|
+
const sharedValue = sharedRepositoryConfigValue(shared);
|
|
546
|
+
if (sharedValue !== undefined) {
|
|
547
|
+
set("core.sharedrepository", sharedValue);
|
|
548
|
+
set("receive.denyNonFastforwards", "true");
|
|
549
|
+
}
|
|
550
|
+
await writeConfig();
|
|
551
|
+
|
|
361
552
|
const open = (create: boolean) =>
|
|
362
553
|
createGitFilesHistoryFromFiles({
|
|
363
|
-
files:
|
|
364
|
-
gitDir,
|
|
554
|
+
files: sharedFiles,
|
|
555
|
+
gitDir: dir,
|
|
365
556
|
create,
|
|
557
|
+
writeConfig: false,
|
|
366
558
|
defaultBranch: initialBranch,
|
|
367
559
|
...(create ? { objectFormat } : {}),
|
|
368
560
|
...(create && refFormat ? { refFormat: refFormat.format } : {}),
|
|
369
561
|
...(this.environment ? { environment: this.environment } : {}),
|
|
370
562
|
});
|
|
371
|
-
|
|
372
|
-
// The directory layout writes `core.bare = true` only for a gitDir of "."; set it
|
|
373
|
-
// before opening, so what reads the config (reflog defaults) sees a bare repository.
|
|
374
|
-
await open(true);
|
|
375
|
-
await markBare(repoFiles, `${dir}/config`);
|
|
376
|
-
history = await open(false);
|
|
377
|
-
} else {
|
|
378
|
-
history = await open(fresh);
|
|
379
|
-
}
|
|
563
|
+
history = await open(fresh);
|
|
380
564
|
await history.initialize();
|
|
565
|
+
// files_ref_store_create_on_disk: refs (adjusted even when it exists), refs/heads and
|
|
566
|
+
// refs/tags made where missing
|
|
567
|
+
if (!fresh && (await readRepositoryFormat(repoFiles, dir)).refStorage === "files") {
|
|
568
|
+
await sharedFiles.mkdir(`${dir}/refs/heads`);
|
|
569
|
+
await sharedFiles.mkdir(`${dir}/refs/tags`);
|
|
570
|
+
}
|
|
571
|
+
if (shared) await adjustSharedPerm(repoFiles, `${dir}/refs`, shared);
|
|
572
|
+
// create_object_database: objects/pack and objects/info, made where missing
|
|
573
|
+
await sharedFiles.mkdir(`${dir}/objects/pack`);
|
|
574
|
+
await sharedFiles.mkdir(`${dir}/objects/info`);
|
|
381
575
|
if (hasHead) {
|
|
382
576
|
// the branch an existing repository's HEAD names (empty when detached)
|
|
383
577
|
const head = await history.refs.get("HEAD");
|
|
@@ -386,15 +580,6 @@ export class InitCommand {
|
|
|
386
580
|
? head.target.slice("refs/heads/".length)
|
|
387
581
|
: "";
|
|
388
582
|
}
|
|
389
|
-
if (fresh) {
|
|
390
|
-
// git init's probe: a filesystem that folds case or Unicode composition
|
|
391
|
-
const folding = await probeCaseFolding(repoFiles as PosixFilesApi, dir);
|
|
392
|
-
if (folding.ignoreCase)
|
|
393
|
-
await setCoreValue(repoFiles, `${dir}/config`, "ignorecase", "true");
|
|
394
|
-
if (folding.precomposeUnicode) {
|
|
395
|
-
await setCoreValue(repoFiles, `${dir}/config`, "precomposeunicode", "true");
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
583
|
if (!hasHead && !fresh) {
|
|
399
584
|
await history.refs.setSymbolic("HEAD", `refs/heads/${initialBranch}`);
|
|
400
585
|
}
|
|
@@ -405,7 +590,7 @@ export class InitCommand {
|
|
|
405
590
|
} else if (this.bare) {
|
|
406
591
|
staging = new MemoryStagingStore();
|
|
407
592
|
} else {
|
|
408
|
-
const fileStaging = new FileStagingStore(
|
|
593
|
+
const fileStaging = new FileStagingStore(sharedFiles, `${dir}/index`, { objectFormat });
|
|
409
594
|
// a SHA-256 repository's index is not read yet: its first use refuses
|
|
410
595
|
if (objectFormat.name === "sha1") await fileStaging.read();
|
|
411
596
|
staging = fileStaging;
|
|
@@ -413,6 +598,7 @@ export class InitCommand {
|
|
|
413
598
|
} else {
|
|
414
599
|
objectFormat = explicitFormat ?? (await this.defaultObjectFormat(warnings));
|
|
415
600
|
await chooseBranch();
|
|
601
|
+
shared = this.shared ?? 0;
|
|
416
602
|
history = createMemoryHistory({ objectFormat });
|
|
417
603
|
await history.initialize();
|
|
418
604
|
// Set initial branch by creating symbolic ref for HEAD
|
|
@@ -420,6 +606,31 @@ export class InitCommand {
|
|
|
420
606
|
staging = this.stagingStore ?? new MemoryStagingStore();
|
|
421
607
|
}
|
|
422
608
|
|
|
609
|
+
if (linkedAdmin !== undefined && this.files) {
|
|
610
|
+
// git's re-init works in the linked worktree: its HEAD, its index, its files
|
|
611
|
+
const relative = (p: string) => (gitDir.startsWith("/") ? p : p.replace(/^\/+/, "") || ".");
|
|
612
|
+
const worktrees = await openGitLinkedWorktrees({
|
|
613
|
+
files,
|
|
614
|
+
gitDir: linkedAdmin,
|
|
615
|
+
history,
|
|
616
|
+
...(this.environment ? { environment: this.environment } : {}),
|
|
617
|
+
});
|
|
618
|
+
const linkedCopy = await worktrees.openCurrent();
|
|
619
|
+
const branch = await linkedCopy.getCurrentBranch?.();
|
|
620
|
+
return {
|
|
621
|
+
git: Git.fromWorkingCopy(linkedCopy),
|
|
622
|
+
workingCopy: linkedCopy,
|
|
623
|
+
repository: linkedCopy.history,
|
|
624
|
+
initialBranch: typeof branch === "string" ? branch.replace(/^refs\/heads\//, "") : "",
|
|
625
|
+
...(hints ? { hints } : {}),
|
|
626
|
+
...(warnings.length > 0 ? { warnings } : {}),
|
|
627
|
+
bare: false,
|
|
628
|
+
gitDir: relative(linkedAdmin),
|
|
629
|
+
reinitialized,
|
|
630
|
+
shared,
|
|
631
|
+
};
|
|
632
|
+
}
|
|
633
|
+
|
|
423
634
|
// Create worktree if enabled and not bare
|
|
424
635
|
let worktree: Worktree | undefined;
|
|
425
636
|
if (this.worktreeEnabled && !this.bare) {
|
|
@@ -429,10 +640,10 @@ export class InitCommand {
|
|
|
429
640
|
files,
|
|
430
641
|
// the worktree is the directory asked for (git init <dir>), the FilesApi's root
|
|
431
642
|
// without one
|
|
432
|
-
rootPath:
|
|
643
|
+
rootPath: worktreeRoot,
|
|
433
644
|
blobs: history.blobs,
|
|
434
645
|
trees: history.trees,
|
|
435
|
-
gitDir,
|
|
646
|
+
gitDir: resultGitDir,
|
|
436
647
|
...(this.environment ? { environment: this.environment } : {}),
|
|
437
648
|
});
|
|
438
649
|
}
|
|
@@ -516,10 +727,239 @@ export class InitCommand {
|
|
|
516
727
|
...(hints ? { hints } : {}),
|
|
517
728
|
...(warnings.length > 0 ? { warnings } : {}),
|
|
518
729
|
bare: this.bare,
|
|
519
|
-
gitDir,
|
|
730
|
+
gitDir: resultGitDir,
|
|
731
|
+
reinitialized,
|
|
732
|
+
shared,
|
|
520
733
|
};
|
|
521
734
|
}
|
|
522
735
|
|
|
736
|
+
/**
|
|
737
|
+
* `core.sharedRepository` of the repository at `dir` as git reads it then (system, global,
|
|
738
|
+
* the repository's file, command line); 0 when unset.
|
|
739
|
+
*/
|
|
740
|
+
private async configuredShared(files: FilesApi, dir: string): Promise<number> {
|
|
741
|
+
const config = await createWorkingCopyConfig(
|
|
742
|
+
configFilesApiOf(files, { readOnly: "init reads the configuration only" }),
|
|
743
|
+
dir,
|
|
744
|
+
undefined,
|
|
745
|
+
this.environment ? { environment: this.environment } : {},
|
|
746
|
+
);
|
|
747
|
+
return sharedRepositoryOf(config.get("core.sharedRepository"));
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* cmd_init_db's `--separate-git-dir` and setup.c's `separate_git_dir`: a repository at
|
|
752
|
+
* `link` (a `.git` directory, or the one its gitfile names; inside a linked worktree the
|
|
753
|
+
* repository's common directory, its main worktree's `.git` then the link) is moved to
|
|
754
|
+
* `real`, the gitfile written in its place, then its linked worktrees repaired (best
|
|
755
|
+
* effort: each one that cannot be is reported in `warnings`). A move that cannot be
|
|
756
|
+
* completed is rolled back, so the repository is never left without its `.git`.
|
|
757
|
+
*
|
|
758
|
+
* @returns the main worktree's top when `link` was a linked worktree's gitfile
|
|
759
|
+
*/
|
|
760
|
+
private async separateGitDirectory(
|
|
761
|
+
files: FilesApi,
|
|
762
|
+
link: string,
|
|
763
|
+
real: string,
|
|
764
|
+
warnings: string[],
|
|
765
|
+
): Promise<{ worktreeTop?: string }> {
|
|
766
|
+
let worktreeTop: string | undefined;
|
|
767
|
+
const kind = (await files.stats(link))?.kind;
|
|
768
|
+
let source: string | undefined;
|
|
769
|
+
if (kind === "directory") source = link;
|
|
770
|
+
else if (kind === "file") {
|
|
771
|
+
const target = await gitfileTargetOf(files, link);
|
|
772
|
+
if (target && !target.reachable) {
|
|
773
|
+
throw new InitOptionsError(
|
|
774
|
+
`fatal: the repository '${target.path}' the gitfile '${link}' names lies outside the storage`,
|
|
775
|
+
);
|
|
776
|
+
}
|
|
777
|
+
source = target?.path;
|
|
778
|
+
// inside a linked worktree the common directory moves (init-db.c: get_common_dir)
|
|
779
|
+
const common = source !== undefined ? await commonDirectoryOf(files, source) : undefined;
|
|
780
|
+
if (common !== undefined) {
|
|
781
|
+
// guess_repository_type: a common directory not named `.git` is a bare repository
|
|
782
|
+
if (!/(^|\/)\.git$/.test(normalizePath(common))) {
|
|
783
|
+
throw new InitOptionsError("fatal: --separate-git-dir incompatible with bare repository");
|
|
784
|
+
}
|
|
785
|
+
source = common;
|
|
786
|
+
link = common;
|
|
787
|
+
worktreeTop = dirname(normalizePath(common)).replace(/^\/+/, "");
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
if (source !== undefined && normalizePath(source) !== normalizePath(real)) {
|
|
791
|
+
// the worktrees' relative links resolve from where the repository was
|
|
792
|
+
const oldHost = await this.hostPath(files, source);
|
|
793
|
+
// the gitfile there before (a repository reached through one): restored on a failure
|
|
794
|
+
const linkMoves = normalizePath(link) === normalizePath(source);
|
|
795
|
+
const previousGitfile = linkMoves ? undefined : await readFile(files, link);
|
|
796
|
+
if (!(await files.move(source, real))) {
|
|
797
|
+
throw new InitOptionsError(`fatal: unable to move ${source} to ${real}`);
|
|
798
|
+
}
|
|
799
|
+
try {
|
|
800
|
+
await this.writeGitfile(files, link, real);
|
|
801
|
+
} catch (error) {
|
|
802
|
+
// never leave the repository without its `.git`: the move is undone, and the link
|
|
803
|
+
// is what it was (a gitfile written where the moved directory was goes first)
|
|
804
|
+
if (linkMoves && (await files.stats(link).catch(() => undefined))?.kind === "file") {
|
|
805
|
+
await files.remove(link).catch(() => false);
|
|
806
|
+
}
|
|
807
|
+
const movedBack = await files.move(real, source).catch(() => false);
|
|
808
|
+
if (previousGitfile !== undefined) {
|
|
809
|
+
await files.write(link, [previousGitfile]).catch(() => undefined);
|
|
810
|
+
}
|
|
811
|
+
if (!movedBack) {
|
|
812
|
+
throw new InitOptionsError(
|
|
813
|
+
`fatal: could not write the gitfile '${link}' (${(error as Error).message}), and the repository could not be moved back: it is now at '${real}', not '${source}'`,
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
throw error;
|
|
817
|
+
}
|
|
818
|
+
await this.repairWorktreesAfterGitdirMove(files, real, oldHost, warnings);
|
|
819
|
+
return worktreeTop !== undefined ? { worktreeTop } : {};
|
|
820
|
+
}
|
|
821
|
+
await this.writeGitfile(files, link, real);
|
|
822
|
+
return worktreeTop !== undefined ? { worktreeTop } : {};
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/** `write_file(git_link, "gitdir: %s", git_dir)`: the git directory's real host path. */
|
|
826
|
+
private async writeGitfile(files: FilesApi, link: string, real: string): Promise<void> {
|
|
827
|
+
await files.write(link, [encodeGitPath(`gitdir: ${await this.hostPath(files, real)}\n`)]);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
/**
|
|
831
|
+
* worktree.c's `repair_worktrees_after_gitdir_move`: each linked worktree of the repository
|
|
832
|
+
* now at `gitDir` (before at host path `oldHost`) whose `.git` file still exists gets its
|
|
833
|
+
* gitfile and back-pointer written anew, absolute or relative as they were. A worktree
|
|
834
|
+
* outside the FilesApi is reached through `GitHostEnvironment.hostFilesOf`; one that cannot
|
|
835
|
+
* be reached or written is reported in `warnings` and the others go on.
|
|
836
|
+
*/
|
|
837
|
+
private async repairWorktreesAfterGitdirMove(
|
|
838
|
+
files: FilesApi,
|
|
839
|
+
gitDir: string,
|
|
840
|
+
oldHost: string,
|
|
841
|
+
warnings: string[],
|
|
842
|
+
): Promise<void> {
|
|
843
|
+
const admin = joinPath(gitDir, "worktrees");
|
|
844
|
+
if ((await files.stats(admin))?.kind !== "directory") return;
|
|
845
|
+
const newHost = (await this.hostPath(files, gitDir)).replace(/\/+$/, "");
|
|
846
|
+
const names: string[] = [];
|
|
847
|
+
for await (const entry of files.list(admin))
|
|
848
|
+
if (entry.kind === "directory") names.push(entry.name);
|
|
849
|
+
for (const id of names.sort()) {
|
|
850
|
+
const backPointer = joinPath(admin, id, "gitdir");
|
|
851
|
+
let dotgitHost: string | undefined;
|
|
852
|
+
try {
|
|
853
|
+
if ((await files.stats(backPointer))?.kind !== "file") continue;
|
|
854
|
+
const dotgit = decodeGitPath(await readFile(files, backPointer)).replace(
|
|
855
|
+
/[ \t\n\v\f\r]+$/,
|
|
856
|
+
"",
|
|
857
|
+
);
|
|
858
|
+
const relative = !isAbsoluteGitPath(dotgit);
|
|
859
|
+
// strbuf_realpath_forgiving of the old place joined with the relative link
|
|
860
|
+
dotgitHost = relative ? resolveDots(`${oldHost}/worktrees/${id}/${dotgit}`) : dotgit;
|
|
861
|
+
const target = await this.reachHostPath(files, dotgitHost);
|
|
862
|
+
if (!target) {
|
|
863
|
+
warnings.push(
|
|
864
|
+
`error: could not repair worktree link, not reachable here: ${dotgitHost} (run 'git worktree repair ${parentOf(dotgitHost)}')`,
|
|
865
|
+
);
|
|
866
|
+
continue;
|
|
867
|
+
}
|
|
868
|
+
// file_exists: a worktree gone is left alone
|
|
869
|
+
if ((await target.files.stats(target.path)) === undefined) continue;
|
|
870
|
+
const adminHost = `${newHost}/worktrees/${id}`;
|
|
871
|
+
const gitfile = relative ? relativePath(parentOf(dotgitHost), adminHost) : adminHost;
|
|
872
|
+
const back = relative ? relativePath(adminHost, dotgitHost) : dotgitHost;
|
|
873
|
+
await target.files.write(target.path, [encodeGitPath(`gitdir: ${gitfile}\n`)]);
|
|
874
|
+
await files.write(backPointer, [encodeGitPath(`${back}\n`)]);
|
|
875
|
+
} catch (error) {
|
|
876
|
+
warnings.push(
|
|
877
|
+
`error: could not repair worktree link: ${dotgitHost ?? backPointer}: ${(error as Error).message}`,
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
/** A host path as a FilesApi reaches it: this one (under its root), else the host's. */
|
|
884
|
+
private async reachHostPath(
|
|
885
|
+
files: FilesApi,
|
|
886
|
+
host: string,
|
|
887
|
+
): Promise<{ files: FilesApi; path: string } | undefined> {
|
|
888
|
+
const root = hostRootOf(files);
|
|
889
|
+
if (root === undefined) return { files, path: host };
|
|
890
|
+
const paths = new HostPaths(root);
|
|
891
|
+
if (paths.inside(host)) return { files, path: paths.filesPath(host) };
|
|
892
|
+
return (await this.environment?.hostFilesOf?.(host).catch(() => undefined)) ?? undefined;
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/** The host's absolute (real) path of `path` in `files`, as git's real_pathdup names it. */
|
|
896
|
+
private async hostPath(files: FilesApi, path: string): Promise<string> {
|
|
897
|
+
const host = hostPathOf(files, normalizePath(path));
|
|
898
|
+
const realPath = this.environment?.realPath;
|
|
899
|
+
if (!realPath) return host;
|
|
900
|
+
const real = await realPath(host).catch(() => undefined);
|
|
901
|
+
if (real !== undefined) return real;
|
|
902
|
+
const slash = host.lastIndexOf("/");
|
|
903
|
+
const parent =
|
|
904
|
+
slash > 0 ? await realPath(host.slice(0, slash)).catch(() => undefined) : undefined;
|
|
905
|
+
return parent !== undefined ? `${parent.replace(/\/+$/, "")}${host.slice(slash)}` : host;
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
/**
|
|
909
|
+
* setup.c's `copy_templates`: the template directory (option, `$GIT_TEMPLATE_DIR`,
|
|
910
|
+
* `init.templateDir`) copied into `dir`, unless its config is of a repository version
|
|
911
|
+
* git does not know.
|
|
912
|
+
*/
|
|
913
|
+
private async copyTemplates(target: FilesApi, dir: string, warnings: string[]): Promise<void> {
|
|
914
|
+
const source = await this.templateSource();
|
|
915
|
+
if (!source) return;
|
|
916
|
+
const { files, path, display } = source;
|
|
917
|
+
if ((await files.stats(path).catch(() => undefined))?.kind !== "directory") {
|
|
918
|
+
warnings.push(`warning: templates not found in ${display}`);
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
// "make sure that template is from the correct vintage"
|
|
922
|
+
const configPath = joinPath(path, "config");
|
|
923
|
+
if ((await files.stats(configPath))?.kind === "file") {
|
|
924
|
+
const text = decodeGitPath(await readFile(files, configPath));
|
|
925
|
+
const version = templateRepositoryVersion(text);
|
|
926
|
+
if (version !== undefined && version > 1) {
|
|
927
|
+
warnings.push(
|
|
928
|
+
`warning: not copying templates from '${display}': Expected git repo version <= 1, found ${version}`,
|
|
929
|
+
);
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
await copyTemplateDirectory(files, path, target, dir);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
/** The templates to copy, as `get_template_dir` finds them; undefined for none. */
|
|
937
|
+
private async templateSource(): Promise<
|
|
938
|
+
{ files: FilesApi; path: string; display: string } | undefined
|
|
939
|
+
> {
|
|
940
|
+
if (this.template !== undefined) {
|
|
941
|
+
if (this.template.path === "") return undefined;
|
|
942
|
+
const files = this.template.files ?? this.files;
|
|
943
|
+
if (!files) return undefined;
|
|
944
|
+
// git names it absolute (absolute_pathdup)
|
|
945
|
+
const display = hostPathOf(files, normalizePath(this.template.path));
|
|
946
|
+
return { files, path: this.template.path, display };
|
|
947
|
+
}
|
|
948
|
+
const environment = this.environment;
|
|
949
|
+
let hostDir = environment?.templateDir;
|
|
950
|
+
if (hostDir === undefined) {
|
|
951
|
+
const configured = (await readProtectedConfig(environment))
|
|
952
|
+
.filter((e) => e.key.toLowerCase() === "init.templatedir")
|
|
953
|
+
.at(-1);
|
|
954
|
+
if (configured?.value !== undefined)
|
|
955
|
+
hostDir = await expandConfigPath(configured.value, environment);
|
|
956
|
+
}
|
|
957
|
+
if (!hostDir || !environment?.hostFilesOf) return undefined;
|
|
958
|
+
const host = await environment.hostFilesOf(hostDir.replace(/\/+$/, "")).catch(() => undefined);
|
|
959
|
+
if (!host) return undefined;
|
|
960
|
+
return { files: host.files, path: host.path, display: hostDir };
|
|
961
|
+
}
|
|
962
|
+
|
|
523
963
|
/**
|
|
524
964
|
* The format of a new repository without `--object-format` (validate_hash_algorithm):
|
|
525
965
|
* `$GIT_DEFAULT_HASH` (an unknown name dies), else `init.defaultObjectFormat` (git 2.45+;
|
|
@@ -609,34 +1049,148 @@ function knownHashAlgorithm(name: string): ObjectFormat {
|
|
|
609
1049
|
return found;
|
|
610
1050
|
}
|
|
611
1051
|
|
|
612
|
-
/**
|
|
613
|
-
|
|
614
|
-
|
|
1052
|
+
/** Options `git init` refuses together, or a step of `--separate-git-dir` that failed: git dies. */
|
|
1053
|
+
export class InitOptionsError extends Error {
|
|
1054
|
+
override name = "InitOptionsError";
|
|
1055
|
+
readonly lines: string[];
|
|
1056
|
+
constructor(line: string) {
|
|
1057
|
+
super(line);
|
|
1058
|
+
this.lines = [line];
|
|
1059
|
+
}
|
|
615
1060
|
}
|
|
616
1061
|
|
|
617
|
-
/**
|
|
618
|
-
|
|
1062
|
+
/**
|
|
1063
|
+
* The git directory the gitfile at `path` names, as a path of `files` (an absolute target
|
|
1064
|
+
* mapped through the FilesApi's `rootDirectory`, a relative one from the gitfile's
|
|
1065
|
+
* directory), and whether the FilesApi reaches it; undefined when `path` is no gitfile.
|
|
1066
|
+
*/
|
|
1067
|
+
async function gitfileTargetOf(
|
|
619
1068
|
files: FilesApi,
|
|
620
|
-
|
|
621
|
-
|
|
1069
|
+
path: string,
|
|
1070
|
+
): Promise<{ path: string; reachable: boolean } | undefined> {
|
|
1071
|
+
if ((await files.stats(path))?.kind !== "file") return undefined;
|
|
1072
|
+
const target = parseGitfile(decodeGitPath(await readFile(files, path)));
|
|
1073
|
+
if (target === undefined) return undefined;
|
|
1074
|
+
if (isAbsoluteGitPath(target)) {
|
|
1075
|
+
const root = hostRootOf(files);
|
|
1076
|
+
if (root === undefined) return { path: target, reachable: true };
|
|
1077
|
+
const paths = new HostPaths(root);
|
|
1078
|
+
return { path: paths.filesPath(target), reachable: paths.inside(target) };
|
|
1079
|
+
}
|
|
1080
|
+
const resolved = resolveDots(joinPath(dirname(path), target));
|
|
1081
|
+
return { path: resolved, reachable: !resolved.startsWith("/..") };
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
/**
|
|
1085
|
+
* The common directory a linked worktree's git directory names (`commondir`, relative to it
|
|
1086
|
+
* unless absolute), as a path of `files`; undefined when `gitDir` has none.
|
|
1087
|
+
*/
|
|
1088
|
+
async function commonDirectoryOf(files: FilesApi, gitDir: string): Promise<string | undefined> {
|
|
1089
|
+
const file = joinPath(gitDir, "commondir");
|
|
1090
|
+
if ((await files.stats(file))?.kind !== "file") return undefined;
|
|
1091
|
+
const value = decodeGitPath(await readFile(files, file)).replace(/[\r\n]+$/, "");
|
|
1092
|
+
if (!value) return undefined;
|
|
1093
|
+
if (isAbsoluteGitPath(value)) {
|
|
1094
|
+
const root = hostRootOf(files);
|
|
1095
|
+
return root === undefined ? value : new HostPaths(root).filesPath(value);
|
|
1096
|
+
}
|
|
1097
|
+
return resolveDots(joinPath(gitDir, value));
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
/** `path` with its `.` and `..` components resolved lexically (`..` above the root kept). */
|
|
1101
|
+
function resolveDots(path: string): string {
|
|
1102
|
+
const absolute = path.startsWith("/");
|
|
1103
|
+
const out: string[] = [];
|
|
1104
|
+
for (const part of path.split("/")) {
|
|
1105
|
+
if (part === "" || part === ".") continue;
|
|
1106
|
+
if (part === ".." && out.length > 0 && out[out.length - 1] !== "..") out.pop();
|
|
1107
|
+
else if (part === ".." && absolute && out.length === 0) out.push("..");
|
|
1108
|
+
else out.push(part);
|
|
1109
|
+
}
|
|
1110
|
+
return `${absolute ? "/" : ""}${out.join("/")}`;
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/** The directory holding `path` (a host path or a path of a FilesApi). */
|
|
1114
|
+
function parentOf(path: string): string {
|
|
1115
|
+
const slash = path.replace(/\/+$/, "").lastIndexOf("/");
|
|
1116
|
+
return slash <= 0 ? "/" : path.slice(0, slash);
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
/** The relative path from directory `from` to `to` (both paths of one FilesApi). */
|
|
1120
|
+
function relativePath(from: string, to: string): string {
|
|
1121
|
+
const a = normalizePath(from).split("/").filter(Boolean);
|
|
1122
|
+
const b = normalizePath(to).split("/").filter(Boolean);
|
|
1123
|
+
let i = 0;
|
|
1124
|
+
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
1125
|
+
return [...a.slice(i).map(() => ".."), ...b.slice(i)].join("/") || ".";
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/** `core.repositoryformatversion` of a template's config text (read_repository_format). */
|
|
1129
|
+
function templateRepositoryVersion(text: string): number | undefined {
|
|
1130
|
+
let section = "";
|
|
1131
|
+
let version: number | undefined;
|
|
1132
|
+
for (const line of text.split("\n")) {
|
|
1133
|
+
const header = /^\s*\[\s*([^\]\s"]+)/.exec(line);
|
|
1134
|
+
if (header) section = header[1].toLowerCase();
|
|
1135
|
+
const match = /^\s*(?:\[[^\]]*\])?\s*repositoryformatversion\s*=\s*(-?\d+)/i.exec(line);
|
|
1136
|
+
if (match && section === "core") version = Number(match[1]);
|
|
1137
|
+
}
|
|
1138
|
+
return version;
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/** git_config_pathname of a value: `~/` and `~user/` expanded (no expansion: as given). */
|
|
1142
|
+
async function expandConfigPath(
|
|
622
1143
|
value: string,
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
const
|
|
627
|
-
const
|
|
628
|
-
const
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
await files.write(configPath, [encodeGitPath(updated)]);
|
|
1144
|
+
environment: GitHostEnvironment | undefined,
|
|
1145
|
+
): Promise<string> {
|
|
1146
|
+
if (!value.startsWith("~")) return value;
|
|
1147
|
+
const slash = value.indexOf("/");
|
|
1148
|
+
const user = value.slice(1, slash < 0 ? undefined : slash);
|
|
1149
|
+
const rest = slash < 0 ? "" : value.slice(slash);
|
|
1150
|
+
const home = user === "" ? environment?.home : await environment?.expandUserHome?.(user);
|
|
1151
|
+
return home === undefined ? value : `${home.replace(/\/+$/, "")}${rest}`;
|
|
632
1152
|
}
|
|
633
1153
|
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
1154
|
+
/**
|
|
1155
|
+
* setup.c's `copy_templates_1`: `source`'s entries copied into `target`'s `dir` (made
|
|
1156
|
+
* first), names starting with `.` skipped, directories recursed into, an existing entry
|
|
1157
|
+
* kept, a link copied as a link, a file with any execute bit made executable (copy_file's
|
|
1158
|
+
* 0777), anything else left out.
|
|
1159
|
+
*/
|
|
1160
|
+
async function copyTemplateDirectory(
|
|
1161
|
+
sourceFiles: FilesApi,
|
|
1162
|
+
source: string,
|
|
1163
|
+
targetFiles: FilesApi,
|
|
1164
|
+
dir: string,
|
|
1165
|
+
): Promise<void> {
|
|
1166
|
+
const from = sourceFiles as PosixFilesApi;
|
|
1167
|
+
const to = targetFiles as PosixFilesApi;
|
|
1168
|
+
await targetFiles.mkdir(dir);
|
|
1169
|
+
const names: string[] = [];
|
|
1170
|
+
for await (const entry of sourceFiles.list(source)) names.push(entry.name);
|
|
1171
|
+
for (const name of names) {
|
|
1172
|
+
if (name.startsWith(".")) continue;
|
|
1173
|
+
const src = joinPath(source, name);
|
|
1174
|
+
const dst = joinPath(dir, name);
|
|
1175
|
+
const st = from.lstat ? await from.lstat(src) : await sourceFiles.stats(src);
|
|
1176
|
+
if (!st) continue;
|
|
1177
|
+
if (st.kind === "directory") {
|
|
1178
|
+
await copyTemplateDirectory(sourceFiles, src, targetFiles, dst);
|
|
1179
|
+
continue;
|
|
1180
|
+
}
|
|
1181
|
+
const exists = to.lstat ? await to.lstat(dst) : await targetFiles.stats(dst);
|
|
1182
|
+
if (exists) continue;
|
|
1183
|
+
if (st.kind === "symlink") {
|
|
1184
|
+
const link = await from.readlink?.(src);
|
|
1185
|
+
if (link && to.symlink) await to.symlink(link, dst);
|
|
1186
|
+
continue;
|
|
1187
|
+
}
|
|
1188
|
+
if (st.kind !== "file") continue;
|
|
1189
|
+
await targetFiles.write(dst, [await readFile(sourceFiles, src)]);
|
|
1190
|
+
const mode = "mode" in st ? st.mode : undefined;
|
|
1191
|
+
if (mode !== undefined && mode & 0o111 && to.lstat && to.chmod) {
|
|
1192
|
+
const made = (await to.lstat(dst))?.mode;
|
|
1193
|
+
if (made !== undefined) await to.chmod(dst, made | ((made & 0o444) >> 2));
|
|
1194
|
+
}
|
|
640
1195
|
}
|
|
641
|
-
return out;
|
|
642
1196
|
}
|