From c11d8464028bf46e1f20bc88e651bda26ea8b774 Mon Sep 17 00:00:00 2001 From: Ryan Hamilton Date: Mon, 4 Aug 2025 19:19:40 -0500 Subject: [PATCH] feat: enhance branch selection in install script with user prompts and default handling --- install.sh | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/install.sh b/install.sh index c4a2832..34d3bb9 100644 --- a/install.sh +++ b/install.sh @@ -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"