-
Notifications
You must be signed in to change notification settings - Fork 0
chore: worktree hygiene rule and safe prune script #722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ttupper92618
wants to merge
3
commits into
dev
Choose a base branch
from
chore/worktree-hygiene
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| #!/bin/bash | ||
| # Remove git worktrees whose work has already merged into origin/dev. | ||
| # | ||
| # A worktree is removed only when ALL of these hold: | ||
| # 1. its HEAD is an ancestor of origin/dev (merge commits make this a | ||
| # reliable merged-content check for this repository), | ||
| # 2. its tree is clean (no staged, unstaged, or untracked changes), | ||
| # 3. it has not been touched for at least MIN_AGE_HOURS (default 48), | ||
| # so an agent's in-flight checkout is never yanked mid-run. | ||
| # | ||
| # Branches and commits are never deleted; `git worktree remove` only | ||
| # deletes the working directory. Pass --dry-run to preview. | ||
| # | ||
| # Usage: scripts/prune_merged_worktrees.sh [--dry-run] [repo-path] | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| DRY_RUN=0 | ||
| if [ "${1:-}" = "--dry-run" ]; then | ||
| DRY_RUN=1 | ||
| shift | ||
| fi | ||
| REPO="${1:-$(git rev-parse --show-toplevel 2>/dev/null || true)}" | ||
| if [ -z "$REPO" ]; then | ||
| echo "error: not inside a git repository and no repo path given" >&2 | ||
| exit 2 | ||
| fi | ||
| MIN_AGE_HOURS="${MIN_AGE_HOURS:-48}" | ||
| NOW_EPOCH=$(date +%s) | ||
|
|
||
| git -C "$REPO" fetch -q origin dev | ||
|
|
||
| # Porcelain lines are "worktree <path>"; strip the prefix with sed so paths | ||
| # containing spaces survive intact. | ||
| MAIN_WT=$(git -C "$REPO" worktree list --porcelain | sed -n 's/^worktree //p' | head -n 1) | ||
|
|
||
| git -C "$REPO" worktree list --porcelain | sed -n 's/^worktree //p' | while IFS= read -r wt_path; do | ||
| [ "$wt_path" = "$MAIN_WT" ] && continue | ||
| if ! wt_head=$(git -C "$wt_path" rev-parse HEAD 2>/dev/null); then | ||
| # Directory or linkage already gone; `git worktree prune` handles it. | ||
| continue | ||
| fi | ||
| if [ -n "$(git -C "$wt_path" status --porcelain 2>/dev/null)" ]; then | ||
| echo "keep (dirty): $wt_path" | ||
| continue | ||
| fi | ||
| if ! git -C "$REPO" merge-base --is-ancestor "$wt_head" origin/dev; then | ||
| echo "keep (unmerged): $wt_path" | ||
| continue | ||
| fi | ||
| # Activity signal: the linked worktree's admin dir (its real git-dir) | ||
| # has entries (HEAD, index, logs) whose mtimes move on checkouts, | ||
| # commits, and resets; the .git linkage file alone is written once at | ||
| # creation and never again, so it cannot distinguish an active checkout | ||
| # from an abandoned one. Take the newest of the linkage file and the | ||
| # admin dir's HEAD/index. | ||
| wt_gitdir=$(git -C "$wt_path" rev-parse --absolute-git-dir 2>/dev/null || echo "") | ||
| wt_mtime=0 | ||
| for probe in "$wt_path/.git" "$wt_gitdir/HEAD" "$wt_gitdir/index"; do | ||
| [ -e "$probe" ] || continue | ||
| probe_mtime=$(stat -f %m "$probe" 2>/dev/null || stat -c %Y "$probe" 2>/dev/null || echo 0) | ||
| if [ "$probe_mtime" -gt "$wt_mtime" ]; then | ||
| wt_mtime=$probe_mtime | ||
| fi | ||
| done | ||
| age_hours=$(( (NOW_EPOCH - wt_mtime) / 3600 )) | ||
|
ttupper92618 marked this conversation as resolved.
|
||
| if [ "$age_hours" -lt "$MIN_AGE_HOURS" ]; then | ||
| echo "keep (recent, ${age_hours}h): $wt_path" | ||
| continue | ||
| fi | ||
| if [ "$DRY_RUN" -eq 1 ]; then | ||
| echo "would remove: $wt_path" | ||
| else | ||
| git -C "$REPO" worktree remove "$wt_path" | ||
| echo "removed: $wt_path" | ||
| fi | ||
| done | ||
|
|
||
| if [ "$DRY_RUN" -eq 0 ]; then | ||
| git -C "$REPO" worktree prune | ||
| fi | ||
| git -C "$REPO" worktree list | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.