Skip to content

Instantly share code, notes, and snippets.

@MarkLodato
Last active May 30, 2023 14:08
Show Gist options
  • Save MarkLodato/c03659d242ea214ef3588f29b582be70 to your computer and use it in GitHub Desktop.
Save MarkLodato/c03659d242ea214ef3588f29b582be70 to your computer and use it in GitHub Desktop.
Compute "dirhash" of a go module directory.
#!/bin/bash
# Compute "dirhash" of a go module directory.
#
# Original go code: https://cs.opensource.google/go/x/mod/+/master:sumdb/dirhash/hash.go
#
# Author: Mark Lodato <[email protected]>
#
# Copyright 2022 Google LLC.
# SPDX-License-Identifier: Apache-2.0
# TODO: Support parsing a zip file, where each path in the zip file starts with
# `<modulename>@<version>/`.
if [[ $# != 2 ]]; then
cat >&2 <<EOF
USAGE: $(basename "$0") <directory> <modulename>@<version>
Outputs the "dirhash" of a go module. If given a directory, the module name and
version must be provided.
Example:
\$ $(basename "$0") . [email protected]
EOF
exit 1
fi
DIR=$1
MODNAME=$2
if [[ ! -d "$DIR" ]]; then
echo "ERROR: not a directory: $DIR" >&2
exit 1
fi
if [[ -z "$MODNAME" ]]; then
MODNAME=$DIR
elif [[ "$MODNAME" =~ [/\n] ]]; then
echo "ERROR: module name must not contain a slash or newline: $MODNAME" >&2
exit 1
fi
# Prints the sha256sum output of all files in $1, sorted in the C locale,
# replacing the directory portion with $2.
sha256sum_directory() {
(
cd "$1" && find . -type f \
| LC_ALL=C sort \
| xargs -r sha256sum \
| sed "s/ ./ $2/"
)
}
# Converts the hex on stdin, up to the first space, to base64.
hex_to_base64() {
cut -f1 -d' ' \
| xxd -r -p \
| base64
}
echo "h1:$(sha256sum_directory "$DIR" "$MODNAME" | sha256sum | hex_to_base64)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment