setup.sh
1 #!/bin/sh 2 # Don't use set -e to avoid terminal closing when sourced with . ./scripts/setup.sh 3 # Instead, check return codes explicitly and report errors 4 5 # Check if rad command is available 6 if ! command -v rad >/dev/null 2>&1; then 7 8 echo "⛓️💥 This script requires the Radicle toolchain to be installed." 9 echo " 💡 Please install it from https://radicle.xyz/download and try again." 10 exit 1 11 fi 12 13 echo "⚙️ Setting up repository..." 14 15 # Get repository root directory using git 16 REPO_ROOT="$(git rev-parse --show-toplevel)" 17 18 echo "🎛️ Configuring 'git patch' command..." 19 # adds git alias to create a patch 20 git config --local alias.patch '!git push rad HEAD:refs/patches -o patch.message="$1" && git push -u upstream $(git rev-parse --abbrev-ref HEAD); #' 21 22 # extract GitHub username and repo from YAML file 23 GITHUB_ACTIONS_FILE="$REPO_ROOT/.radicle/github_actions.yaml" 24 if [ ! -f "$GITHUB_ACTIONS_FILE" ]; then 25 echo "⛓️💥 Could not find GitHub actions config file at: $GITHUB_ACTIONS_FILE" 26 exit 1 27 fi 28 29 GITHUB_USERNAME=$(grep "github_username:" "$GITHUB_ACTIONS_FILE" | cut -d ":" -f2 | tr -d ' ') 30 if [ -z "$GITHUB_USERNAME" ]; then 31 echo "⛓️💥 Could not extract github_username from $GITHUB_ACTIONS_FILE" 32 echo " This variable is required to configure GitHub remotes." 33 exit 1 34 fi 35 36 GITHUB_REPO=$(grep "github_repo:" "$GITHUB_ACTIONS_FILE" | cut -d ":" -f2 | tr -d ' ') 37 if [ -z "$GITHUB_REPO" ]; then 38 echo "⛓️💥 Could not extract github_repo from $GITHUB_ACTIONS_FILE" 39 echo " This variable is required to configure GitHub remotes." 40 exit 1 41 fi 42 43 # Extract RID from radicle.yaml 44 RADICLE_FILE="$REPO_ROOT/.radicle/radicle.yaml" 45 if [ ! -f "$RADICLE_FILE" ]; then 46 echo "⛓️💥 Could not find Radicle config file at: $RADICLE_FILE" 47 exit 1 48 fi 49 50 RID=$(grep "RADICLE_REPOSITORY_ID:" "$RADICLE_FILE" | cut -d ":" -f2 | tr -d ' ') 51 if [ -z "$RID" ]; then 52 echo "⛓️💥 Could not extract RADICLE_REPOSITORY_ID from $RADICLE_FILE" 53 echo " This variable is required to configure Radicle remotes." 54 exit 1 55 fi 56 57 NODE_ID=$(rad self --nid) 58 if [ -z "$NODE_ID" ]; then 59 echo "⛓️💥 Could not get Radicle Node ID using 'rad self --nid'" 60 echo " This is required to configure Radicle remotes." 61 exit 1 62 fi 63 64 # Define reusable URL variables 65 RAD_FETCH_URL="rad://$RID" 66 RAD_PUSH_URL="rad://${RID}/${NODE_ID}" 67 GITHUB_PUSH_URL="https://github.com/${GITHUB_USERNAME}/${GITHUB_REPO}.git" 68 69 70 71 echo "⛓️ Setting up remotes to automate CI flows for submitted patches..." 72 73 # Check if upstream remote is already configured 74 if git remote | grep -q "^upstream$"; then 75 # Check upstream remote configuration 76 UPSTREAM_URL=$(git remote get-url upstream 2>/dev/null) 77 78 if [ "$UPSTREAM_URL" != "$RAD_FETCH_URL" ]; then 79 echo " ⚠️ 'upstream' remote URL doesn't match expected value." 80 echo " Current: $UPSTREAM_URL" 81 echo " Expected: $RAD_FETCH_URL" 82 echo " 🚫 Aborting to avoid overwriting your customized remote configuration." 83 exit 1 84 fi 85 86 # Check push URLs 87 88 # Get all push URLs for upstream (including all configured push URLs) 89 PUSH_URLS=$(git remote get-url --all --push upstream 2>/dev/null | tr '\n' ' ') 90 91 # Check if GitHub URL is configured 92 # Use a looser grep check to match the GitHub URL regardless of exact format 93 GITHUB_URL_FOUND=0 94 if echo "$PUSH_URLS" | grep -q "github.com/${GITHUB_USERNAME}/${GITHUB_REPO}"; then 95 GITHUB_URL_FOUND=1 96 fi 97 98 if [ "$GITHUB_URL_FOUND" -eq 0 ]; then 99 echo " ⚠️ GitHub push URL is not configured for upstream." 100 echo " Expected: $GITHUB_PUSH_URL" 101 echo " 🚫 Aborting to avoid overwriting your customized remote configuration." 102 exit 1 103 fi 104 105 # Check if any Radicle URL is configured 106 RAD_URL_FOUND=0 107 if echo "$PUSH_URLS" | grep -q "rad://"; then 108 RAD_URL_FOUND=1 109 fi 110 111 if [ "$RAD_URL_FOUND" -eq 0 ]; then 112 echo " ⚠️ No Radicle push URL is configured for upstream." 113 echo " Expected: $RAD_PUSH_URL" 114 echo " 🚫 Aborting to avoid overwriting your customized remote configuration." 115 exit 1 116 fi 117 118 # upstream remote is properly configured 119 else 120 echo " ⛓️ Adding 'upstream' remote" 121 git remote add upstream "$RAD_FETCH_URL" 122 git remote set-url --push upstream "$GITHUB_PUSH_URL" 123 git remote set-url --add --push upstream "$RAD_PUSH_URL" 124 fi 125 126 # Check/configure 'rad' remote 127 if git remote | grep -q "^rad$"; then 128 # Check rad remote configuration 129 RAD_URL=$(git remote get-url rad 2>/dev/null) 130 131 if [ "$RAD_URL" != "$RAD_FETCH_URL" ]; then 132 echo " ⚠️ 'rad' remote URL doesn't match expected value." 133 echo " Current: $RAD_URL" 134 echo " Expected: $RAD_FETCH_URL" 135 echo " 🚫 Aborting to avoid overwriting your customized remote configuration." 136 exit 1 137 fi 138 139 # Check if any Radicle push URL exists (less strict check) 140 # Get all push URLs for rad and combine them 141 RAD_PUSH_URLS=$(git remote get-url --all --push rad 2>/dev/null | tr '\n' ' ' || echo "") 142 143 # Check if any line contains a Radicle URL 144 RAD_URL_FOUND=0 145 if echo "$RAD_PUSH_URLS" | grep -q "rad://"; then 146 RAD_URL_FOUND=1 147 fi 148 149 if [ "$RAD_URL_FOUND" -eq 0 ]; then 150 echo " ⚠️ 'rad' push URL is not a Radicle URL." 151 echo " Expected: A URL starting with 'rad://'" 152 echo " 🚫 Aborting to avoid overwriting your customized remote configuration." 153 exit 1 154 fi 155 156 # rad remote is properly configured 157 else 158 echo " ⛓️ Adding 'rad' remote" 159 git remote add rad "$RAD_FETCH_URL" 160 git remote set-url --push rad "$RAD_PUSH_URL" 161 fi 162 163 echo "👾 Success! You can now use 'git patch \"Your PR title\"' to submit pull requests"