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/pollen-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"
 91APEX_MERMAID_WASM="${SCRIPT_DIR}/../plugins/apex_mermaid/apex_mermaid-0.0.1.wasm"
 92POLLEN_WASM="${SCRIPT_DIR}/../plugins/pollen/pollen-0.0.3.wasm"
 93HOP_WASM="${SCRIPT_DIR}/../plugins/hop/hop-0.0.2.wasm"
 94STIGMA_WASM="${SCRIPT_DIR}/../plugins/stigma/stigma-0.0.1.wasm"
 95
 96##### clean
 97report "🏁 clean"
 98
 99cd /tmp
100CACHE=${SERVER_DATA_DIR}/data/cache/
101if [ -d "$CACHE" ]; then
102    chmod -R +w "$CACHE"
103fi
104rm -rf ${SERVER_DATA_DIR}
105rm -rf ${ROOT_REPO_NAME}
106rm -f /tmp/mylog.txt
107APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
108if [ -z "$APP" ]; then
109    report "🟢 Gitroot not launched"
110else 
111    kill ${APP}
112    report "🟢 Gitroot killed"
113fi
114ssh-keygen -f "$HOME/.ssh/known_hosts" -R "[127.0.0.1]:$SERVER_PORT"
115
116##### launch gitroot
117report "🏁 launch gitroot from gitroot.dev"
118
119if [ ! -f "/tmp/testUpdate/gitroot" ]; then
120    mkdir /tmp/testUpdate
121    cd /tmp/testUpdate
122
123    wget -O gitroot https://gitroot.dev/releases/gitroot-last && chmod +x gitroot && ./gitroot --initConfig ./conf.yml
124
125    for value in "${ALL_PLUGINS_PROD_VERSION[@]}"
126    do
127        wget $value
128    done
129fi
130
131/tmp/testUpdate/gitroot --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
132wait_for "starting SSH server on" /tmp/mylog.txt
133
134mySleep 1
135
136##### first clone
137report "🏁 first clone"
138
139cd /tmp
140quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${ROOT_REPO_URL}
141cd ${ROOT_REPO_NAME}
142quiet_git remote add upstream ssh://gitroot.dev/
143.gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email admin@gitroot.dev --name admin >> /tmp/mylog.txt
144sed -i -e 's/- main/- dev/g' .gitroot/repoConfiguration.yml
145quiet_git add .
146quiet_git commit -m "my init"
147quiet_git push origin main
148quiet_git pull --rebase -X theirs upstream main
149quiet_git push -f origin main
150quiet_git remote remove upstream
151
152##### kill last gitroot
153report "🏁 kill last gitroot"
154APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
155if [ -z "$APP" ]; then
156    report "🟢 Gitroot not launched"
157else 
158    kill ${APP}
159    report "🟢 Gitroot killed"
160fi
161rm -f /tmp/mylog.txt
162
163##### launch next gitroot
164report "🏁 launch next gitroot"
165
166cd ${SCRIPT_DIR}/../server
167GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
168wait_for "starting SSH server on" /tmp/mylog.txt
169
170cd /tmp/root
171quiet_git pull origin main
172
173sed -i "s|${LADYBUG_PROD_VERSION}|file://${LADYBUG_WASM}|g" .gitroot/plugins.yml
174sed -i "s|${SILO_PROD_VERSION}|file://${SILO_WASM}|g" .gitroot/plugins.yml
175sed -i "s|${GRAFTER_PROD_VERSION}|file://${GRAFTER_WASM}|g" .gitroot/plugins.yml
176sed -i "s|${APEX_PROD_VERSION}|file://${APEX_WASM}|g" .gitroot/plugins.yml
177sed -i "s|${POLLEN_PROD_VERSION}|file://${POLLEN_WASM}|g" .gitroot/plugins.yml
178sed -i "s|${HOP_PROD_VERSION}|file://${HOP_WASM}|g" .gitroot/plugins.yml
179echo "- url: '${APEX_CODE_WASM}'" >> .gitroot/plugins.yml
180echo "  active: true" >> .gitroot/plugins.yml
181echo "- url: '${APEX_MARKDOWN_WASM}'" >> .gitroot/plugins.yml
182echo "  active: true" >> .gitroot/plugins.yml
183echo "- url: '${APEX_MERMAID_WASM}'" >> .gitroot/plugins.yml
184echo "  active: true" >> .gitroot/plugins.yml
185echo "- url: '${STIGMA_WASM}'" >> .gitroot/plugins.yml
186echo "  active: true" >> .gitroot/plugins.yml
187echo "- url: '${HOP_WASM}'" >> .gitroot/plugins.yml
188echo "  active: true" >> .gitroot/plugins.yml
189
190sed -i 's|checksum: sha256:[a-f0-9]*|checksum: ""|g' .gitroot/plugins.yml
191
192sed -i '/bwrap:/,/enabled:/ s/enabled: false/enabled: true/' .gitroot/forgeConfig.yml
193
194quiet_git add .
195quiet_git commit -m "update plugins"
196quiet_git push origin main
197
198mySleep 35
199
200quiet_git pull origin main
201
202##### kill gitroot
203report "🏁 kill current gitroot"
204APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
205if [ -z "$APP" ]; then
206    report "🟢 Gitroot not launched"
207else 
208    kill ${APP}
209    report "🟢 Gitroot killed"
210fi
211rm -f /tmp/mylog.txt
212
213cd ${SCRIPT_DIR}/../server
214GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
215wait_for "starting SSH server on" /tmp/mylog.txt
216
217cd /tmp/root
218quiet_git pull origin main
219
220rm .gitroot/plugins.yml
221cp ${SCRIPT_DIR}/update-ressources/plugins.yml .gitroot/
222quiet_git add .
223quiet_git commit -m "update plugins 2"
224rm makefile
225cp ${SCRIPT_DIR}/update-ressources/makefile .
226quiet_git add .
227quiet_git commit -m "makefile"
228cp -r ${SCRIPT_DIR}/../../plugins .
229cp -r ${SCRIPT_DIR}/update-ressources/layout.html app/plugins/
230cp -r ${SCRIPT_DIR}/../plugins app
231quiet_git add .
232quiet_git commit -m "makefile and plugins"
233quiet_git push origin main