@@ -1549,8 +1549,39 @@ async function extendCommitsWithBranchEnds(
15491549
15501550 let matchedRefs : Git . Reference [ ] ;
15511551
1552+ const removeLocalRegex = / ^ r e f s \/ h e a d s \/ / ;
1553+ const removeRemoteRegex = / ^ r e f s \/ r e m o t e s \/ [ ^ / ] * \/ / ;
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 ( / ^ r e f s \/ h e a d s \/ / , "" )
1567- . replace ( / ^ r e f s \/ r e m o t e s \/ [ ^ / ] * \/ / , "" )
1597+ . replace ( removeLocalRegex , "" )
1598+ . replace ( removeRemoteRegex , "" )
15681599 )
15691600 ) . length === 1 ) ,
15701601 "" +
0 commit comments