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