Created
June 18, 2020 15:44
-
-
Save mattt/e212ae74adf848a450345e31e25e7911 to your computer and use it in GitHub Desktop.
An example of how Git can be used as a transparent log for a package registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -o errexit | |
set -o pipefail | |
set -o nounset | |
# Returns the SHA-256 checksum of a string or file | |
function checksum() { | |
if [ -f "$1" ]; then | |
shasum -a 256 "$1" | cut -d ' ' -f1 | |
else | |
echo "$1" | shasum -a 256 | cut -d ' ' -f1 | |
fi | |
} | |
# Returns the content-addressable path for a qualified package name | |
function path() { | |
checksum "$1" | sed 's_._&/_2;s_._&/_5;s_._&/_8' | |
} | |
# Setup git repository | |
( | |
if [ ! -d .git ]; then | |
git init . | |
git lfs install | |
fi | |
if [ ! -f .gitattributes ]; then | |
cat >.gitattributes <<EOF | |
*.zip filter=lfs diff=lfs merge=lfs -text | |
*.asc binary | |
EOF | |
git add .gitattributes | |
git commit -m "Add .gitattributes" | |
fi | |
) | |
# Add a package release | |
( | |
author="Mattt <[email protected]>" | |
package="Flight-School/Money" | |
version="1.2.0" | |
mkdir -p "$(path $package)" | |
archive="$(path $package)/$version.zip" | |
if [ ! -f "$archive" ]; then | |
head -c 12345 </dev/urandom >"$archive" | |
fi | |
if [ ! -f "$archive.asc" ]; then | |
gpg --armor --detach-sign --output "$archive.asc" "$archive" | |
fi | |
git add "$(path $package)" | |
git commit -m "sha-256=$(checksum "$archive")" -s --author "$author" | |
) | |
# Add release metadata using git notes | |
( | |
metadata=$(mktemp) | |
cat >"$metadata" <<EOF | |
{ | |
"url": "https://github.com/Flight-School/Money" | |
} | |
EOF | |
git notes add -F "$metadata" | |
rm -f "$metadata" | |
) | |
# Show results with git log | |
git log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: