@aspruyt/xfg 3.10.3 → 3.12.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.
|
@@ -47,6 +47,11 @@ export declare class GitHubLifecycleProvider implements IRepoLifecycleProvider {
|
|
|
47
47
|
*/
|
|
48
48
|
private applyRepoSettings;
|
|
49
49
|
receiveMigration(repoInfo: RepoInfo, sourceDir: string, settings?: CreateRepoSettings, token?: string): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Rename a branch via the GitHub branch rename API.
|
|
52
|
+
* GitHub automatically updates the default branch pointer.
|
|
53
|
+
*/
|
|
54
|
+
private renameBranch;
|
|
50
55
|
/**
|
|
51
56
|
* Delete the README.md that --add-readme creates.
|
|
52
57
|
* This leaves the repo with a default branch established (from the initial
|
|
@@ -149,6 +149,24 @@ export class GitHubLifecycleProvider {
|
|
|
149
149
|
await withRetry(() => this.executor.exec(command, this.cwd), {
|
|
150
150
|
retries: this.retries,
|
|
151
151
|
});
|
|
152
|
+
// Rename default branch if requested and it differs from what GitHub created.
|
|
153
|
+
if (settings?.defaultBranch) {
|
|
154
|
+
const tokenPrefix = this.buildTokenPrefix(token);
|
|
155
|
+
const hostnameFlag = getHostnameFlag(repoInfo);
|
|
156
|
+
const hostnamePart = hostnameFlag ? `${hostnameFlag} ` : "";
|
|
157
|
+
const apiPath = `repos/${escapeShellArg(repoInfo.owner)}/${escapeShellArg(repoInfo.repo)}`;
|
|
158
|
+
// After repo creation, GitHub may return 404 due to eventual consistency.
|
|
159
|
+
// Exclude 404/not-found from permanent errors so withRetry retries them.
|
|
160
|
+
const postCreatePermanentPatterns = DEFAULT_PERMANENT_ERROR_PATTERNS.filter((p) => !p.test("404 Not Found"));
|
|
161
|
+
// Detect the actual default branch name
|
|
162
|
+
const actualBranch = (await withRetry(() => this.executor.exec(`${tokenPrefix}gh api ${hostnamePart}${apiPath} --jq '.default_branch'`, this.cwd), {
|
|
163
|
+
retries: this.retries,
|
|
164
|
+
permanentErrorPatterns: postCreatePermanentPatterns,
|
|
165
|
+
})).trim();
|
|
166
|
+
if (actualBranch !== settings.defaultBranch) {
|
|
167
|
+
await this.renameBranch(repoInfo, actualBranch, settings.defaultBranch, token);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
152
170
|
// Delete the README so xfg sync starts from a clean state.
|
|
153
171
|
await this.deleteReadme(repoInfo, token);
|
|
154
172
|
}
|
|
@@ -258,6 +276,20 @@ export class GitHubLifecycleProvider {
|
|
|
258
276
|
catch {
|
|
259
277
|
// No refs to remove — ignore
|
|
260
278
|
}
|
|
279
|
+
// Rename default branch in mirror clone if requested.
|
|
280
|
+
if (settings?.defaultBranch) {
|
|
281
|
+
const headRef = (await this.executor.exec(`git -C ${escapeShellArg(sourceDir)} symbolic-ref HEAD`, this.cwd)).trim();
|
|
282
|
+
const prefix = "refs/heads/";
|
|
283
|
+
if (!headRef.startsWith(prefix)) {
|
|
284
|
+
throw new Error(`Mirror clone HEAD symbolic-ref is '${headRef}', expected to start with '${prefix}'. ` +
|
|
285
|
+
`Cannot rename default branch.`);
|
|
286
|
+
}
|
|
287
|
+
const sourceBranch = headRef.slice(prefix.length);
|
|
288
|
+
if (sourceBranch !== settings.defaultBranch) {
|
|
289
|
+
await this.executor.exec(`git -C ${escapeShellArg(sourceDir)} branch -m ${escapeShellArg(sourceBranch)} ${escapeShellArg(settings.defaultBranch)}`, this.cwd);
|
|
290
|
+
await this.executor.exec(`git -C ${escapeShellArg(sourceDir)} symbolic-ref HEAD refs/heads/${escapeShellArg(settings.defaultBranch)}`, this.cwd);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
261
293
|
// Use gh repo create --source --push to create and mirror in one step.
|
|
262
294
|
// For bare repos (from git clone --mirror), --push mirrors all refs.
|
|
263
295
|
// This uses gh CLI authentication, avoiding raw git auth issues with GHE.
|
|
@@ -294,6 +326,20 @@ export class GitHubLifecycleProvider {
|
|
|
294
326
|
retries: this.retries,
|
|
295
327
|
});
|
|
296
328
|
}
|
|
329
|
+
/**
|
|
330
|
+
* Rename a branch via the GitHub branch rename API.
|
|
331
|
+
* GitHub automatically updates the default branch pointer.
|
|
332
|
+
*/
|
|
333
|
+
async renameBranch(repoInfo, current, desired, token) {
|
|
334
|
+
const renameTokenPrefix = this.buildTokenPrefix(token);
|
|
335
|
+
const hostnameFlag = getHostnameFlag(repoInfo);
|
|
336
|
+
const hostnamePart = hostnameFlag ? `${hostnameFlag} ` : "";
|
|
337
|
+
const apiPath = `repos/${escapeShellArg(repoInfo.owner)}/${escapeShellArg(repoInfo.repo)}`;
|
|
338
|
+
await withRetry(() => this.executor.exec(`${renameTokenPrefix}gh api ${hostnamePart}${apiPath}/branches/${escapeShellArg(current)}/rename ` +
|
|
339
|
+
`--method POST -f new_name=${escapeShellArg(desired)}`, this.cwd), {
|
|
340
|
+
retries: this.retries,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
297
343
|
/**
|
|
298
344
|
* Delete the README.md that --add-readme creates.
|
|
299
345
|
* This leaves the repo with a default branch established (from the initial
|
|
@@ -17,6 +17,8 @@ export function toCreateRepoSettings(repo) {
|
|
|
17
17
|
result.hasIssues = repo.hasIssues;
|
|
18
18
|
if (repo.hasWiki !== undefined)
|
|
19
19
|
result.hasWiki = repo.hasWiki;
|
|
20
|
+
if (repo.defaultBranch !== undefined)
|
|
21
|
+
result.defaultBranch = repo.defaultBranch;
|
|
20
22
|
return Object.keys(result).length > 0 ? result : undefined;
|
|
21
23
|
}
|
|
22
24
|
/**
|
package/package.json
CHANGED