101 lines
3.1 KiB
Bash
101 lines
3.1 KiB
Bash
#!/bin/bash
|
|
# scripts/05-branch-selection.sh - Allow user to select git branch
|
|
|
|
set -euo pipefail
|
|
|
|
# Source utilities
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
source "$SCRIPT_DIR/utils.sh"
|
|
|
|
select_branch() {
|
|
local dotfiles_dir="$1"
|
|
|
|
# Change to dotfiles directory
|
|
cd "$dotfiles_dir"
|
|
|
|
# Get current branch
|
|
local current_branch
|
|
current_branch=$(git branch --show-current 2>/dev/null || echo "unknown")
|
|
|
|
log_info "Current branch: $current_branch"
|
|
|
|
# Only prompt if we're in an interactive terminal
|
|
if ! is_interactive; then
|
|
log_info "Non-interactive mode, staying on current branch: $current_branch"
|
|
return 0
|
|
fi
|
|
|
|
# Get available branches (both local and remote)
|
|
log_info "Available branches:"
|
|
git branch -a --format="%(refname:short)" | grep -v "HEAD" | sort -u | nl -w2 -s'. '
|
|
|
|
echo
|
|
read -p "Switch to a different branch? (y/N): " -n 1 -r
|
|
echo
|
|
|
|
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
log_info "Staying on current branch: $current_branch"
|
|
return 0
|
|
fi
|
|
|
|
# Get list of branches for selection
|
|
local branches=()
|
|
while IFS= read -r branch; do
|
|
# Clean up branch names (remove origin/ prefix, etc.)
|
|
clean_branch=$(echo "$branch" | sed 's|^origin/||' | sed 's|^remotes/origin/||')
|
|
# Skip HEAD and duplicates
|
|
if [[ "$clean_branch" != "HEAD" ]] && [[ ! " ${branches[@]} " =~ " ${clean_branch} " ]]; then
|
|
branches+=("$clean_branch")
|
|
fi
|
|
done < <(git branch -a --format="%(refname:short)" | grep -v "HEAD")
|
|
|
|
# Sort branches
|
|
IFS=$'\n' branches=($(sort <<<"${branches[*]}"))
|
|
|
|
echo "Select a branch:"
|
|
for i in "${!branches[@]}"; do
|
|
echo "$((i+1)). ${branches[i]}"
|
|
done
|
|
echo
|
|
|
|
while true; do
|
|
read -p "Enter branch number (1-${#branches[@]}): " branch_num
|
|
|
|
if [[ "$branch_num" =~ ^[0-9]+$ ]] && [ "$branch_num" -ge 1 ] && [ "$branch_num" -le "${#branches[@]}" ]; then
|
|
selected_branch="${branches[$((branch_num-1))]}"
|
|
break
|
|
else
|
|
log_warning "Invalid selection. Please enter a number between 1 and ${#branches[@]}"
|
|
fi
|
|
done
|
|
|
|
# Switch to selected branch
|
|
log_info "Switching to branch: $selected_branch"
|
|
|
|
if git show-ref --verify --quiet "refs/heads/$selected_branch"; then
|
|
# Local branch exists
|
|
git checkout "$selected_branch"
|
|
elif git show-ref --verify --quiet "refs/remotes/origin/$selected_branch"; then
|
|
# Remote branch exists, create local tracking branch
|
|
git checkout -b "$selected_branch" "origin/$selected_branch"
|
|
else
|
|
log_error "Branch $selected_branch not found"
|
|
return 1
|
|
fi
|
|
|
|
# Pull latest changes
|
|
log_info "Pulling latest changes..."
|
|
git pull --quiet || true
|
|
|
|
log_success "Switched to branch: $selected_branch"
|
|
}
|
|
|
|
# Run if called directly
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
if [ $# -ne 1 ]; then
|
|
log_error "Usage: $0 <dotfiles_directory>"
|
|
exit 1
|
|
fi
|
|
select_branch "$1"
|
|
fi
|