Skip to content

Commit 3941ac2

Browse files
committed
start differenciating between lineNumber and nthCommand properly (prerequisite for next commit)
Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent 08453c5 commit 3941ac2

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,20 @@ export function parseNewGoodCommands(
123123
}
124124
};
125125

126-
if (goodOldCommand.index < goodCommandMinIndex) {
126+
/**
127+
* TODO `lineNumber` -- shouldn't it be `nthCommand`?
128+
* need to experiment w/ e.g. "break", and comments.
129+
*/
130+
if (goodOldCommand.lineNumber < goodCommandMinIndex) {
127131
// TODO VERIFY
128132
console.warn(
129-
`goodCommandOld.index (${goodOldCommand.index}) < goodCommandMinIndex (${goodCommandMinIndex}), continue'ing.`
133+
`goodCommandOld.index (${goodOldCommand.lineNumber}) < goodCommandMinIndex (${goodCommandMinIndex}), continue'ing.`
130134
);
131135

132136
// goodCommandMinIndex++;
133137

134138
continue;
135-
} else if (goodOldCommand.index === goodCommandMinIndex) {
139+
} else if (goodOldCommand.lineNumber === goodCommandMinIndex) {
136140
// perfect?
137141
// TODO VERIFY
138142
console.info(`index match`);

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

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -260,25 +260,42 @@ export const namesOfRebaseCommandsThatMakeRebaseExitToPause: EitherRebaseCommand
260260
(cmd) => cmd.nameButNeverAlias
261261
);
262262

263-
type BadCommand = {
264-
command: string;
263+
type LineNr = {
264+
/**
265+
* indexed from 0.
266+
* counts comments/empty-lines/etc, see `nthCommand` instead
267+
*/
265268
lineNumber: number;
269+
270+
/**
271+
* indexed from 1.
272+
* counts comments/empty-lines/etc, see `nthCommand` instead
273+
*/
274+
humanLineNumber: number;
275+
276+
/**
277+
* indexed from 0.
278+
* counts only commands
279+
* (both good and bad, though irrelevant, because will error if has bad commands)
280+
*/
281+
nthCommand: number;
282+
};
283+
284+
type BadCommand = LineNr & {
285+
command: string;
266286
fullLine: string;
267287
reasons: string[];
268288
};
269289

270-
export type GoodCommandBase = {
290+
export type GoodCommandBase = LineNr & {
271291
commandOrAliasName: EitherRebaseEitherCommandOrAlias;
272-
lineNumber: number;
273292
fullLine: string;
274293
rest: string;
275294
/**
276295
* SHA or branch or label (all commands, except `break`, have >=1)
277296
* TODO: handle >1
278297
*/
279298
targets: string[] | null;
280-
index: number;
281-
282299
// commandName: EitherRebaseCommand;
283300
};
284301
export type GoodCommandRegular = GoodCommandBase & {
@@ -317,32 +334,36 @@ export function validate(
317334
return command in allEitherRebaseCommands || command in allEitherRebaseCommandAliases;
318335
}
319336

320-
let previousCommitSHA: string | null;
337+
let previousCommitSHA: string | null = null;
338+
let nthCommand: number = -1;
321339
/**
322340
* we're not processing command-by-command, we're processing line-by-line.
323341
*/
324-
linesOfEditedRebaseTodo.forEach((fullLine, index) => {
342+
for (let lineNumber = 0; lineNumber < linesOfEditedRebaseTodo.length; lineNumber++) {
343+
const fullLine: string = linesOfEditedRebaseTodo[lineNumber];
344+
325345
if (fullLine.startsWith("#")) {
326346
/**
327347
* ignore comments
328348
*/
329-
return;
349+
continue;
330350
}
351+
nthCommand++;
331352

332353
const [commandOrAliasName, ..._rest] = fullLine.split(" ");
333354
const rest = _rest.join(" ");
334355

335-
const lineNumber: number = index + 1;
336-
337356
if (!commandOrAliasExists(commandOrAliasName)) {
338357
badCommands.push({
339358
command: commandOrAliasName,
359+
nthCommand,
340360
lineNumber,
361+
humanLineNumber: lineNumber + 1,
341362
fullLine,
342363
reasons: ["unrecognized command"],
343364
});
344365

345-
return;
366+
continue;
346367
}
347368

348369
const commandName: EitherRebaseCommand =
@@ -357,7 +378,7 @@ export function validate(
357378
const reasonsIfBad: string[] = [];
358379

359380
if (enforceRequirementsSpecificToStackedRebase) {
360-
if (index === 0) {
381+
if (nthCommand === 0) {
361382
if (commandName !== "branch-end-initial") {
362383
reasonsIfBad.push("initial command must be `branch-end-initial`");
363384
}
@@ -396,15 +417,18 @@ export function validate(
396417
badCommands.push({
397418
command: commandName,
398419
lineNumber,
420+
humanLineNumber: lineNumber + 1,
421+
nthCommand,
399422
fullLine,
400423
reasons: reasonsIfBad,
401424
});
402425
} else {
403426
goodCommands.push({
404427
commandOrAliasName,
405428
targets,
406-
index,
407429
lineNumber,
430+
humanLineNumber: lineNumber + 1,
431+
nthCommand,
408432
fullLine,
409433
rest,
410434
...(commandName in regularRebaseCommands
@@ -427,15 +451,15 @@ export function validate(
427451
previousCommitSHA = targets?.[0] ?? null;
428452
}
429453
}
430-
});
454+
}
431455

432456
if (badCommands.length) {
433457
throw new Termination(
434458
"\n" +
435459
joinWith("\n\n")([
436460
"found errors in rebase commands:",
437461
...badCommands.map((cmd) =>
438-
bullets(` line ${cmd.lineNumber}: ${tick(cmd.command)}`, cmd.reasons, " - ")
462+
bullets(` line ${cmd.humanLineNumber}: ${tick(cmd.command)}`, cmd.reasons, " - ")
439463
),
440464
"to edit & fix, use:",
441465
" git-stacked-rebase -e|--edit-todo\n",

0 commit comments

Comments
 (0)