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"
107
108if [ ! -f "/tmp/testUpdate/pollen-0.0.1.wasm" ]; then
109    mkdir /tmp/testUpdate
110    cd /tmp/testUpdate
111
112    cp ${SCRIPT_DIR}/../plugins/*/*-0.0.2.wasm /tmp/testUpdate/
113    cp ${SCRIPT_DIR}/../plugins/pollen/*-0.0.1.wasm /tmp/testUpdate/
114fi
115
116cd ${SCRIPT_DIR}/../server
117GIT_TRACE_PACKET=false go run -race . -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
118wait_for "starting SSH server on" /tmp/mylog.txt
119
120##### forgeConfig
121report "🏁 forgeConfig"
122
123cd /tmp
124quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${ROOT_REPO_URL}
125cd ${ROOT_REPO_NAME}
126.gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email forgeConfig@gitroot.dev --name forgeConfig >> /tmp/mylog.txt
127
128##### install plugins
129report "🏁 install plugins"
130
131cd /tmp/${ROOT_REPO_NAME}
132echo "- url: 'https://gitroot.dev/ladybug-0.0.1.wasm'" >> .gitroot/plugins.yml
133echo "  checksum: 'sha256:af00a9f14a27399f383d0cc23415e14e7d6180c167d1504018f11166346ebb55'" >> .gitroot/plugins.yml
134echo "- url: 'https://gitroot.dev/silo-0.0.1.wasm'" >> .gitroot/plugins.yml
135echo "  checksum: 'sha256:99bc06d9d6f020c5df216fd6134306079aec26eacee9ad9bbabbc259097e618a'" >> .gitroot/plugins.yml
136echo "- url: 'https://gitroot.dev/grafter-0.0.1.wasm'" >> .gitroot/plugins.yml
137echo "  checksum: 'sha256:6645615df36a9938adac300d8d46fe298f8f53fb2e69713a495b83b0f114f40a'" >> .gitroot/plugins.yml
138echo "- url: 'https://gitroot.dev/apex-0.0.1.wasm'" >> .gitroot/plugins.yml
139echo "  checksum: 'sha256:9570f005ad70be04eab3c900acd11d5c2ccf48fef5d4c17536c9cf79c8fd333e'" >> .gitroot/plugins.yml
140quiet_git add .
141quiet_git commit -m "init plugins"
142quiet_git push origin main
143
144wait_ls "${SERVER_DATA_DIR}/data/plugins/ladybug" 3
145wait_ls "${SERVER_DATA_DIR}/data/plugins/silo" 3
146wait_ls "${SERVER_DATA_DIR}/data/plugins/grafter" 3
147wait_ls "${SERVER_DATA_DIR}/data/plugins/apex" 3
148
149quiet_git pull origin main
150
151sed -i -e 's/active: false/active: true/g' .gitroot/plugins.yml
152quiet_git add .
153quiet_git commit -m "active plugins"
154quiet_git push origin main
155
156mySleep 0.5
157
158quiet_git pull origin main
159
160mkdir issues
161for (( i=0; i<=10; i++ ))
162do
163    echo "my issue $i" >> issues/$i.md
164done
165quiet_git add .
166quiet_git commit -m "create 10 issues"
167quiet_git push origin main
168
169mySleep 0.5
170
171quiet_git pull origin main
172
173mySleep 3
174
175cd /tmp/${ROOT_REPO_NAME}
176
177sed -i -e 's/https:\/\/gitroot.dev\/ladybug-0.0.1.wasm/\/tmp\/testUpdate\/ladybug-0.0.2.wasm/g' .gitroot/plugins.yml
178sed -i -e 's/sha256:af00a9f14a27399f383d0cc23415e14e7d6180c167d1504018f11166346ebb55/null/g' .gitroot/plugins.yml
179sed -i -e 's/https:\/\/gitroot.dev\/silo-0.0.1.wasm/\/tmp\/testUpdate\/silo-0.0.2.wasm/g' .gitroot/plugins.yml
180sed -i -e 's/sha256:99bc06d9d6f020c5df216fd6134306079aec26eacee9ad9bbabbc259097e618a/null/g' .gitroot/plugins.yml
181sed -i -e 's/https:\/\/gitroot.dev\/grafter-0.0.1.wasm/\/tmp\/testUpdate\/grafter-0.0.2.wasm/g' .gitroot/plugins.yml
182sed -i -e 's/sha256:6645615df36a9938adac300d8d46fe298f8f53fb2e69713a495b83b0f114f40a/null/g' .gitroot/plugins.yml
183sed -i -e 's/https:\/\/gitroot.dev\/apex-0.0.1.wasm/\/tmp\/testUpdate\/apex-0.0.2.wasm/g' .gitroot/plugins.yml
184sed -i -e 's/sha256:9570f005ad70be04eab3c900acd11d5c2ccf48fef5d4c17536c9cf79c8fd333e/null/g' .gitroot/plugins.yml
185echo "- url: '/tmp/testUpdate/pollen-0.0.1.wasm'" >> .gitroot/plugins.yml
186echo "  active: true" >> .gitroot/plugins.yml
187quiet_git add .
188quiet_git commit -m "update plugins"
189quiet_git push origin main
190
191wait_ls "${SERVER_DATA_DIR}/data/plugins/ladybug" 4
192wait_ls "${SERVER_DATA_DIR}/data/plugins/silo" 4
193wait_ls "${SERVER_DATA_DIR}/data/plugins/grafter" 4
194wait_ls "${SERVER_DATA_DIR}/data/plugins/apex" 4
195wait_ls "${SERVER_DATA_DIR}/data/plugins/pollen" 3
196
197mySleep 12
198
199quiet_git pull origin main
200
201for (( i=0; i<=10; i++ ))
202do
203    echo "my issue $i second" >> issues/$i.md
204done
205quiet_git add .
206quiet_git commit -m "update 10 issues"
207quiet_git push origin main
208
209mySleep 0.5
210
211quiet_git pull origin main
212
213report "🏁 create $REPO1_NAME"
214
215echo "$REPO1_NAME:" >> .gitroot/repositories.yml
216echo "  defaultbranch: main" >> .gitroot/repositories.yml
217quiet_git add .
218quiet_git commit -m "create $REPO1_NAME"
219quiet_git push origin main
220
221mySleep 0.5
222
223quiet_git pull origin main
224
225cd /tmp
226quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${REPO1_URL}
227cd ${REPO1_NAME}
228.gitroot/init.sh --pubKey "${SSH_KEY}.pub" --privKey "${SSH_KEY}" --email forgeConfig@gitroot.dev --name forgeConfig >> /tmp/mylog.txt
229quiet_git add .
230quiet_git commit -m "init user"
231quiet_git push origin main
232
233if grep -q -i "version: 0.0.2" .gitroot/plugins.yml
234then
235    report "🟢 Plugins version ok"
236else
237    report "🛑 Plugins need to contains 0.0.2"
238    EXIT_CODE=1
239fi