(
baseSha: string,
files: { path: string; sha: string | null; newPath?: string }[],
branch = this.branch,
hasSubfolders = true,
)
| 1421 | } |
| 1422 | |
| 1423 | async updateTree( |
| 1424 | baseSha: string, |
| 1425 | files: { path: string; sha: string | null; newPath?: string }[], |
| 1426 | branch = this.branch, |
| 1427 | hasSubfolders = true, |
| 1428 | ) { |
| 1429 | const toMove: { from: string; to: string; sha: string }[] = []; |
| 1430 | const tree = files.reduce((acc, file) => { |
| 1431 | const entry = { |
| 1432 | path: trimStart(file.path, '/'), |
| 1433 | mode: '100644', |
| 1434 | type: 'blob', |
| 1435 | sha: file.sha, |
| 1436 | } as TreeEntry; |
| 1437 | |
| 1438 | if (file.newPath) { |
| 1439 | toMove.push({ from: file.path, to: file.newPath, sha: file.sha as string }); |
| 1440 | } else { |
| 1441 | acc.push(entry); |
| 1442 | } |
| 1443 | |
| 1444 | return acc; |
| 1445 | }, [] as TreeEntry[]); |
| 1446 | |
| 1447 | for (const { from, to, sha } of toMove) { |
| 1448 | if (!hasSubfolders) { |
| 1449 | // New behavior (subfolders: false): Only move the specific file |
| 1450 | // Delete the file at the old path |
| 1451 | tree.push({ |
| 1452 | path: trimStart(from, '/'), |
| 1453 | mode: '100644', |
| 1454 | type: 'blob', |
| 1455 | sha: null, |
| 1456 | }); |
| 1457 | // Create the file at the new path |
| 1458 | tree.push({ |
| 1459 | path: trimStart(to, '/'), |
| 1460 | mode: '100644', |
| 1461 | type: 'blob', |
| 1462 | sha, |
| 1463 | }); |
| 1464 | } else { |
| 1465 | // Legacy behavior (subfolders: true, default): Move all files in the directory |
| 1466 | // This is for collections where all files in a folder represent a single entry |
| 1467 | const sourceDir = dirname(from); |
| 1468 | const destDir = dirname(to); |
| 1469 | const files = await this.listFiles(sourceDir, { branch, depth: 100 }); |
| 1470 | for (const file of files) { |
| 1471 | // delete current path |
| 1472 | tree.push({ |
| 1473 | path: file.path, |
| 1474 | mode: '100644', |
| 1475 | type: 'blob', |
| 1476 | sha: null, |
| 1477 | }); |
| 1478 | // create in new path |
| 1479 | tree.push({ |
| 1480 | path: file.path.replace(sourceDir, destDir), |
no test coverage detected