-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve last divergent commit behaviour
Previously it didn't consider if there were no diverged commits and or no remote commits. This handles those two cases.
- Loading branch information
1 parent
c421fd4
commit 3049192
Showing
2 changed files
with
33 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,34 @@ | ||
import type { DetailedCommit } from '$lib/vbranches/types'; | ||
|
||
type DivergenceResult = | ||
| { type: 'localDiverged'; commit: DetailedCommit } | ||
| { type: 'notDiverged' | 'onlyRemoteDiverged' }; | ||
|
||
/** | ||
* Find the last commit that diverged from the remote branch. | ||
*/ | ||
export function findLastDivergentCommit(commits: DetailedCommit[]): DetailedCommit | undefined { | ||
export function findLastDivergentCommit( | ||
remoteCommits: DetailedCommit[], | ||
commits: DetailedCommit[] | ||
): DivergenceResult { | ||
const noLocalDiverged = commits.every((commit) => !commit.remoteCommitId); | ||
|
||
// If there are no diverged or remote commits, then there is no last | ||
// diverged commit. | ||
if (noLocalDiverged && remoteCommits.length === 0) { | ||
return { type: 'notDiverged' }; | ||
} | ||
|
||
if (noLocalDiverged) { | ||
return { type: 'onlyRemoteDiverged' }; | ||
} | ||
|
||
for (let i = commits.length - 1; i >= 0; i--) { | ||
const commit = commits[i]; | ||
if (commit!.id !== commit!.remoteCommitId) { | ||
return commit; | ||
const commit = commits[i]!; | ||
if (commit.id !== commit.remoteCommitId) { | ||
return { type: 'localDiverged', commit }; | ||
} | ||
} | ||
return undefined; | ||
|
||
return { type: 'notDiverged' }; | ||
} |