/ .legacy-gitfield / gitfield-radicle
gitfield-radicle
  1  #!/bin/bash
  2  set -euo pipefail
  3  IFS=$'\n\t'
  4  
  5  # ╭───────────────────────────────╮
  6  # │         Config & Paths        │
  7  # ╰───────────────────────────────╯
  8  PROJECT_NAME=$(basename "$(pwd)")
  9  DEFAULT_NAME="Mark Randall Havens"
 10  DEFAULT_EMAIL="mark.r.havens@gmail.com"
 11  
 12  RAD_HOME="$HOME/.radicle"
 13  RAD_BIN="$RAD_HOME/bin/rad"
 14  RAD_KEYS="$RAD_HOME/keys.json"
 15  RAD_BACKUP=".radicle-backup/keys.json"
 16  RAD_PATH_LINE='export PATH="$HOME/.radicle/bin:$PATH"'
 17  PROFILE_FILE="$HOME/.bashrc"
 18  PUSH_STATE_FILE=".radicle-push-state"
 19  
 20  # ╭───────────────────────────────╮
 21  # │         Logging Utils         │
 22  # ╰───────────────────────────────╯
 23  info()  { echo -e "\e[1;34m[INFO]\e[0m $*"; }
 24  warn()  { echo -e "\e[1;33m[WARN]\e[0m $*"; }
 25  error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; exit 1; }
 26  
 27  # ╭───────────────────────────────╮
 28  # │     Git + Tools Precheck      │
 29  # ╰───────────────────────────────╯
 30  info "Checking Git..."
 31  command -v git >/dev/null || {
 32    info "Installing Git..."
 33    sudo apt update && sudo apt install -y git || error "Failed to install Git"
 34  }
 35  info "Git version: $(git --version)"
 36  
 37  NAME=$(git config --global user.name || true)
 38  EMAIL=$(git config --global user.email || true)
 39  [[ -z "$NAME" || -z "$EMAIL" ]] && {
 40    info "Setting Git identity..."
 41    git config --global user.name "$DEFAULT_NAME"
 42    git config --global user.email "$DEFAULT_EMAIL"
 43  }
 44  info "Git identity: $(git config --global user.name) <$(git config --global user.email)>"
 45  
 46  # ╭───────────────────────────────╮
 47  # │     Radicle CLI Setup         │
 48  # ╰───────────────────────────────╯
 49  if [ ! -x "$RAD_BIN" ]; then
 50    info "Installing Radicle CLI..."
 51    sudo apt install -y curl jq unzip || error "Missing dependencies"
 52    curl -sSf https://radicle.xyz/install | sh || error "Radicle install failed"
 53  fi
 54  
 55  export PATH="$HOME/.radicle/bin:$PATH"
 56  if ! grep -Fxq "$RAD_PATH_LINE" "$PROFILE_FILE"; then
 57    echo "$RAD_PATH_LINE" >> "$PROFILE_FILE"
 58    info "→ Added PATH to $PROFILE_FILE"
 59    warn "→ Run 'source $PROFILE_FILE' to make Radicle CLI persistent"
 60  fi
 61  
 62  command -v rad >/dev/null || error "Radicle CLI still unavailable. Try restarting terminal."
 63  
 64  info "Radicle CLI ready: $(rad --version)"
 65  
 66  # ╭────────────────────────────────────────────────────╮
 67  # │     Restore or Create Radicle Identity & Backup    │
 68  # ╰────────────────────────────────────────────────────╯
 69  mkdir -p "$(dirname "$RAD_BACKUP")"
 70  if [ ! -f "$RAD_KEYS" ]; then
 71    if [ -f "$RAD_BACKUP" ]; then
 72      info "Restoring Radicle identity from backup..."
 73      cp "$RAD_BACKUP" "$RAD_KEYS" || error "Failed to restore identity"
 74    else
 75      info "Creating new Radicle identity..."
 76      rad auth || error "Identity creation failed"
 77      cp "$RAD_KEYS" "$RAD_BACKUP" || warn "Backup of identity failed"
 78    fi
 79  else
 80    info "Radicle identity already exists."
 81  fi
 82  
 83  # ╭───────────────────────────────╮
 84  # │        Start Rad Node         │
 85  # ╰───────────────────────────────╯
 86  pgrep -f "rad node start" >/dev/null || {
 87    info "Starting Radicle node..."
 88    nohup rad node start > /dev/null 2>&1 &
 89    sleep 3
 90  }
 91  
 92  # ╭───────────────────────────────╮
 93  # │     Git Repo Initialization   │
 94  # ╰───────────────────────────────╯
 95  if [ ! -d .git ]; then
 96    git init
 97    git add . || warn "Nothing to add"
 98    git commit -m "Initial commit" || warn "Nothing to commit"
 99  else
100    info "Git repo already initialized."
101  fi
102  
103  # ╭───────────────────────────────╮
104  # │   Radicle Project Registration│
105  # ╰───────────────────────────────╯
106  if ! rad projects | grep -q "$PROJECT_NAME"; then
107    info "Registering Radicle project '$PROJECT_NAME'..."
108    rad init --name "$PROJECT_NAME" --description "Radicle sovereign repo for $PROJECT_NAME" || warn "Repo may already exist"
109  else
110    info "Project '$PROJECT_NAME' already registered."
111  fi
112  
113  # ╭───────────────────────────────╮
114  # │    Push Current Commit Logic  │
115  # ╰───────────────────────────────╯
116  CURRENT_BRANCH=$(git symbolic-ref --short HEAD)
117  CURRENT_COMMIT=$(git rev-parse HEAD)
118  LAST_PUSHED_COMMIT=$(cat "$PUSH_STATE_FILE" 2>/dev/null || echo "none")
119  
120  if [[ "$CURRENT_COMMIT" == "$LAST_PUSHED_COMMIT" ]]; then
121    info "✓ Already pushed commit: $CURRENT_COMMIT"
122  else
123    info "Pushing commit '$CURRENT_COMMIT' on branch '$CURRENT_BRANCH'..."
124    if git push rad "$CURRENT_BRANCH"; then
125      echo "$CURRENT_COMMIT" > "$PUSH_STATE_FILE"
126      info "✓ Pushed to Radicle successfully"
127    else
128      warn "Push may have failed — check 'rad sync status'"
129    fi
130  fi
131  
132  # ╭───────────────────────────────╮
133  # │         Final Output Block    │
134  # ╰───────────────────────────────╯
135  PROJECT_ID=$(rad self | grep 'Project ID' | awk '{print $NF}' || true)
136  PEER_ID=$(rad self | grep 'Peer ID' | awk '{print $NF}' || true)
137  
138  [[ -n "$PROJECT_ID" ]] && info "✓ Project ID: $PROJECT_ID"
139  [[ -n "$PEER_ID" ]] && info "→ Peer ID: $PEER_ID (Share to connect)"