GitRoot

craft your forge, build your project, grow your community freely
  1#!/usr/bin/env bash
  2
  3# SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
  4#
  5# SPDX-License-Identifier: EUPL-1.2
  6
  7trap 'catch $LINENO' ERR
  8
  9EXPECTED_ERROR=0
 10
 11catch() {
 12    if [[ $EXPECTED_ERROR == 0 ]]; then
 13        echo "🛑 unexpected error line $1"
 14        exit 1
 15    fi
 16    report "🟢 expected err line $1"
 17}
 18
 19quiet_git() {
 20    echo "🚀 git $@" >> /tmp/mylog.txt
 21    GIT_TRACE=false GIT_TRACE_PACKET=false git "$@" &>> /tmp/mylog.txt
 22}
 23
 24report() {
 25    echo "$1" >> /tmp/mylog.txt
 26    echo "$1"
 27}
 28
 29mySleep() {
 30    echo "🕐 $1" >> /tmp/mylog.txt
 31    sleep $1
 32}
 33
 34function wait_for() {
 35    start=`date +%s`
 36    timeout=100
 37    until [ $timeout -le 0 ] || (grep -q $1 $2 &> /dev/null); do
 38        sleep 0.1
 39        timeout=$(( timeout - 1 ))
 40    done
 41    if [ $timeout -le 0 ]; then
 42        return 1
 43    fi
 44    end=`date +%s`
 45    echo "🕐 $@ in `expr $end - $start` seconds"
 46}
 47
 48function wait_ls() {
 49    start=`date +%s`
 50    timeout=100
 51    until [ $timeout -le 0 ] || [ $(ls $1 | wc -l) -eq $2 ]; do
 52        sleep 0.1
 53        timeout=$(( timeout - 1 ))
 54    done
 55    if [ $timeout -le 0 ]; then
 56        return 1
 57    fi
 58    end=`date +%s`
 59    echo "🕐 $@ in `expr $end - $start` seconds"
 60    mySleep 0.3
 61}
 62
 63EXIT_CODE=0
 64
 65SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
 66
 67SERVER_PORT="4545"
 68SERVER_URL=${1:-"127.0.0.1:$SERVER_PORT"}
 69SERVER_DATA_DIR="/tmp/gitrootData"
 70
 71ROOT_REPO_NAME="root"
 72ROOT_REPO_URL="ssh://user@${SERVER_URL}/${ROOT_REPO_NAME}"
 73
 74SSH_KEY="${SCRIPT_DIR}/user1/ed25519"
 75SSH_KEY2="${SCRIPT_DIR}/user2/ed25519"
 76
 77LADYBUG_PROD_VERSION="https://gitroot.dev/releases/0.3.0/ladybug-0.0.3.wasm"
 78SILO_PROD_VERSION="https://gitroot.dev/releases/0.3.0/silo-0.0.3.wasm"
 79GRAFTER_PROD_VERSION="https://gitroot.dev/releases/0.3.0/grafter-0.0.3.wasm"
 80APEX_PROD_VERSION="https://gitroot.dev/releases/0.3.0/apex-0.0.3.wasm"
 81POLLEN_PROD_VERSION="https://gitroot.dev/releases/0.3.0/hop-0.0.2.wasm"
 82HOP_PROD_VERSION="https://gitroot.dev/releases/0.3.0/hop-0.0.1.wasm"
 83ALL_PLUGINS_PROD_VERSION=( $LADYBUG_PROD_VERSION $SILO_PROD_VERSION $GRAFTER_PROD_VERSION $APEX_PROD_VERSION $POLLEN_PROD_VERSION $HOP_PROD_VERSION )
 84
 85LADYBUG_WASM="${SCRIPT_DIR}/../plugins/ladybug/ladybug-0.0.4.wasm"
 86SILO_WASM="${SCRIPT_DIR}/../plugins/silo/silo-0.0.4.wasm"
 87GRAFTER_WASM="${SCRIPT_DIR}/../plugins/grafter/grafter-0.0.4.wasm"
 88APEX_WASM="${SCRIPT_DIR}/../plugins/apex/apex-0.0.4.wasm"
 89APEX_MARKDOWN_WASM="${SCRIPT_DIR}/../plugins/apex/apex_markdown-0.0.4.wasm"
 90APEX_CODE_WASM="${SCRIPT_DIR}/../plugins/apex/apex_code-0.0.4.wasm"
 91POLLEN_WASM="${SCRIPT_DIR}/../plugins/pollen/pollen-0.0.3.wasm"
 92HOP_WASM="${SCRIPT_DIR}/../plugins/hop/hop-0.0.2.wasm"
 93ALL_PLUGIN_LOCAL=( $LADYBUG_WASM $SILO_WASM $GRAFTER_WASM $APEX_WASM $APEX_MARKDOWN_WASM $APEX_CODE_WASM $POLLEN_WASM $HOP_WASM )
 94
 95##### clean
 96report "🏁 clean"
 97
 98cd /tmp
 99chmod -R +w ${SERVER_DATA_DIR}/data/cache/
