Skip to content

Commit 8ae3932

Browse files
committed
Order PR stack tables by base chain
1 parent b36028c commit 8ae3932

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/core/CommitMetadata.order.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ test("stack_order preserves stack group order", () => {
1010
expect(ids(actual)).toEqual(["A", "B", "C"]);
1111
});
1212

13+
test("stack_order derives stack group order from base chain", () => {
14+
const commit_range = range(["A", "B", "C"]);
15+
commit_range.group_list.reverse();
16+
17+
const actual = CommitMetadata.stack_order(commit_range);
18+
19+
expect(ids(actual)).toEqual(["A", "B", "C"]);
20+
});
21+
22+
test("stack_order does not drop groups with incomplete base links", () => {
23+
const commit_range = range(["A", "B", "C"]);
24+
commit_range.group_list[1]!.base = "missing";
25+
26+
const actual = CommitMetadata.stack_order(commit_range);
27+
28+
expect(ids(actual)).toEqual(["A", "B", "C"]);
29+
});
30+
1331
test("rebase_order reverses stack order", () => {
1432
const commit_range = range(["A", "B", "C"]);
1533
const actual = CommitMetadata.rebase_order(commit_range);

src/core/CommitMetadata.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,46 @@ export async function range(commit_group_map?: CommitGroupMap) {
331331
}
332332

333333
export function stack_order(commit_range: CommitRange): CommitGroupList {
334-
return [...commit_range.group_list];
334+
const group_by_id = new Map(commit_range.group_list.map((group) => [group.id, group]));
335+
const children_by_base = new Map<string, CommitGroupList>();
336+
337+
for (const group of commit_range.group_list) {
338+
if (!group.base || !group_by_id.has(group.base)) {
339+
continue;
340+
}
341+
342+
const children = children_by_base.get(group.base) || [];
343+
children.push(group);
344+
children_by_base.set(group.base, children);
345+
}
346+
347+
const ordered_group_list: CommitGroupList = [];
348+
const visited = new Set<string>();
349+
350+
function visit(group: CommitGroupList[number]) {
351+
if (visited.has(group.id)) {
352+
return;
353+
}
354+
355+
visited.add(group.id);
356+
ordered_group_list.push(group);
357+
358+
for (const child of children_by_base.get(group.id) || []) {
359+
visit(child);
360+
}
361+
}
362+
363+
for (const group of commit_range.group_list) {
364+
if (!group.base || !group_by_id.has(group.base)) {
365+
visit(group);
366+
}
367+
}
368+
369+
for (const group of commit_range.group_list) {
370+
visit(group);
371+
}
372+
373+
return ordered_group_list;
335374
}
336375

337376
export function rebase_order(commit_range: CommitRange): CommitGroupList {

0 commit comments

Comments
 (0)