summaryrefslogtreecommitdiffstats
path: root/pkgs/shell/update-sources
blob: a50a1ed2fb3786d6f9b5a54559d2d049c54736a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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