stockholm/bin/prefetch

89 lines
2.5 KiB
Plaintext
Raw Normal View History

2015-06-24 18:00:52 +00:00
#! /bin/sh
2015-06-26 15:58:30 +00:00
#
2015-06-29 22:15:03 +00:00
# usage: prefetch repo_name out_link
2015-06-26 15:58:30 +00:00
#
# Make the specified repository available as out_link.
2015-06-26 15:58:30 +00:00
#
2015-06-24 18:00:52 +00:00
set -euf
repo_name=$1
out_link=$2
if test "$repo_name" != nixpkgs; then
2015-06-29 22:15:03 +00:00
echo "prefetch: cannot fetch $repo_name, yet" >&2
exit -1
fi
git_rev=$(nixos-query nixpkgs.rev)
git_url=$(nixos-query nixpkgs.url)
dirty=$(nixos-query nixpkgs.dirty)
case $dirty in true)
ln -snf "$git_url" "$out_link"
2015-06-29 22:15:03 +00:00
echo "prefetch: using $git_url as it is" >&2
exit
esac
2015-06-24 18:00:52 +00:00
2015-06-26 15:58:30 +00:00
# Put all bases in the same place as out_link.
# Notice how out_link must not clash with cache_dir and work_dir.
cache_base=$(dirname "$out_link")
work_base=$(dirname "$out_link")
2015-06-24 18:00:52 +00:00
2015-06-26 15:58:30 +00:00
# cache_dir points to a (maybe non-existent) directory, where a shared cache of
# the repository should be maintained. The shared cache is used to create
# multiple working trees of the repository.
cache_dir=$cache_base/$(echo "$git_url" | urlencode)
2015-06-24 18:00:52 +00:00
2015-06-26 15:58:30 +00:00
# work_dir points to a (maybe non-existent) directory, where a specific
# revision of the repository is checked out.
2015-06-28 11:47:21 +00:00
work_dir=$work_base/$(echo "$git_rev" | urlencode)
2015-06-24 18:00:52 +00:00
2015-06-26 15:58:30 +00:00
cache_git() {
git --git-dir="$cache_dir" "$@"
}
work_git() {
git -C "$work_dir" "$@"
}
2015-06-24 18:00:52 +00:00
2015-06-26 15:58:30 +00:00
is_up_to_date() {
test -d "$cache_dir" &&
test -d "$work_dir" &&
2015-06-28 12:54:46 +00:00
test "$(cache_git rev-parse --verify "$git_rev")" = "$git_rev" &&
test "$(work_git rev-parse --verify HEAD)" = "$git_rev"
2015-06-26 15:58:30 +00:00
}
2015-06-24 18:00:52 +00:00
# Notice how the remote name "origin" has been chosen arbitrarily, but must be
# kept in sync with the default value of nixpkgs.rev.
2015-06-26 15:58:30 +00:00
if ! is_up_to_date; then
if ! test -d "$cache_dir"; then
mkdir -p "$cache_dir"
cache_git init --bare
fi
if ! cache_git_url=$(cache_git config remote.origin.url); then
cache_git remote add origin "$git_url"
elif test "$cache_git_url" != "$git_url"; then
cache_git remote set-url origin "$git_url"
fi
cache_git fetch origin
if ! test -d "$work_dir"; then
git clone -n --shared "$cache_dir" "$work_dir"
fi
2015-06-28 12:54:46 +00:00
commit_name=$(cache_git rev-parse --verify "$git_rev")
2015-06-28 11:47:21 +00:00
work_git checkout "$commit_name" -- "$(readlink -f "$work_dir")"
work_git checkout -q "$commit_name"
2015-06-26 15:58:30 +00:00
work_git submodule init
work_git submodule update
fi
work_git clean -dxf
# Relative links are nicer, and actually we know that work_dir and out_link are
# the same. But, for robustness, check anyway.. :)
if test "$(dirname "$work_dir")" = "$(dirname "$out_link")"; then
ln -snf "$(basename "$work_dir")" "$out_link"
else
ln -snf "$work_dir" "$out_link"
fi
2015-06-28 13:09:01 +00:00
2015-06-29 22:15:03 +00:00
echo "prefetch: using $git_url $(work_git log --oneline -n1)" >&2