From 3af350e491a8a7f0b6a33126a39053866059ab18 Mon Sep 17 00:00:00 2001 From: "Alexander J. Vincent" Date: Sun, 7 Jul 2024 12:36:57 -0700 Subject: [PATCH] #97, tweaks to file hashing --- stage_3_snapshot/spec-snapshot/fileHashes.ts | 15 ++++++----- utilities/source/hash-all-files.ts | 28 +++++++++----------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/stage_3_snapshot/spec-snapshot/fileHashes.ts b/stage_3_snapshot/spec-snapshot/fileHashes.ts index 42edf9a..ee917f0 100644 --- a/stage_3_snapshot/spec-snapshot/fileHashes.ts +++ b/stage_3_snapshot/spec-snapshot/fileHashes.ts @@ -135,8 +135,8 @@ async function compareOneSnapshot( if (compareFileLists(stage_two_snapshot, stage_three_snapshot, [ stage_two_file ], [ stage_three_file ]) === false) return; - const stage_two_hash = await hashOneFile(stage_two_snapshot, stage_two_file); - const stage_three_hash = await hashOneFile(stage_three_snapshot, stage_three_file); + const stage_two_hash = await hashOneFile(stage_two_file); + const stage_three_hash = await hashOneFile(stage_three_file); expect(stage_three_hash).withContext("file hashes").toEqual(stage_two_hash); } @@ -201,11 +201,14 @@ async function hashDirectories( stage_three_dir: string, ): Promise<[readonly string[], readonly string[]]> { - const [stage_two_hash, stage_three_hash] = await Promise.all([ - hashAllFiles(stage_two_dir, true), - hashAllFiles(stage_three_dir, true) + const [stage_two_key_hash_pairs, stage_three_key_hash_pairs] = await Promise.all([ + hashAllFiles(stage_two_dir), + hashAllFiles(stage_three_dir) ]); - return [stage_two_hash.split("\n"), stage_three_hash.split("\n")]; + return [ + stage_two_key_hash_pairs.map(([key, hash]) => key + " " + hash), + stage_three_key_hash_pairs.map(([key, hash]) => key + " " + hash) + ]; } function getArrayDiff( diff --git a/utilities/source/hash-all-files.ts b/utilities/source/hash-all-files.ts index bb635ce..e050af2 100644 --- a/utilities/source/hash-all-files.ts +++ b/utilities/source/hash-all-files.ts @@ -1,5 +1,6 @@ import crypto from "crypto"; import fs from "fs/promises"; +import path from "path"; import readDirsDeep from "./readDirsDeep.js"; import { PromiseAllParallel } from "./PromiseTypes.js"; @@ -40,33 +41,28 @@ export async function getHashFileList( * @returns The hash of all non-ignored contents. */ export async function hashAllFiles( - root: string, - verbose: boolean -): Promise + root: string +): Promise { const allFiles = await getHashFileList(root); - const fileHashes = await PromiseAllParallel( - allFiles, async file => hashOneFile(root, file) + const fileHashes: readonly [string, string][] = await PromiseAllParallel( + allFiles, async file => [path.relative(root, file), await hashOneFile(file)] ); - const contents = fileHashes.join("\n"); - - if (verbose) { - return contents; - } - - const hash = crypto.createHash('sha512'); - hash.update(contents); - return hash.digest('hex'); + return fileHashes; } export async function hashOneFile( - root: string, file: string, ): Promise { const contents = await fs.readFile(file, "utf-8"); + return hashContents(contents); +} + +export function hashContents(contents: string): string { const hash = crypto.createHash('sha512'); hash.update(contents); - return hash.digest('hex') + " " + file.replace(root, ""); + return hash.digest('hex'); } +