Skip to content

Commit

Permalink
#97, tweaks to file hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jul 7, 2024
1 parent 7253e52 commit 3af350e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
15 changes: 9 additions & 6 deletions stage_3_snapshot/spec-snapshot/fileHashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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(
Expand Down
28 changes: 12 additions & 16 deletions utilities/source/hash-all-files.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<string>
root: string
): Promise<readonly [string, string][]>
{
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<string>
{
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');
}

0 comments on commit 3af350e

Please sign in to comment.