-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete-nix-branch
executable file
·112 lines (89 loc) · 2.16 KB
/
delete-nix-branch
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
exit_if_branch_invalid () {
BRANCH="$1"
if ! git rev-parse --verify "${BRANCH}" >/dev/null 2>&1 ; then
echo "Branch ${BRANCH} does not exist."
exit 2
fi
echo "Branch ${BRANCH} selected."
if [ "${BRANCH}" = "main" ]; then
echo "Never deleting ${BRANCH}."
exit 3
elif [ "${BRANCH}" = "master" ]; then
echo "Never deleting ${BRANCH}."
exit 3
fi
}
POSITIONAL_ARGS=()
FORCE=false
while [[ $# -gt 0 ]]; do
case $1 in
--force|-f)
FORCE=true
echo "Warning force enabled!"
shift
;;
--*|-*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
if [[ -n "$1" ]]; then
BRANCH="$1"
fi
if [ -z ${BRANCH+x} ]; then
echo "No branch specified!"
exit 1
fi
exit_if_branch_invalid "$BRANCH"
SYSTEM_PROFILES_ROOT="/nix/var/nix/profiles/system-profiles"
shopt -s nullglob
MATCHING_BUILDS=()
for link in "${SYSTEM_PROFILES_ROOT}/${BRANCH}"*; do
[ -L "$link" ] || continue
MATCHING_BUILDS+=("$link")
done
N_MATCHING_BUILDS=${#MATCHING_BUILDS[@]}
if [ "$FORCE" = true ]; then
GIT_CMD="git branch -D ${BRANCH}"
else
GIT_CMD="git branch -d ${BRANCH}"
fi
# TODO only use -D when explicitly forced
echo "1. Will run \"${GIT_CMD}\""
echo "2. Will unlink $N_MATCHING_BUILDS matching builds."
echo "3. Regenerate boot entries."
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
# handle exits from shell or function but don't exit interactive shell
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1
fi
# exit on nonzero status code
set -e
echo "Deleting branch."
# FIXME unify with GIT_CMD
if [ "$FORCE" = true ]; then
git branch -D "${BRANCH}"
else
git branch -d "${BRANCH}"
fi
if [[ "$N_MATCHING_BUILDS" -gt 0 ]]; then
echo "Unlinking builds."
for link in "${MATCHING_BUILDS[@]}"; do
echo "Unlinking ${link}."
sudo unlink "$link"
done
echo "Regenerating boot entries."
sudo /run/current-system/bin/switch-to-configuration boot
else
# TODO actually check that there are no boot entries
echo "No profiles and boot entries."
fi