Skip to content

Commit a45d600

Browse files
committed
handle edge cases
1 parent 984a6c3 commit a45d600

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

src/makePatch.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
PatchState,
4141
savePatchApplicationState,
4242
STATE_FILE_NAME,
43+
verifyAppliedPatches,
4344
} from "./stateFile"
4445

4546
function printNoPackageFoundError(
@@ -92,8 +93,10 @@ export function makePatch({
9293
mode = { type: "append", name: "initial" }
9394
}
9495

95-
// TODO: verify applied patch hashes
96-
// TODO: handle case where rebase appending and the name is the same as the next one in the sequence
96+
if (isRebasing && state) {
97+
verifyAppliedPatches({ appPath, patchDir, state })
98+
}
99+
97100
if (
98101
mode.type === "overwrite_last" &&
99102
isRebasing &&
@@ -424,12 +427,8 @@ export function makePatch({
424427
// scoped package
425428
mkdirSync(dirname(patchPath))
426429
}
427-
writeFileSync(patchPath, diffResult.stdout)
428-
console.log(
429-
`${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}\n`,
430-
)
431430

432-
// if we inserted a new patch into a sequence we may need to update the sequence numbers
431+
// if we are inserting a new patch into a sequence we most likely need to update the sequence numbers
433432
if (isRebasing && mode.type === "append") {
434433
const patchesToNudge = existingPatches.slice(state!.patches.length)
435434
if (sequenceNumber === undefined) {
@@ -460,6 +459,11 @@ export function makePatch({
460459
}
461460
}
462461

462+
writeFileSync(patchPath, diffResult.stdout)
463+
console.log(
464+
`${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}\n`,
465+
)
466+
463467
const prevState: PatchState[] = patchesToApplyBeforeDiffing.map(
464468
(p): PatchState => ({
465469
patchFilename: p.patchFilename,

src/rebase.ts

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import chalk from "chalk"
2-
import { existsSync } from "fs"
32
import { join, resolve } from "path"
43
import { applyPatch } from "./applyPatches"
54
import { hashFile } from "./hash"
@@ -8,6 +7,7 @@ import { getGroupedPatches } from "./patchFs"
87
import {
98
getPatchApplicationState,
109
savePatchApplicationState,
10+
verifyAppliedPatches,
1111
} from "./stateFile"
1212

1313
export function rebase({
@@ -76,28 +76,7 @@ export function rebase({
7676
)
7777
}
7878
// check hashes
79-
for (let i = 0; i < state.patches.length; i++) {
80-
const patch = state.patches[i]
81-
const fullPatchPath = join(
82-
patchesDirectory,
83-
packagePatches[i].patchFilename,
84-
)
85-
if (!existsSync(fullPatchPath)) {
86-
console.log(
87-
chalk.blueBright("Expected patch file"),
88-
fullPatchPath,
89-
"to exist but it is missing. Try completely reinstalling node_modules first.",
90-
)
91-
process.exit(1)
92-
}
93-
if (patch.patchContentHash !== hashFile(fullPatchPath)) {
94-
console.log(
95-
chalk.blueBright("Patch file"),
96-
fullPatchPath,
97-
"has changed since it was applied. Try completely reinstalling node_modules first.",
98-
)
99-
}
100-
}
79+
verifyAppliedPatches({ appPath, patchDir, state })
10180

10281
if (targetPatch === "0") {
10382
// unapply all

src/stateFile.ts

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { readFileSync, unlinkSync, writeFileSync } from "fs"
1+
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs"
22
import { join } from "path"
33
import { PackageDetails } from "./PackageDetails"
44
import stringify from "json-stable-stringify"
5+
import { hashFile } from "./hash"
6+
import chalk from "chalk"
57
export interface PatchState {
68
patchFilename: string
79
patchContentHash: string
@@ -69,3 +71,37 @@ export function clearPatchApplicationState(packageDetails: PackageDetails) {
6971
// noop
7072
}
7173
}
74+
75+
export function verifyAppliedPatches({
76+
appPath,
77+
patchDir,
78+
state,
79+
}: {
80+
appPath: string
81+
patchDir: string
82+
state: PatchApplicationState
83+
}) {
84+
const patchesDirectory = join(appPath, patchDir)
85+
for (const patch of state.patches) {
86+
if (!patch.didApply) {
87+
break
88+
}
89+
const fullPatchPath = join(patchesDirectory, patch.patchFilename)
90+
if (!existsSync(fullPatchPath)) {
91+
console.log(
92+
chalk.blueBright("Expected patch file"),
93+
fullPatchPath,
94+
"to exist but it is missing. Try removing and reinstalling node_modules first.",
95+
)
96+
process.exit(1)
97+
}
98+
if (patch.patchContentHash !== hashFile(fullPatchPath)) {
99+
console.log(
100+
chalk.blueBright("Patch file"),
101+
fullPatchPath,
102+
"has changed since it was applied. Try removing and reinstalling node_modules first.",
103+
)
104+
process.exit(1)
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)