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}"
 73REPO1_NAME="repo1"
 74REPO1_URL="ssh://user@${SERVER_URL}/${REPO1_NAME}"
 75REPO2_NAME="repo2"
 76REPO2_URL="ssh://user@${SERVER_URL}/${REPO2_NAME}"
 77REPO3_NAME="repo3"
 78REPO3_URL="ssh://user@${SERVER_URL}/${REPO3_NAME}"
 79
 80SSH_KEY="${SCRIPT_DIR}/user1/ed25519"
 81SSH_KEY2="${SCRIPT_DIR}/user2/ed25519"
 82
 83##### clean
 84report "🏁 clean"
 85
 86cd /tmp
 87rm -rf ${SERVER_DATA_DIR}
 88rm -rf ${ROOT_REPO_NAME}
 89rm -rf ${ROOT_REPO_NAME}_2
 90rm -rf ${REPO1_NAME}
 91rm -rf ${REPO2_NAME}
 92rm -rf ${REPO1_NAME}_2
 93rm -rf ${REPO2_NAME}_2
 94rm -rf ${REPO3_NAME}
 95rm -f /tmp/mylog.txt
 96APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
 97if [ -z "$APP" ]; then
 98    report "🟢 Gitroot not launched"
 99else 
100    kill ${APP}
101    report "🟢 Gitroot killed"
102fi
103ssh-keygen -f "$HOME/.ssh/known_hosts" -R "[127.0.0.1]:$SERVER_PORT"
104
105##### launch gitroot
106report "🏁 launch gitroot from gitroot.dev"
107
108if [ ! -f "/tmp/testUpdate/gitroot" ]; then
109    mkdir /tmp/testUpdate
110    cd /tmp/testUpdate
111
112    wget -O gitroot https://gitroot.dev/releases/gitroot-last && chmod +x gitroot && ./gitroot --initConfig ./conf.yml
113
114    wget https://gitroot.dev/pollen-0.0.1.wasm
115    wget https://gitroot.dev/ladybug-0.0.2.wasm
116    wget https://gitroot.dev/silo-0.0.2.wasm
117    wget https://gitroot.dev/grafter-0.0.2.wasm
118    wget https://gitroot.dev/apex-0.0.2.wasm
119
120    cp ${SCRIPT_DIR}/../plugins/*/*-0.0.2.wasm /tmp/testUpdate/
121    cp ${SCRIPT_DIR}/../plugins/pollen/*-0.0.1.wasm /tmp/testUpdate/
122fi
123
124/tmp/testUpdate/gitroot --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
125wait_for "starting SSH server on" /tmp/mylog.txt
126
127mySleep 1
128
129##### first clone
130report "🏁 first clone"
131
132cd /tmp
133quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${ROOT_REPO_URL}
134cd ${ROOT_REPO_NAME}
135quiet_git remote add upstream ssh://gitroot.dev/
136.gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email admin@gitroot.dev --name admin >> /tmp/mylog.txt
137sed -i -e 's/- main/- dev/g' .gitroot/repoConfiguration.yml
138quiet_git add .
139quiet_git commit -m "my init"
140quiet_git push origin main
141quiet_git pull --rebase -X theirs upstream main
142quiet_git push -f origin main
143quiet_git remote remove upstream
144
145##### kill last gitroot
146report "🏁 kill last gitroot"
147APP=$(lsof -i tcp:${SERVER_PORT} | awk 'NR!=1 {print $2}') 
148if [ -z "$APP" ]; then
149    report "🟢 Gitroot not launched"
150else 
151    kill ${APP}
152    report "🟢 Gitroot killed"
153fi
154rm -f /tmp/mylog.txt
155
156##### launch next gitroot
157report "🏁 launch next gitroot"
158
159cd ${SCRIPT_DIR}/../server
160GIT_TRACE_PACKET=false go run -race . --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
161wait_for "starting SSH server on" /tmp/mylog.txt
162
163cd /tmp/root
164quiet_git pull origin main
165
166sed -i -e 's/https:\/\/gitroot.dev\/releases\/0.2.0\/ladybug-0.0.2.wasm/file:\/\/\/home\/rmaneschi\/projects\/gitroot\/app\/plugins\/ladybug\/ladybug-0.0.3.wasm/g' .gitroot/plugins.yml
167sed -i -e 's/checksum: sha256:0c755bb2dc5cc037216b29ba1709875afee425ee429b6be97f0d5e137067acff/checksum: ""/g' .gitroot/plugins.yml
168
169sed -i -e 's/https:\/\/gitroot.dev\/releases\/0.2.0\/silo-0.0.2.wasm/file:\/\/\/home\/rmaneschi\/projects\/gitroot\/app\/plugins\/silo\/silo-0.0.3.wasm/g' .gitroot/plugins.yml
170sed -i -e 's/checksum: sha256:66fe44b4e0984154bda0867a9b35ea39b0be3d92a89e4e542b70472fadf2f78e/checksum: ""/g' .gitroot/plugins.yml
171
172sed -i -e 's/https:\/\/gitroot.dev\/releases\/0.2.0\/grafter-0.0.2.wasm/file:\/\/\/home\/rmaneschi\/projects\/gitroot\/app\/plugins\/grafter\/grafter-0.0.3.wasm/g' .gitroot/plugins.yml
173sed -i -e 's/checksum: sha256:9ce5e36af1fd53a6547ec51d55356a661a05ae617b16ef97b1eae0dd0cce22c0/checksum: ""/g' .gitroot/plugins.yml
174
175sed -i -e 's/https:\/\/gitroot.dev\/releases\/0.2.0\/apex-0.0.2.wasm/file:\/\/\/home\/rmaneschi\/projects\/gitroot\/app\/plugins\/apex\/apex-0.0.3.wasm/g' .gitroot/plugins.yml
176sed -i -e 's/checksum: sha256:4c7d1b84f768a6aea11393ea2bc116a7be30fd12380fd7e11f283d5970028119/checksum: ""/g' .gitroot/plugins.yml
177
178sed -i -e 's/https:\/\/gitroot.dev\/releases\/0.2.0\/apex-0.0.2.wasm/file:\/\/\/home\/rmaneschi\/projects\/gitroot\/app\/plugins\/apex\/apex-0.0.3.wasm/g' .gitroot/plugins.yml
179sed -i -e 's/checksum: sha256:4c7d1b84f768a6aea11393ea2bc116a7be30fd12380fd7e11f283d5970028119/checksum: ""/g' .gitroot/plugins.yml
180
181sed -i -e 's/https:\/\/gitroot.dev\/releases\/0.2.0\/pollen-0.0.1.wasm/file:\/\/\/home\/rmaneschi\/projects\/gitroot\/app\/plugins\/pollen\/pollen-0.0.2.wasm/g' .gitroot/plugins.yml
182sed -i -e 's/checksum: sha256:49cd8e1846b1612a0d5686918b502fe29e070e7bf9613c1ba9a1dfe5417d0f2a/checksum: ""/g' .gitroot/plugins.yml
183
184quiet_git add .
185quiet_git commit -m "update plugins"
186quiet_git push origin main
187
188mySleep 5
189
190quiet_git pull origin main