@statewalker/vcs-commands 0.6.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.
Files changed (166) hide show
  1. package/dist/commands/apply/apply-command.d.ts.map +1 -1
  2. package/dist/commands/apply/apply-engine.d.ts.map +1 -1
  3. package/dist/commands/apply/apply-parse.d.ts.map +1 -1
  4. package/dist/commands/bisect/bisect-engine.d.ts.map +1 -1
  5. package/dist/commands/clone-command.d.ts +17 -0
  6. package/dist/commands/clone-command.d.ts.map +1 -1
  7. package/dist/commands/comment-char-config.d.ts +35 -0
  8. package/dist/commands/comment-char-config.d.ts.map +1 -0
  9. package/dist/commands/commit-command.d.ts.map +1 -1
  10. package/dist/commands/core-config.d.ts.map +1 -1
  11. package/dist/commands/default-branch.d.ts +70 -0
  12. package/dist/commands/default-branch.d.ts.map +1 -0
  13. package/dist/commands/diff-command.d.ts.map +1 -1
  14. package/dist/commands/fetch-command.d.ts.map +1 -1
  15. package/dist/commands/format-patch-command.d.ts.map +1 -1
  16. package/dist/commands/fsck/fsck-command.d.ts +2 -0
  17. package/dist/commands/fsck/fsck-command.d.ts.map +1 -1
  18. package/dist/commands/fsck/fsck-loose.d.ts +3 -0
  19. package/dist/commands/fsck/fsck-loose.d.ts.map +1 -1
  20. package/dist/commands/fsck/fsck-object-checks.d.ts +25 -14
  21. package/dist/commands/fsck/fsck-object-checks.d.ts.map +1 -1
  22. package/dist/commands/fsck/fsck-object-table.d.ts +15 -7
  23. package/dist/commands/fsck/fsck-object-table.d.ts.map +1 -1
  24. package/dist/commands/fsck/fsck-pack.d.ts +11 -4
  25. package/dist/commands/fsck/fsck-pack.d.ts.map +1 -1
  26. package/dist/commands/fsck/verify-pack-command.d.ts.map +1 -1
  27. package/dist/commands/gc-command.d.ts +72 -48
  28. package/dist/commands/gc-command.d.ts.map +1 -1
  29. package/dist/commands/git-date.d.ts +1 -65
  30. package/dist/commands/git-date.d.ts.map +1 -1
  31. package/dist/commands/git-hooks.d.ts +8 -0
  32. package/dist/commands/git-hooks.d.ts.map +1 -1
  33. package/dist/commands/index-adder.d.ts +2 -0
  34. package/dist/commands/index-adder.d.ts.map +1 -1
  35. package/dist/commands/index.d.ts +2 -0
  36. package/dist/commands/index.d.ts.map +1 -1
  37. package/dist/commands/index.js +3 -3
  38. package/dist/commands/init-command.d.ts +50 -3
  39. package/dist/commands/init-command.d.ts.map +1 -1
  40. package/dist/commands/legacy-remotes.d.ts +55 -0
  41. package/dist/commands/legacy-remotes.d.ts.map +1 -0
  42. package/dist/commands/log-command.d.ts.map +1 -1
  43. package/dist/commands/ls-remote-command.d.ts.map +1 -1
  44. package/dist/commands/merge-command.d.ts +12 -0
  45. package/dist/commands/merge-command.d.ts.map +1 -1
  46. package/dist/commands/merge-support.d.ts +11 -3
  47. package/dist/commands/merge-support.d.ts.map +1 -1
  48. package/dist/commands/notes-merge-command.d.ts.map +1 -1
  49. package/dist/commands/notes-tree.d.ts +11 -1
  50. package/dist/commands/notes-tree.d.ts.map +1 -1
  51. package/dist/commands/patch-id.d.ts +3 -0
  52. package/dist/commands/patch-id.d.ts.map +1 -1
  53. package/dist/commands/pretty-format.d.ts.map +1 -1
  54. package/dist/commands/pull-command.d.ts +18 -0
  55. package/dist/commands/pull-command.d.ts.map +1 -1
  56. package/dist/commands/push-command.d.ts.map +1 -1
  57. package/dist/commands/read-tree-command.d.ts.map +1 -1
  58. package/dist/commands/rebase-command.d.ts.map +1 -1
  59. package/dist/commands/remote-command.d.ts.map +1 -1
  60. package/dist/commands/reset-command.d.ts.map +1 -1
  61. package/dist/commands/rev-parse-command.d.ts +9 -0
  62. package/dist/commands/rev-parse-command.d.ts.map +1 -1
  63. package/dist/commands/revision-resolver.d.ts +6 -2
  64. package/dist/commands/revision-resolver.d.ts.map +1 -1
  65. package/dist/commands/sequencer-pick.d.ts +1 -1
  66. package/dist/commands/sequencer-pick.d.ts.map +1 -1
  67. package/dist/commands/sequencer-support.d.ts.map +1 -1
  68. package/dist/commands/stash-create-command.d.ts +1 -1
  69. package/dist/commands/stash-create-command.d.ts.map +1 -1
  70. package/dist/commands/status-command.d.ts.map +1 -1
  71. package/dist/commands/submodule-summary-command.d.ts.map +1 -1
  72. package/dist/commands/untracked-cache-support.d.ts +2 -1
  73. package/dist/commands/untracked-cache-support.d.ts.map +1 -1
  74. package/dist/commands/worktree-list-command.d.ts.map +1 -1
  75. package/dist/{commands-DnmlfwGV.js → commands-DIJ91NnD.js} +1111 -506
  76. package/dist/{git-command-DxvwPrPB.js → git-command-BGiOJ8Zk.js} +630 -996
  77. package/dist/git-command.d.ts +16 -1
  78. package/dist/git-command.d.ts.map +1 -1
  79. package/dist/git.d.ts +9 -2
  80. package/dist/git.d.ts.map +1 -1
  81. package/dist/index.js +4 -4
  82. package/dist/remote-config/index.d.ts +1 -1
  83. package/dist/remote-config/index.d.ts.map +1 -1
  84. package/dist/remote-config/remote-config-store.d.ts +62 -3
  85. package/dist/remote-config/remote-config-store.d.ts.map +1 -1
  86. package/dist/rename/detect-renames.d.ts.map +1 -1
  87. package/dist/results/clone-result.d.ts +8 -0
  88. package/dist/results/clone-result.d.ts.map +1 -1
  89. package/dist/results/diff-output.d.ts.map +1 -1
  90. package/dist/results/index.js +1 -1
  91. package/dist/results/init-result.d.ts +7 -0
  92. package/dist/results/init-result.d.ts.map +1 -1
  93. package/dist/results/pull-result.d.ts +5 -0
  94. package/dist/results/pull-result.d.ts.map +1 -1
  95. package/dist/results/push-result.d.ts +5 -0
  96. package/dist/results/push-result.d.ts.map +1 -1
  97. package/dist/results/status-result.d.ts.map +1 -1
  98. package/dist/{results-BZGa2Z3I.js → results-WVlBhVb1.js} +11 -10
  99. package/package.json +12 -12
  100. package/src/commands/add-patch/add-interactive.ts +3 -3
  101. package/src/commands/am/am-command.ts +1 -1
  102. package/src/commands/apply/apply-command.ts +12 -2
  103. package/src/commands/apply/apply-engine.ts +5 -6
  104. package/src/commands/apply/apply-parse.ts +3 -2
  105. package/src/commands/attributes-source.ts +1 -1
  106. package/src/commands/bisect/bisect-engine.ts +2 -1
  107. package/src/commands/checkout-command.ts +1 -1
  108. package/src/commands/clone-command.ts +116 -4
  109. package/src/commands/comment-char-config.ts +160 -0
  110. package/src/commands/commit-command.ts +8 -7
  111. package/src/commands/core-config.ts +7 -2
  112. package/src/commands/default-branch.ts +196 -0
  113. package/src/commands/diff-command.ts +45 -27
  114. package/src/commands/fetch-command.ts +19 -5
  115. package/src/commands/format-patch-command.ts +7 -2
  116. package/src/commands/fsck/fsck-command.ts +183 -66
  117. package/src/commands/fsck/fsck-loose.ts +14 -4
  118. package/src/commands/fsck/fsck-object-checks.ts +87 -46
  119. package/src/commands/fsck/fsck-object-table.ts +52 -42
  120. package/src/commands/fsck/fsck-pack.ts +64 -34
  121. package/src/commands/fsck/verify-pack-command.ts +5 -2
  122. package/src/commands/gc-command.ts +208 -107
  123. package/src/commands/git-date.ts +9 -895
  124. package/src/commands/git-hooks.ts +11 -0
  125. package/src/commands/index-adder.ts +8 -3
  126. package/src/commands/index.ts +2 -0
  127. package/src/commands/init-command.ts +167 -11
  128. package/src/commands/legacy-remotes.ts +224 -0
  129. package/src/commands/log-command.ts +37 -22
  130. package/src/commands/ls-remote-command.ts +4 -0
  131. package/src/commands/merge-command.ts +31 -1
  132. package/src/commands/merge-support.ts +29 -8
  133. package/src/commands/notes-merge-command.ts +23 -14
  134. package/src/commands/notes-tree.ts +28 -13
  135. package/src/commands/octopus-merge.ts +3 -2
  136. package/src/commands/patch-id.ts +16 -8
  137. package/src/commands/pretty-format.ts +2 -1
  138. package/src/commands/pull-command.ts +128 -20
  139. package/src/commands/push-command.ts +23 -8
  140. package/src/commands/read-tree-command.ts +6 -2
  141. package/src/commands/rebase-command.ts +24 -13
  142. package/src/commands/remote-command.ts +35 -5
  143. package/src/commands/reset-command.ts +4 -11
  144. package/src/commands/rev-parse-command.ts +22 -0
  145. package/src/commands/revision-resolver.ts +32 -14
  146. package/src/commands/sequencer-pick.ts +7 -2
  147. package/src/commands/sequencer-support.ts +10 -3
  148. package/src/commands/stash-apply-command.ts +1 -1
  149. package/src/commands/stash-create-command.ts +2 -2
  150. package/src/commands/status-command.ts +2 -4
  151. package/src/commands/status-header.ts +3 -1
  152. package/src/commands/submodule-summary-command.ts +6 -7
  153. package/src/commands/untracked-cache-support.ts +16 -12
  154. package/src/commands/worktree-list-command.ts +3 -5
  155. package/src/core-commands/add.command.impl.ts +1 -1
  156. package/src/git-command.ts +32 -2
  157. package/src/git.ts +16 -3
  158. package/src/remote-config/index.ts +2 -0
  159. package/src/remote-config/remote-config-store.ts +137 -11
  160. package/src/rename/detect-renames.ts +5 -1
  161. package/src/results/clone-result.ts +9 -0
  162. package/src/results/diff-output.ts +9 -7
  163. package/src/results/init-result.ts +9 -0
  164. package/src/results/pull-result.ts +5 -0
  165. package/src/results/push-result.ts +5 -0
  166. package/src/results/status-result.ts +5 -2
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  type Commit,
3
- EMPTY_TREE_ID,
4
3
  FileMode,
5
4
  type ObjectId,
6
5
  type PersonIdent,
@@ -763,7 +762,7 @@ export class CommitCommand extends GitCommand<CommitResult> {
763
762
  // `cache_nr - ita_nr > 0`. The recorded tree says both without a status run.
764
763
  const parentTree = ctx.firstParent
765
764
  ? (await this.commits.load(ctx.firstParent))?.tree
766
- : EMPTY_TREE_ID;
765
+ : this.objectFormat.emptyTreeId;
767
766
  committable = ctx.treeId !== parentTree;
768
767
  }
769
768
  // COMMIT_EDITMSG written in full, then an empty commit refused before the editor runs
@@ -1155,7 +1154,7 @@ export class CommitCommand extends GitCommand<CommitResult> {
1155
1154
  hostPath: await this.hooks.gitPath(name, true),
1156
1155
  reread: async () => {
1157
1156
  await temp.read();
1158
- return writeIndexAsTree(temp, this.trees, { write: false });
1157
+ return writeIndexAsTree(temp, this.trees, this.objectFormat, { write: false });
1159
1158
  },
1160
1159
  remove,
1161
1160
  };
@@ -1246,13 +1245,15 @@ export class CommitCommand extends GitCommand<CommitResult> {
1246
1245
  } else if (index) {
1247
1246
  // `-a`: the index with a fully valid cache-tree (cache_tree_update), written into the
1248
1247
  // lock
1249
- treeId = await writeIndexAsTree(this.staging, this.trees, { write: false });
1248
+ treeId = await writeIndexAsTree(this.staging, this.trees, this.objectFormat, {
1249
+ write: false,
1250
+ });
1250
1251
  await index.write();
1251
1252
  } else {
1252
1253
  // Normal mode: use full staging area; as git's prepare_index, the index is written
1253
1254
  // again with a fully valid cache-tree (cache_tree_update, which keeps the valid nodes:
1254
1255
  // a stored subtree no edit touched stays the one stored)
1255
- treeId = await writeIndexAsTree(this.staging, this.trees);
1256
+ treeId = await writeIndexAsTree(this.staging, this.trees, this.objectFormat);
1256
1257
  }
