1#!/usr/bin/env bash
2set -e
3
4: ${NIXOS_CHANNELS:=https://nixos.org/channels/}
5: ${CHANNELS_NAMESPACE:=refs/heads/channels/}
6
7# List all channels which are currently in the repository which we would
8# have to remove if they are not found again.
9deadChannels=$(git for-each-ref --format="%(refname)" "$CHANNELS_NAMESPACE")
10
11updateRef() {
12 local channelName=$1
13 local newRev=$2
14
15 # if the inputs are not valid, then we do not update any branch.
16 test -z "$newRev" -o -z "$channelName" && return;
17
18 # Update the local refs/heads/channels/* branches to be in-sync with the
19 # channel references.
20 local branch=$CHANNELS_NAMESPACE$channelName
21 oldRev=$(git rev-parse --short "$branch" 2>/dev/null || true)
22 if test "$oldRev" != "$newRev"; then
23 if git update-ref "$branch" "$newRev" 2>/dev/null; then
24 if test -z "$oldRev"; then
25 echo " * [new branch] $newRev -> ${branch#refs/heads/}"
26 else
27 echo " $oldRev..$newRev -> ${branch#refs/heads/}"
28 fi
29 else
30 if test -z "$oldRev"; then
31 echo " * [missing rev] $newRev -> ${branch#refs/heads/}"
32 else
33 echo " [missing rev] $oldRev..$newRev -> ${branch#refs/heads/}"
34 fi
35 fi
36 fi
37
38 # Filter out the current channel from the list of dead channels.
39 deadChannels=$(grep -v "$CHANNELS_NAMESPACE$channelName" <<EOF
40$deadChannels
41EOF
42) ||true
43}
44
45# Find the name of all channels which are listed in the directory.
46echo "Fetching channels from $NIXOS_CHANNELS:"
47for channelName in : $(curl -s "$NIXOS_CHANNELS" | sed -n '/folder/ { s,.*href=",,; s,/".*,,; p }'); do
48 test "$channelName" = : && continue;
49
50 # Do not follow redirections, such that we can extract the
51 # short-changeset from the name of the directory where we are
52 # redirected to.
53 sha1=$(curl -sI "$NIXOS_CHANNELS$channelName" | sed -n '/Location/ { s,.*\.\([a-f0-9]*\)[ \r]*$,\1,; p; }')
54
55 updateRef "remotes/$channelName" "$sha1"
56done
57
58echo "Fetching channels from nixos-version:"
59if currentSystem=$(nixos-version 2>/dev/null); then
60 # If the system is entirely build from a custom nixpkgs version,
61 # then the version is not annotated in git version. This sed
62 # expression is basically matching that the expressions end with
63 # ".<sha1> (Name)" to extract the sha1.
64 sha1=$(echo "$currentSystem" | sed -n 's,^.*\.\([a-f0-9]*\) *(.*)$,\1,; T skip; p; :skip;')
65
66 updateRef current-system "$sha1"
67fi
68
69echo "Fetching channels from $HOME/.nix-defexpr:"
70for revFile in : $(find -L "$HOME/.nix-defexpr/" -maxdepth 4 -name svn-revision); do
71 test "$revFile" = : && continue;
72
73 # Deconstruct a path such as, into:
74 #
75 # /home/luke/.nix-defexpr/channels_root/nixos/nixpkgs/svn-revision
76 # channelName = root/nixos
77 #
78 # /home/luke/.nix-defexpr/channels/nixpkgs/svn-revision
79 # channelName = nixpkgs
80 #
81 user=${revFile#*.nix-defexpr/channels}
82 repo=${user#*/}
83 repo=${repo%%/*}
84 user=${user%%/*}
85 user=${user#_}
86 test -z "$user" && user=$USER
87 channelName="$user${user:+/}$repo"
88
89 sha1=$(sed -n 's,^.*\.\([a-f0-9]*\)$,\1,; T skip; p; :skip;' "$revFile")
90
91 updateRef "$channelName" "$sha1"
92done
93
94# Suggest to remove channel branches which are no longer found by this
95# script. This is to handle the cases where a local/remote channel
96# disappear. We should not attempt to remove manually any branches, as they
97# might be user branches.
98if test -n "$deadChannels"; then
99
100 echo "
101Some old channel branches are still in your repository, if you
102want to remove them, run the following command(s):
103"
104
105 while read branch; do
106 echo " git update-ref -d $branch"
107 done <<EOF
108$deadChannels
109EOF
110
111 echo
112fi