@statewalker/vcs-commands 0.1.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +180 -466
- 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 +25 -16
- 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,487 @@
|
|
|
1
|
+
import type { ObjectId, Ref } from "@statewalker/vcs-core";
|
|
2
|
+
import { isSymbolicRef } from "@statewalker/vcs-core";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
CannotDeleteCurrentBranchError,
|
|
6
|
+
InvalidRefNameError,
|
|
7
|
+
NotMergedError,
|
|
8
|
+
RefAlreadyExistsError,
|
|
9
|
+
RefNotFoundError,
|
|
10
|
+
} from "../errors/index.js";
|
|
11
|
+
import { GitCommand } from "../git-command.js";
|
|
12
|
+
import { ListBranchMode } from "../types.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Validate a branch name according to Git rules.
|
|
16
|
+
*
|
|
17
|
+
* @param name Branch name to validate
|
|
18
|
+
* @returns true if valid
|
|
19
|
+
*/
|
|
20
|
+
function isValidBranchName(name: string): boolean {
|
|
21
|
+
// Cannot be empty
|
|
22
|
+
if (!name || name.length === 0) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Cannot start or end with /
|
|
27
|
+
if (name.startsWith("/") || name.endsWith("/")) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Cannot contain ..
|
|
32
|
+
if (name.includes("..")) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Cannot contain special characters (control chars, space, ~^:?*[\)
|
|
37
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: Git ref validation requires checking for control characters
|
|
38
|
+
if (/[\x00-\x1f\x7f ~^:?*[\\]/.test(name)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Cannot end with .lock
|
|
43
|
+
if (name.endsWith(".lock")) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Cannot start with -
|
|
48
|
+
if (name.startsWith("-")) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Cannot be @
|
|
53
|
+
if (name === "@") {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Cannot contain @{
|
|
58
|
+
if (name.includes("@{")) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Create a new branch.
|
|
67
|
+
*
|
|
68
|
+
* Equivalent to `git branch <name>`.
|
|
69
|
+
*
|
|
70
|
+
* Based on JGit's CreateBranchCommand.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // Create branch from HEAD
|
|
75
|
+
* await git.branchCreate().setName("feature").call();
|
|
76
|
+
*
|
|
77
|
+
* // Create branch from specific commit
|
|
78
|
+
* await git.branchCreate()
|
|
79
|
+
* .setName("hotfix")
|
|
80
|
+
* .setStartPoint("abc123")
|
|
81
|
+
* .call();
|
|
82
|
+
*
|
|
83
|
+
* // Force create (overwrite existing)
|
|
84
|
+
* await git.branchCreate()
|
|
85
|
+
* .setName("existing")
|
|
86
|
+
* .setForce(true)
|
|
87
|
+
* .call();
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export class CreateBranchCommand extends GitCommand<Ref> {
|
|
91
|
+
private name?: string;
|
|
92
|
+
private startPoint?: string;
|
|
93
|
+
private force = false;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Set the name of the branch to create.
|
|
97
|
+
*
|
|
98
|
+
* @param name Branch name (without refs/heads/ prefix)
|
|
99
|
+
*/
|
|
100
|
+
setName(name: string): this {
|
|
101
|
+
this.checkCallable();
|
|
102
|
+
this.name = name;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Set the starting point for the new branch.
|
|
108
|
+
*
|
|
109
|
+
* Can be a commit ID, branch name, or tag name.
|
|
110
|
+
* If not set, defaults to HEAD.
|
|
111
|
+
*
|
|
112
|
+
* @param startPoint Commit-ish to start from
|
|
113
|
+
*/
|
|
114
|
+
setStartPoint(startPoint: string): this {
|
|
115
|
+
this.checkCallable();
|
|
116
|
+
this.startPoint = startPoint;
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Set whether to force create the branch.
|
|
122
|
+
*
|
|
123
|
+
* If true, overwrites an existing branch.
|
|
124
|
+
*
|
|
125
|
+
* @param force Whether to force create
|
|
126
|
+
*/
|
|
127
|
+
setForce(force: boolean): this {
|
|
128
|
+
this.checkCallable();
|
|
129
|
+
this.force = force;
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Execute the branch creation.
|
|
135
|
+
*
|
|
136
|
+
* @returns The created branch ref
|
|
137
|
+
* @throws InvalidRefNameError if branch name is invalid
|
|
138
|
+
* @throws RefAlreadyExistsError if branch exists and force is false
|
|
139
|
+
*/
|
|
140
|
+
async call(): Promise<Ref> {
|
|
141
|
+
this.checkCallable();
|
|
142
|
+
|
|
143
|
+
if (!this.name) {
|
|
144
|
+
throw new InvalidRefNameError("", "Branch name is required");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!isValidBranchName(this.name)) {
|
|
148
|
+
throw new InvalidRefNameError(this.name);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const refName = `refs/heads/${this.name}`;
|
|
152
|
+
|
|
153
|
+
// Check if already exists
|
|
154
|
+
if (!this.force && (await this.refsStore.has(refName))) {
|
|
155
|
+
throw new RefAlreadyExistsError(refName, `Branch '${this.name}' already exists`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Resolve start point
|
|
159
|
+
const targetId = this.startPoint
|
|
160
|
+
? await this.resolveRef(this.startPoint)
|
|
161
|
+
: await this.resolveHead();
|
|
162
|
+
|
|
163
|
+
await this.refsStore.set(refName, targetId);
|
|
164
|
+
|
|
165
|
+
this.setCallable(false);
|
|
166
|
+
|
|
167
|
+
const ref = await this.refsStore.get(refName);
|
|
168
|
+
return ref as Ref;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Delete branches.
|
|
174
|
+
*
|
|
175
|
+
* Equivalent to `git branch -d` or `git branch -D`.
|
|
176
|
+
*
|
|
177
|
+
* Based on JGit's DeleteBranchCommand.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```typescript
|
|
181
|
+
* // Delete a single branch
|
|
182
|
+
* await git.branchDelete().setBranchNames("feature").call();
|
|
183
|
+
*
|
|
184
|
+
* // Delete multiple branches
|
|
185
|
+
* await git.branchDelete()
|
|
186
|
+
* .setBranchNames("feature", "hotfix")
|
|
187
|
+
* .call();
|
|
188
|
+
*
|
|
189
|
+
* // Force delete (even if not merged)
|
|
190
|
+
* await git.branchDelete()
|
|
191
|
+
* .setBranchNames("feature")
|
|
192
|
+
* .setForce(true)
|
|
193
|
+
* .call();
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
export class DeleteBranchCommand extends GitCommand<string[]> {
|
|
197
|
+
private branchNames: string[] = [];
|
|
198
|
+
private force = false;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Set the branches to delete.
|
|
202
|
+
*
|
|
203
|
+
* @param names Branch names (without refs/heads/ prefix)
|
|
204
|
+
*/
|
|
205
|
+
setBranchNames(...names: string[]): this {
|
|
206
|
+
this.checkCallable();
|
|
207
|
+
this.branchNames.push(...names);
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Set whether to force delete.
|
|
213
|
+
*
|
|
214
|
+
* If true, deletes even if branch is not fully merged.
|
|
215
|
+
*
|
|
216
|
+
* @param force Whether to force delete
|
|
217
|
+
*/
|
|
218
|
+
setForce(force: boolean): this {
|
|
219
|
+
this.checkCallable();
|
|
220
|
+
this.force = force;
|
|
221
|
+
return this;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Execute the branch deletion.
|
|
226
|
+
*
|
|
227
|
+
* @returns List of deleted branch names (full ref names)
|
|
228
|
+
* @throws CannotDeleteCurrentBranchError if trying to delete current branch
|
|
229
|
+
* @throws RefNotFoundError if branch doesn't exist
|
|
230
|
+
* @throws NotMergedError if branch is not merged and force is false
|
|
231
|
+
*/
|
|
232
|
+
async call(): Promise<string[]> {
|
|
233
|
+
this.checkCallable();
|
|
234
|
+
this.setCallable(false);
|
|
235
|
+
|
|
236
|
+
const currentBranch = await this.getCurrentBranch();
|
|
237
|
+
const deleted: string[] = [];
|
|
238
|
+
|
|
239
|
+
for (const name of this.branchNames) {
|
|
240
|
+
const refName = name.startsWith("refs/") ? name : `refs/heads/${name}`;
|
|
241
|
+
|
|
242
|
+
// Cannot delete current branch
|
|
243
|
+
if (currentBranch === refName) {
|
|
244
|
+
throw new CannotDeleteCurrentBranchError(name);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Check if exists
|
|
248
|
+
if (!(await this.refsStore.has(refName))) {
|
|
249
|
+
throw new RefNotFoundError(refName, `Branch '${name}' not found`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Check if fully merged (unless force)
|
|
253
|
+
if (!this.force) {
|
|
254
|
+
const isMerged = await this.isBranchMerged(refName);
|
|
255
|
+
if (!isMerged) {
|
|
256
|
+
throw new NotMergedError(name);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
await this.refsStore.delete(refName);
|
|
261
|
+
deleted.push(refName);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return deleted;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Check if a branch is fully merged into HEAD or upstream.
|
|
269
|
+
*/
|
|
270
|
+
private async isBranchMerged(refName: string): Promise<boolean> {
|
|
271
|
+
const ref = await this.refsStore.resolve(refName);
|
|
272
|
+
if (!ref?.objectId) {
|
|
273
|
+
return true; // Non-existent is "merged"
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Check if branch commit is ancestor of HEAD
|
|
277
|
+
try {
|
|
278
|
+
const headId = await this.resolveHead();
|
|
279
|
+
return this.commits.isAncestor(ref.objectId, headId);
|
|
280
|
+
} catch {
|
|
281
|
+
// If HEAD doesn't exist, consider it merged
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* List branches.
|
|
289
|
+
*
|
|
290
|
+
* Equivalent to `git branch -l`.
|
|
291
|
+
*
|
|
292
|
+
* Based on JGit's ListBranchCommand.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* // List local branches
|
|
297
|
+
* const branches = await git.branchList().call();
|
|
298
|
+
*
|
|
299
|
+
* // List remote branches
|
|
300
|
+
* const remote = await git.branchList()
|
|
301
|
+
* .setListMode(ListBranchMode.REMOTE)
|
|
302
|
+
* .call();
|
|
303
|
+
*
|
|
304
|
+
* // List all branches
|
|
305
|
+
* const all = await git.branchList()
|
|
306
|
+
* .setListMode(ListBranchMode.ALL)
|
|
307
|
+
* .call();
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
export class ListBranchCommand extends GitCommand<Ref[]> {
|
|
311
|
+
private listMode = ListBranchMode.LOCAL;
|
|
312
|
+
private containsCommit?: ObjectId;
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Set the listing mode.
|
|
316
|
+
*
|
|
317
|
+
* @param mode Which branches to list
|
|
318
|
+
*/
|
|
319
|
+
setListMode(mode: ListBranchMode): this {
|
|
320
|
+
this.checkCallable();
|
|
321
|
+
this.listMode = mode;
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Filter to branches containing the given commit.
|
|
327
|
+
*
|
|
328
|
+
* @param commit Commit that must be in branch history
|
|
329
|
+
*/
|
|
330
|
+
setContains(commit: ObjectId): this {
|
|
331
|
+
this.checkCallable();
|
|
332
|
+
this.containsCommit = commit;
|
|
333
|
+
return this;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Execute the branch listing.
|
|
338
|
+
*
|
|
339
|
+
* @returns List of branch refs
|
|
340
|
+
*/
|
|
341
|
+
async call(): Promise<Ref[]> {
|
|
342
|
+
this.checkCallable();
|
|
343
|
+
this.setCallable(false);
|
|
344
|
+
|
|
345
|
+
const prefixes: string[] = [];
|
|
346
|
+
if (this.listMode === ListBranchMode.LOCAL || this.listMode === ListBranchMode.ALL) {
|
|
347
|
+
prefixes.push("refs/heads/");
|
|
348
|
+
}
|
|
349
|
+
if (this.listMode === ListBranchMode.REMOTE || this.listMode === ListBranchMode.ALL) {
|
|
350
|
+
prefixes.push("refs/remotes/");
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const branches: Ref[] = [];
|
|
354
|
+
|
|
355
|
+
for (const prefix of prefixes) {
|
|
356
|
+
for await (const ref of this.refsStore.list(prefix)) {
|
|
357
|
+
// Skip symbolic refs
|
|
358
|
+
if (isSymbolicRef(ref)) {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Filter by contains
|
|
363
|
+
if (this.containsCommit && ref.objectId) {
|
|
364
|
+
const contains = await this.commits.isAncestor(this.containsCommit, ref.objectId);
|
|
365
|
+
if (!contains) {
|
|
366
|
+
continue;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
branches.push(ref);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Sort by name
|
|
375
|
+
branches.sort((a, b) => a.name.localeCompare(b.name));
|
|
376
|
+
|
|
377
|
+
return branches;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Rename a branch.
|
|
383
|
+
*
|
|
384
|
+
* Equivalent to `git branch -m`.
|
|
385
|
+
*
|
|
386
|
+
* Based on JGit's RenameBranchCommand.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```typescript
|
|
390
|
+
* // Rename current branch
|
|
391
|
+
* await git.branchRename().setNewName("new-name").call();
|
|
392
|
+
*
|
|
393
|
+
* // Rename specific branch
|
|
394
|
+
* await git.branchRename()
|
|
395
|
+
* .setOldName("old-branch")
|
|
396
|
+
* .setNewName("new-branch")
|
|
397
|
+
* .call();
|
|
398
|
+
* ```
|
|
399
|
+
*/
|
|
400
|
+
export class RenameBranchCommand extends GitCommand<Ref> {
|
|
401
|
+
private oldName?: string;
|
|
402
|
+
private newName?: string;
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Set the old branch name.
|
|
406
|
+
*
|
|
407
|
+
* If not set, uses the current branch.
|
|
408
|
+
*
|
|
409
|
+
* @param oldName Current branch name
|
|
410
|
+
*/
|
|
411
|
+
setOldName(oldName: string): this {
|
|
412
|
+
this.checkCallable();
|
|
413
|
+
this.oldName = oldName;
|
|
414
|
+
return this;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Set the new branch name.
|
|
419
|
+
*
|
|
420
|
+
* @param newName New branch name
|
|
421
|
+
*/
|
|
422
|
+
setNewName(newName: string): this {
|
|
423
|
+
this.checkCallable();
|
|
424
|
+
this.newName = newName;
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Execute the branch rename.
|
|
430
|
+
*
|
|
431
|
+
* @returns The renamed branch ref
|
|
432
|
+
* @throws InvalidRefNameError if new name is invalid
|
|
433
|
+
* @throws RefNotFoundError if old branch doesn't exist
|
|
434
|
+
* @throws RefAlreadyExistsError if new branch already exists
|
|
435
|
+
*/
|
|
436
|
+
async call(): Promise<Ref> {
|
|
437
|
+
this.checkCallable();
|
|
438
|
+
|
|
439
|
+
if (!this.newName) {
|
|
440
|
+
throw new InvalidRefNameError("", "New branch name is required");
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
if (!isValidBranchName(this.newName)) {
|
|
444
|
+
throw new InvalidRefNameError(this.newName);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// Determine old ref name
|
|
448
|
+
let oldRefName: string;
|
|
449
|
+
if (this.oldName) {
|
|
450
|
+
oldRefName = this.oldName.startsWith("refs/") ? this.oldName : `refs/heads/${this.oldName}`;
|
|
451
|
+
} else {
|
|
452
|
+
const currentBranch = await this.getCurrentBranch();
|
|
453
|
+
if (!currentBranch) {
|
|
454
|
+
throw new RefNotFoundError("HEAD", "HEAD is detached, specify old branch name");
|
|
455
|
+
}
|
|
456
|
+
oldRefName = currentBranch;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const newRefName = `refs/heads/${this.newName}`;
|
|
460
|
+
|
|
461
|
+
// Check old exists
|
|
462
|
+
const oldRef = await this.refsStore.resolve(oldRefName);
|
|
463
|
+
if (!oldRef?.objectId) {
|
|
464
|
+
throw new RefNotFoundError(oldRefName);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// Check new doesn't exist
|
|
468
|
+
if (await this.refsStore.has(newRefName)) {
|
|
469
|
+
throw new RefAlreadyExistsError(newRefName, `Branch '${this.newName}' already exists`);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// Create new and delete old
|
|
473
|
+
await this.refsStore.set(newRefName, oldRef.objectId);
|
|
474
|
+
await this.refsStore.delete(oldRefName);
|
|
475
|
+
|
|
476
|
+
// Update HEAD if it was pointing to old branch
|
|
477
|
+
const head = await this.refsStore.get("HEAD");
|
|
478
|
+
if (head && isSymbolicRef(head) && head.target === oldRefName) {
|
|
479
|
+
await this.refsStore.setSymbolic("HEAD", newRefName);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
this.setCallable(false);
|
|
483
|
+
|
|
484
|
+
const ref = await this.refsStore.get(newRefName);
|
|
485
|
+
return ref as Ref;
|
|
486
|
+
}
|
|
487
|
+
}
|