1257
1258
  let previousCommit: Commit | undefined;
1258
1259
  let amendedId: ObjectId | undefined;
@@ -1407,7 +1408,7 @@ export class CommitCommand extends GitCommand<CommitResult> {
1407
1408
  // no first parent (an initial commit, or --amend of a root commit): git counts the
1408
1409
  // index, `cache_nr - ita_nr > 0`; only intent-to-add entries (or none) leave the tree
1409
1410
  // empty
1410
- if (treeId === EMPTY_TREE_ID) throw new EmptyCommitError();
1411
+ if (treeId === this.objectFormat.emptyTreeId) throw new EmptyCommitError();
1411
1412
  return;
1412
1413
  }
1413
1414
  if (this.amend && (previousCommit?.parents.length ?? 0) > 1) return;
@@ -1887,7 +1888,7 @@ export class CommitCommand extends GitCommand<CommitResult> {
1887
1888
  }
1888
1889
  // the real index, with a cache-tree made valid as git's cache_tree_update before
1889
1890
  // write_locked_index (none while it holds conflicts: the edits' invalidations stay)
1890
- await updateIndexCacheTree(this.staging, this.trees);
1891
+ await updateIndexCacheTree(this.staging, this.trees, this.objectFormat);
1891
1892
  await index.write();
1892
1893
 
1893
1894
  // the false index: HEAD's tree with the same paths as the index now has them
@@ -12,6 +12,7 @@ import {
12
12
  findAbbrevLengths,
13
13
  type GitObjects,
14
14
  type ObjectId,
15
+ objectFormatOf,
15
16
  parseCoreAbbrev,
16
17
  } from "@statewalker/vcs-core";
17
18
  import { parseGitBool } from "@statewalker/vcs-utils";
