Skip to content

Commit ad3ee94

Browse files
committed
TEMP
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent ca4faed commit ad3ee94

File tree

4 files changed

+153
-28
lines changed

4 files changed

+153
-28
lines changed

git-stacked-rebase.ts

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { assertNever } from "./util/assertNever";
3838
import { Single, Tuple } from "./util/tuple";
3939
import { isDirEmptySync } from "./util/fs";
4040
import {
41+
getParseTargetsCtxFromLine,
4142
GoodCommand,
4243
GoodCommandRegular,
4344
GoodCommandStacked, //
@@ -46,6 +47,8 @@ import {
4647
RegularRebaseEitherCommandOrAlias,
4748
StackedRebaseCommand,
4849
StackedRebaseCommandAlias,
50+
stackedRebaseCommands,
51+
Targets,
4952
} from "./parse-todo-of-stacked-rebase/validator";
5053

5154
export * from "./options";
@@ -514,16 +517,8 @@ export async function gitStackedRebase(
514517
if (cmd.commandName === "branch-end-new") {
515518
await createBranchForCommand(cmd as any); // TODO TS
516519
} else if (cmd.commandName === "branch-end-new-from-remote") {
517-
const nameWithoutRefsRemotesOriginPrefix: string = cmd.targets![0];
518-
const remote: string = cmd.targets![1];
519-
const fullNameOfBranchWithRemote: string =
520-
"refs/remotes/" + remote + "/" + nameWithoutRefsRemotesOriginPrefix;
521-
522-
branchesWhoNeedLocalCheckout.push({
523-
nameWithoutRefsRemotesOriginPrefix,
524-
remote,
525-
fullNameOfBranchWithRemote,
526-
});
520+
const b = parseBranchWhichNeedsLocalCheckout(cmd.targets!); // TODO TS NARROWER TYPES
521+
branchesWhoNeedLocalCheckout.push(b);
527522
}
528523
} else {
529524
assertNever(cmd);
@@ -883,13 +878,17 @@ async function createInitialEditTodoOfGitStackedRebase(
883878
*
884879
*/
885880
if (branch.isRemote() || branch.name().startsWith("refs/remotes/")) {
886-
const nameWithoutRefsRemotesOriginPrefix: string = branch
881+
const wantedLocalBranchName: string = branch
887882
.name()
888883
.replace(removeRemoteRegex, "");
889884

890-
const remote: string = branch.name().match(removeRemoteRegex)![1];
885+
const remoteName: string = branch.name().match(removeRemoteRegex)![1];
891886

892-
return `branch-end-new-from-remote ${nameWithoutRefsRemotesOriginPrefix} ${remote}`;
887+
return encodeCmdToLine({
888+
wantedLocalBranchName,
889+
remoteName,
890+
fullNameOfBranchWithRemote: getFullNameOfBranchWithRemote({ wantedLocalBranchName, remoteName })
891+
})
893892
} else {
894893
return `branch-end ${branch.name()}`;
895894
}
@@ -909,12 +908,49 @@ async function createInitialEditTodoOfGitStackedRebase(
909908
return;
910909
}
911910

912-
type BranchWhoNeedsLocalCheckout = {
913-
nameWithoutRefsRemotesOriginPrefix: string; //
914-
remote: string;
911+
export type BranchWhoNeedsLocalCheckout = {
912+
wantedLocalBranchName: string; //
913+
remoteName: string;
915914
fullNameOfBranchWithRemote: string;
916915
};
917916

917+
/**
918+
* expects targets from the format of "branch-end-new-from-remote"
919+
* TODO TS NARROWER TYPES (specific targets for each type of cmd)
920+
*/
921+
export function parseBranchWhichNeedsLocalCheckout(targets: NonNullable<Targets>): BranchWhoNeedsLocalCheckout {
922+
const wantedLocalBranchName: string = targets[0];
923+
const remoteAndRemoteBranchName: string = targets[1];
924+
const remoteName: string = remoteAndRemoteBranchName.split("/")[0];
925+
const fullNameOfBranchWithRemote: string = getFullNameOfBranchWithRemote({ fullNameOfBranchWithRemote: remoteAndRemoteBranchName });
926+
927+
return {
928+
wantedLocalBranchName,
929+
remoteName,
930+
fullNameOfBranchWithRemote,
931+
}
932+
}
933+
934+
function getFullNameOfBranchWithRemote(b: Pick<BranchWhoNeedsLocalCheckout, "fullNameOfBranchWithRemote"> | Pick<BranchWhoNeedsLocalCheckout, "wantedLocalBranchName" | "remoteName">): string {
935+
return "refs/remotes/" + (
936+
"fullNameOfBranchWithRemote" in b
937+
? b.fullNameOfBranchWithRemote
938+
: b.remoteName + "/" + b.wantedLocalBranchName
939+
);
940+
}
941+
942+
// TODO RENAME
943+
export function encodeCmdToLine(b: BranchWhoNeedsLocalCheckout) {
944+
return `branch-end-new-from-remote ${b.wantedLocalBranchName} ${b.fullNameOfBranchWithRemote}`;
945+
}
946+
947+
// TODO RENAME
948+
export function decodeLineToCmd(line: string): BranchWhoNeedsLocalCheckout {
949+
// TODO TS NARROWER TYPES
950+
const targets: NonNullable<Targets> = stackedRebaseCommands["branch-end-new-from-remote"].parseTargets(getParseTargetsCtxFromLine(line))!
951+
return parseBranchWhichNeedsLocalCheckout(targets);
952+
}
953+
918954
function checkoutRemotePartialBranchesLocally(
919955
repo: Git.Repository, //
920956
currentBranch: Git.Reference,
@@ -929,7 +965,7 @@ function checkoutRemotePartialBranchesLocally(
929965
const execSyncInRepo = createExecSyncInRepo(repo);
930966

931967
for (const b of branchesWhoNeedLocalCheckout) {
932-
const cmd = `git checkout -b ${b.nameWithoutRefsRemotesOriginPrefix} --track ${b.fullNameOfBranchWithRemote}`;
968+
const cmd = `git checkout -b ${b.wantedLocalBranchName} --track ${b.fullNameOfBranchWithRemote}`;
933969
execSyncInRepo(cmd);
934970
}
935971

humanOp.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export function humanOpRemoveLineOfCommit({ filePath, commitSHA }: CommonArgs):
5959
writeLines(filePath, lines);
6060
}
6161

62+
export function modifyLines(filePath: string, modifier = (lines: string[]) => lines): void {
63+
const lines: string[] = readLines(filePath);
64+
const modifiedLines: string[] = modifier(lines);
65+
writeLines(filePath, modifiedLines);
66+
};
67+
6268
export function readLines(filePath: string): string[] {
6369
const file = fs.readFileSync(filePath, { encoding: "utf-8" });
6470
const lines = file.split("\n");

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import { Termination } from "../util/error";
1010
*/
1111
type Validator = (ctx: { rest: string; reasonsIfBad: string[] }) => boolean;
1212

13-
type ParseTargets = (ctx: {
13+
export type Targets = string[] | null;
14+
15+
type ParseTargetsCtx = {
1416
line: string; //
1517
split: string[];
1618
rest: string;
17-
}) => string[] | null;
19+
}
20+
type ParseTargets = (ctx: ParseTargetsCtx) => Targets
1821

1922
type Command = {
2023
/**
@@ -428,7 +431,7 @@ export function validate(
428431
nthCommand++;
429432

430433
const [commandOrAliasName, ..._rest] = fullLine.split(" ");
431-
const rest = _rest.join(" ");
434+
const rest = getRest(fullLine);
432435

433436
if (!commandOrAliasExists(commandOrAliasName)) {
434437
badCommands.push({
@@ -482,13 +485,11 @@ export function validate(
482485

483486
const command: Command = allEitherRebaseCommands[commandName];
484487

488+
/**
489+
* TODO TS NARROWER TYPES: combine the 2 below:
490+
*/
485491
command.isRestValid({ rest, reasonsIfBad });
486-
487-
const targets: string[] | null = command.parseTargets({
488-
line: fullLine, //
489-
split: fullLine.split(" ").filter((word) => !!word),
490-
rest,
491-
});
492+
const targets: Targets = command.parseTargets(getParseTargetsCtxFromLine(fullLine));
492493

493494
if (reasonsIfBad.length) {
494495
badCommands.push({
@@ -548,3 +549,16 @@ export function validate(
548549

549550
return goodCommands;
550551
}
552+
553+
export function getParseTargetsCtxFromLine(fullLine: string): ParseTargetsCtx {
554+
return {
555+
line: fullLine, //
556+
split: fullLine.split(" ").filter((word) => !!word),
557+
rest: getRest(fullLine),
558+
}
559+
}
560+
561+
export function getRest(fullLine: string): ParseTargetsCtx["rest"] {
562+
return fullLine.split(" ").slice(1).join(" ")
563+
}
564+

test/auto-checkout-remote-partial-branches.spec.ts

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55

66
import assert from "assert";
77

8-
import { gitStackedRebase } from "../git-stacked-rebase";
8+
import { BranchWhoNeedsLocalCheckout, decodeLineToCmd, encodeCmdToLine, gitStackedRebase } from "../git-stacked-rebase";
99
import { editor__internal } from "../internal";
1010
import { nativeGetBranchNames } from "../native-git/branch";
11+
import { modifyLines } from "../humanOp";
1112

1213
import { setupRemoteRepo } from "./util/setupRemoteRepo";
14+
import { noop } from "../util/noop";
1315

1416
export default async function run() {
1517
await auto_checks_out_remote_partial_branches();
18+
//noop(auto_checks_out_remote_partial_branches)
19+
noop(give_chosen_name_to_local_branch)
20+
//await give_chosen_name_to_local_branch();
1621
}
1722

1823
async function auto_checks_out_remote_partial_branches() {
@@ -39,8 +44,8 @@ async function auto_checks_out_remote_partial_branches() {
3944
);
4045

4146
await gitStackedRebase(RemoteAlice.initialBranch, {
42-
[editor__internal]: () => void 0 /** no edit */,
4347
gitDir: LocalBob.repo.workdir(),
48+
[editor__internal]: () => void 0 /** no edit */,
4449
});
4550

4651
const localPartialBranchesInBobAfter: string[] = findPartialBranchesThatArePresentLocally();
@@ -58,6 +63,70 @@ async function auto_checks_out_remote_partial_branches() {
5863
);
5964
}
6065

66+
async function give_chosen_name_to_local_branch() {
67+
const { RemoteAlice, LocalBob } = await setupRemoteRepo();
68+
69+
/**
70+
* switch to latest branch to perform stacked rebase
71+
*/
72+
LocalBob.execSyncInRepo(`git checkout ${RemoteAlice.latestStackedBranchName}`);
73+
74+
const remotePartialBranchesInAlice: string[] = RemoteAlice.partialBranches.map((b) => b.shorthand());
75+
const localPartialBranchesInBobBefore: string[] = findPartialBranchesThatArePresentLocally();
76+
77+
function findPartialBranchesThatArePresentLocally(
78+
localBranches: string[] = nativeGetBranchNames(LocalBob.repo.workdir())("local")
79+
) {
80+
return remotePartialBranchesInAlice.filter((partial) => localBranches.includes(partial));
81+
}
82+
83+
assert.deepStrictEqual(
84+
localPartialBranchesInBobBefore.length,
85+
0,
86+
"expected partial branches to __not be__ checked out locally, to be able to test later that they will be."
87+
);
88+
89+
const renamedLocalBranch = "renamed-local-branch-hehe" as const;
90+
91+
await gitStackedRebase(RemoteAlice.initialBranch, {
92+
gitDir: LocalBob.repo.workdir(),
93+
[editor__internal]: ({filePath}) => {
94+
const branchNameOf2ndBranch: string = RemoteAlice.newPartialBranches[1][0];
95+
modifyLines(filePath, (lines) => {
96+
const lineIdx: number = lines.findIndex(l => l.includes(branchNameOf2ndBranch))!
97+
const line: string = lines[lineIdx];
98+
const cmd: BranchWhoNeedsLocalCheckout = decodeLineToCmd(line)
99+
console.log({
100+
lineIdx,
101+
line,
102+
cmd
103+
})
104+
const newCmd: BranchWhoNeedsLocalCheckout = {
105+
...cmd,
106+
wantedLocalBranchName: renamedLocalBranch,
107+
}
108+
const newLine: string = encodeCmdToLine(newCmd)
109+
const newLines: string[] = lines.map((oldLine, i) => i === lineIdx ? newLine : oldLine)
110+
return newLines;
111+
})
112+
},
113+
});
114+
115+
const localPartialBranchesInBobAfter: string[] = findPartialBranchesThatArePresentLocally();
116+
117+
console.log({
118+
remotePartialBranchesInAlice,
119+
localPartialBranchesInBobBefore,
120+
localPartialBranchesInBobAfter,
121+
});
122+
123+
assert.deepStrictEqual(
124+
localPartialBranchesInBobAfter.length,
125+
remotePartialBranchesInAlice.length,
126+
"expected partial branches to __be__ checked out locally by git-stacked-rebase."
127+
);
128+
};
129+
61130
if (!module.parent) {
62131
run();
63132
}

0 commit comments

Comments
 (0)