1
0

feat: enhance branch selection in install script with user prompts and default handling

This commit is contained in:
2025-08-04 19:19:40 -05:00
parent 66cbb97f91
commit c11d846402

View File

@@ -4,6 +4,51 @@ set -euo pipefail
DOTFILES_REPO="https://gitea.purpleraft.com/ryan/dotfiles"
DOTFILES_DIR="$HOME/.dotfiles"
# Handle branch selection
if [ $# -eq 0 ]; then
echo "🌿 Fetching available branches..."
# Get list of remote branches
branches=$(git ls-remote --heads "$DOTFILES_REPO" | sed 's/.*refs\/heads\///' | sort)
if [ -z "$branches" ]; then
echo "⚠️ Could not fetch branches, using default 'main'"
BRANCH="main"
else
echo "Available branches:"
branch_array=()
i=1
while IFS= read -r branch; do
echo "$i. $branch"
branch_array+=("$branch")
((i++))
done <<< "$branches"
echo "$i. Enter custom branch name"
echo ""
read -p "Choose branch (1-$i) [default: main]: " branch_choice
if [[ "$branch_choice" =~ ^[0-9]+$ ]] && [ "$branch_choice" -ge 1 ] && [ "$branch_choice" -lt "$i" ]; then
# Valid branch selection
BRANCH="${branch_array[$((branch_choice-1))]}"
elif [ "$branch_choice" -eq "$i" ]; then
# Custom branch name
read -p "Enter branch name: " custom_branch
BRANCH="$custom_branch"
else
# Default or invalid choice
if [[ " ${branch_array[*]} " =~ " main " ]]; then
BRANCH="main"
else
BRANCH="${branch_array[0]}" # Use first available branch
fi
echo "Using default branch: $BRANCH"
fi
fi
else
BRANCH="$1"
fi
# Function to install packages from a file
install_packages() {
local package_file="$1"
@@ -57,13 +102,17 @@ if ! command -v stow &> /dev/null; then
exit 1
fi
echo "🚀 Installing dotfiles from branch: $BRANCH"
# Clone or update the repo
if [ -d "$DOTFILES_DIR/.git" ]; then
echo "🔄 Updating existing dotfiles repo..."
git -C "$DOTFILES_DIR" fetch --quiet
git -C "$DOTFILES_DIR" checkout "$BRANCH" --quiet
git -C "$DOTFILES_DIR" pull --quiet
else
echo "📥 Cloning dotfiles into $DOTFILES_DIR..."
git clone "$DOTFILES_REPO" "$DOTFILES_DIR"
git clone -b "$BRANCH" "$DOTFILES_REPO" "$DOTFILES_DIR"
fi
cd "$DOTFILES_DIR"