Skip to content

Commit 8d0fc8c

Browse files
committed
feat: set-upstream to remote when pushing for the first time
Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent 67cbe2e commit 8d0fc8c

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

forcePush.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
// import fs from "fs";
44

5+
import Git from "nodegit";
6+
57
import { getWantedCommitsWithBranchBoundariesOurCustomImpl } from "./git-stacked-rebase";
68
import {
79
branchSequencer, //
@@ -10,6 +12,8 @@ import {
1012
// getBackupPathOfPreviousStackedRebase,
1113
} from "./branchSequencer";
1214

15+
import { createQuestion } from "./util/createQuestion";
16+
1317
export const forcePush: BranchSequencerBase = (argsBase) =>
1418
// /**
1519
// * TODO TESTS we __really__ need to make sure we test this one lmao
@@ -36,8 +40,57 @@ export const forcePush: BranchSequencerBase = (argsBase) =>
3640
branchSequencer({
3741
...argsBase,
3842
// pathToStackedRebaseDirInsideDotGit,
39-
actionInsideEachCheckedOutBranch: ({ execSyncInRepo }) => {
40-
execSyncInRepo(`${argsBase.gitCmd} push --force`);
43+
actionInsideEachCheckedOutBranch: async ({ execSyncInRepo, repo }) => {
44+
const branch: Git.Reference = await repo.getCurrentBranch();
45+
const upstreamBranch: Git.Reference | null = await Git.Branch.upstream(branch).catch(() => null);
46+
47+
if (!upstreamBranch) {
48+
const remotes: string[] = await repo.getRemoteNames();
49+
50+
if (remotes.length === 0) {
51+
throw new Error("0 remotes found, cannot push a new branch into a remote.");
52+
}
53+
54+
let remote: string;
55+
56+
if (remotes.length === 1) {
57+
remote = remotes[0];
58+
} else {
59+
const indices: string[] = remotes.map((_, i) => i + 1).map((x) => x.toString());
60+
61+
const question = createQuestion();
62+
63+
let answer: string = "";
64+
65+
let first = true;
66+
while (!remotes.includes(answer)) {
67+
answer = (
68+
await question(
69+
(first ? "\n" : "") +
70+
"multiple remotes detected, please choose one for new branch:" +
71+
remotes.map((r, i) => `\n ${i + 1} ${r}`).join("") +
72+
"\n> "
73+
)
74+
)
75+
.trim()
76+
.toLowerCase();
77+
78+
if (indices.includes(answer)) {
79+
answer = remotes[Number(answer) - 1];
80+
}
81+
82+
first = false;
83+
}
84+
85+
remote = answer;
86+
}
87+
88+
const cmd = `push -u ${remote} ${branch.name()} --force`;
89+
console.log(`running ${cmd}`);
90+
execSyncInRepo(`${argsBase.gitCmd} ${cmd}`);
91+
} else {
92+
execSyncInRepo(`${argsBase.gitCmd} push --force`);
93+
}
4194
},
4295
delayMsBetweenCheckouts: 0,
4396
getBoundariesInclInitial: () =>

0 commit comments

Comments
 (0)