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="pkg:gitroot/gitroot/ladybug@0.0.5?checksum=sha256:afb22a26e7d489d72db0a2305d433a3c3ac2692959ba2412011c0d759b4c8518®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
94SILO_PROD_VERSION="pkg:gitroot/gitroot/silo@0.0.5?checksum=sha256:eb4142a27958ee40993abb3b3317c49c862f89757ee9e9ef9ccdf3213e56174c®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
95GRAFTER_PROD_VERSION="pkg:gitroot/gitroot/grafter@0.0.5?checksum=sha256:de248bc6ab15c2a49b45339e3802792a87fff4f5b63465cbd03c63dd304108a2®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
96APEX_PROD_VERSION="pkg:gitroot/gitroot/apex@0.0.5?checksum=sha256:0a286c8375da421d5273fafc0082759d7f77dba0b0c960abb1cfc3e016df1a6d®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
97APEX_MARKDOWN_PROD_VERSION="pkg:gitroot/gitroot/apex_markdown@0.0.5?checksum=sha256:8477d0f4817a53a0ff27589fc11d88182234aa94a93a6ad23acd9a73e1d82dab®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
98APEX_CODE_PROD_VERSION="pkg:gitroot/gitroot/apex_code@0.0.5?checksum=sha256:9e1e2ea42d2bbf1c91f74b8c3f3beb3e4b5f242006080c8eed734e82ddbb425f®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
99APEX_MERMAID_PROD_VERSION="pkg:gitroot/gitroot/apex_mermaid@0.0.2?checksum=sha256:67ae4633da5e7f6eac83f5842caca0ab91f3769a872c68e83292cf9873e4ede8®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
100POLLEN_PROD_VERSION="pkg:gitroot/gitroot/pollen@0.0.4?checksum=sha256:c445beb55eb1bd02ebbdd85d05cdb142bb961fc12d2a286dc75917b03c7558fb®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
101HOP_PROD_VERSION="pkg:gitroot/gitroot/hop@0.0.3?checksum=sha256:5b4d450f2790e7b599edf1b5b60dac362bd2c0ffc73580591d7ace7478ee8db6®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
102STIGMA_PROD_VERSION="pkg:gitroot/gitroot/stigma@0.0.2?checksum=sha256:6c07c0e60466f7693943d7f63afdbaa6c982dca6e1d33d9d4fd1ceaeeded4c0e®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
103FIREFLY_PROD_VERSION="pkg:gitroot/gitroot/firefly@0.0.1?checksum=sha256:fae956f5cc9239a87b1c2a149a5466fec85476377de4ada33dda190edb3700c4®istry=https:%2F%2Fgitroot.dev%2Freleases%2F0.5.0"
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
145fi
146
147/tmp/testUpdate/gitroot --config /tmp/testUpdate/conf.yml -data="${SERVER_DATA_DIR}" &>> /tmp/mylog.txt &
148wait_for "starting SSH server on" /tmp/mylog.txt
149
150mySleep 1
151
152##### first clone
153report "🏁 first clone"
154
155cd /tmp
156quiet_git clone --quiet -c "core.sshCommand=ssh -i ${SSH_KEY} -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new" ${ROOT_REPO_URL}
157cd ${ROOT_REPO_NAME}
158quiet_git remote add upstream ssh://gitroot.dev/
159init_local_git admin@gitroot.dev admin ${SSH_KEY}.pub ${SSH_KEY}
160sed -i -e 's/- main/- dev/g' .gitroot/repoConfiguration.yml
161quiet_git add .
162quiet_git commit -m "my init"
163quiet_git push origin main
164quiet_git pull --rebase -X theirs upstream main
165quiet_git push -f origin main
166quiet_git remote remove upstream
167
168##### kill last gitroot
169report "🏁 kill last gitroot"
170restart
171
172##### launch next gitroot
173report "🏁 launch next gitroot"
174
175cd /tmp/root
176quiet_git pull origin main
177
178sed -i "s|purl: ${LADYBUG_PROD_VERSION}|purl: ${LADYBUG_WASM//&/\\&}|g" .gitroot/plugins.yml
179sed -i "s|purl: ${SILO_PROD_VERSION}|purl: ${SILO_WASM//&/\\&}|g" .gitroot/plugins.yml
180sed -i "s|purl: ${GRAFTER_PROD_VERSION}|purl: ${GRAFTER_WASM//&/\\&}|g" .gitroot/plugins.yml
181sed -i "s|purl: ${APEX_PROD_VERSION}|purl: ${APEX_WASM//&/\\&}|g" .gitroot/plugins.yml
182sed -i "s|purl: ${POLLEN_PROD_VERSION}|purl: ${POLLEN_WASM//&/\\&}|g" .gitroot/plugins.yml
183sed -i "s|purl: ${HOP_PROD_VERSION}|purl: ${HOP_WASM//&/\\&}|g" .gitroot/plugins.yml
184sed -i "s|purl: ${STIGMA_PROD_VERSION}|purl: ${STIGMA_WASM//&/\\&}|g" .gitroot/plugins.yml
185sed -i "s|purl: ${APEX_MARKDOWN_PROD_VERSION}|purl: ${APEX_MARKDOWN_WASM//&/\\&}|g" .gitroot/plugins.yml
186sed -i "s|purl: ${APEX_CODE_PROD_VERSION}|purl: ${APEX_CODE_WASM//&/\\&}|g" .gitroot/plugins.yml
187sed -i "s|purl: ${APEX_MERMAID_PROD_VERSION}|purl: ${APEX_MERMAID_WASM//&/\\&}|g" .gitroot/plugins.yml
188sed -i "s|purl: ${FIREFLY_PROD_VERSION}|purl: ${FIREFLY_WASM//&/\\&}|g" .gitroot/plugins.yml
189
190# rm -rf .gitroot/plugins.yml
191# cp ${SCRIPT_DIR}/update-ressources/plugins.yml .gitroot/plugins.yml
192
193sed -i '/bwrap:/,/enabled:/ s/enabled: false/enabled: true/' .gitroot/forgeConfig.yml
194
195quiet_git add .
196quiet_git commit -m "update plugins"
197quiet_git push origin main
198
199mySleep 240
200
201push_and_wait origin main
202
203quiet_git pull origin main
204
205##### kill gitroot
206report "🏁 kill current gitroot"
207restart
208
209cd /tmp/root
210quiet_git pull origin main