-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-delete-pruned
More file actions
executable file
·78 lines (68 loc) · 2.11 KB
/
git-delete-pruned
File metadata and controls
executable file
·78 lines (68 loc) · 2.11 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
#!/bin/bash
set -e # Exit immediately if a command exits with a non-zero status
# Parse command line arguments
dry_run=false
force=false
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dry-run)
dry_run=true
shift
;;
-f|--force)
force=true
shift
;;
*)
echo "Unknown option: $1"
echo "Usage: $(basename "$0") [-d|--dry-run] [-f|--force]"
exit 1
;;
esac
done
# Function to handle errors
error_handler() {
echo "Error: An error occurred during script execution"
exit 1
}
# Set up error handling
trap error_handler ERR
# Fetch and prune remote branches first
echo "Fetching and pruning remote branches..."
git fetch --prune || {
echo "Error: Failed to fetch from remote. Please check your network connection and try again."
exit 1
}
# Get list of local branches that were tracking remote branches
stale_branches=$(git branch -vv | grep ': gone]' | awk '{print $1}' || true)
# If we found any stale branches, show/delete them
if [ -n "$stale_branches" ]; then
if [ "$dry_run" = true ]; then
echo "[DRY RUN] The following branches would be deleted:"
echo "$stale_branches"
else
if [ "$force" = true ]; then
while IFS= read -r branch; do
git branch -D "$branch"
done <<< "$stale_branches"
echo "The following branches were deleted as their remote tracking branches are gone:"
echo "$stale_branches"
echo
else
echo "The following branches will be deleted as their remote tracking branches are gone:"
echo "$stale_branches"
echo
read -p "Do you want to proceed with deletion? [y/N] " confirm
if [[ $confirm =~ ^[Yy]$ ]]; then
while IFS= read -r branch; do
git branch -D "$branch"
done <<< "$stale_branches"
else
echo "Operation cancelled."
exit 0
fi
fi
fi
else
echo "No stale branches found."
fi