Using continues to reduce nesting

This commit is contained in:
Auto Rebase
2025-01-09 13:51:47 +01:00
parent f6c17c51a1
commit f61c87e29e
2 changed files with 32 additions and 35 deletions

65
app.ts
View File

@@ -71,9 +71,8 @@ const getCommitsForBranch = async (branch: string): Promise<Set<string>> => {
const removeIgnoredBranches = (branchesWithDependencies: Record<string, any>) => {
let withoutIgnoredBranches = {}
for (const [branch, value] of Object.entries(branchesWithDependencies)) {
if(!value.ignore) {
withoutIgnoredBranches[branch] = value;
}
if(value.ignore) continue;
withoutIgnoredBranches[branch] = value;
}
return withoutIgnoredBranches;
}
@@ -90,45 +89,43 @@ const buildRebaseDependencyGraph = async (branches: string[]): Promise<Record<st
let finalBranches: Record<string, BranchWithDependencies> = {};
for (const branchA of branches) {
for (const branchB of branches) {
if(branchA !== branchB) {
const isSuperset: boolean = commitHistories[branchA].isSupersetOf(commitHistories[branchB]);
const difference: Set<string> = commitHistories[branchA].difference(commitHistories[branchB]);
if(branchA !== branchB) continue;
if (isSuperset) {
if(branchB === mainBranch) {
// SUPERSET OF MAIN BRANCH, MEANING ALREADY REBASED
finalBranches[branchA] = {
ignore: true
}
}
if (difference.size === 0) {
const prevBranches: string[] = finalBranches[branchA]?.equalBranches ?? [];
finalBranches[branchA] = {
rebaseBranch: mainBranch,
...finalBranches[branchA],
equalBranches: [...prevBranches, branchB]
};
} else {
if (branchA !== mainBranch && (!finalBranches[branchA] || finalBranches[branchA].differenceWithRebase > difference.size)) {
finalBranches[branchA] = {
...finalBranches[branchA],
rebaseBranch: branchB,
differenceWithRebase: difference.size
};
}
}
const isSuperset: boolean = commitHistories[branchA].isSupersetOf(commitHistories[branchB]);
if (!isSuperset) continue;
const difference: Set<string> = commitHistories[branchA].difference(commitHistories[branchB]);
if(branchB === mainBranch) {
// SUPERSET OF MAIN BRANCH, MEANING ALREADY REBASED
finalBranches[branchA] = {
ignore: true
}
}
if (difference.size === 0) {
const prevBranches: string[] = finalBranches[branchA]?.equalBranches ?? [];
finalBranches[branchA] = {
rebaseBranch: mainBranch,
...finalBranches[branchA],
equalBranches: [...prevBranches, branchB]
};
} else if (branchA !== mainBranch && (!finalBranches[branchA] || finalBranches[branchA].differenceWithRebase > difference.size)) {
finalBranches[branchA] = {
...finalBranches[branchA],
rebaseBranch: branchB,
differenceWithRebase: difference.size
};
}
}
}
// Set rebase for branches with no dependencies
for (const branch of branches) {
if(branch !== mainBranch) {
finalBranches[branch] = finalBranches[branch] ?? {
rebaseBranch: mainBranch,
differenceWithRebase: 0
}
if(branch !== mainBranch) continue;
finalBranches[branch] = finalBranches[branch] ?? {
rebaseBranch: mainBranch,
differenceWithRebase: 0
}
}