|
#!/bin/bash |
|
|
|
# =============================== |
|
# 🧼 Git Branch Cleaner v1.0 |
|
# =============================== |
|
# Elimina ramas locales excepto las protegidas o que coinciden con patrones. |
|
# Usa --run para ejecutar, o por defecto solo muestra (dry run). |
|
# =============================== |
|
|
|
# 🚫 Ramas protegidas por nombre o patrón (usa globbing) |
|
PROTECTED_PATTERNS=("master" "main" "dev" "qa" "feat/*" "fix/*") |
|
|
|
RED='\033[0;31m' |
|
GREEN='\033[0;32m' |
|
YELLOW='\033[1;33m' |
|
CYAN='\033[0;36m' |
|
NC='\033[0m' |
|
|
|
# 🔒 Verifica si una rama coincide con patrón protegido |
|
is_protected() { |
|
local branch=$1 |
|
for pattern in "${PROTECTED_PATTERNS[@]}"; do |
|
if [[ "$branch" == $pattern ]]; then |
|
return 0 |
|
fi |
|
done |
|
return 1 |
|
} |
|
|
|
# 🧹 Limpia ramas locales |
|
clean_local_branches() { |
|
local run=$1 |
|
local deleted_count=0 |
|
local skipped_count=0 |
|
local all_branches |
|
all_branches=$(git branch | sed 's/..//') |
|
|
|
echo -e "${CYAN}📂 Limpieza de ramas locales:${NC}" |
|
for branch in $all_branches; do |
|
if is_protected "$branch"; then |
|
echo -e "${GREEN}✔ Protegida: $branch${NC}" |
|
((skipped_count++)) |
|
else |
|
if [[ "$run" == "true" ]]; then |
|
echo -e "${RED}🗑 Eliminando local: $branch${NC}" |
|
git branch -D "$branch" |
|
else |
|
echo -e "${YELLOW}🔸 Se eliminaría local: $branch${NC}" |
|
fi |
|
((deleted_count++)) |
|
fi |
|
done |
|
|
|
echo -e "${CYAN}🔚 Local: $deleted_count eliminables, $skipped_count protegidas.${NC}" |
|
} |
|
|
|
# 🌐 Limpia ramas remotas |
|
clean_remote_branches() { |
|
local run=$1 |
|
local deleted_count=0 |
|
local skipped_count=0 |
|
local remote_branches |
|
remote_branches=$(git branch -r | grep origin/ | sed 's/origin\///') |
|
|
|
echo -e "${CYAN}🌐 Limpieza de ramas remotas (origin):${NC}" |
|
for branch in $remote_branches; do |
|
if is_protected "$branch"; then |
|
echo -e "${GREEN}✔ Protegida: $branch${NC}" |
|
((skipped_count++)) |
|
else |
|
if [[ "$run" == "true" ]]; then |
|
echo -e "${RED}🗑 Eliminando remoto: origin/$branch${NC}" |
|
git push origin --delete "$branch" |
|
else |
|
echo -e "${YELLOW}🔸 Se eliminaría remoto: origin/$branch${NC}" |
|
fi |
|
((deleted_count++)) |
|
fi |
|
done |
|
echo -e "${CYAN}🔚 Remoto: $deleted_count eliminables, $skipped_count protegidas.${NC}" |
|
} |
|
|
|
# 📘 Ayuda |
|
show_help() { |
|
echo -e "${CYAN}Uso:${NC}" |
|
echo " ./git-clean-branches.sh [opciones]" |
|
echo "" |
|
echo "Opciones:" |
|
echo " --run Ejecuta la eliminación local" |
|
echo " --remote También limpia ramas en origin" |
|
echo " --help Muestra esta ayuda" |
|
echo "" |
|
echo "${CYAN}Protección activa:${NC}" |
|
for pattern in "${PROTECTED_PATTERNS[@]}"; do |
|
echo " - $pattern" |
|
done |
|
} |
|
|
|
# 🚀 Lógica principal |
|
run=false |
|
remote=false |
|
|
|
# Analiza argumentos |
|
for arg in "$@"; do |
|
case $arg in |
|
--run) run=true ;; |
|
--remote) remote=true ;; |
|
--help) show_help; exit 0 ;; |
|
*) echo -e "${RED}❌ Opción no válida: $arg${NC}"; show_help; exit 1 ;; |
|
esac |
|
done |
|
|
|
# Ejecuta procesos |
|
clean_local_branches "$run" |
|
if [[ "$remote" == "true" ]]; then |
|
clean_remote_branches "$run" |
|
fi |
|
|
|
# ======== |
|
# README |
|
# ======== |
|
# chmod +x git-clean-branches.sh |
|
# ./git-clean-branches.sh |
|
# ./git-clean-branches.sh --run |
|
# ./git-clean-branches.sh --run --remote |
|
# ./git-clean-branches.sh --help |