From 23a135e257aed806bd6206a4a14aa6107b6e750a Mon Sep 17 00:00:00 2001
From: Kipras Melnikovas <kipras@kipras.org>
Date: Mon, 11 Apr 2022 04:24:56 +0300
Subject: [PATCH] fix: use the *last* rebase list, instead of the first

a) if the was only 1 rebase, nothing changes
b) in other cases i've encountered,
   using the last rebase always proved better
   than the 1st one:
     1st one would error oftenly; succeeded rarily
     last one would work just fine

but, there's still a lot of testing to do.
especially for >2 rebases, since i've only encountered 2.

it's a bit dangerous because if we pick the wrong rebase,
then we could lose commits / their content.

now, picking just 1 rebase is still incomplete --
this fix is just a (seemingly) better heuristic,

(!) but eventually we'll need to
take into account all of the rebases.

there might be ways to eliminate this need,
e.g. auto-calling `git-stacked-rebase --apply`
in the `post-rewrite` script in the background,
but at least for now, don't want to mess with it
and would prefer to avoid, until i have done
more dog-fooding & encounter more edge-cases
& maybe potential solutions/invariants as well.

Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
---
 reducePath.ts | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/reducePath.ts b/reducePath.ts
index 48113d77..66c36b41 100755
--- a/reducePath.ts
+++ b/reducePath.ts
@@ -307,7 +307,11 @@ export function combineRewrittenLists(rewrittenListFileContent: string): Combine
 		}
 
 		prev = []
-	}
+	} 
+
+	if (!lastRebaseList) {
+		throw new Error(`NOT IMPLEMENTED - did not find any "rebase"(s).`)
+	} 
 
 	/**
 	 * TODO handle multiple rebases
@@ -317,7 +321,7 @@ export function combineRewrittenLists(rewrittenListFileContent: string): Combine
 
 	console.log("mergedReducedRewrittenLists", mergedReducedRewrittenLists)
 
-	const combinedRewrittenList = Object.entries(mergedReducedRewrittenLists[0].mapping).map(([k, v]) => k + " " + v).join("\n") + "\n"
+	const combinedRewrittenList = Object.entries(lastRebaseList.mapping).map(([k, v]) => k + " " + v).join("\n") + "\n"
 	// fs.writeFileSync("rewritten-list", combinedRewrittenList)
 
 	return {