1
0
Files
dotfiles/scripts/utils.sh

83 lines
2.0 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# scripts/utils.sh - Shared utilities and functions
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Logging functions
log_info() {
echo -e "${BLUE} $1${NC}"
}
log_success() {
echo -e "${GREEN}$1${NC}"
}
log_warning() {
echo -e "${YELLOW}⚠️ $1${NC}"
}
log_error() {
echo -e "${RED}$1${NC}"
}
# Check if command exists
command_exists() {
command -v "$1" &> /dev/null
}
# Check if we're in an interactive shell
is_interactive() {
[[ $- == *i* ]]
}
# Branch selection utilities
list_branches() {
local dotfiles_dir="$1"
cd "$dotfiles_dir"
# Get available branches (both local and remote)
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 and return branches
printf '%s\n' "${branches[@]}" | sort
}
switch_to_branch() {
local dotfiles_dir="$1"
local selected_branch="$2"
cd "$dotfiles_dir"
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"
}