@statewalker/vcs-commands 0.1.1 → 0.3.1
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/commit-command.d.ts.map +1 -1
- package/dist/commands/init-command.d.ts.map +1 -1
- package/dist/commands/push-command.d.ts.map +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts +1 -1
- package/dist/core-commands/checkout.command.impl.d.ts.map +1 -1
- package/dist/git-command.d.ts.map +1 -1
- package/package.json +19 -14
- package/src/commands/add-command.ts +494 -0
- package/src/commands/blame-command.ts +1241 -0
- package/src/commands/branch-command.ts +487 -0
- package/src/commands/checkout-command.ts +681 -0
- package/src/commands/cherry-pick-command.ts +544 -0
- package/src/commands/clean-command.ts +218 -0
- package/src/commands/clone-command.ts +532 -0
- package/src/commands/commit-command.ts +529 -0
- package/src/commands/describe-command.ts +454 -0
- package/src/commands/diff-command.ts +257 -0
- package/src/commands/fetch-command.ts +615 -0
- package/src/commands/gc-command.ts +155 -0
- package/src/commands/index.ts +31 -0
- package/src/commands/init-command.ts +365 -0
- package/src/commands/log-command.ts +549 -0
- package/src/commands/ls-remote-command.ts +237 -0
- package/src/commands/merge-command.ts +750 -0
- package/src/commands/pack-refs-command.ts +101 -0
- package/src/commands/pull-command.ts +258 -0
- package/src/commands/push-command.ts +682 -0
- package/src/commands/rebase-command.ts +665 -0
- package/src/commands/reflog-command.ts +68 -0
- package/src/commands/remote-command.ts +385 -0
- package/src/commands/reset-command.ts +292 -0
- package/src/commands/revert-command.ts +552 -0
- package/src/commands/rm-command.ts +178 -0
- package/src/commands/stash-apply-command.ts +402 -0
- package/src/commands/stash-create-command.ts +339 -0
- package/src/commands/stash-drop-command.ts +115 -0
- package/src/commands/stash-list-command.ts +116 -0
- package/src/commands/status-command.ts +173 -0
- package/src/commands/tag-command.ts +378 -0
- package/src/commands/tree-restore.ts +126 -0
- package/src/core-commands/add.command.impl.ts +274 -0
- package/src/core-commands/add.command.ts +81 -0
- package/src/core-commands/checkout.command.impl.ts +586 -0
- package/src/core-commands/checkout.command.ts +125 -0
- package/src/core-commands/index.ts +13 -0
- package/src/errors/add-errors.ts +20 -0
- package/src/errors/checkout-errors.ts +42 -0
- package/src/errors/command-errors.ts +68 -0
- package/src/errors/commit-errors.ts +40 -0
- package/src/errors/git-api-error.ts +11 -0
- package/src/errors/index.ts +12 -0
- package/src/errors/merge-errors.ts +94 -0
- package/src/errors/rebase-errors.ts +34 -0
- package/src/errors/ref-errors.ts +100 -0
- package/src/errors/remote-errors.ts +27 -0
- package/src/errors/stash-errors.ts +42 -0
- package/src/errors/tag-errors.ts +16 -0
- package/src/errors/transport-errors.ts +68 -0
- package/src/git-command.ts +421 -0
- package/src/git.ts +910 -0
- package/src/index.ts +18 -0
- package/src/pack-import/index.ts +8 -0
- package/src/pack-import/pack-import.ts +24 -0
- package/src/remote-config/index.ts +12 -0
- package/src/remote-config/remote-config-store.ts +285 -0
- package/src/rename/index.ts +5 -0
- package/src/rename/similarity-index.ts +185 -0
- package/src/results/cherry-pick-result.ts +35 -0
- package/src/results/clone-result.ts +25 -0
- package/src/results/diff-entry.ts +162 -0
- package/src/results/diff-formatter.ts +416 -0
- package/src/results/fetch-result.ts +91 -0
- package/src/results/index.ts +13 -0
- package/src/results/init-result.ts +29 -0
- package/src/results/merge-result.ts +135 -0
- package/src/results/pull-result.ts +27 -0
- package/src/results/push-result.ts +86 -0
- package/src/results/rebase-result.ts +140 -0
- package/src/results/revert-result.ts +35 -0
- package/src/results/stash-result.ts +60 -0
- package/src/results/status-result.ts +65 -0
- package/src/transport-command.ts +125 -0
- package/src/tree-merge/index.ts +8 -0
- package/src/tree-merge/tree-merge.ts +249 -0
- package/src/types.ts +70 -0
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
import { serializeCommit, serializeTree, type TreeEntry } from "@statewalker/vcs-core";
|
|
2
|
+
import {
|
|
3
|
+
mapRejectReason,
|
|
4
|
+
type PushCommandResult,
|
|
5
|
+
type PushObject,
|
|
6
|
+
push as transportPush,
|
|
7
|
+
} from "@statewalker/vcs-transport";
|
|
8
|
+
|
|
9
|
+
import { InvalidRemoteError, NonFastForwardError, PushRejectedException } from "../errors/index.js";
|
|
10
|
+
import { RemoteConfigStore } from "../remote-config/index.js";
|
|
11
|
+
import { type PushResult, PushStatus, type RemoteRefUpdate } from "../results/push-result.js";
|
|
12
|
+
import { TransportCommand } from "../transport-command.js";
|
|
13
|
+
|
|
14
|
+
/** All-zero object id — Git's "no object", used to express a ref delete. */
|
|
15
|
+
const ZERO_OBJECT_ID = "0".repeat(40);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* How the transport's rejection reasons land on the statuses this package
|
|
19
|
+
* reports.
|
|
20
|
+
*
|
|
21
|
+
* The two vocabularies are not the same: the transport tells seven kinds of
|
|
22
|
+
* rejection apart, while PushStatus — following JGit's RemoteRefUpdate — keeps
|
|
23
|
+
* only the non-fast-forward case separate, because that is the one callers act
|
|
24
|
+
* on (callOrThrow raises NonFastForwardError for it). Everything else is a
|
|
25
|
+
* rejection whose detail lives in the server's message, so it collapses to
|
|
26
|
+
* REJECTED_OTHER. Spelled out per reason rather than defaulted, so that a new
|
|
27
|
+
* transport reason is a compile error here instead of a silent REJECTED_OTHER.
|
|
28
|
+
*/
|
|
29
|
+
const REJECT_REASON_STATUS: Record<PushCommandResult, PushStatus> = {
|
|
30
|
+
NOT_ATTEMPTED: PushStatus.NOT_ATTEMPTED,
|
|
31
|
+
OK: PushStatus.OK,
|
|
32
|
+
REJECTED_NONFASTFORWARD: PushStatus.REJECTED_NONFASTFORWARD,
|
|
33
|
+
REJECTED_NOCREATE: PushStatus.REJECTED_OTHER,
|
|
34
|
+
REJECTED_NODELETE: PushStatus.REJECTED_OTHER,
|
|
35
|
+
REJECTED_CURRENT_BRANCH: PushStatus.REJECTED_OTHER,
|
|
36
|
+
REJECTED_MISSING_OBJECT: PushStatus.REJECTED_OTHER,
|
|
37
|
+
REJECTED_OTHER_REASON: PushStatus.REJECTED_OTHER,
|
|
38
|
+
LOCK_FAILURE: PushStatus.REJECTED_OTHER,
|
|
39
|
+
ATOMIC_REJECTED: PushStatus.REJECTED_OTHER,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Classify a server's rejection message.
|
|
44
|
+
*
|
|
45
|
+
* Reuses the transport's `mapRejectReason` so both sides read the same wire
|
|
46
|
+
* text the same way; a message the transport cannot place — or none at all —
|
|
47
|
+
* ends up as REJECTED_OTHER.
|
|
48
|
+
*/
|
|
49
|
+
function classifyRejection(message: string | undefined): PushStatus {
|
|
50
|
+
return REJECT_REASON_STATUS[mapRejectReason(message ?? "")];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Split a refspec into its source and destination ref names.
|
|
55
|
+
*
|
|
56
|
+
* Mirrors the parsing the transport performs so that the destination keys we
|
|
57
|
+
* derive here line up exactly with the ones it reports back:
|
|
58
|
+
* an optional leading `+` (force) is stripped, and a refspec with no `:` pushes
|
|
59
|
+
* a ref to its own name.
|
|
60
|
+
*/
|
|
61
|
+
function parseRefSpec(refSpec: string): { source: string; dest: string } {
|
|
62
|
+
const normalized = refSpec.startsWith("+") ? refSpec.slice(1) : refSpec;
|
|
63
|
+
const [source = "", dest] = normalized.split(":");
|
|
64
|
+
return { source, dest: dest || source };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Push objects and refs to a remote repository.
|
|
69
|
+
*
|
|
70
|
+
* Equivalent to `git push`.
|
|
71
|
+
*
|
|
72
|
+
* Based on JGit's PushCommand.
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* // Push current branch to origin
|
|
77
|
+
* const result = await git.push().call();
|
|
78
|
+
*
|
|
79
|
+
* // Push to specific remote
|
|
80
|
+
* const result = await git.push()
|
|
81
|
+
* .setRemote("upstream")
|
|
82
|
+
* .call();
|
|
83
|
+
*
|
|
84
|
+
* // Push specific refs
|
|
85
|
+
* const result = await git.push()
|
|
86
|
+
* .add("refs/heads/feature:refs/heads/feature")
|
|
87
|
+
* .call();
|
|
88
|
+
*
|
|
89
|
+
* // Force push
|
|
90
|
+
* const result = await git.push()
|
|
91
|
+
* .setForce(true)
|
|
92
|
+
* .call();
|
|
93
|
+
*
|
|
94
|
+
* // Delete remote branch (empty source ref). The resulting RemoteRefUpdate has
|
|
95
|
+
* // `delete: true` and an all-zero `newObjectId`.
|
|
96
|
+
* const result = await git.push()
|
|
97
|
+
* .add(":refs/heads/old-branch")
|
|
98
|
+
* .call();
|
|
99
|
+
*
|
|
100
|
+
* // Atomic push (all-or-nothing)
|
|
101
|
+
* const result = await git.push()
|
|
102
|
+
* .setAtomic(true)
|
|
103
|
+
* .call();
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export class PushCommand extends TransportCommand<PushResult> {
|
|
107
|
+
private remote = "origin";
|
|
108
|
+
private refSpecs: string[] = [];
|
|
109
|
+
private force = false;
|
|
110
|
+
private atomic = false;
|
|
111
|
+
private thin = true;
|
|
112
|
+
private dryRun = false;
|
|
113
|
+
private pushAll = false;
|
|
114
|
+
private pushTags = false;
|
|
115
|
+
private useBitmaps = true;
|
|
116
|
+
private pushOptions: string[] = [];
|
|
117
|
+
private receivePack?: string;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Set the remote to push to.
|
|
121
|
+
*
|
|
122
|
+
* Can be either a remote name (e.g., "origin") or a URL.
|
|
123
|
+
* Default is "origin".
|
|
124
|
+
*
|
|
125
|
+
* @param remote Remote name or URL
|
|
126
|
+
*/
|
|
127
|
+
setRemote(remote: string): this {
|
|
128
|
+
this.checkCallable();
|
|
129
|
+
this.remote = remote;
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Get the remote being pushed to.
|
|
135
|
+
*/
|
|
136
|
+
getRemote(): string {
|
|
137
|
+
return this.remote;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Add a refspec to push.
|
|
142
|
+
*
|
|
143
|
+
* @param refSpec Refspec string (e.g., "refs/heads/main:refs/heads/main")
|
|
144
|
+
*/
|
|
145
|
+
add(refSpec: string): this {
|
|
146
|
+
this.checkCallable();
|
|
147
|
+
this.refSpecs.push(refSpec);
|
|
148
|
+
return this;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Set refspecs to push.
|
|
153
|
+
*
|
|
154
|
+
* @param refSpecs Refspec strings
|
|
155
|
+
*/
|
|
156
|
+
setRefSpecs(...refSpecs: string[]): this {
|
|
157
|
+
this.checkCallable();
|
|
158
|
+
this.refSpecs = refSpecs;
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Set force push mode.
|
|
164
|
+
*
|
|
165
|
+
* When enabled, allows non-fast-forward updates.
|
|
166
|
+
*
|
|
167
|
+
* @param force Whether to force push
|
|
168
|
+
*/
|
|
169
|
+
setForce(force: boolean): this {
|
|
170
|
+
this.checkCallable();
|
|
171
|
+
this.force = force;
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Whether force push is enabled.
|
|
177
|
+
*/
|
|
178
|
+
isForce(): boolean {
|
|
179
|
+
return this.force;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Set atomic push mode.
|
|
184
|
+
*
|
|
185
|
+
* When enabled, either all refs update or none do.
|
|
186
|
+
*
|
|
187
|
+
* @param atomic Whether to use atomic push
|
|
188
|
+
*/
|
|
189
|
+
setAtomic(atomic: boolean): this {
|
|
190
|
+
this.checkCallable();
|
|
191
|
+
this.atomic = atomic;
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Whether atomic push is enabled.
|
|
197
|
+
*/
|
|
198
|
+
isAtomic(): boolean {
|
|
199
|
+
return this.atomic;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Set thin-pack preference.
|
|
204
|
+
*
|
|
205
|
+
* @param thin Whether to use thin packs
|
|
206
|
+
*/
|
|
207
|
+
setThin(thin: boolean): this {
|
|
208
|
+
this.checkCallable();
|
|
209
|
+
this.thin = thin;
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Whether thin packs are enabled.
|
|
215
|
+
*/
|
|
216
|
+
isThin(): boolean {
|
|
217
|
+
return this.thin;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Set dry run mode.
|
|
222
|
+
*
|
|
223
|
+
* In dry run mode, refs are not actually updated on remote.
|
|
224
|
+
*
|
|
225
|
+
* @param dryRun Whether to do a dry run
|
|
226
|
+
*/
|
|
227
|
+
setDryRun(dryRun: boolean): this {
|
|
228
|
+
this.checkCallable();
|
|
229
|
+
this.dryRun = dryRun;
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Whether dry run mode is enabled.
|
|
235
|
+
*/
|
|
236
|
+
isDryRun(): boolean {
|
|
237
|
+
return this.dryRun;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Set whether to push all branches.
|
|
242
|
+
*
|
|
243
|
+
* @param pushAll Whether to push all branches
|
|
244
|
+
*/
|
|
245
|
+
setPushAll(pushAll: boolean): this {
|
|
246
|
+
this.checkCallable();
|
|
247
|
+
this.pushAll = pushAll;
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Set whether to push all tags.
|
|
253
|
+
*
|
|
254
|
+
* @param pushTags Whether to push all tags
|
|
255
|
+
*/
|
|
256
|
+
setPushTags(pushTags: boolean): this {
|
|
257
|
+
this.checkCallable();
|
|
258
|
+
this.pushTags = pushTags;
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Set whether to use bitmaps for push.
|
|
264
|
+
*
|
|
265
|
+
* Default is true.
|
|
266
|
+
*
|
|
267
|
+
* @param useBitmaps Whether to use bitmaps
|
|
268
|
+
*/
|
|
269
|
+
setUseBitmaps(useBitmaps: boolean): this {
|
|
270
|
+
this.checkCallable();
|
|
271
|
+
this.useBitmaps = useBitmaps;
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Whether bitmaps are used for push.
|
|
277
|
+
*/
|
|
278
|
+
isUseBitmaps(): boolean {
|
|
279
|
+
return this.useBitmaps;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Set push options associated with the push operation.
|
|
284
|
+
*
|
|
285
|
+
* Push options are strings passed to the receive-pack on the server
|
|
286
|
+
* side, where they can be used by hooks.
|
|
287
|
+
*
|
|
288
|
+
* @param options Push option strings
|
|
289
|
+
*/
|
|
290
|
+
setPushOptions(options: string[]): this {
|
|
291
|
+
this.checkCallable();
|
|
292
|
+
this.pushOptions = [...options];
|
|
293
|
+
return this;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Get push options.
|
|
298
|
+
*/
|
|
299
|
+
getPushOptions(): string[] {
|
|
300
|
+
return [...this.pushOptions];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Set the remote executable providing receive-pack service.
|
|
305
|
+
*
|
|
306
|
+
* @param receivePack Name of the remote executable
|
|
307
|
+
*/
|
|
308
|
+
setReceivePack(receivePack: string): this {
|
|
309
|
+
this.checkCallable();
|
|
310
|
+
this.receivePack = receivePack;
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Get the receive-pack executable name.
|
|
316
|
+
*/
|
|
317
|
+
getReceivePack(): string | undefined {
|
|
318
|
+
return this.receivePack;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Execute the push operation.
|
|
323
|
+
*
|
|
324
|
+
* @returns Push result with updated refs
|
|
325
|
+
* @throws InvalidRemoteError if remote cannot be resolved
|
|
326
|
+
* @throws PushRejectedException if push is rejected
|
|
327
|
+
*/
|
|
328
|
+
async call(): Promise<PushResult> {
|
|
329
|
+
this.checkCallable();
|
|
330
|
+
this.setCallable(false);
|
|
331
|
+
|
|
332
|
+
// Resolve remote URL
|
|
333
|
+
const remoteUrl = await this.resolveRemoteUrl(this.remote);
|
|
334
|
+
if (!remoteUrl) {
|
|
335
|
+
throw new InvalidRemoteError(this.remote);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Build refspecs
|
|
339
|
+
const refspecs = await this.buildRefSpecs();
|
|
340
|
+
|
|
341
|
+
// Map each destination ref back to the local ref it came from. The transport
|
|
342
|
+
// keys its update results by the destination ref, and carries no object ids,
|
|
343
|
+
// so this is what lets us report srcRef/newObjectId per update below.
|
|
344
|
+
const sourceByDest = new Map<string, string>();
|
|
345
|
+
for (const spec of refspecs) {
|
|
346
|
+
const { source, dest } = parseRefSpec(spec);
|
|
347
|
+
sourceByDest.set(dest, source);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (refspecs.length === 0) {
|
|
351
|
+
// Nothing to push
|
|
352
|
+
return {
|
|
353
|
+
uri: remoteUrl,
|
|
354
|
+
remoteUpdates: [],
|
|
355
|
+
bytesSent: 0,
|
|
356
|
+
objectCount: 0,
|
|
357
|
+
messages: [],
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Build exportPack if history supports serialization (HistoryWithOperations)
|
|
362
|
+
const history = this._workingCopy.history;
|
|
363
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
364
|
+
const serialization = (history as any).serialization as
|
|
365
|
+
| { createPack(objectIds: AsyncIterable<string>): AsyncIterable<Uint8Array> }
|
|
366
|
+
| undefined;
|
|
367
|
+
|
|
368
|
+
const exportPack = serialization
|
|
369
|
+
? async function* (wants: Set<string>, exclude: Set<string>): AsyncIterable<Uint8Array> {
|
|
370
|
+
const objectIds = history.collectReachableObjects(wants, exclude);
|
|
371
|
+
yield* serialization.createPack(objectIds);
|
|
372
|
+
}
|
|
373
|
+
: undefined;
|
|
374
|
+
|
|
375
|
+
// Object ids resolved for each source ref, captured as the transport asks
|
|
376
|
+
// for them, so we report the value that was really sent on the wire.
|
|
377
|
+
const resolvedLocalOids = new Map<string, string>();
|
|
378
|
+
|
|
379
|
+
// Execute push using high-level push API
|
|
380
|
+
const transportResult = await transportPush({
|
|
381
|
+
url: remoteUrl,
|
|
382
|
+
refspecs,
|
|
383
|
+
auth: this.credentials,
|
|
384
|
+
headers: this.headers,
|
|
385
|
+
timeout: this.timeout,
|
|
386
|
+
force: this.force,
|
|
387
|
+
atomic: this.atomic,
|
|
388
|
+
onProgressMessage: this.progressMessageCallback,
|
|
389
|
+
exportPack,
|
|
390
|
+
getLocalRef: async (refName: string) => {
|
|
391
|
+
// A refspec with an empty source (":refs/heads/x") is a delete request:
|
|
392
|
+
// Git expresses it as an update to the all-zero object id.
|
|
393
|
+
if (refName === "") {
|
|
394
|
+
resolvedLocalOids.set(refName, ZERO_OBJECT_ID);
|
|
395
|
+
return ZERO_OBJECT_ID;
|
|
396
|
+
}
|
|
397
|
+
const ref = await this.refsStore.resolve(refName);
|
|
398
|
+
// Memoise exactly the object id the transport used for this source ref,
|
|
399
|
+
// so the reported newObjectId is the value that was actually pushed.
|
|
400
|
+
if (ref?.objectId !== undefined) {
|
|
401
|
+
resolvedLocalOids.set(refName, ref.objectId);
|
|
402
|
+
}
|
|
403
|
+
return ref?.objectId;
|
|
404
|
+
},
|
|
405
|
+
getObjectsToPush: (newIds: string[], oldIds: string[]) =>
|
|
406
|
+
this.getObjectsToPush(newIds, oldIds),
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// Convert to PushResult.
|
|
410
|
+
//
|
|
411
|
+
// `transportResult.updates` is keyed by the DESTINATION ref name and carries
|
|
412
|
+
// {ok, message, oldOid}: the new object ids come from the refspecs we built
|
|
413
|
+
// and the source-ref lookups the transport performed through `getLocalRef`
|
|
414
|
+
// above, while `oldOid` is the remote's pre-push value — the expectation the
|
|
415
|
+
// server compared its ref against — as read from its advertisement.
|
|
416
|
+
const remoteUpdates: RemoteRefUpdate[] = [];
|
|
417
|
+
for (const [refName, updateResult] of transportResult.updates) {
|
|
418
|
+
const srcRef = sourceByDest.get(refName);
|
|
419
|
+
const newObjectId = srcRef !== undefined ? resolvedLocalOids.get(srcRef) : undefined;
|
|
420
|
+
remoteUpdates.push({
|
|
421
|
+
// Omit srcRef for a delete: there is no local ref behind it.
|
|
422
|
+
...(srcRef ? { srcRef } : {}),
|
|
423
|
+
remoteName: refName,
|
|
424
|
+
expectedOldObjectId: updateResult.oldOid,
|
|
425
|
+
newObjectId: newObjectId ?? "",
|
|
426
|
+
status: updateResult.ok ? PushStatus.OK : classifyRejection(updateResult.message),
|
|
427
|
+
message: updateResult.message,
|
|
428
|
+
forceUpdate: this.force,
|
|
429
|
+
delete: newObjectId === ZERO_OBJECT_ID,
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return {
|
|
434
|
+
uri: remoteUrl,
|
|
435
|
+
remoteUpdates,
|
|
436
|
+
bytesSent: transportResult.bytesSent,
|
|
437
|
+
objectCount: transportResult.objectCount,
|
|
438
|
+
messages: [],
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Push and throw on failure.
|
|
444
|
+
*/
|
|
445
|
+
async callOrThrow(): Promise<PushResult> {
|
|
446
|
+
const result = await this.call();
|
|
447
|
+
|
|
448
|
+
for (const update of result.remoteUpdates) {
|
|
449
|
+
if (update.status === PushStatus.REJECTED_NONFASTFORWARD) {
|
|
450
|
+
throw new NonFastForwardError(update.remoteName, result.uri);
|
|
451
|
+
}
|
|
452
|
+
if (update.status === PushStatus.REJECTED_OTHER || update.status === PushStatus.FAILED) {
|
|
453
|
+
throw new PushRejectedException(
|
|
454
|
+
update.remoteName,
|
|
455
|
+
update.message || "rejected",
|
|
456
|
+
result.uri,
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
return result;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Resolve remote name to URL.
|
|
466
|
+
*
|
|
467
|
+
* A named remote resolves through the working copy configuration, preferring
|
|
468
|
+
* `remote.<name>.pushurl` over `remote.<name>.url` as git does; an
|
|
469
|
+
* unconfigured name is passed through unchanged.
|
|
470
|
+
*/
|
|
471
|
+
private async resolveRemoteUrl(remote: string): Promise<string | undefined> {
|
|
472
|
+
// If it looks like a URL, use it directly
|
|
473
|
+
if (remote.includes("://") || remote.includes("@")) {
|
|
474
|
+
return remote;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return RemoteConfigStore.from(this.workingCopy).urlFor(remote, { push: true }) ?? remote;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Build refspecs for the push.
|
|
482
|
+
*/
|
|
483
|
+
private async buildRefSpecs(): Promise<string[]> {
|
|
484
|
+
const specs: string[] = [];
|
|
485
|
+
|
|
486
|
+
// Add explicit refspecs
|
|
487
|
+
for (const spec of this.refSpecs) {
|
|
488
|
+
if (this.force && !spec.startsWith("+")) {
|
|
489
|
+
specs.push(`+${spec}`);
|
|
490
|
+
} else {
|
|
491
|
+
specs.push(spec);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Push all branches
|
|
496
|
+
if (this.pushAll) {
|
|
497
|
+
for await (const ref of this.refsStore.list("refs/heads/")) {
|
|
498
|
+
const spec = `${ref.name}:${ref.name}`;
|
|
499
|
+
specs.push(this.force ? `+${spec}` : spec);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// Push all tags
|
|
504
|
+
if (this.pushTags) {
|
|
505
|
+
for await (const ref of this.refsStore.list("refs/tags/")) {
|
|
506
|
+
const spec = `${ref.name}:${ref.name}`;
|
|
507
|
+
specs.push(this.force ? `+${spec}` : spec);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// Default: push current branch
|
|
512
|
+
if (specs.length === 0) {
|
|
513
|
+
const currentBranch = await this.getCurrentBranch();
|
|
514
|
+
if (currentBranch) {
|
|
515
|
+
const spec = `${currentBranch}:${currentBranch}`;
|
|
516
|
+
specs.push(this.force ? `+${spec}` : spec);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return specs;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* Get objects to push.
|
|
525
|
+
*
|
|
526
|
+
* Returns objects reachable from newIds but not from oldIds.
|
|
527
|
+
*/
|
|
528
|
+
private async *getObjectsToPush(newIds: string[], oldIds: string[]): AsyncIterable<PushObject> {
|
|
529
|
+
// Build set of commits to exclude (already on remote)
|
|
530
|
+
const excludeCommits = new Set<string>();
|
|
531
|
+
for (const oldId of oldIds) {
|
|
532
|
+
if (oldId !== "0".repeat(40)) {
|
|
533
|
+
await this.collectReachableCommits(oldId, excludeCommits);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// Walk from new commits and yield objects not in exclude set
|
|
538
|
+
const visitedObjects = new Set<string>();
|
|
539
|
+
const commitQueue: string[] = [...newIds.filter((id) => id !== "0".repeat(40))];
|
|
540
|
+
|
|
541
|
+
while (commitQueue.length > 0) {
|
|
542
|
+
const commitId = commitQueue.shift();
|
|
543
|
+
if (commitId === undefined) break;
|
|
544
|
+
|
|
545
|
+
if (excludeCommits.has(commitId) || visitedObjects.has(commitId)) {
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
visitedObjects.add(commitId);
|
|
549
|
+
|
|
550
|
+
try {
|
|
551
|
+
const commit = await this.commits.load(commitId);
|
|
552
|
+
if (!commit) continue;
|
|
553
|
+
|
|
554
|
+
// Yield commit object
|
|
555
|
+
yield await this.loadObjectForPush(commitId, 1);
|
|
556
|
+
|
|
557
|
+
// Yield tree and its contents
|
|
558
|
+
yield* this.yieldTreeObjects(commit.tree, visitedObjects);
|
|
559
|
+
|
|
560
|
+
// Queue parent commits
|
|
561
|
+
for (const parent of commit.parents) {
|
|
562
|
+
if (!excludeCommits.has(parent) && !visitedObjects.has(parent)) {
|
|
563
|
+
commitQueue.push(parent);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
} catch {
|
|
567
|
+
// Commit not found, skip
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Collect all commits reachable from a given commit.
|
|
574
|
+
*/
|
|
575
|
+
private async collectReachableCommits(
|
|
576
|
+
commitId: string,
|
|
577
|
+
result: Set<string>,
|
|
578
|
+
maxDepth = 1000,
|
|
579
|
+
): Promise<void> {
|
|
580
|
+
const queue: string[] = [commitId];
|
|
581
|
+
let depth = 0;
|
|
582
|
+
|
|
583
|
+
while (queue.length > 0 && depth < maxDepth) {
|
|
584
|
+
const id = queue.shift();
|
|
585
|
+
if (id === undefined) break;
|
|
586
|
+
if (result.has(id)) {
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
result.add(id);
|
|
590
|
+
depth++;
|
|
591
|
+
|
|
592
|
+
try {
|
|
593
|
+
const commit = await this.commits.load(id);
|
|
594
|
+
if (commit) queue.push(...commit.parents);
|
|
595
|
+
} catch {
|
|
596
|
+
// Commit not found, skip
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Yield tree and blob objects recursively.
|
|
603
|
+
*/
|
|
604
|
+
private async *yieldTreeObjects(
|
|
605
|
+
treeId: string,
|
|
606
|
+
visited: Set<string>,
|
|
607
|
+
): AsyncGenerator<PushObject> {
|
|
608
|
+
if (visited.has(treeId)) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
visited.add(treeId);
|
|
612
|
+
|
|
613
|
+
// Yield tree object
|
|
614
|
+
yield await this.loadObjectForPush(treeId, 2);
|
|
615
|
+
|
|
616
|
+
// Recursively process tree entries
|
|
617
|
+
for await (const entry of this.trees.loadTree(treeId)) {
|
|
618
|
+
if (visited.has(entry.id)) {
|
|
619
|
+
continue;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
const isTree = (entry.mode & 0o170000) === 0o040000;
|
|
623
|
+
if (isTree) {
|
|
624
|
+
yield* this.yieldTreeObjects(entry.id, visited);
|
|
625
|
+
} else {
|
|
626
|
+
// Blob
|
|
627
|
+
visited.add(entry.id);
|
|
628
|
+
yield await this.loadObjectForPush(entry.id, 3);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* Load an object for pushing.
|
|
635
|
+
*
|
|
636
|
+
* Serializes objects to raw Git format based on type:
|
|
637
|
+
* - type 1 (commit): serialize via serializeCommit()
|
|
638
|
+
* - type 2 (tree): serialize via serializeTree()
|
|
639
|
+
* - type 3 (blob): load raw content from blob store
|
|
640
|
+
*/
|
|
641
|
+
private async loadObjectForPush(objectId: string, type: number): Promise<PushObject> {
|
|
642
|
+
let content: Uint8Array;
|
|
643
|
+
|
|
644
|
+
if (type === 1) {
|
|
645
|
+
// Commit
|
|
646
|
+
const commit = await this.commits.load(objectId);
|
|
647
|
+
if (!commit) {
|
|
648
|
+
throw new Error(`Commit not found: ${objectId}`);
|
|
649
|
+
}
|
|
650
|
+
content = serializeCommit(commit);
|
|
651
|
+
} else if (type === 2) {
|
|
652
|
+
// Tree
|
|
653
|
+
const entries: TreeEntry[] = [];
|
|
654
|
+
for await (const entry of this.trees.loadTree(objectId)) {
|
|
655
|
+
entries.push(entry);
|
|
656
|
+
}
|
|
657
|
+
content = serializeTree(entries);
|
|
658
|
+
} else {
|
|
659
|
+
// Blob (type 3) or other
|
|
660
|
+
const chunks: Uint8Array[] = [];
|
|
661
|
+
const blobContent = await this.blobs.load(objectId);
|
|
662
|
+
if (blobContent) {
|
|
663
|
+
for await (const chunk of blobContent) {
|
|
664
|
+
chunks.push(chunk);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
668
|
+
content = new Uint8Array(totalLength);
|
|
669
|
+
let offset = 0;
|
|
670
|
+
for (const chunk of chunks) {
|
|
671
|
+
content.set(chunk, offset);
|
|
672
|
+
offset += chunk.length;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
return {
|
|
677
|
+
id: objectId,
|
|
678
|
+
type,
|
|
679
|
+
content,
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
}
|