zen-fs-config 0.3.14 → 0.3.15

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/index.js CHANGED
@@ -441,9 +441,42 @@ registerBackend("Gitee", async (options) => {
441
441
  const baseUrl = this.api.baseUrl || "https://gitee.com/api/v5";
442
442
  const auth = `access_token=${this.api.token}`;
443
443
  const branchUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${this.api.branch}?${auth}`;
444
- const branchRes = await fetch(branchUrl);
445
- if (!branchRes.ok) throw new Error(`Gitee: branch "${this.api.branch}" not found (${branchRes.status})`);
446
- const branchData = await branchRes.json();
444
+ let branchRes = await fetch(branchUrl);
445
+ let branchData;
446
+ if (branchRes.ok) {
447
+ branchData = await branchRes.json();
448
+ } else {
449
+ console.log(`[Gitee] Branch "${this.api.branch}" not found, creating...`);
450
+ const defaultBranch = this.api.branch === "master" ? "main" : "master";
451
+ let defaultSha;
452
+ for (const name of [defaultBranch, "main", "master"]) {
453
+ const dr = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${name}?${auth}`);
454
+ if (dr.ok) {
455
+ const dd = await dr.json();
456
+ defaultSha = dd.commit?.sha;
457
+ if (defaultSha) break;
458
+ }
459
+ }
460
+ if (!defaultSha) {
461
+ console.log(`[Gitee] No branches found in repo, skipping tree init`);
462
+ this.initialized = true;
463
+ return;
464
+ }
465
+ const createRes = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches?${auth}`, {
466
+ method: "POST",
467
+ headers: { "Content-Type": "application/json" },
468
+ body: JSON.stringify({
469
+ refs: defaultSha,
470
+ branch_name: this.api.branch
471
+ })
472
+ });
473
+ if (!createRes.ok) {
474
+ const errText = await createRes.text().catch(() => "");
475
+ throw new Error(`Gitee: failed to create branch "${this.api.branch}": ${createRes.status} ${errText}`);
476
+ }
477
+ branchData = await createRes.json();
478
+ console.log(`[Gitee] Branch "${this.api.branch}" created from ${defaultSha.slice(0, 8)}`);
479
+ }
447
480
  const commitSha = branchData.commit?.sha;
448
481
  if (!commitSha) throw new Error(`Gitee: could not get commit SHA for branch "${this.api.branch}"`);
449
482
  const commitUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/git/commits/${commitSha}?${auth}`;
package/dist/index.mjs CHANGED
@@ -394,9 +394,42 @@ registerBackend("Gitee", async (options) => {
394
394
  const baseUrl = this.api.baseUrl || "https://gitee.com/api/v5";
395
395
  const auth = `access_token=${this.api.token}`;
396
396
  const branchUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${this.api.branch}?${auth}`;
397
- const branchRes = await fetch(branchUrl);
398
- if (!branchRes.ok) throw new Error(`Gitee: branch "${this.api.branch}" not found (${branchRes.status})`);
399
- const branchData = await branchRes.json();
397
+ let branchRes = await fetch(branchUrl);
398
+ let branchData;
399
+ if (branchRes.ok) {
400
+ branchData = await branchRes.json();
401
+ } else {
402
+ console.log(`[Gitee] Branch "${this.api.branch}" not found, creating...`);
403
+ const defaultBranch = this.api.branch === "master" ? "main" : "master";
404
+ let defaultSha;
405
+ for (const name of [defaultBranch, "main", "master"]) {
406
+ const dr = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${name}?${auth}`);
407
+ if (dr.ok) {
408
+ const dd = await dr.json();
409
+ defaultSha = dd.commit?.sha;
410
+ if (defaultSha) break;
411
+ }
412
+ }
413
+ if (!defaultSha) {
414
+ console.log(`[Gitee] No branches found in repo, skipping tree init`);
415
+ this.initialized = true;
416
+ return;
417
+ }
418
+ const createRes = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches?${auth}`, {
419
+ method: "POST",
420
+ headers: { "Content-Type": "application/json" },
421
+ body: JSON.stringify({
422
+ refs: defaultSha,
423
+ branch_name: this.api.branch
424
+ })
425
+ });
426
+ if (!createRes.ok) {
427
+ const errText = await createRes.text().catch(() => "");
428
+ throw new Error(`Gitee: failed to create branch "${this.api.branch}": ${createRes.status} ${errText}`);
429
+ }
430
+ branchData = await createRes.json();
431
+ console.log(`[Gitee] Branch "${this.api.branch}" created from ${defaultSha.slice(0, 8)}`);
432
+ }
400
433
  const commitSha = branchData.commit?.sha;
401
434
  if (!commitSha) throw new Error(`Gitee: could not get commit SHA for branch "${this.api.branch}"`);
402
435
  const commitUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/git/commits/${commitSha}?${auth}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.3.14",
3
+ "version": "0.3.15",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",