Files
just-nixpkgs/clean.sh
2025-03-21 19:14:52 +01:00

97 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# TODO: list worktree where a --force-with-lease would fail, those are often changed from a different host
test -e master/.git || {
>&2 echo "ERROR: not right cwd?"
exit 1
}
if test -t 0 && test -t 1 && command -v gum >/dev/null; then
>&2 echo gumming...
cleanlist="$(./clean.sh)" || exit 1
gum choose --no-limit --height="$(($(wc -l <<<"$cleanlist") + 5))" <<<"$cleanlist" | bash -x -
exit 0
fi
(
cd master
# TODO: assert upstream?
git fetch upstream master staging staging-next python-updates
)
porcelain2jsonl() {
uniq | (
printf "["
while read key data; do
if test -z "$key"; then
printf "]\n["
else
printf '{"%s": "%s"}, ' "$key" "$data"
fi
done
echo "]"
) | sed -e 's/, ]/]/g' | grep -v '^\[\]$' | jq add -c
}
worktrees="$( cd master; git worktree list --porcelain | porcelain2jsonl )"
#cat <<<"$worktrees"
#exit
(
cd master
(
git branch --merged upstream/master
git branch --merged upstream/staging
git branch --merged upstream/staging-next
git branch --merged upstream/python-updates
git branch --merged upstream/release-24.05
# git branch --merged upstream/release-23.11
) | sort -u
) |
while IFS= read line; do
branch="$(echo "$line" | cut -c3-)"
if test "$branch" = "master" \
|| test "$branch" = "staging" \
|| test "$branch" = "staging-next" \
|| test "$branch" = "python-updates" \
|| test "$branch" = "release-24.05" \
|| test "$branch" = "release-23.11"
then
continue
fi
#jq <<<"$worktrees" '.branch'
worktree="$( jq <<<"$worktrees" 'select(.branch == "refs/heads/'"$branch"'")' -c | head -n1 )"
#echo "$line"
#echo "$branch"
#echo "$worktree"
if test -n "$worktree"; then
path="$(jq <<<"$worktree" .worktree -r )"
if test -d "$path"; then
( cd "$path" ;
test -n "$(git diff --staged)" ||
test -n "$(git diff)" ||
test -n "$(git ls-files --others --exclude-standard | grep -Ev '^(update-executables\.txt|results|build\.nix|(asd|do|foo|test|spismeg|packages)[2-9]?.(sh|txt|json)|eval.json|[^/]*\.log)$')"
) &>/dev/null && continue
if test "$(basename "$path")" != "tmp" \
&& test "$(basename "$path")" != "scratch" \
;then
echo rm -rf "$path"
fi
fi
fi
echo "( cd master && git worktree prune && git branch -D \"$branch\" )"
done
#echo "( cd master && git worktree prune && git prune )" # slow
echo "( cd master && git worktree prune )"
echo "rmdir */ --ignore-fail-on-non-empty"