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 &
132# wait_for "starting SSH server on" /tmp/mylog.txt
133cd ${SCRIPT_DIR}/../server
134GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
135wait_for "starting SSH server on" /tmp/mylog.txt
136
137mySleep 1
138
139##### first clone
140report "🏁 first clone"
141
142cd /tmp
143quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${ROOT_REPO_URL}
144cd ${ROOT_REPO_NAME}
145# .gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email admin@gitroot.dev --name admin >> /tmp/mylog.txt
146quiet_git config user.email "admin@gitroot.dev"
147quiet_git config user.name "admin"
148SSH_PUB_CONTENT=$(cat $SSH_KEY.pub)
149echo "gitroot:" >> .gitroot/repositories.yml
150echo "  kind: fork" >> .gitroot/repositories.yml
151echo "  defaultbranch: main" >> .gitroot/repositories.yml
152echo "  owners:" >> .gitroot/repositories.yml
153echo "  - $SSH_PUB_CONTENT" >> .gitroot/repositories.yml
154echo "  forkurl: ssh://gitroot.dev/" >> .gitroot/repositories.yml
155quiet_git add .
156quiet_git commit -m "add gitroot"
157quiet_git push origin main
158sleep 15
159cd ..
160quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ssh://user@${SERVER_URL}/gitroot
161exit 1
162# quiet_git remote add upstream ssh://gitroot.dev/
163# .gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email admin@gitroot.dev --name admin >> /tmp/mylog.txt
164# sed -i -e 's/- main/- dev/g' .gitroot/repoConfiguration.yml
165# quiet_git add .
166# quiet_git commit -m "my init"
167# quiet_git push origin main
168# quiet_git pull --rebase -X theirs upstream main
169# quiet_git push -f origin main
170# quiet_git remote remove upstream
171
172##### kill last gitroot
173report "🏁 kill last gitroot"
174APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
175if [ -z "$APP" ]; then
176    report "🟢 Gitroot not launched"
177else 
178    kill ${APP}
179    report "🟢 Gitroot killed"
180fi
181rm -f /tmp/mylog.txt
182
183##### launch next gitroot
184report "🏁 launch next gitroot"
185
186cd ${SCRIPT_DIR}/../server
187GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
188wait_for "starting SSH server on" /tmp/mylog.txt
189
190cd /tmp/root
191quiet_git pull origin main
192
193sed -i "s|${LADYBUG_PROD_VERSION}|file://${LADYBUG_WASM}|g" .gitroot/plugins.yml
194sed -i "s|${SILO_PROD_VERSION}|file://${SILO_WASM}|g" .gitroot/plugins.yml
195sed -i "s|${GRAFTER_PROD_VERSION}|file://${GRAFTER_WASM}|g" .gitroot/plugins.yml
196sed -i "s|${APEX_PROD_VERSION}|file://${APEX_WASM}|g" .gitroot/plugins.yml
197sed -i "s|${POLLEN_PROD_VERSION}|file://${POLLEN_WASM}|g" .gitroot/plugins.yml
198sed -i "s|${HOP_PROD_VERSION}|file://${HOP_WASM}|g" .gitroot/plugins.yml
199echo "- url: '${APEX_CODE_WASM}'" >> .gitroot/plugins.yml
200echo "  active: true" >> .gitroot/plugins.yml
201echo "- url: '${APEX_MARKDOWN_WASM}'" >> .gitroot/plugins.yml
202echo "  active: true" >> .gitroot/plugins.yml
203echo "- url: '${APEX_MERMAID_WASM}'" >> .gitroot/plugins.yml
204echo "  active: true" >> .gitroot/plugins.yml
205echo "- url: '${STIGMA_WASM}'" >> .gitroot/plugins.yml
206echo "  active: true" >> .gitroot/plugins.yml
207echo "- url: '${HOP_WASM}'" >> .gitroot/plugins.yml
208echo "  active: true" >> .gitroot/plugins.yml
209
210sed -i 's|checksum: sha256:[a-f0-9]*|checksum: ""|g' .gitroot/plugins.yml
211
212sed -i '/bwrap:/,/enabled:/ s/enabled: false/enabled: true/' .gitroot/forgeConfig.yml
213
214quiet_git add .
215quiet_git commit -m "update plugins"
216quiet_git push origin main
217
218mySleep 35
219
220quiet_git pull origin main
221
222##### kill gitroot
223report "🏁 kill current gitroot"
224APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
225if [ -z "$APP" ]; then
226    report "🟢 Gitroot not launched"
227else 
228    kill ${APP}
229    report "🟢 Gitroot killed"
230fi
231rm -f /tmp/mylog.txt
232
233cd ${SCRIPT_DIR}/../server
234GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
235wait_for "starting SSH server on" /tmp/mylog.txt
236
237cd /tmp/root
238quiet_git pull origin main
239
240rm .gitroot/plugins.yml
241cp ${SCRIPT_DIR}/update-ressources/plugins.yml .gitroot/
242quiet_git add .
243quiet_git commit -m "update plugins 2"
244rm makefile
245cp ${SCRIPT_DIR}/update-ressources/makefile .
246quiet_git add .
247quiet_git commit -m "makefile"
248cp -r ${SCRIPT_DIR}/../../plugins .
249cp -r ${SCRIPT_DIR}/update-ressources/layout.html app/plugins/
250cp -r ${SCRIPT_DIR}/../plugins app
251quiet_git add .
252quiet_git commit -m "makefile and plugins"
253quiet_git push origin main