Skip to content

Commit 57995ef

Browse files
committed
initial setup (broken!) for using regular git rebase to create initial todo
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 6d91ba6 commit 57995ef

File tree

2 files changed

+101
-13
lines changed

2 files changed

+101
-13
lines changed

git-stacked-rebase.ts

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,88 @@ export const gitStackedRebase = async (
244244
console.log({ wasRegularRebaseInProgress });
245245

246246
if (!wasRegularRebaseInProgress) {
247-
await createInitialEditTodoOfGitStackedRebase(
247+
// await createInitialEditTodoOfGitStackedRebase(
248+
// repo, //
249+
// initialBranch,
250+
// currentBranch,
251+
// // __default__pathToStackedRebaseTodoFile
252+
// pathToStackedRebaseTodoFile
253+
// );
254+
255+
const referenceToOid = (ref: Git.Reference): Promise<Git.Oid> =>
256+
ref.peel(Git.Object.TYPE.COMMIT).then((x) => x.id());
257+
258+
// const commitOfInitialBranch: Git.Oid = await referenceToOid(bb);
259+
const commitOfInitialBranch: Git.Oid = await referenceToOid(initialBranch);
260+
const commitOfCurrentBranch: Git.Oid = await referenceToOid(currentBranch);
261+
262+
// https://stackoverflow.com/a/1549155/9285308
263+
const latestCommitOfOursThatInitialBranchAlreadyHas: Git.Oid = await Git.Merge.base(
248264
repo, //
249-
initialBranch,
250-
currentBranch,
251-
// __default__pathToStackedRebaseTodoFile
252-
pathToStackedRebaseTodoFile
265+
commitOfInitialBranch,
266+
commitOfCurrentBranch
253267
);
268+
269+
const editorScript = `\
270+
#!/usr/bin/env bash
271+
272+
printf "yes sir\n\n"
273+
274+
pushd "${dotGitDirPath}"
275+
276+
printf "pwd: $(pwd)\n"
277+
278+
# cat rebase-merge/git-rebase-todo
279+
cat ${pathToRegularRebaseTodoFile}
280+
281+
# cat ${pathToRegularRebaseTodoFile} > ${pathToStackedRebaseTodoFile}.regular
282+
cp -r ${pathToRegularRebaseDirInsideDotGit} ${pathToRegularRebaseDirInsideDotGit}.bp
283+
284+
# abort the rebase before even starting it
285+
exit 1
286+
`;
287+
const editorScriptPath: string = path.join(dotGitDirPath, "editorScript.sh");
288+
fs.writeFileSync(editorScriptPath, editorScript, { mode: 0o777 });
289+
290+
try {
291+
execSyncInRepo(
292+
[
293+
options.gitCmd,
294+
"rebase",
295+
"--interactive",
296+
latestCommitOfOursThatInitialBranchAlreadyHas.tostrS(),
297+
"--onto",
298+
initialBranch.name(),
299+
">/dev/null 2>&1",
300+
].join(" "),
301+
{
302+
env: {
303+
// https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt-sequenceeditor
304+
GIT_SEQUENCE_EDITOR: editorScriptPath,
305+
},
306+
}
307+
);
308+
} catch (e) {
309+
// as expected. do nothing.
310+
// TODO verify that it actually came from our script exiting intentionally
311+
}
312+
313+
console.log("rebase -i exited");
314+
315+
const [_exit, goodRegularCommands] = parseTodoOfStackedRebase(
316+
path.join(pathToRegularRebaseDirInsideDotGit + ".bp", filenames.gitRebaseTodo)
317+
);
318+
319+
console.log("parsedRegular: %O", _exit, goodRegularCommands);
320+
321+
execSyncInRepo("read");
322+
323+
/**
324+
* TODO - would now have to use the logic from `getWantedCommitsWithBranchBoundaries`
325+
* & subsequent utils, though adapted differently - we already have the commits,
326+
* now we gotta add the branch boundaries & then continue like regular.
327+
*
328+
*/
254329
}
255330

256331
if (!wasRegularRebaseInProgress || options.viewTodoOnly) {
@@ -672,6 +747,7 @@ export function removeUndefinedProperties<T, K extends keyof Partial<T>>(
672747
);
673748
}
674749

750+
noop(createInitialEditTodoOfGitStackedRebase);
675751
async function createInitialEditTodoOfGitStackedRebase(
676752
repo: Git.Repository, //
677753
initialBranch: Git.Reference,

parse-todo-of-stacked-rebase/validator.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,10 @@ export type GoodCommand = {
292292
}
293293
);
294294

295-
export function validate(linesOfEditedRebaseTodo: string[]): GoodCommand[] {
295+
export function validate(
296+
linesOfEditedRebaseTodo: string[], //
297+
{ enforceRequirementsSpecificToStackedRebase = false } = {}
298+
): GoodCommand[] {
296299
const badCommands: BadCommand[] = [];
297300
const goodCommands: GoodCommand[] = [];
298301

@@ -319,6 +322,13 @@ export function validate(linesOfEditedRebaseTodo: string[]): GoodCommand[] {
319322
* we're not processing command-by-command, we're processing line-by-line.
320323
*/
321324
linesOfEditedRebaseTodo.forEach((fullLine, index) => {
325+
if (fullLine.startsWith("#")) {
326+
/**
327+
* ignore comments
328+
*/
329+
return;
330+
}
331+
322332
const [commandOrAliasName, ..._rest] = fullLine.split(" ");
323333
const rest = _rest.join(" ");
324334

@@ -346,14 +356,16 @@ export function validate(linesOfEditedRebaseTodo: string[]): GoodCommand[] {
346356

347357
const reasonsIfBad: string[] = [];
348358

349-
if (index === 0) {
350-
if (commandName !== "branch-end-initial") {
351-
reasonsIfBad.push("initial command must be `branch-end-initial`");
359+
if (enforceRequirementsSpecificToStackedRebase) {
360+
if (index === 0) {
361+
if (commandName !== "branch-end-initial") {
362+
reasonsIfBad.push("initial command must be `branch-end-initial`");
363+
}
352364
}
353-
}
354-
if (index === linesOfEditedRebaseTodo.length - 1) {
355-
if (commandName !== "branch-end-last") {
356-
reasonsIfBad.push("last command must be `branch-end-last`");
365+
if (index === linesOfEditedRebaseTodo.length - 1) {
366+
if (commandName !== "branch-end-last") {
367+
reasonsIfBad.push("last command must be `branch-end-last`");
368+
}
357369
}
358370
}
359371

0 commit comments

Comments
 (0)