From 718a03118c8bd268b34f0e2645b7897236782a28 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Mon, 24 Mar 2025 16:55:06 +0100 Subject: [PATCH] clean --- clean.sh | 96 -------------------------------------------------------- justfile | 84 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 101 deletions(-) delete mode 100755 clean.sh diff --git a/clean.sh b/clean.sh deleted file mode 100755 index cb37e75..0000000 --- a/clean.sh +++ /dev/null @@ -1,96 +0,0 @@ -#!/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" diff --git a/justfile b/justfile index 5566868..ee4aa31 100644 --- a/justfile +++ b/justfile @@ -378,8 +378,6 @@ pr $number=`just _a_pr`: test -s "$FNAME" && bat --language markdown --style plain --pager never "$FNAME" && gum confirm && gh pr {{ mode }} "$NUMBER" --body-file "$FNAME"; \ rm -v "$FNAME" -# TODO: clean - remote-branch $remote_ref=`just _a_remote_branch origin | cut -d/ -f2-`: #!/usr/bin/env -S bash -euo pipefail branch_dir=$(cut <<<"$remote_ref" -d- -f1) @@ -517,6 +515,7 @@ setup: add_upstream() { local branch="$1" if [[ ! -d ../upstream/"$branch" ]]; then + $GIT fetch upstream "$branch" $GIT worktree add ../upstream/"$branch" -B "$branch" upstream/"$branch" fi } @@ -538,6 +537,72 @@ setup: # === helpers === # TODO stat the file and redo if too old, or tie it to the master checkout commit +clean: + #!/usr/bin/env -S bash -euo pipefail + cleanlist="$(just _mk_clean_list)" || exit 1 + gum choose --no-limit --height="$(($(wc -l <<<"$cleanlist") + 5))" <<<"$cleanlist" | bash -x - + +_mk_clean_list: + #!/usr/bin/env -S bash -euo pipefail + # set -x + ( + cd master + # TODO: assert "upstream" present? + just _upstream_release_branches "" | xargs -d'\n' $GIT fetch upstream + ) + 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" + ( + cd master + just _upstream_release_branches | xe -j0 $GIT branch --merged ||: + ) | sort -u | + while IFS= read line; do + branch="$(cut <<<"$line" -c3-)" + if grep <<<"$branch" "$(just _upstream_release_branches '^' '$')" --quiet; then + continue + fi + #jq <<<"$worktrees" '.branch' + worktree="$( jq <<<"$worktrees" 'select(.branch == "refs/heads/'"$branch"'")' -c | head -n1 )" + #echo "$line" + #echo "$branch" + #echo "$worktree" + if [[ -n "$worktree" ]]; then + path="$(jq <<<"$worktree" .worktree -r )" + if [[ -d "$path" ]]; then + if ( + cd "$path" + ! $GIT diff HEAD --quiet --exit-code >&/dev/null || + $GIT ls-files --others --exclude-standard | + grep -qEv '^(update-executables\.txt|results|build\.nix|(asd|do|foo|test|spismeg|packages)[2-9]?.(sh|txt|json)|eval.json|[^/]*\.log)$' + ) ; then + continue + fi + if [[ "$(basename "$path")" != "tmp" ]] \ + && [[ "$(basename "$path")" != "scratch" ]] + then + printf "rm -rf %q\n" "$path" + fi + fi + fi + printf "%s\n" "( cd master && $GIT worktree prune && git branch -D \"$branch\" )" + done + #printf "%s\n" "( cd master && git worktree prune && git prune )" # slow + printf "%s\n" "( cd master && git worktree prune )" + printf "%s\n" "rmdir */ --ignore-fail-on-non-empty" + _packages_json: #!/usr/bin/env -S bash -euo pipefail if [[ ! -s packages.json || @@ -581,9 +646,18 @@ _packages_json: @_a_pr: cd master; gh pr list --limit 1000 --json 'number,title' --state open | jq '.[]|"\(.number) - \(.title)"' -r | gum filter --placeholder "Pick PR..." --height 10 | cut -d' ' -f1 -@_a_upstream_release_branch $header *$extra_branches: - test -n "${prefix:-}" || prefix="upstream/"; \ - shift; gum choose --header="$header" "$@" "${prefix}"master "${prefix}"staging "${prefix}"staging-next "${prefix}"python-updates "${prefix}"release-24.11 "${prefix}"staging-24.11 "${prefix}"staging-next-24.11 +@_upstream_release_branches $prefix="upstream/" $suffix="": + printf "${prefix//%/%%}%s${suffix//%/%%}\n" \ + master \ + staging \ + staging-next \ + python-updates \ + release-24.11 \ + staging-24.11 \ + staging-next-24.11 + +@_a_upstream_release_branch $header *extra_branches: + shift; gum choose --header="$header" "$@" $(just _upstream_release_branches "${prefix:-upstream/}") # --ordered @_sanitize_pr_url $number: