From 244d2a5a635811b309caf3d99013bcb3f2e6a69f Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 29 Nov 2026 14:14:05 +0000 Subject: update-sources: init Moves the entries of sources.json that follow a branch to its head, or the named ones. It refuses entries pinned by hand, and ones with a hash, which it can't compute. --- pkgs/shell/update-sources | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 pkgs/shell/update-sources (limited to 'pkgs/shell/update-sources') diff --git a/pkgs/shell/update-sources b/pkgs/shell/update-sources new file mode 100755 index 00000000..a50a1ed2 --- /dev/null +++ b/pkgs/shell/update-sources @@ -0,0 +1,55 @@ +#! /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 -- cgit v1.3.1