| 1 | #!/bin/sh |
| 2 | |
| 3 | set -e |
| 4 | |
| 5 | REPO="git-lfs/git-lfs" |
| 6 | WORKDIR="$(mktemp -d)" |
| 7 | |
| 8 | trap 'rm -fr "$WORKDIR"' EXIT |
| 9 | |
| 10 | say () { |
| 11 | [ -n "$QUIET" ] && return |
| 12 | local format="$1" |
| 13 | shift |
| 14 | printf "$format\n" "$@" >&2 |
| 15 | } |
| 16 | |
| 17 | abort () { |
| 18 | local format="$1" |
| 19 | shift |
| 20 | printf "$format\n" "$@" >&2 |
| 21 | exit 2 |
| 22 | } |
| 23 | |
| 24 | uri_encode () { |
| 25 | ruby -e 'print ARGV[0].gsub(/[^A-Za-z0-9_.-]/) { |x| "%%%02x" % x.ord }' "$1" |
| 26 | } |
| 27 | |
| 28 | curl () { |
| 29 | if [ -n "$GITHUB_TOKEN" ] |
| 30 | then |
| 31 | command curl -u "token:$GITHUB_TOKEN" -fSs "$@" |
| 32 | else |
| 33 | command curl -nfSs "$@" |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | categorize_os () { |
| 38 | local os="$1" |
| 39 | |
| 40 | if [ "$os" = "freebsd" ] |
| 41 | then |
| 42 | echo FreeBSD |
| 43 | else |
| 44 | ruby -e 'puts ARGV[0].capitalize' "$os" |
| 45 | fi |
| 46 | } |
| 47 | |
| 48 | categorize_arch () { |
| 49 | local arch="$1" |
| 50 | |
| 51 | if [ "$arch" = "ppc64le" ] |
| 52 | then |
| 53 | echo "Little-endian 64-bit PowerPC" |
| 54 | elif [ "$arch" = "riscv64" ] |
| 55 | then |
| 56 | echo "64-bit RISC-V" |
| 57 | elif [ "$arch" = "loong64" ] |
| 58 | then |
| 59 | echo "64-bit LoongArch" |
| 60 | else |
| 61 | echo "$arch" | tr a-z A-Z |
| 62 | fi |
| 63 | } |
| 64 | |
| 65 | # Categorize a release asset and print its human readable name to standard |
| 66 | # output. |
| 67 | categorize_asset () { |
| 68 | local file="$1" |
| 69 | local os=$(echo "$file" | sed -e 's/^git-lfs-//' -e 's/[-.].*$//') |
| 70 | local arch=$(echo "$file" | ruby -pe '$_.gsub!(/\Agit-lfs-[^-]+-([^-]+)[-.].*/, "\\1")') |
| 71 | |
| 72 | case "$file" in |
| 73 | git-lfs-vendor-v*.*.*.tar.gz) |
| 74 | echo "Source with Vendored Modules";; |
| 75 | git-lfs-v*.*.*.tar.gz) |
| 76 | echo "Source";; |
| 77 | git-lfs-windows-v*.*.*.exe) |
| 78 | echo "Windows Installer";; |
| 79 | sha256sums) |
| 80 | echo "Unsigned SHA-256 Hashes";; |
| 81 | sha256sums.asc) |
| 82 | echo "Signed SHA-256 Hashes";; |
| 83 | hashes) |
| 84 | echo "Unsigned Hashes";; |
| 85 | hashes.asc) |
| 86 | echo "Signed Hashes";; |
| 87 | *) |
| 88 | printf "%s %s\n" "$(categorize_os "$os")" "$(categorize_arch "$arch")";; |
| 89 | esac |
| 90 | } |
| 91 | |
| 92 | # Provide a content type for the asset based on its file name. |
| 93 | content_type () { |
| 94 | local file="$1" |
| 95 | |
| 96 | case "$file" in |
| 97 | *.zip) |
| 98 | echo "application/zip";; |
| 99 | *.tar.gz) |
| 100 | echo "application/gzip";; |
| 101 | *.exe) |
| 102 | echo "application/octet-stream";; |
| 103 | *.asc|sha256sums*|hashes*) |
| 104 | echo "text/plain";; |
| 105 | esac |
| 106 | } |
| 107 | |
| 108 | # Format the JSON for creating the release and print it to standard output. |
| 109 | format_release_json () { |
| 110 | local version="$1" |
| 111 | local bodyfile="$2" |
| 112 | |
| 113 | ruby -rjson -e 'puts JSON.generate({ |
| 114 | tag_name: ARGV[0], |
| 115 | name: ARGV[0], |
| 116 | draft: true, |
| 117 | body: File.read(ARGV[1]), |
| 118 | })' "$version" "$bodyfile" |
| 119 | } |
| 120 | |
| 121 | # Create a draft release and print the upload URL for release assets to the |
| 122 | # standard output. If a release with that version already exists, do nothing |
| 123 | # instead. |
| 124 | create_release () { |
| 125 | local version="$1" |
| 126 | local bodyfile="$2" |
| 127 | |
| 128 | # Check to see if we already have such a release. If so, don't create it. |
| 129 | curl https://api.github.com/repos/$REPO/releases | \ |
| 130 | jq -r '.[].name' | grep -qsF "$version" && { |
| 131 | say "Found an existing release for this version." |
| 132 | curl https://api.github.com/repos/$REPO/releases | \ |
| 133 | jq -r '.[] | select(.name == "'"$version"'") | .upload_url' | \ |
| 134 | sed -e 's/{.*}//g' |
| 135 | return |
| 136 | } |
| 137 | |
| 138 | # This can be large, so pass it in a file. |
| 139 | format_release_json "$version" "$bodyfile" >> "$WORKDIR/release-json" |
| 140 | |
| 141 | curl -H'Content-Type: application/json' -d"@$WORKDIR/release-json" \ |
| 142 | https://api.github.com/repos/$REPO/releases | \ |
| 143 | jq -r '.upload_url' | |
| 144 | sed -e 's/{.*}//g' |
| 145 | } |
| 146 | |
| 147 | # Update the draft release with a new body and print the upload URL for release assets to the |
| 148 | # standard output. A release with the given version must already exist. |
| 149 | patch_release () { |
| 150 | local version="$1" |
| 151 | local bodyfile="$2" |
| 152 | |
| 153 | # Find the URL of this release. |
| 154 | local url=$(curl https://api.github.com/repos/$REPO/releases | \ |
| 155 | jq -r '.[] | select(.name == "'"$version"'") | .url') |
| 156 | |
| 157 | [ -n "$url" ] || abort "No existing release found for version $version." |
| 158 | say "Found the existing release for this version." |
| 159 | |
| 160 | # This can be large, so pass it in a file. |
| 161 | format_release_json "$version" "$bodyfile" >> "$WORKDIR/release-json" |
| 162 | |
| 163 | curl -XPATCH -H'Content-Type: application/json' -d"@$WORKDIR/release-json" \ |
| 164 | $url | \ |
| 165 | jq -r '.upload_url' | |
| 166 | sed -e 's/{.*}//g' |
| 167 | } |
| 168 | |
| 169 | # Find the release files for the given version. |
| 170 | release_files () { |
| 171 | local version="$1" |
| 172 | local assets="${2:-bin/releases}" |
| 173 | |
| 174 | [ -n "$version" ] || return 1 |
| 175 | |
| 176 | find "$assets" -name '*.tar.gz' -o \ |
| 177 | -name '*386*.zip' -o \ |
| 178 | -name '*amd64*.zip' -o \ |
| 179 | -name '*arm64*.zip' -o \ |
| 180 | -name '*.exe' -o \ |
| 181 | -name 'sha256sums.asc' -o \ |
| 182 | -name 'hashes.asc' | \ |
| 183 | grep -E "$version|sha256sums.asc|hashes.asc" | \ |
| 184 | grep -v "assets" | \ |
| 185 | LC_ALL=C sort |
| 186 | } |
| 187 | |
| 188 | # Format the body message and print the file which contains it to the standard |
| 189 | # output. |
| 190 | finalize_body_message () { |
| 191 | local version="$1" |
| 192 | local changelog="$2" |
| 193 | local assets="$3" |
| 194 | |
| 195 | version=$(echo "$version" | sed -e 's/^v//') |
| 196 | |
| 197 | cat "$changelog" > "$WORKDIR/body-template" |
| 198 | cat <<EOM >> "$WORKDIR/body-template" |
| 199 | ## Packages |
| 200 | |
| 201 | Up to date packages are available on [PackageCloud](https://packagecloud.io/github/git-lfs) and [Homebrew](http://brew.sh/). |
| 202 | |
| 203 | $(script/distro-tool --distro-markdown) |
| 204 | |
| 205 | ## SHA-256 hashes: |
| 206 | EOM |
| 207 | |
| 208 | shasum -a256 $(release_files "$version" "$assets") | \ |
| 209 | ruby -pe '$_.chomp!' \ |
| 210 | -e '$_.gsub!(/^([0-9a-f]+)\s+.*\/([^\/]+)$/, "**\\2**\n\\1\n\n")' | \ |
| 211 | ruby -0777 -pe '$_.gsub!(/\n+\z/, "\n")' >> "$WORKDIR/body-template" |
| 212 | |
| 213 | sed -e "s/VERSION/$version/g" < "$WORKDIR/body-template" > "$WORKDIR/body" |
| 214 | echo "$WORKDIR/body" |
| 215 | } |
| 216 | |
| 217 | # Filter a list of files from standard input, removing entries found in the file |
| 218 | # provided. |
| 219 | filter_files () { |
| 220 | local filter="$1" |
| 221 | |
| 222 | # If the filter file is empty (that is, no assets have been uploaded), grep |
| 223 | # will produce no output, and therefore nothing will be uploaded. That's not |
| 224 | # what we want, so handle this case specially. |
| 225 | if [ -s "$filter" ] |
| 226 | then |
| 227 | grep -vF -f "$filter" |
| 228 | else |
| 229 | cat |
| 230 | fi |
| 231 | } |
| 232 | |
| 233 | # Upload assets from the release directory to GitHub. Only assets that are not |
| 234 | # already existing should be uploaded. |
| 235 | upload_assets () { |
| 236 | local version="$1" |
| 237 | local upload_url="$2" |
| 238 | local src="$3" |
| 239 | local file desc base ct encdesc encbase |
| 240 | |
| 241 | curl https://api.github.com/repos/$REPO/releases | \ |
| 242 | jq -r '.[] | select(.name == "'"$version"'") | .assets | .[] | .name' \ |
| 243 | > "$WORKDIR/existing-assets" |
| 244 | |
| 245 | for file in $(release_files "$version" "$src" | filter_files "$WORKDIR/existing-assets") |
| 246 | do |
| 247 | base=$(basename "$file") |
| 248 | desc=$(categorize_asset "$base") |
| 249 | ct=$(content_type "$base") |
| 250 | encbase=$(uri_encode "$base") |
| 251 | encdesc=$(uri_encode "$desc") |
| 252 | |
| 253 | say "\tUploading %s as \"%s\" (Content-Type %s)..." "$base" "$desc" "$ct" |
| 254 | curl --data-binary "@$file" -H'Accept: application/vnd.github.v3+json' \ |
| 255 | -H"Content-Type: $ct" "$upload_url?name=$encbase&label=$encdesc" \ |
| 256 | >"$WORKDIR/response" |
| 257 | download=$(jq -r '.url' "$WORKDIR/response") |
| 258 | done |
| 259 | |
| 260 | say "Assets uploaded." |
| 261 | } |
| 262 | |
| 263 | # Download assets from GitHub to the specified directory. |
| 264 | download_assets () { |
| 265 | local version="$1" |
| 266 | local dir="$2" |
| 267 | |
| 268 | curl https://api.github.com/repos/$REPO/releases | \ |
| 269 | jq -rc '.[] | select(.name == "'"$version"'") | .assets | .[] | [.name,.url]' | \ |
| 270 | ruby -rjson -ne 'puts JSON.parse($_).join(" ")' \ |
| 271 | > "$WORKDIR/assets" |
| 272 | |
| 273 | cat "$WORKDIR/assets" | (while read base url |
| 274 | do |
| 275 | say "\tDownloading %s..." "$base" |
| 276 | ( |
| 277 | cd "$dir" && |
| 278 | curl -Lo "$base" -H"Accept: application/octet-stream" "$url" |
| 279 | ) |
| 280 | done) |
| 281 | } |
| 282 | |
| 283 | # Download the assets and verify the signature made on them. |
| 284 | verify_assets () { |
| 285 | local version="$1" |
| 286 | local dir="$WORKDIR/verify" |
| 287 | mkdir "$dir" |
| 288 | download_assets "$version" "$dir" |
| 289 | |
| 290 | # If the OpenPGP data is not valid, gpg -d will output nothing to stdout, and |
| 291 | # shasum will then fail. |
| 292 | say "Checking assets for integrity with SHA-256..." |
| 293 | (cd "$dir" && gpg -d sha256sums.asc | shasum -a 256 -c) |
| 294 | say "Checking assets for integrity with SHA-2..." |
| 295 | (cd "$dir" && gpg -d hashes.asc | grep 'SHA[0-9][^-]' | shasum -c) |
| 296 | if command -v sha3sum >/dev/null 2>&1 |
| 297 | then |
| 298 | say "Checking assets for integrity with SHA-3..." |
| 299 | (cd "$dir" && gpg -d hashes.asc | grep 'SHA3-256' | sed 's/.*(\(.*\)) = \(.*\)$/\2 \1/' | sha3sum -a 256 -c) |
| 300 | (cd "$dir" && gpg -d hashes.asc | grep 'SHA3-384' | sed 's/.*(\(.*\)) = \(.*\)$/\2 \1/' | sha3sum -a 384 -c) |
| 301 | (cd "$dir" && gpg -d hashes.asc | grep 'SHA3-512' | sed 's/.*(\(.*\)) = \(.*\)$/\2 \1/' | sha3sum -a 512 -c) |
| 302 | fi |
| 303 | if command -v b2sum >/dev/null 2>&1 |
| 304 | then |
| 305 | say "Checking assets for integrity with BLAKE2b..." |
| 306 | # b2sum on Linux does not handle BLAKE2s, only BLAKE2b. |
| 307 | (cd "$dir" && gpg -d hashes.asc | grep 'BLAKE2b' | b2sum -c) |
| 308 | fi |
| 309 | |
| 310 | say "\nAssets look good!" |
| 311 | } |
| 312 | |
| 313 | # Extract the changelog for the given version from the history and save it in a |
| 314 | # file. Print the filename of the changelog to standard output. |
| 315 | extract_changelog () { |
| 316 | local version="$1" |
| 317 | |
| 318 | git cat-file blob "$version:CHANGELOG.md" | \ |
| 319 | ruby -ne "version=%Q($version)[1..-1]; state ||= :silent; text ||= [];" \ |
| 320 | -e 'if state == :print && $_.start_with?("## "); puts text.join.strip; exit; end;' \ |
| 321 | -e 'text << $_ if state == :print;' \ |
| 322 | -e 'state = :print if $_.start_with?("## #{version}")' \ |
| 323 | > "$WORKDIR/changelog" |
| 324 | echo "$WORKDIR/changelog" |
| 325 | } |
| 326 | |
| 327 | # Perform the final steps to verify a release |
| 328 | finalize () { |
| 329 | local version="$1" |
| 330 | local inspect="$2" |
| 331 | local downloads="$WORKDIR/finalize" |
| 332 | local uploads="$WORKDIR/finalize-uploads" |
| 333 | |
| 334 | say "Finalizing the release process..." |
| 335 | say "Downloading assets..." |
| 336 | |
| 337 | mkdir "$downloads" |
| 338 | mkdir "$uploads" |
| 339 | download_assets "$version" "$downloads" |
| 340 | |
| 341 | if [ -n "$inspect" ] |
| 342 | then |
| 343 | say "Dropping you to a shell to inspect the assets." |
| 344 | say "Type 'exit 0' to continue, or 'exit 1' to abort." |
| 345 | |
| 346 | (cd "$downloads" && $SHELL) |
| 347 | fi |
| 348 | |
| 349 | say "Signing asset manifest..." |
| 350 | ( |
| 351 | root="$(git rev-parse --show-toplevel)" && |
| 352 | cd "$downloads" && \ |
| 353 | shasum -a256 -b * | grep -vE '(assets|sha256sums|hashes)' | \ |
| 354 | gpg --digest-algo SHA256 --clearsign >sha256sums.asc && |
| 355 | "$root/script/hash-files" * | grep -vE '(assets|sha256sums|hashes)' | \ |
| 356 | gpg --digest-algo SHA512 --clearsign >hashes.asc |
| 357 | ) |
| 358 | |
| 359 | say "Formatting the final body of the GitHub release now..." |
| 360 | |
| 361 | local changelog=$(extract_changelog "$version") |
| 362 | local bodyfile=$(finalize_body_message "$version" "$changelog" "$downloads") |
| 363 | |
| 364 | say "Uploading final release body..." |
| 365 | |
| 366 | local upload_url=$(patch_release "$version" "$bodyfile") |
| 367 | |
| 368 | say "Uploading final versions of assets..." |
| 369 | cp "$downloads/sha256sums.asc" "$downloads/hashes.asc" "$uploads" |
| 370 | upload_assets "$version" "$upload_url" "$uploads" |
| 371 | |
| 372 | # Verification occurs in caller below. |
| 373 | } |
| 374 | |
| 375 | # Provide a helpful usage message and exit. |
| 376 | usage () { |
| 377 | local status="$1" |
| 378 | cat <<EOM |
| 379 | Usage: $0 VERSION |
| 380 | |
| 381 | Create a draft GitHub release for Git LFS using the tag specified by VERSION and |
| 382 | the changelog specified in the file CHANGELOG. Before running this script, the |
| 383 | release assets should be built and ready for upload, including the signed |
| 384 | sha256sums.asc and hashes.asc files. |
| 385 | |
| 386 | This script requires ruby, gpg, curl, shasum, and jq. sha3sum and b2sum will be |
| 387 | used if available, but are optional. |
| 388 | |
| 389 | This command must be run from within the repository. |
| 390 | EOM |
| 391 | exit $status |
| 392 | } |
| 393 | |
| 394 | # Check that this script has the prerequisites to continue. |
| 395 | sanity_check () { |
| 396 | local version="$1" |
| 397 | local finalize="$2" |
| 398 | |
| 399 | say "Checking that you've got some release artifacts..." |
| 400 | if [ -z "$(release_files "$version")" ] |
| 401 | then |
| 402 | [ -n "$finalize" ] || abort "I couldn't find any release files for $version." |
| 403 | fi |
| 404 | |
| 405 | if [ -n "$GITHUB_TOKEN" ] |
| 406 | then |
| 407 | say "Found a token in the GITHUB_TOKEN environment variable." |
| 408 | else |
| 409 | say "Looking for the necessary entries in .netrc..." |
| 410 | grep -qsF api.github.com "$HOME/.netrc" || \ |
| 411 | abort "I couldn't find api.github.com in your .netrc." |
| 412 | grep -qsF uploads.github.com "$HOME/.netrc" || \ |
| 413 | abort "I couldn't find uploads.github.com in your .netrc." |
| 414 | fi |
| 415 | |
| 416 | say "Okay, everything looks good." |
| 417 | } |
| 418 | |
| 419 | # The main program. |
| 420 | main () { |
| 421 | local inspect="" |
| 422 | |
| 423 | while [ -n "$1" ] |
| 424 | do |
| 425 | case "$1" in |
| 426 | --help) |
| 427 | usage 0;; |
| 428 | --inspect) |
| 429 | inspect=1 |
| 430 | shift;; |
| 431 | --skip-verify) |
| 432 | SKIP_VERIFY=1 |
| 433 | shift;; |
| 434 | --finalize) |
| 435 | FINALIZE=1 |
| 436 | shift;; |
| 437 | --) |
| 438 | shift |
| 439 | break;; |
| 440 | *) |
| 441 | break;; |
| 442 | esac |
| 443 | done |
| 444 | |
| 445 | local version="$1" |
| 446 | |
| 447 | [ -z "$version" ] && usage 1 >&2 |
| 448 | |
| 449 | sanity_check "$version" "$FINALIZE" |
| 450 | |
| 451 | if [ -n "$FINALIZE" ] |
| 452 | then |
| 453 | finalize "$version" "$inspect" |
| 454 | else |
| 455 | say "Formatting the body of the GitHub release now..." |
| 456 | |
| 457 | local changelog=$(extract_changelog "$version") |
| 458 | local bodyfile=$(finalize_body_message "$version" "$changelog") |
| 459 | |
| 460 | say "Creating a GitHub release for %s..." "$version" |
| 461 | |
| 462 | local upload_url=$(create_release "$version" "$bodyfile") |
| 463 | |
| 464 | say "Uploading assets to GitHub..." |
| 465 | upload_assets "$version" "$upload_url" bin/releases |
| 466 | fi |
| 467 | |
| 468 | if [ -z "$SKIP_VERIFY" ] |
| 469 | then |
| 470 | say "Verifying assets..." |
| 471 | verify_assets "$version" |
| 472 | fi |
| 473 | |
| 474 | say "Okay, done. Sanity-check the release and publish it." |
| 475 | } |
| 476 | |
| 477 | main "$@" |