Skip to content

Commit

Permalink
handle edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ds300 committed Jul 19, 2023
1 parent 984a6c3 commit a45d600
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
18 changes: 11 additions & 7 deletions src/makePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
PatchState,
savePatchApplicationState,
STATE_FILE_NAME,
verifyAppliedPatches,
} from "./stateFile"

function printNoPackageFoundError(
Expand Down Expand Up @@ -92,8 +93,10 @@ export function makePatch({
mode = { type: "append", name: "initial" }
}

// TODO: verify applied patch hashes
// TODO: handle case where rebase appending and the name is the same as the next one in the sequence
if (isRebasing && state) {
verifyAppliedPatches({ appPath, patchDir, state })
}

if (
mode.type === "overwrite_last" &&
isRebasing &&
Expand Down Expand Up @@ -424,12 +427,8 @@ export function makePatch({
// scoped package
mkdirSync(dirname(patchPath))
}
writeFileSync(patchPath, diffResult.stdout)
console.log(
`${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}\n`,
)

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

writeFileSync(patchPath, diffResult.stdout)
console.log(
`${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}\n`,
)

const prevState: PatchState[] = patchesToApplyBeforeDiffing.map(
(p): PatchState => ({
patchFilename: p.patchFilename,
Expand Down
25 changes: 2 additions & 23 deletions src/rebase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import chalk from "chalk"
import { existsSync } from "fs"
import { join, resolve } from "path"
import { applyPatch } from "./applyPatches"
import { hashFile } from "./hash"
Expand All @@ -8,6 +7,7 @@ import { getGroupedPatches } from "./patchFs"
import {
getPatchApplicationState,
savePatchApplicationState,
verifyAppliedPatches,
} from "./stateFile"

export function rebase({
Expand Down Expand Up @@ -76,28 +76,7 @@ export function rebase({
)
}
// check hashes
for (let i = 0; i < state.patches.length; i++) {
const patch = state.patches[i]
const fullPatchPath = join(
patchesDirectory,
packagePatches[i].patchFilename,
)
if (!existsSync(fullPatchPath)) {
console.log(
chalk.blueBright("Expected patch file"),
fullPatchPath,
"to exist but it is missing. Try completely reinstalling node_modules first.",
)
process.exit(1)
}
if (patch.patchContentHash !== hashFile(fullPatchPath)) {
console.log(
chalk.blueBright("Patch file"),
fullPatchPath,
"has changed since it was applied. Try completely reinstalling node_modules first.",
)
}
}
verifyAppliedPatches({ appPath, patchDir, state })

if (targetPatch === "0") {
// unapply all
Expand Down
38 changes: 37 additions & 1 deletion src/stateFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { readFileSync, unlinkSync, writeFileSync } from "fs"
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs"
import { join } from "path"
import { PackageDetails } from "./PackageDetails"
import stringify from "json-stable-stringify"
import { hashFile } from "./hash"
import chalk from "chalk"
export interface PatchState {
patchFilename: string
patchContentHash: string
Expand Down Expand Up @@ -69,3 +71,37 @@ export function clearPatchApplicationState(packageDetails: PackageDetails) {
// noop
}
}

export function verifyAppliedPatches({
appPath,
patchDir,
state,
}: {
appPath: string
patchDir: string
state: PatchApplicationState
}) {
const patchesDirectory = join(appPath, patchDir)
for (const patch of state.patches) {
if (!patch.didApply) {
break
}
const fullPatchPath = join(patchesDirectory, patch.patchFilename)
if (!existsSync(fullPatchPath)) {
console.log(
chalk.blueBright("Expected patch file"),
fullPatchPath,
"to exist but it is missing. Try removing and reinstalling node_modules first.",
)
process.exit(1)
}
if (patch.patchContentHash !== hashFile(fullPatchPath)) {
console.log(
chalk.blueBright("Patch file"),
fullPatchPath,
"has changed since it was applied. Try removing and reinstalling node_modules first.",
)
process.exit(1)
}
}
}

0 comments on commit a45d600

Please sign in to comment.