#! /bin/sh #!buildShellBin path=coreutils:git:jq # usage: update-sources [NAME...] # # Moves each named entry of sources.json, or each one with a branch, to # the head of its branch. set -efu cd "$HOME/sync/src/hrm" if test $# = 0; then set -- $(jq -r 'to_entries[] | select(.value.branch) | .key' sources.json) fi tmpdir=$(mktemp -d --tmpdir update-sources.XXXXXXXX) trap 'rm -fR "$tmpdir"' EXIT new=$(cat sources.json) for name; do if ! entry=$(jq -ce --arg name "$name" '.[$name]' sources.json); then echo "update-sources: $name: no such source" >&2 exit 1 fi url=$(echo "$entry" | jq -r .url) branch=$(echo "$entry" | jq -r '.branch // empty') if test -z "$branch"; then echo "update-sources: $name: pinned by hand" >&2 exit 1 fi if echo "$entry" | jq -e 'has("hash")' >/dev/null; then echo "update-sources: $name: can't compute the hash of a source that follows a branch" >&2 exit 1 fi repo=$tmpdir/$(echo "$name" | tr / _) git init -q --bare "$repo" git -C "$repo" fetch -q --depth 1 --filter=tree:0 "$url" "refs/heads/$branch" rev=$(git -C "$repo" rev-parse FETCH_HEAD) date=$(TZ=UTC git -C "$repo" log -1 --format=%ad --date=format-local:%Y-%m-%dT%H:%M:%SZ FETCH_HEAD) old_rev=$(echo "$entry" | jq -r .rev) if test "$rev" = "$old_rev"; then echo "$name: $(echo "$rev" | cut -c-7) (no change since $date)" else echo "$name: $(echo "$old_rev" | cut -c-7) -> $(echo "$rev" | cut -c-7) (last change $date)" new=$(echo "$new" | jq -S --arg name "$name" --arg rev "$rev" --arg date "$date" ' .[$name] += { rev: $rev, date: $date } ') fi done cp sources.json sources.json~ echo "$new" | jq -S . > sources.json