(
baseSha: string,
files: { path: string; sha: string | null; newPath?: string }[],
branch = this.branch,
hasSubfolders = true,
)
| 1460 | } |
| 1461 | |
| 1462 | async updateTree( |
| 1463 | baseSha: string, |
| 1464 | files: { path: string; sha: string | null; newPath?: string }[], |
| 1465 | branch = this.branch, |
| 1466 | hasSubfolders = true, |
| 1467 | ) { |
| 1468 | const toMove: { from: string; to: string; sha: string }[] = []; |
| 1469 | const tree = files.reduce((acc, file) => { |
| 1470 | const entry = { |
| 1471 | path: trimStart(file.path, '/'), |
| 1472 | mode: '100644', |
| 1473 | type: 'blob', |
| 1474 | sha: file.sha, |
| 1475 | } as TreeEntry; |
| 1476 | |
| 1477 | if (file.newPath) { |
| 1478 | toMove.push({ from: file.path, to: file.newPath, sha: file.sha as string }); |
| 1479 | } else { |
| 1480 | acc.push(entry); |
| 1481 | } |
| 1482 | |
| 1483 | return acc; |
| 1484 | }, [] as TreeEntry[]); |
| 1485 | |
| 1486 | for (const { from, to, sha } of toMove) { |
| 1487 | if (!hasSubfolders) { |
| 1488 | // New behavior (subfolders: false): Only move the specific file |
| 1489 | // Delete the file at the old path |
| 1490 | tree.push({ |
| 1491 | path: trimStart(from, '/'), |
| 1492 | mode: '100644', |
| 1493 | type: 'blob', |
| 1494 | sha: null, |
| 1495 | }); |
| 1496 | // Create the file at the new path |
| 1497 | tree.push({ |
| 1498 | path: trimStart(to, '/'), |
| 1499 | mode: '100644', |
| 1500 | type: 'blob', |
| 1501 | sha, |
| 1502 | }); |
| 1503 | } else { |
| 1504 | // Legacy behavior (subfolders: true, default): Move all files in the directory |
| 1505 | // This is for collections where all files in a folder represent a single entry |
| 1506 | const sourceDir = dirname(from); |
| 1507 | const destDir = dirname(to); |
| 1508 | const files = await this.listFiles(sourceDir, { branch, depth: 100 }); |
| 1509 | for (const file of files) { |
| 1510 | // delete current path |
| 1511 | tree.push({ |
| 1512 | path: file.path, |
| 1513 | mode: '100644', |
| 1514 | type: 'blob', |
| 1515 | sha: null, |
| 1516 | }); |
| 1517 | // create in new path |
| 1518 | tree.push({ |
| 1519 | path: file.path.replace(sourceDir, destDir), |
no test coverage detected