-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_world.sh
More file actions
executable file
·89 lines (72 loc) · 2.48 KB
/
git_world.sh
File metadata and controls
executable file
·89 lines (72 loc) · 2.48 KB
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
#!/usr/bin/env bash
set -euo pipefail
usage() {
local cmd
cmd=$(basename "$0")
cat << EOF
Usage: $cmd [OPTIONS]
Tidy up a git repository by fetching, pruning remotes, and cleaning up stale
local branches and worktrees.
Performs the following steps in order:
1. Fetch all remotes (skipped with --offline)
2. Prune stale remote-tracking references (skipped with --offline)
3. Delete local branches whose upstream is gone
4. Clean up stale worktrees (if any exist)
OPTIONS:
-h, --help Show this help message and exit
-o, --offline Skip network operations (fetch, prune remotes)
EXAMPLES:
$cmd Clean up the current git repository
$cmd --offline Clean up without fetching from remotes
$cmd --help Show this help
EXIT CODES:
0 Cleanup completed (even if some branches could not be deleted)
1 Not in a git repository or fatal error
EOF
}
offline=false
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help) usage; exit 0 ;;
-o|--offline) offline=true; shift ;;
*) echo "Unknown option: $1"; usage; exit 1 ;;
esac
done
setup_colors() {
if [[ -t 1 ]] && [[ -z "${NO_COLOR:-}" ]]; then
bold=$(tput bold 2>/dev/null) || bold=""
green=$(tput setaf 2 2>/dev/null) || green=""
red=$(tput setaf 1 2>/dev/null) || red=""
cyan=$(tput setaf 6 2>/dev/null) || cyan=""
reset=$(tput sgr0 2>/dev/null) || reset=""
else
bold="" green="" red="" cyan="" reset=""
fi
}
main() {
setup_colors
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "${red}Error: not in a git repository${reset}"
exit 1
fi
if [[ "$offline" == false ]]; then
echo "${bold}${cyan}Fetching all remotes...${reset}"
git fetch --all --prune
echo "${bold}${cyan}Pruning unreachable objects...${reset}"
git prune
fi
echo "${bold}${cyan}Deleting local branches with gone upstreams...${reset}"
git branch -vv | awk '/: gone]/{print $1}' | while IFS= read -r branch; do
if git branch -D "$branch" 2>/dev/null; then
echo " ${green}✓ Deleted branch: $branch${reset}"
else
echo " ${red}✗ Could not delete branch: $branch${reset}"
fi
done
if git worktree list --porcelain | grep -q '^worktree '; then
echo "${bold}${cyan}Cleaning up stale worktrees...${reset}"
git wt world 2>/dev/null || true
fi
echo "${bold}${green}Done.${reset}"
}
main "$@"