Rework hash_files to return (str, str) result

This commit is contained in:
Bryan Bennett 2024-03-13 21:42:17 -04:00
parent 98c3131f7f
commit 6b313a6ff6
No known key found for this signature in database
GPG key ID: EE149E4215408DE9
3 changed files with 60 additions and 22 deletions

View file

@ -17,23 +17,39 @@ let nix = args =>
);
let hash_files = filenames => {
/*** Hash all entries in [filenames], returning a hex-encoded string of the hash of their contents */
/*** Hash all entries in [filenames]
Returns Some(hex-string) or None if no filenames are found.
*/
let ctx = Sha1.init();
let () =
let files_to_hash =
filenames
|> Array.filter(~f=f =>
switch (Sys_unix.file_exists(f)) {
| `Yes => true
| _ => false
| _ =>
// let fullpth = Filename_unix.realpath(f);
Printf.eprintf(
"Cannot find file %s (cwd: %s)\n",
f,
Core_unix.getcwd(),
);
false;
}
)
|> Array.iter(~f=f => {
f
|> In_channel.create
|> In_channel.input_all
|> Sha1.update_string(ctx)
});
Sha1.finalize(ctx) |> Sha1.to_hex;
);
switch (files_to_hash |> Array.length) {
| 0 => Error("No files found to hash")
| _ =>
let () =
files_to_hash
|> Array.iter(~f=f => {
f
|> In_channel.create
|> In_channel.input_all
|> Sha1.update_string(ctx)
});
Ok(Sha1.finalize(ctx) |> Sha1.to_hex);
};
};
let rec rmrf = path => {