Skip to content

Commit 0ec03fb

Browse files
committed
fix: prune matchedRefs if ref is remote and local ref is also present
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 749073f commit 0ec03fb

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

git-stacked-rebase.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,8 +1549,39 @@ async function extendCommitsWithBranchEnds(
15491549

15501550
let matchedRefs: Git.Reference[];
15511551

1552+
const removeLocalRegex = /^refs\/heads\//;
1553+
const removeRemoteRegex = /^refs\/remotes\/[^/]*\//;
1554+
15521555
const extend = (c: Git.Commit, i: number): CommitAndBranchBoundary => (
15531556
(matchedRefs = refs.filter((ref) => !!ref.target()?.equal(c.id()))),
1557+
/**
1558+
* if there exists a local branch with the same name as a remote one,
1559+
* then get rid of the remote branch ref,
1560+
* because the local one will cover it.
1561+
* (with the exception of the initial branch (it should always be remote)).
1562+
*
1563+
* this helps multiple scenarios:
1564+
*
1565+
* - a new commit is created in the latest branch, but not pushed to a remote yet.
1566+
* (a duplicate remote branch would show up earlier in history,
1567+
* meanwhile, for our purposes, it shouldn't)
1568+
*
1569+
* - a new latest branch got created (old one was moved previously).
1570+
* here, the old remote branch would point to the latest commit,
1571+
* i.e. in the new latest branch, i.e. it would be ahead when it shouldn't.
1572+
*
1573+
* - possibly others
1574+
*
1575+
*/
1576+
(matchedRefs = matchedRefs.filter(
1577+
(r) =>
1578+
r.name() === initialBranch.name() ||
1579+
!r.isRemote() ||
1580+
!refs
1581+
.filter((ref) => !ref.isRemote())
1582+
.map((ref) => ref.name().replace(removeLocalRegex, ""))
1583+
.includes(r.name().replace(removeRemoteRegex, ""))
1584+
)),
15541585
assert(
15551586
matchedRefs.length <= 1 ||
15561587
/**
@@ -1563,8 +1594,8 @@ async function extendCommitsWithBranchEnds(
15631594
matchedRefs.map((r) =>
15641595
r
15651596
?.name()
1566-
.replace(/^refs\/heads\//, "")
1567-
.replace(/^refs\/remotes\/[^/]*\//, "")
1597+
.replace(removeLocalRegex, "")
1598+
.replace(removeRemoteRegex, "")
15681599
)
15691600
).length === 1),
15701601
"" +

0 commit comments

Comments
 (0)