Skip to content

Commit caa906d

Browse files
committed
fix: if initalBranch gets updated, select wanted commits properly & work correctly
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 398f36d commit caa906d

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

git-stacked-rebase.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ export const gitStackedRebase = async (
246246
await createInitialEditTodoOfGitStackedRebase(
247247
repo, //
248248
initialBranch,
249+
currentBranch,
249250
// __default__pathToStackedRebaseTodoFile
250251
pathToStackedRebaseTodoFile
251252
);
@@ -666,6 +667,7 @@ export function removeUndefinedProperties<T, K extends keyof Partial<T>>(
666667
async function createInitialEditTodoOfGitStackedRebase(
667668
repo: Git.Repository, //
668669
initialBranch: Git.Reference,
670+
currentBranch: Git.Reference,
669671
pathToRebaseTodoFile: string
670672
): Promise<void> {
671673
// .catch(logErr);
@@ -678,7 +680,8 @@ async function createInitialEditTodoOfGitStackedRebase(
678680
const commitsWithBranchBoundaries: CommitAndBranchBoundary[] = (
679681
await getWantedCommitsWithBranchBoundaries(
680682
repo, //
681-
initialBranch
683+
initialBranch,
684+
currentBranch
682685
)
683686
).reverse();
684687

@@ -817,7 +820,8 @@ type CommitAndBranchBoundary = {
817820
async function getWantedCommitsWithBranchBoundaries(
818821
repo: Git.Repository, //
819822
/** beginningBranch */
820-
bb: Git.Reference
823+
bb: Git.Reference,
824+
currentBranch: Git.Reference
821825
): Promise<CommitAndBranchBoundary[]> {
822826
const fixBranchName = (name: string): string =>
823827
name
@@ -873,9 +877,44 @@ async function getWantedCommitsWithBranchBoundaries(
873877
resolved: (await bb.resolve()).name(),
874878
});
875879

876-
const commitOfBB: Git.Oid = (await bb.peel(Git.Object.TYPE.COMMIT)).id();
880+
const referenceToOid = (ref: Git.Reference): Promise<Git.Oid> =>
881+
ref.peel(Git.Object.TYPE.COMMIT).then((x) => x.id());
877882

878-
const wantedCommits: Git.Commit[] = await getCommitHistoryUntilIncl(repo, commitOfBB);
883+
const commitOfInitialBranch: Git.Oid = await referenceToOid(bb);
884+
const commitOfCurrentBranch: Git.Oid = await referenceToOid(currentBranch);
885+
886+
// https://stackoverflow.com/a/1549155/9285308
887+
const latestCommitOfOursThatInitialBranchAlreadyHas: Git.Oid = await Git.Merge.base(
888+
repo, //
889+
commitOfInitialBranch,
890+
commitOfCurrentBranch
891+
);
892+
console.log({
893+
latestCommitOfOursThatInitialBranchAlreadyHas: latestCommitOfOursThatInitialBranchAlreadyHas.tostrS(),
894+
});
895+
896+
const commitOfInitialBranchAsCommit: Git.Commit = await Git.Commit.lookup(repo, commitOfInitialBranch);
897+
898+
const wantedCommits: Git.Commit[] = await getCommitHistoryUntilIncl(
899+
repo, //
900+
latestCommitOfOursThatInitialBranchAlreadyHas
901+
).then(
902+
(commits) => (
903+
commits.pop() /** remove the unwanted commit that initial branch already has, that we have too */,
904+
commits.push(commitOfInitialBranchAsCommit) /** add the commit of the initial branch itself */,
905+
/**
906+
* the operations above are pop() and push(), instead of shift() and unshift()
907+
* (operate at end of array, instead of the start),
908+
*
909+
* because of how the `getCommitHistoryUntilIncl` returns the commits
910+
* (the order - from newest to oldest).
911+
*
912+
* TODO FIXME - this is done later, but probably should be done directly
913+
* in the underlying function to avoid confusion.
914+
*/
915+
commits
916+
)
917+
);
879918

880919
console.log({
881920
wantedCommits: wantedCommits.map((c) =>

0 commit comments

Comments
 (0)