100rm -rf ${SERVER_DATA_DIR}
101rm -rf ${ROOT_REPO_NAME}
102rm -f /tmp/mylog.txt
103APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
104if [ -z "$APP" ]; then
105    report "🟢 Gitroot not launched"
106else 
107    kill ${APP}
108    report "🟢 Gitroot killed"
109fi
110ssh-keygen -f "$HOME/.ssh/known_hosts" -R "[127.0.0.1]:$SERVER_PORT"
111
112##### launch gitroot
113report "🏁 launch gitroot from gitroot.dev"
114
115if [ ! -f "/tmp/testUpdate/gitroot" ]; then
116    mkdir /tmp/testUpdate
117    cd /tmp/testUpdate
118
119    wget -O gitroot https://gitroot.dev/releases/gitroot-last && chmod +x gitroot && ./gitroot --initConfig ./conf.yml
120
121    for value in "${ALL_PLUGINS_PROD_VERSION[@]}"
122    do
123        wget $value
124    done
125fi
126
127/tmp/testUpdate/gitroot --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
128wait_for "starting SSH server on" /tmp/mylog.txt
129
130mySleep 1
131
132##### first clone
133report "🏁 first clone"
134
135cd /tmp
136quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${ROOT_REPO_URL}
137cd ${ROOT_REPO_NAME}
138quiet_git remote add upstream ssh://gitroot.dev/
139.gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email admin@gitroot.dev --name admin >> /tmp/mylog.txt
140sed -i -e 's/- main/- dev/g' .gitroot/repoConfiguration.yml
141quiet_git add .
142quiet_git commit -m "my init"
143quiet_git push origin main
144quiet_git pull --rebase -X theirs upstream main
145quiet_git push -f origin main
146quiet_git remote remove upstream
147
148##### kill last gitroot
149report "🏁 kill last gitroot"
150APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
151if [ -z "$APP" ]; then
152    report "🟢 Gitroot not launched"
153else 
154    kill ${APP}
155    report "🟢 Gitroot killed"
156fi
157rm -f /tmp/mylog.txt
158
159##### launch next gitroot
160report "🏁 launch next gitroot"
161
162cd ${SCRIPT_DIR}/../server
163GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
164wait_for "starting SSH server on" /tmp/mylog.txt
165
166cd /tmp/root
167quiet_git pull origin main
168
169sed -i "s|${LADYBUG_PROD_VERSION}|file://${LADYBUG_WASM}|g" .gitroot/plugins.yml
170sed -i "s|${SILO_PROD_VERSION}|file://${SILO_WASM}|g" .gitroot/plugins.yml
171sed -i "s|${GRAFTER_PROD_VERSION}|file://${GRAFTER_WASM}|g" .gitroot/plugins.yml
172sed -i "s|${APEX_PROD_VERSION}|file://${APEX_WASM}|g" .gitroot/plugins.yml
173sed -i "s|${POLLEN_PROD_VERSION}|file://${POLLEN_WASM}|g" .gitroot/plugins.yml
174sed -i "s|${HOP_PROD_VERSION}|file://${HOP_WASM}|g" .gitroot/plugins.yml
175echo "- url: '${APEX_CODE_WASM}'" >> .gitroot/plugins.yml
176echo "  active: true" >> .gitroot/plugins.yml
177echo "- url: '${APEX_MARKDOWN_WASM}'" >> .gitroot/plugins.yml
178echo "  active: true" >> .gitroot/plugins.yml
179echo "- url: '${HOP_WASM}'" >> .gitroot/plugins.yml
180echo "  active: true" >> .gitroot/plugins.yml
181
182sed -i 's|checksum: sha256:[a-f0-9]*|checksum: ""|g' .gitroot/plugins.yml
183
184sed -i '/bwrap:/,/enabled:/ s/enabled: false/enabled: true/' .gitroot/forgeConfig.yml
185
186quiet_git add .
187quiet_git commit -m "update plugins"
188quiet_git push origin main
189
190mySleep 35
191
192quiet_git pull origin main
193
194##### kill gitroot
195report "🏁 kill current gitroot"
196APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
197if [ -z "$APP" ]; then
198    report "🟢 Gitroot not launched"
199else 
200    kill ${APP}
201    report "🟢 Gitroot killed"
202fi
203rm -f /tmp/mylog.txt
204
205cd ${SCRIPT_DIR}/../server
206GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
207wait_for "starting SSH server on" /tmp/mylog.txt
208
209cd /tmp/root
210quiet_git pull origin main
211
212rm .gitroot/plugins.yml
213cp ${SCRIPT_DIR}/update-ressources/plugins.yml .gitroot/
214quiet_git add .
215quiet_git commit -m "update plugins 2"
216rm makefile
217cp ${SCRIPT_DIR}/update-ressources/makefile .
218quiet_git add .
219quiet_git commit -m "makefile"
220quiet_git push origin main