Skip to content

Commit e844780

Browse files
committed
contrib: add a wrapper to be able to check deploys use up-to-date git
This is adapted from an internal project. It's a simple solution that probably has wider applicability than just our little project.
1 parent c84ccd0 commit e844780

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

contrib/colmena-wrapper.nix

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# A wrapper for colmena that prevents accidentally deploying changes without
2+
# having pulled.
3+
{ colmena, runCommandNoCC }:
4+
runCommandNoCC "colmena-wrapper"
5+
{
6+
env = {
7+
colmena = "${colmena}/bin/colmena";
8+
remote_name = "origin";
9+
upstream_branch = "main";
10+
};
11+
} ''
12+
mkdir -p $out
13+
ln -s ${colmena}/share $out/share
14+
mkdir $out/bin
15+
16+
substituteAll ${./colmena-wrapper.sh.in} $out/bin/colmena
17+
chmod +x $out/bin/colmena
18+
''

contrib/colmena-wrapper.sh.in

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
doChecks() {
4+
# creates refs in the refs/prefetch/remotes/origin namespace
5+
echo "Prefetching repo changes..." >&2
6+
git fetch --quiet --prefetch --no-write-fetch-head @remote_name@
7+
8+
diffs=$(git rev-list --left-right --count HEAD...refs/prefetch/remotes/@remote_name@/@upstream_branch@)
9+
only_in_local=$(echo "$diffs" | cut -f1)
10+
only_in_main=$(echo "$diffs" | cut -f2)
11+
12+
if [[ $only_in_main -gt 0 && ! -v $FOOTGUN_ME_UWU ]]; then
13+
echo >&2
14+
echo "Attempting to deploy when @upstream_branch@ has $only_in_main commits not in your branch!" >&2
15+
echo "This will probably revert someone's changes. Consider merging them." >&2
16+
echo "If you really mean it, set the environment variable FOOTGUN_ME_UWU" >&2
17+
exit 1
18+
fi
19+
20+
if [[ $only_in_local -gt 0 ]]; then
21+
echo "You have $only_in_local commits not yet pushed to @upstream_branch@. Reminder to push them after :)" >&2
22+
fi
23+
}
24+
25+
if [[ $1 == 'apply' ]]; then
26+
doChecks
27+
fi
28+
29+
exec @colmena@ "$@"

0 commit comments

Comments
 (0)