1#!/usr/bin/env nix-shell
2#!nix-shell -i bash -p nix-update curl jq gnused
3
4set -euo pipefail
5
6# Do the actual update.
7nix-update "${UPDATE_NIX_ATTR_PATH}"
8
9# Get the src metadata.
10src=$(
11 nix-instantiate --json --eval --strict --expr '
12 with import ./. {};
13 {
14 owner = '"${UPDATE_NIX_ATTR_PATH}"'.src.owner;
15 repo = '"${UPDATE_NIX_ATTR_PATH}"'.src.repo;
16 tag = '"${UPDATE_NIX_ATTR_PATH}"'.src.rev;
17 }'
18)
19owner=$(jq -r '.owner' <<< "${src}")
20repo=$(jq -r '.repo' <<< "${src}")
21tag=$(jq -r '.tag' <<< "${src}")
22
23# Curl the release to get the commit sha.
24curlFlags=("-fsSL")
25curlFlags+=(${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"})
26
27read -r type tag_sha < <(
28 curl "${curlFlags[@]}" "https://api.github.com/repos/${owner}/${repo}/git/ref/tags/${tag}" |
29 jq -j '.object.type, " ", .object.sha, "\n"'
30)
31
32if [[ "${type}" == "commit" ]]; then
33 sha="${tag_sha}"
34else
35 sha=$(
36 curl "${curlFlags[@]}" "https://api.github.com/repos/${owner}/${repo}/git/tags/${tag_sha}" |
37 jq '.object.sha'
38 )
39fi
40
41if [[ -z "${sha}" ]]; then
42 echo "failed to get commit sha of ${owner}/${repo} @ ${tag}" >&2
43 exit 1
44fi
45
46echo "updating commit hash of ${owner}/${repo} @ ${tag} to ${sha}" >&2
47
48cd "$(dirname "$(readlink -f "$0")")"
49
50sed -i "s|\".*\"; # update-commit-sha|${sha}; # update-commit-sha|" default.nix