@@ -165,8 +166,11 @@ export function createIdAbbreviator(
165
166
  ids: Iterable<ObjectId>,
166
167
  minLength?: number,
167
168
  ): Promise<Map<ObjectId, number>> => {
169
+ const hexLength = objectFormatOf(workingCopy.history).hexLength;
168
170
  let start =
169
- minLength ?? parseCoreAbbrev(RemoteConfigStore.from(workingCopy).value("core.abbrev")) ?? -1;
171
+ minLength ??
172
+ parseCoreAbbrev(RemoteConfigStore.from(workingCopy).value("core.abbrev"), hexLength) ??
173
+ -1;
170
174
  const objects = workingCopy.history?.objects;
171
175
  if (objects) {
172
176
  if (start < 0) {
@@ -175,7 +179,8 @@ export function createIdAbbreviator(
175
179
  }
176
180
  return findAbbrevLengths(objects, ids, start, listings);
177
181
  }
178
- const fixed = start < 0 ? DEFAULT_ABBREV_LENGTH : start === 0 ? 40 : Math.min(start, 40);
182
+ const fixed =
183
+ start < 0 ? DEFAULT_ABBREV_LENGTH : start === 0 ? hexLength : Math.min(start, hexLength);
179
184
  return new Map([...ids].map((id) => [id, fixed]));
180
185
  };
181
186
  return {
@@ -0,0 +1,196 @@
1
+ /**
2
+ * The initial branch of a new repository, as git's `repo_default_branch_name` (refs.c) picks
3
+ * it: `init.defaultBranch` from the configuration a new repository sees (system, global, the
4
+ * command line), else `master`, or `main` in a git built with `WITH_BREAKING_CHANGES`
5
+ * (`GitHostEnvironment.breakingChanges`, git 3.0). With no name configured, git explains the
6
+ * default in an `advice.defaultBranchName` hint, which {@link DefaultBranch.hint} holds.
7
+ *
8
+ * ```typescript
9
+ * const { name, hint } = await defaultInitialBranch(environment);
10
+ * // name: "master"; hint: ["hint: Using 'master' as the name for the initial branch. ...", ...]
11
+ * ```
12
+ */
13
+ import { isValidRefName } from "@statewalker/vcs-core";
14
+ import { readProtectedConfig } from "@statewalker/vcs-store-files";
15
+ import { parseGitBool } from "@statewalker/vcs-utils";
16
+ import type { GitHostEnvironment } from "@statewalker/vcs-working-tree";
17
+
18
+ /** The initial branch git picks, and the hint it prints with it. */
19
+ export interface DefaultBranch {
20
+ name: string;
21
+ /** git's `advice.defaultBranchName` lines (stderr), when the name is the built-in default */
22
+ hint?: string[];
23
+ }
24
+
25
+ /** `init.defaultBranch` names no valid branch: git dies (exit 128). */
26
+ export class InvalidDefaultBranchError extends Error {
27
+ override name = "InvalidDefaultBranchError";
28
+ readonly lines: string[];
29
+ constructor(readonly value: string) {
30
+ const line = `fatal: invalid branch name: init.defaultBranch = ${value}`;
31
+ super(line);
32
+ this.lines = [line];
33
+ }
34
+ }
35
+
36
+ /**
37
+ * `init.defaultBranch` without a value (`[init] defaultBranch`): git's config parser dies
38
+ * (`missing value`, then the variable's location), exit 128.
39
+ */
40
+ export class MissingDefaultBranchValueError extends Error {
41
+ override name = "MissingDefaultBranchValueError";
42
+ readonly lines: string[];
43
+ constructor(file?: string, line?: number) {
44
+ const fatal =
45
+ file !== undefined && line !== undefined
46
+ ? `fatal: bad config variable 'init.defaultbranch' in file '${file}' at line ${line}`
47
+ : "fatal: unable to parse 'init.defaultbranch' from command-line config";
48
+ const lines = ["error: missing value for 'init.defaultbranch'", fatal];
49
+ super(lines.join("\n"));
50
+ this.lines = lines;
51
+ }
52
+ }
53
+
54
+ /** `--initial-branch` names no valid branch: git dies (exit 128). */
55
+ export class InvalidInitialBranchError extends Error {
56
+ override name = "InvalidInitialBranchError";
57
+ readonly lines: string[];
58
+ constructor(readonly value: string) {
59
+ const line = `fatal: invalid initial branch name: '${value}'`;
60
+ super(line);
61
+ this.lines = [line];
62
+ }
63
+ }
64
+
65
+ /** Whether `name` is a valid branch name (`check_refname_format("refs/heads/<name>")`). */
66
+ export function isValidBranchName(name: string): boolean {
67
+ return name !== "" && isValidRefName(`refs/heads/${name}`);
68
+ }
69
+
70
+ /**
71
+ * The initial branch of a repository created now. Reads the protected configuration (the
72
+ * repository's own does not exist yet); without an environment, nothing is configured.
73
+ *
74
+ * @throws InvalidDefaultBranchError when `init.defaultBranch` names no valid branch (empty too)
75
+ * @throws MissingDefaultBranchValueError when `init.defaultBranch` has no value
76
+ */
77
+ export async function defaultInitialBranch(
78
+ environment: GitHostEnvironment | undefined,
79
+ ): Promise<DefaultBranch> {
80
+ const config = await readProtectedConfig(environment);
81
+ const last = (key: string) => config.filter((e) => e.key === key).at(-1);
82
+ const configured = last("init.defaultbranch");
83
+ if (configured && configured.value === undefined) {
84
+ throw new MissingDefaultBranchValueError(configured.file, configured.line);
85
+ }
86
+ if (configured?.value !== undefined) {
87
+ // an empty value is no branch name either: git dies on it
88
+ if (!isValidBranchName(configured.value)) {
89
+ throw new InvalidDefaultBranchError(configured.value);
90
+ }
91
+ return { name: configured.value };
92
+ }
93
+ const name = environment?.breakingChanges ? "main" : "master";
94
+ const advice = last("advice.defaultbranchname");
95
+ const enabled = advice === undefined || parseGitBool(advice.value ?? "true") !== false;
96
+ return enabled ? { name, hint: defaultBranchHint(name, environment?.breakingChanges) } : { name };
97
+ }
98
+
99
+ /** git's `default_branch_name_advice` (2.5x, or 3.0), as `advise_if_enabled` prints it. */
100
+ export function defaultBranchHint(name: string, breakingChanges: boolean | undefined): string[] {
101
+ const text = breakingChanges
102
+ ? `Using '${name}' as the name for the initial branch since Git 3.0.\n` +
103
+ "If you expected Git to create 'master', the just-created\n" +
104
+ "branch can be renamed via this command:\n" +
105
+ "\n" +
106
+ "\tgit branch -m master\n"
107
+ : `Using '${name}' as the name for the initial branch. This default branch name\n` +
108
+ 'will change to "main" in Git 3.0. To configure the initial branch name\n' +
109
+ "to use in all of your new repositories, which will suppress this warning,\n" +
110
+ "call:\n" +
111
+ "\n" +
112
+ "\tgit config --global init.defaultBranch <name>\n" +
113
+ "\n" +
114
+ "Names commonly chosen instead of 'master' are 'main', 'trunk' and\n" +
115
+ "'development'. The just-created branch can be renamed via this command:\n" +
116
+ "\n" +
117
+ "\tgit branch -m <name>\n";
118
+ // released 2.5x says `git config set advice...`; git master (the 3.0 build) adds `--global`
119
+ const scope = breakingChanges ? "--global " : "";
120
+ const withOff = `${text}\nDisable this message with "git config set ${scope}advice.defaultBranchName false"`;
121
+ return withOff.split("\n").map((line) => (line === "" ? "hint:" : `hint: ${line}`));
122
+ }
123
+
124
+ /** A ref storage format git does not know: git dies (exit 128). */
125
+ export class UnknownRefFormatError extends Error {
126
+ override name = "UnknownRefFormatError";
127
+ readonly lines: string[];
128
+ constructor(
129
+ readonly value: string,
130
+ source: "command line" | "GIT_DEFAULT_REF_FORMAT",
131
+ ) {
132
+ const line =
133
+ source === "command line"
134
+ ? `fatal: unknown ref storage format specified via command line: '${value}'`
135
+ : `fatal: unknown ref storage format specified via GIT_DEFAULT_REF_FORMAT: '${value}'`;
136
+ super(line);
137
+ this.lines = [line];
138
+ }
139
+ }
140
+
141
+ /** The ref storage format of a new repository, and git's warnings while choosing it. */
142
+ export interface DefaultRefFormat {
143
+ format: "files" | "reftable";
144
+ warnings: string[];
145
+ }
146
+
147
+ const refFormatOf = (value: string): "files" | "reftable" | undefined =>
148
+ value === "files" || value === "reftable" ? value : undefined;
149
+
150
+ /**
151
+ * The ref storage format git gives a new repository (setup.c `repository_format_configure`):
152
+ * `requested` (`git init --ref-format`), else `$GIT_DEFAULT_REF_FORMAT`
153
+ * (`GitHostEnvironment.defaultRefFormat`), else `init.defaultRefFormat` from the system,
154
+ * global and command-line config (`feature.experimental` choosing reftable while that is
155
+ * unset), else `files`, or `reftable` with `GitHostEnvironment.breakingChanges` (git 3.0).
156
+ * An unknown name given or in the environment dies (`UnknownRefFormatError`); one in the
157
+ * config warns and is passed over.
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const { format } = await defaultRefFormat(environment); // "reftable" under git 3.0
162
+ * ```
163
+ */
164
+ export async function defaultRefFormat(
165
+ environment: GitHostEnvironment | undefined,
166
+ requested?: string,
167
+ ): Promise<DefaultRefFormat> {
168
+ const warnings: string[] = [];
169
+ if (requested !== undefined) {
170
+ const format = refFormatOf(requested);
171
+ if (!format) throw new UnknownRefFormatError(requested, "command line");
172
+ return { format, warnings };
173
+ }
174
+ const env = environment?.defaultRefFormat;
175
+ if (env !== undefined) {
176
+ const format = refFormatOf(env);
177
+ if (!format) throw new UnknownRefFormatError(env, "GIT_DEFAULT_REF_FORMAT");
178
+ return { format, warnings };
179
+ }
180
+ let configured: "files" | "reftable" | undefined;
181
+ for (const e of await readProtectedConfig(environment)) {
182
+ if (e.key === "init.defaultrefformat" || e.key === "init.defaultrefstorageformat") {
183
+ const value = e.value ?? "";
184
+ configured = refFormatOf(value);
185
+ if (!configured) warnings.push(`warning: unknown ref storage format '${value}'`);
186
+ } else if (
187
+ e.key === "feature.experimental" &&
188
+ configured === undefined &&
189
+ parseGitBool(e.value ?? "true") === true
190
+ ) {
191
+ configured = "reftable";
192
+ }
193
+ }
194
+ if (configured) return { format: configured, warnings };
195
+ return { format: environment?.breakingChanges ? "reftable" : "files", warnings };
196
+ }
@@ -1,11 +1,15 @@
1
1
  import {
2
- EMPTY_TREE_ID,
3
2
  type FileModeValue,
4
3
  type ObjectId,
5
4
  type TreeEntry,
6
5
  TreeEntryCursor,
7
6
  } from "@statewalker/vcs-core";
8
- import { ByteSink, parseGitBool, type XdiffAlgorithm } from "@statewalker/vcs-utils";
7
+ import {
8
+ ByteSink,
9
+ isNullObjectId,
10
+ parseGitBool,
11
+ type XdiffAlgorithm,
12
+ } from "@statewalker/vcs-utils";
9
13
  import { compareGitPaths, decodeGitPath } from "@statewalker/vcs-utils/encoding";
10
14
  import {
11
15
  type AttributesIndex,
@@ -92,7 +96,8 @@ const PREFETCH_FORMATS = new Set<string>(["patch", "stat", "numstat", "shortstat
92
96
  function prefetchIds(entries: readonly DiffEntry[]): ObjectId[] {
93
97
  const out: ObjectId[] = [];
94
98
  const add = (id: ObjectId | undefined, mode: number | undefined) => {
95
- if (id && id !== NULL_ID && mode !== GITLINK && mode !== undefined && mode !== 0) out.push(id);
99
+ if (id && !isNullObjectId(id) && mode !== GITLINK && mode !== undefined && mode !== 0)
100
+ out.push(id);
96
101
  };
97
102
  for (const e of entries) {
98
103
  if (e.unmerged) continue;
@@ -262,7 +267,6 @@ function submoduleIgnoreValue(
262
267
 
263
268
  const S_IFMT = 0o170000;
264
269
  const GITLINK = 0o160000;
265
- const NULL_ID = "0".repeat(40);
266
270
  const encoder = new TextEncoder();
267
271
 
268
272
  /** The chunks as one array. */
@@ -800,7 +804,12 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
800
804
  const pair: DiffFilePair = entry.unmerged
801
805
  ? {
802
806
  status: "M",
803
- old: contentSide(entry.oldPath ?? "", 0, NULL_ID, async () => new Uint8Array(0)),
807
+ old: contentSide(
808
+ entry.oldPath ?? "",
809
+ 0,
810
+ this.objectFormat.nullId,
811
+ async () => new Uint8Array(0),
812
+ ),
804
813
  }
805
814
  : this.withTextconvInputs(diffEntryToPair(entry, this.blobs), entry);
806
815
  if (entry.unmerged) unmerged.add(pair);
@@ -822,7 +831,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
822
831
  // combine-diff.c's index lines take DEFAULT_ABBREV, whatever --abbrev says
823
832
  let combinedLengths: ReadonlyMap<string, number> | undefined;
824
833
  if (!options.fullIndex) {
825
- const ids = new Set<string>([NULL_ID]);
834
+ const ids = new Set<string>([this.objectFormat.nullId]);
826
835
  for (const c of combined) {
827
836
  ids.add(c.ours.objectId);
828
837
  ids.add(c.theirs.objectId);
@@ -863,8 +872,8 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
863
872
  const ids = new Set<string>();
864
873
  for (const pair of pairs) {
865
874
  if (unmerged.has(pair)) continue;
866
- ids.add(pair.old?.id ?? NULL_ID);
867
- ids.add(pair.new?.id ?? NULL_ID);
875
+ ids.add(pair.old?.id ?? this.objectFormat.nullId);
876
+ ids.add(pair.new?.id ?? this.objectFormat.nullId);
868
877
  }
869
878
  // `--abbrev=<n>`: at least n digits
870
879
  options.abbrevLengths = await this.idAbbreviator().lengths(ids, options.abbrev);
@@ -929,8 +938,8 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
929
938
  format: SubmoduleDiffFormat,
930
939
  ): Promise<void> {
931
940
  const path = pair.old?.path ?? pair.new?.path ?? "";
932
- const one = pair.old?.id ?? NULL_ID;
933
- const two = pair.new?.id ?? NULL_ID;
941
+ const one = pair.old?.id ?? this.objectFormat.nullId;
942
+ const two = pair.new?.id ?? this.objectFormat.nullId;
934
943
  const dirt = pair.new?.submoduleDirt;
935
944
  // strbuf_add_unique_abbrev(..., DEFAULT_ABBREV): the superproject's abbreviation
936
945
  const abbreviate = (id: ObjectId) => this.idAbbreviator().abbreviate(id);
@@ -943,7 +952,11 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
943
952
  const header = await submoduleHeader({ path, one, two, dirt, sub, abbreviate });
944
953
  sink.text(header.text);
945
954
  // a valid left and right commit (or none on that side) to diff
946
- if (!(header.left || one === NULL_ID) || !(header.right || two === NULL_ID)) return;
955
+ if (
956
+ !(header.left || one === this.objectFormat.nullId) ||
957
+ !(header.right || two === this.objectFormat.nullId)
958
+ )
959
+ return;
947
960
  if (!sub) return;
948
961
  const failed = "(diff failed)\n";
949
962
  // `git diff` runs in the submodule's directory: one that holds no repository of its own
@@ -953,14 +966,14 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
953
966
  return;
954
967
  }
955
968
  const inner = new DiffCommand(sub.repository)
956
- .setOldTree(header.left ? one : EMPTY_TREE_ID)
969
+ .setOldTree(header.left ? one : this.objectFormat.emptyTreeId)
957
970
  .setSubmoduleFormat("diff")
958
971
  .setOptions({
959
972
  srcPrefix: `${options.srcPrefix ?? "a/"}${path}/`,
960
973
  dstPrefix: `${options.dstPrefix ?? "b/"}${path}/`,
961
974
  });
962
975
  // modified content: the submodule's worktree is diffed, not its commit
963
- if (!dirt?.modified) inner.setNewTree(header.right ? two : EMPTY_TREE_ID);
976
+ if (!dirt?.modified) inner.setNewTree(header.right ? two : this.objectFormat.emptyTreeId);
964
977
  try {
965
978
  sink.push(await inner.callBytes("patch"));
966
979
  } catch {
@@ -981,7 +994,8 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
981
994
  const textconv = driver?.textconv;
982
995
  const stage = async (e: StageEntry) => {
983
996
  const side = blobSide(this.blobs, c.path, e.mode, e.objectId);
984
- const convert = textconv && (e.mode & S_IFMT) !== GITLINK && e.objectId !== NULL_ID;
997
+ const convert =
998
+ textconv && (e.mode & S_IFMT) !== GITLINK && e.objectId !== this.objectFormat.nullId;
985
999
  return {
986
1000
  id: e.objectId,
987
1001
  mode: e.mode,
@@ -997,7 +1011,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
997
1011
  if (worktree && file) {
998
1012
  if (file.isDirectory) {
999
1013
  mode = GITLINK;
1000
- content = encoder.encode(`Subproject commit ${NULL_ID}\n`);
1014
+ content = encoder.encode(`Subproject commit ${this.objectFormat.nullId}\n`);
1001
1015
  } else {
1002
1016
  const kind = file.mode & S_IFMT;
1003
1017
  mode = kind === 0o120000 ? 0o120000 : file.mode & 0o111 ? 0o100755 : 0o100644;
@@ -1013,7 +1027,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1013
1027
  {
1014
1028
  path: c.path,
1015
1029
  parents: [await stage(c.ours), await stage(c.theirs)],
1016
- result: { id: NULL_ID, mode, content },
1030
+ result: { id: this.objectFormat.nullId, mode, content },
1017
1031
  },
1018
1032
  {
1019
1033
  ...options,
@@ -1022,7 +1036,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1022
1036
  : driver?.binary !== undefined
1023
1037
  ? { binary: driver.binary }
1024
1038
  : {}),
1025
- abbreviate: (id) => id.slice(0, options.fullIndex ? 40 : (lengths?.get(id) ?? 7)),
1039
+ abbreviate: (id) => id.slice(0, options.fullIndex ? id.length : (lengths?.get(id) ?? 7)),
1026
1040
  },
1027
1041
  );
1028
1042
  }
@@ -1627,7 +1641,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1627
1641
  private async treeSide(refOrId: string, implicitHead = false): Promise<Map<string, Side>> {
1628
1642
  const out = new Map<string, Side>();
1629
1643
  // the empty tree (a root commit's parent side), which a store need not hold
1630
- if (refOrId === EMPTY_TREE_ID) return out;
1644
+ if (refOrId === this.objectFormat.emptyTreeId) return out;
1631
1645
  let treeId: ObjectId;
1632
1646
  try {
1633
1647
  treeId = await this.resolveTreeId(refOrId);
@@ -1652,11 +1666,11 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1652
1666
  const oldSide = new Map<string, Side>();
1653
1667
  const newSide = new Map<string, Side>();
1654
1668
  const treeOf = async (ref: string, implicit: boolean): Promise<ObjectId | undefined> => {
1655
- if (ref === EMPTY_TREE_ID) return undefined;
1669
+ if (ref === this.objectFormat.emptyTreeId) return undefined;
1656
1670
  // a tree's full id (as `git log -p` passes its commits' trees): read at once, its
1657
1671
  // entries kept for the comparison
1658
1672
  const trees = this.trees;
1659
- if (/^[0-9a-f]{40}$/.test(ref)) {
1673
+ if (this.isFullObjectId(ref)) {
1660
1674
  const known = trees.raw ? trees.raw(ref) : trees.entries?.(ref);
1661
1675
  if (known && (await known)) return ref;
1662
1676
  }
@@ -1687,8 +1701,10 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1687
1701
  ): Promise<void> {
1688
1702
  if (a === b) return;
1689
1703
  if (this.trees.raw) return this.diffRawTrees(a, b, prefix, oldSide, newSide);
1690
- const ea = a === undefined || a === EMPTY_TREE_ID ? [] : await this.treeEntries(a);
1691
- const eb = b === undefined || b === EMPTY_TREE_ID ? [] : await this.treeEntries(b);
1704
+ const ea =
1705
+ a === undefined || a === this.objectFormat.emptyTreeId ? [] : await this.treeEntries(a);
1706
+ const eb =
1707
+ b === undefined || b === this.objectFormat.emptyTreeId ? [] : await this.treeEntries(b);
1692
1708
  let i = 0;
1693
1709
  let j = 0;
1694
1710
  while (i < ea.length || j < eb.length) {
@@ -1741,9 +1757,11 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1741
1757
  if (a === b) return;
1742
1758
  const trees = this.trees;
1743
1759
  const read = async (id: ObjectId | undefined) =>
1744
- id === undefined || id === EMPTY_TREE_ID ? NO_BYTES : ((await trees.raw?.(id)) ?? NO_BYTES);
1745
- const x = new TreeEntryCursor(await read(a));
1746
- const y = new TreeEntryCursor(await read(b));
1760
+ id === undefined || id === this.objectFormat.emptyTreeId
1761
+ ? NO_BYTES
1762
+ : ((await trees.raw?.(id)) ?? NO_BYTES);
1763
+ const x = new TreeEntryCursor(await read(a), this.objectFormat.rawLength);
1764
+ const y = new TreeEntryCursor(await read(b), this.objectFormat.rawLength);
1747
1765
  const pathOf = (name: string) => (prefix ? `${prefix}/${name}` : name);
1748
1766
  while (!x.done || !y.done) {
1749
1767
  const order = x.done ? 1 : y.done ? -1 : x.compare(y);
@@ -1940,7 +1958,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1940
1958
  */
1941
1959
  private async resolveTreeId(refOrId: string): Promise<ObjectId> {
1942
1960
  // a full object id names that object, as get_oid takes it: no ref is looked up
1943
- if (/^[0-9a-f]{40}$/.test(refOrId)) {
1961
+ if (this.isFullObjectId(refOrId)) {
1944
1962
  const tree = await this.revisionResolver()
1945
1963
  .peelToType(refOrId, "tree")
1946
1964
  .catch(() => undefined);
@@ -1950,7 +1968,7 @@ export class DiffCommand extends GitCommand<DiffEntry[]> {
1950
1968
  try {
1951
1969
  id = await this.resolveRevision(refOrId);
1952
1970
  } catch (e) {
1953
- if (/^[0-9a-f]{40}$/.test(refOrId) && (await this.isTree(refOrId))) return refOrId;
1971
+ if (this.isFullObjectId(refOrId) && (await this.isTree(refOrId))) return refOrId;
1954
1972
  if (refOrId === "HEAD") throw new NoHeadError("HEAD cannot be resolved");
1955
1973
  if (e instanceof RefNotFoundError) {
1956
1974
  throw new RefNotFoundError(refOrId, `bad revision '${refOrId}'`);
@@ -25,14 +25,15 @@ import {
25
25
  formatFetchHead,
26
26
  matchDestination,
27
27
  matchSource,
28
+ ObjectFormatMismatchError,
28
29
  parseRefSpec,
29
30
  type RefSpec,
30
31
  type TagMode,
31
32
  type FetchOptions as TransportFetchOptions,
32
33
  transportSummaryWidth,
33
- ZERO_OID,
34
34
  } from "@statewalker/vcs-transport";
35
35
  import { parseGitBool } from "@statewalker/vcs-utils";
36
+ import { nullObjectIdLike } from "@statewalker/vcs-utils/hash";
36
37
  import type { WorktreeAdmin } from "@statewalker/vcs-working-tree";
37
38
  import {
38
39
  FetchIntoCheckedOutBranchError,
@@ -658,6 +659,9 @@ export class FetchCommand extends TransportCommand<FetchResult> {
658
659
  this.checkCallable();
659
660
  this.setCallable(false);
660
661
 
662
+ // remote_get: a remote the configuration gives no URL may come from .git/remotes/ or
663
+ // .git/branches/ (git 2.x); 2.5x warns that it read one
664
+ const legacyMessages = await this.loadLegacyRemote(this.remote);
661
665
  // Resolve remote URL
662
666
  const remoteUrl = await this.resolveRemoteUrl(this.remote);
663
667
  if (!remoteUrl) {
@@ -716,7 +720,7 @@ export class FetchCommand extends TransportCommand<FetchResult> {
716
720
  : (this.refmap ?? configured).filter((spec) => spec !== "");
717
721
  // do_fetch: refs/remotes/<name>/HEAD follows the remote HEAD on a configured fetch
718
722
  const followHead = fromConfig && configured.length > 0 ? this.followRemoteHead(store) : null;
719
- const messages: string[] = [];
723
+ const messages: string[] = [...legacyMessages];
720
724
  const errors: string[] = [];
721
725
  const trackingUpdates: TrackingRefUpdate[] = [];
722
726
 
@@ -797,7 +801,10 @@ export class FetchCommand extends TransportCommand<FetchResult> {
797
801
  return defer ? { deletes: pruned.map((u) => u.localRef) } : undefined;
798
802
  },
799
803
  missingRefsOk: useConfig,
800
- tags: toTagMode(this.effectiveTagOption()),
804
+ tags:
805
+ !this.tagOptionSet && store.followsTagsAlways(this.remote)
806
+ ? "auto-always"
807
+ : toTagMode(this.effectiveTagOption()),
801
808
  depth: this.depth,
802
809
  deepenSince: this.shallowSince,
803
810
  deepenNot: this.shallowExcludes.length > 0 ? this.shallowExcludes : undefined,
@@ -839,6 +846,10 @@ export class FetchCommand extends TransportCommand<FetchResult> {
839
846
  if (message.startsWith("couldn't find remote ref ")) {
840
847
  throw new TransportError(`fatal: ${message}`, undefined, { cause: error });
841
848
  }
849
+ // fetch-pack dies: the remote names objects with another hash
850
+ if (error instanceof ObjectFormatMismatchError) {
851
+ throw new TransportError(`fatal: ${message}`, undefined, { cause: error });
852
+ }
842
853
  if (error instanceof FetchCheckError) {
843
854
  // fetch-pack ran: its start-up warnings came first
844
855
  const lines = [...fsckWarnings, ...error.lines];
@@ -985,6 +996,7 @@ export class FetchCommand extends TransportCommand<FetchResult> {
985
996
  const lines: string[] = [];
986
997
  const pruned = tracking.filter((u) => u.status === RefUpdateStatus.DELETED);
987
998
  if (pruned.length > 0) {
999
+ const ZERO_OID = this.objectFormat.nullId;
988
1000
  const ids = pruned.flatMap((u) => [ZERO_OID, u.oldObjectId ?? ZERO_OID]);
989
1001
  const width = transportSummaryWidth(ids, await this.idAbbreviator().lengths(ids));
990
1002
  const prunedEntries = pruned.map((u) => ({
@@ -1029,7 +1041,7 @@ export class FetchCommand extends TransportCommand<FetchResult> {
1029
1041
  summary: fetchHeadKind(f.remoteRef),
1030
1042
  remote: f.remoteRef,
1031
1043
  local: "FETCH_HEAD",
1032
- oldOid: ZERO_OID,
1044
+ oldOid: this.objectFormat.nullId,
1033
1045
  newOid: f.oid,
1034
1046
  });
1035
1047
  }
@@ -1476,5 +1488,7 @@ function refusedUpdate(result: FetchClientResult): boolean {
1476
1488
  * the old and new ids in full (zeros for none), the local ref's full name.
1477
1489
  */
1478
1490
  function porcelainLine(entry: FetchDisplayEntry): string {
1479
- return `${entry.code} ${entry.oldOid ?? ZERO_OID} ${entry.newOid ?? ZERO_OID} ${entry.local}`;
1491
+ // zeros of the width of the entry's other id (the repository's format)
1492
+ const zero = nullObjectIdLike(entry.oldOid ?? entry.newOid);
1493
+ return `${entry.code} ${entry.oldOid ?? zero} ${entry.newOid ?? zero} ${entry.local}`;
1480
1494
  }
@@ -7,6 +7,7 @@
7
7
  */
8
8
 
9
9
  import type { ObjectId, PersonIdent } from "@statewalker/vcs-core";
10
+ import { nullObjectIdLike } from "@statewalker/vcs-utils";
10
11
  import { decodeGitPath, encodeGitPath } from "@statewalker/vcs-utils/encoding";
11
12
  import { GitCommand } from "../git-command.js";
12
13
  import { ChangeType } from "../results/diff-entry.js";
@@ -720,7 +721,11 @@ export class FormatPatchCommand extends GitCommand<FormatPatchResult> {
720
721
  ...(!del && e.newId ? { new: { mode: e.newMode as number, id: e.newId } } : {}),
721
722
  });
722
723
  }
723
- ids.push(changes.length ? await patchIdOfChanges(this.blobs, changes) : "0".repeat(40));
724
+ ids.push(
725
+ changes.length
726
+ ? await patchIdOfChanges(this.blobs, changes, { objectFormat: this.objectFormat })
727
+ : this.objectFormat.nullId,
728
+ );
724
729
  }
725
730
  let text = `\nbase-commit: ${base}\n`;
726
731
  for (let i = ids.length - 1; i >= 0; i--) text += `prerequisite-patch-id: ${ids[i]}\n`;
@@ -760,7 +765,7 @@ export class FormatPatchCommand extends GitCommand<FormatPatchResult> {
760
765
  const date = fromIdent
761
766
  ? { timestamp: 0, tzOffset: "+0000" }
762
767
  : { timestamp: mail.committer.timestamp, tzOffset: mail.committer.tzOffset };
763
- let text = `From ${mail.zeroCommit ? "0".repeat(40) : head} Mon Sep 17 00:00:00 2001\n`;
768
+ let text = `From ${mail.zeroCommit ? nullObjectIdLike(head) : head} Mon Sep 17 00:00:00 2001\n`;
764
769
  text += mail.thread;
765
770
  text += mailFromHeader(who.name, who.email, { encodeHeaders: mail.encodeHeaders });
766
771
  text += `Date: ${showDate(date.timestamp, date.tzOffset, { type: "rfc2822", local: false })}\n`;