Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions forcePush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// import fs from "fs";

import Git from "nodegit";

import { getWantedCommitsWithBranchBoundariesOurCustomImpl } from "./git-stacked-rebase";
import {
branchSequencer, //
Expand All @@ -10,6 +12,8 @@ import {
// getBackupPathOfPreviousStackedRebase,
} from "./branchSequencer";

import { createQuestion } from "./util/createQuestion";

export const forcePush: BranchSequencerBase = (argsBase) =>
// /**
// * TODO TESTS we __really__ need to make sure we test this one lmao
Expand All @@ -36,8 +40,57 @@ export const forcePush: BranchSequencerBase = (argsBase) =>
branchSequencer({
...argsBase,
// pathToStackedRebaseDirInsideDotGit,
actionInsideEachCheckedOutBranch: ({ execSyncInRepo }) => {
execSyncInRepo(`${argsBase.gitCmd} push --force`);
actionInsideEachCheckedOutBranch: async ({ execSyncInRepo, repo }) => {
const branch: Git.Reference = await repo.getCurrentBranch();
const upstreamBranch: Git.Reference | null = await Git.Branch.upstream(branch).catch(() => null);

if (!upstreamBranch) {
const remotes: string[] = await repo.getRemoteNames();

if (remotes.length === 0) {
throw new Error("0 remotes found, cannot push a new branch into a remote.");
}

let remote: string;

if (remotes.length === 1) {
remote = remotes[0];
} else {
const indices: string[] = remotes.map((_, i) => i + 1).map((x) => x.toString());

const question = createQuestion();

let answer: string = "";

let first = true;
while (!remotes.includes(answer)) {
answer = (
await question(
(first ? "\n" : "") +
"multiple remotes detected, please choose one for new branch:" +
remotes.map((r, i) => `\n ${i + 1} ${r}`).join("") +
"\n> "
)
)
.trim()
.toLowerCase();

if (indices.includes(answer)) {
answer = remotes[Number(answer) - 1];
}

first = false;
}

remote = answer;
}

const cmd = `push -u ${remote} ${branch.name()} --force`;
console.log(`running ${cmd}`);
execSyncInRepo(`${argsBase.gitCmd} ${cmd}`);
} else {
execSyncInRepo(`${argsBase.gitCmd} push --force`);
}
},
delayMsBetweenCheckouts: 0,
getBoundariesInclInitial: () =>
Expand Down