@@ -1,646 +0,0 @@
1−#!/usr/bin/env bash
2−# Self-test for sync-forkgram.sh and export-patches.sh. Builds a fake Forkgram remote and a
3−# fake MultiGram remote in a temporary folder (nothing touches GitHub or your clone) and
4−# runs the sync through its cases: no new release, --force, a release that applies, a
5−# release that conflicts, the hand fix (rerere, a multigram-next the sync must not replace,
6−# --use-next and the check that it carries all of multigram), promote, the .github guard,
7−# a missing multigram branch, local branches that must not move, merge commits, a full
8−# clone that must stay full, and the patch export.
9−#
10−# bash scripts/selftest-sync.sh [--keep] (--keep leaves the folder for a look)
11−set -euo pipefail
12−
13−HERE=$(cd "$(dirname "$0")" && pwd)
14−SYNC=$HERE/sync-forkgram.sh
15−EXPORT=$HERE/export-patches.sh
16−T=$(mktemp -d "${TMPDIR:-/tmp}/multigram-selftest.XXXXXX")
17−KEEP=no
18−[ "${1:-}" != --keep ] || KEEP=yes
19−trap '[ $KEEP = yes ] && echo "left in $T" || rm -rf "$T"' EXIT
20−
21−# A clean git setup, whatever the user's own config says.
22−export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=$T/gitconfig
23−git config --global user.name "Self Test"
24−git config --global user.email selftest@example.invalid
25−git config --global init.defaultBranch main
26−git config --global advice.detachedHead false
27−unset GITHUB_ACTIONS GITHUB_OUTPUT GITHUB_STEP_SUMMARY RUNNER_TEMP FORKGRAM_URL FORKGRAM_BRANCH || true
28−export FORKGRAM_URL=file://$T/forkgram.git FORKGRAM_BRANCH=dev
29−
30−PASS=0
31−FAIL=0
32−ok() { PASS=$((PASS + 1)); printf ' ok %s\n' "$1"; }
33−bad() { FAIL=$((FAIL + 1)); printf ' FAIL %s\n' "$1"; }
34−eq() { if [ "$2" = "$3" ]; then ok "$1"; else bad "$1: got [$2], want [$3]"; fi; }
35−has() { case $2 in *"$3"*) ok "$1" ;; *) bad "$1: [$3] not in [$2]" ;; esac; }
36−title() { printf '\n== %s\n' "$*"; }
37−
38−# Runs the sync script; keeps its exit code in RC and its outputs in $T/out.
39−run() { # dir args...
40− local d=$1
41− shift
42− : >"$T/out"
43− RC=0
44− (cd "$d" && GITHUB_OUTPUT=$T/out bash "$SYNC" "$@") >"$T/log" 2>&1 || RC=$?
45− sed -e '/^---- outputs ----$/,$d' -e 's/^/ | /' "$T/log"
46−}
47−o() { # key -> value from the last run's outputs
48− python3 - "$T/out" "$1" <<'PY'
49−import sys
50−path, key = sys.argv[1], sys.argv[2]
51−lines = open(path).read().split('\n')
52−vals, i = {}, 0
53−while i < len(lines):
54− line = lines[i]
55− if '<<' in line:
56− k, d = line.split('<<', 1)
57− j = lines.index(d, i + 1)
58− vals[k] = '\n'.join(lines[i + 1:j])
59− i = j + 1
60− else:
61− i += 1
62−print(vals.get(key, ''), end='')
63−PY
64−}
65−rref() { git --git-dir="$T/multigram.git" rev-parse -q --verify "$1" || true; }
66−
67−# ------------------------------------------------------------------ fake Forkgram
68−
69−git init -q --bare "$T/forkgram.git"
70−git --git-dir="$T/forkgram.git" config uploadpack.allowFilter true
71−git --git-dir="$T/forkgram.git" config uploadpack.allowAnySHA1InWant true
72−git init -q "$T/fg"
73−cd "$T/fg"
74−
75−main_java() { # name line, tail line
76− printf 'class Main {\n String name = "%s";\n int a = 1;\n int b = 2;\n int c = 3;\n int d = 4;\n // %s\n}\n' "$1" "$2"
77−}
78−props() { printf 'APP_VERSION_CODE=%s\nAPP_VERSION_NAME=%s\nAPP_PACKAGE=org.example\n' "$1" "$2"; }
79−gitlink() { git update-index --add --cacheinfo "160000,$1,$2"; }
80−subst() { sed "$1" "$2" >"$2.new" && mv "$2.new" "$2"; } # sed -i, on any sed
81−setver() { subst "s/^APP_VERSION_NAME=.*/APP_VERSION_NAME=$1/" gradle.properties; }
82−upstream() { # version code name tail sublink : a Telegram "update to" commit
83− mkdir -p app
84− props "$2" "$1" >gradle.properties
85− main_java "$3" "$4" >app/Main.java
86− printf 'class Util {}\n' >app/Util.java
87− printf '[submodule "lib/sub"]\n\tpath = lib/sub\n\turl = https://example.invalid/sub.git\n[submodule "topsub"]\n\tpath = topsub\n\turl = https://example.invalid/top.git\n' >.gitmodules
88− git add -A
89− gitlink "$5" lib/sub
90− gitlink 2222222222222222222222222222222222222222 topsub
91− git commit -q -m "update to $1 ($2)"
92−}
93−fork_commits() { # version : Forkgram's own commits, then the release commit
94− mkdir -p .github/workflows
95− printf 'on: push\n' >.github/workflows/ci.yml
96− printf 'class Fork {}\n' >app/Fork.java
97− git add -A
98− git commit -q -m "Added fork settings."
99− setver "$1"
100− printf '// release %s\n' "$1" >>app/Fork.java
101− git commit -q -am "[Release] $1."
102−}
103−
104−upstream 1.0.0 100 Fork "end" 1111111111111111111111111111111111111111
105−fork_commits 1.0.1
106−git push -q "$T/forkgram.git" HEAD:refs/heads/dev
107−R1=$(git rev-parse HEAD)
108−U1=$(git rev-parse HEAD~2)
109−
110−# ------------------------------------------------------------------ fake MultiGram
111−
112−git init -q --bare "$T/multigram.git"
113−git --git-dir="$T/multigram.git" config uploadpack.allowFilter true
114−git --git-dir="$T/multigram.git" config uploadpack.allowAnySHA1InWant true
115−git init -q "$T/mgsetup"
116−cd "$T/mgsetup"
117−git fetch -q "$T/forkgram.git" dev
118−tree=$(git ls-tree FETCH_HEAD | grep -v " .github$" | git mktree)
119−S1=$(printf 'Forkgram 1.0.1 snapshot\n\nSource: https://github.com/forkgram/TelegramAndroid/commit/%s\nForkgram-Commit: %s\nForkgram-Version: 1.0.1\nTelegram-Base: %s (update to 1.0.0)\n\nTree is Forkgram'"'"'s dev branch at that commit with the .github folder removed.\n' "$R1" "$R1" "$U1" | git commit-tree "$tree")
120−git switch -q -c multigram "$S1"
121−mkdir -p multigram
122−printf 'MultiGram notes\n' >multigram/README.md
123−git add -A && git commit -q -m "Add MultiGram notes"
124−subst 's/"Fork"/"MultiGram"/' app/Main.java
125−git commit -q -am "Rename the app"
126−printf 'class Util { /* MultiGram: hook */ }\n' >app/Util.java
127−git commit -q -am "Add a MultiGram hook"
128−git switch -q --orphan main
129−printf 'main\n' >README.md
130−git add README.md && git commit -q -m "Start main"
131−git push -q "$T/multigram.git" main "$S1:refs/heads/forkgram" multigram
132−MG0=$(rref multigram)
133−
134−# "CI": a blobless, depth-1 clone of main, like actions/checkout with filter: blob:none.
135−ci_clone() {
136− rm -rf "$T/ci"
137− git clone -q --filter=blob:none --depth 1 --branch main "file://$T/multigram.git" "$T/ci"
138− git -C "$T/ci" config user.name "github-actions[bot]"
139− git -C "$T/ci" config user.email "41898282+github-actions[bot]@users.noreply.github.com"
140−}
141−
142−# ------------------------------------------------------------------ A: no new release
143−title "A. No new Forkgram release"
144−ci_clone
145−run "$T/ci" --remote origin --push
146−eq "exit code" "$RC" 0
147−eq "status" "$(o status)" up-to-date
148−eq "new_snapshot" "$(o new_snapshot)" no
149−eq "forkgram unchanged" "$(rref forkgram)" "$S1"
150−eq "no multigram-next" "$(rref multigram-next)" ""
151−
152−# ------------------------------------------------------------------ B: --force
153−title "B. --force with no new release, then promote"
154−ci_clone
155−run "$T/ci" --remote origin --push --force
156−eq "exit code" "$RC" 0
157−eq "status" "$(o status)" candidate
158−eq "new_snapshot" "$(o new_snapshot)" no
159−eq "candidate is multigram (nothing to replay)" "$(o candidate)" "$MG0"
160−eq "multigram-next pushed" "$(rref multigram-next)" "$MG0"
161−eq "pushed" "$(o pushed)" multigram-next
162−cand=$(o candidate) prev=$(o multigram) tv=$(o tag_version)
163−ci_clone
164−run "$T/ci" promote --remote origin --candidate "$cand" --previous "$prev" --version "$tv" --shallow
165−eq "promote exit code" "$RC" 0
166−eq "promote status" "$(o status)" unchanged
167−eq "multigram unchanged" "$(rref multigram)" "$MG0"
168−eq "multigram-next deleted" "$(rref multigram-next)" ""
169−
170−# ------------------------------------------------------------------ C: clean release
171−title "C. New Forkgram release 1.1.0 that applies cleanly"
172−cd "$T/fg"
173−git reset -q --hard "$U1"
174−printf 'class Other {}\n' >app/Other.java
175−git add -A
176−upstream 1.1.0 110 Fork "end, 1.1.0" 3333333333333333333333333333333333333333
177−U2=$(git rev-parse HEAD)
178−fork_commits 1.1.0
179−R2=$(git rev-parse HEAD)
180−git push -q -f "$T/forkgram.git" HEAD:refs/heads/dev
181−ci_clone
182−run "$T/ci" --remote origin --push
183−eq "exit code" "$RC" 0
184−eq "status" "$(o status)" candidate
185−eq "new_snapshot" "$(o new_snapshot)" yes
186−eq "forkgram_commit" "$(o forkgram_commit)" "$R2"
187−eq "forkgram_version" "$(o forkgram_version)" 1.1.0
188−eq "telegram_base" "$(o telegram_base)" "$U2 (update to 1.1.0)"
189−eq "previous_version" "$(o previous_version)" 1.0.1
190−S2=$(rref forkgram)
191−eq "forkgram pushed" "$S2" "$(o snapshot)"
192−eq "snapshot parent is the previous snapshot" "$(git --git-dir="$T/multigram.git" rev-parse "$S2^")" "$S1"
193−eq "snapshot has no .github" "$(git --git-dir="$T/multigram.git" ls-tree --name-only "$S2" .github)" ""
194−want=$(git -C "$T/fg" ls-tree "$R2" | grep -v " .github$")
195−eq "snapshot tree = Forkgram tree minus .github" "$(git --git-dir="$T/multigram.git" ls-tree "$S2")" "$want"
196−eq "gitlink lib/sub kept" "$(git --git-dir="$T/multigram.git" ls-tree "$S2" lib/sub | cut -f1)" "160000 commit 3333333333333333333333333333333333333333"
197−eq "gitlink topsub kept" "$(git --git-dir="$T/multigram.git" ls-tree "$S2" topsub | cut -f1)" "160000 commit 2222222222222222222222222222222222222222"
198−msg=$(git --git-dir="$T/multigram.git" log -1 --format=%B "$S2")
199−has "message title" "$msg" "Forkgram 1.1.0 snapshot"
200−has "message Source" "$msg" "Source: file://$T/forkgram.git (dev) at $R2"
201−has "message Forkgram-Commit" "$msg" "Forkgram-Commit: $R2"
202−has "message Forkgram-Version" "$msg" "Forkgram-Version: 1.1.0"
203−has "message Telegram-Base" "$msg" "Telegram-Base: $U2 (update to 1.1.0)"
204−has "message tail" "$msg" "Tree is Forkgram's dev branch at that commit with the .github folder removed."
205−C1=$(rref multigram-next)
206−eq "multigram-next = candidate" "$C1" "$(o candidate)"
207−eq "candidate has 3 commits on the snapshot" "$(git --git-dir="$T/multigram.git" rev-list --count "$S2..$C1")" 3
208−eq "candidate subjects" "$(git --git-dir="$T/multigram.git" log --reverse --format=%s "$S2..$C1" | tr '\n' '|')" "Add MultiGram notes|Rename the app|Add a MultiGram hook|"
209−eq "candidate Main.java: stack change on the new upstream" "$(git --git-dir="$T/multigram.git" show "$C1:app/Main.java" | sed -n '2p;7p' | tr '\n' '|')" ' String name = "MultiGram";| // end, 1.1.0|'
210−eq "candidate keeps upstream's new file" "$(git --git-dir="$T/multigram.git" show "$C1:app/Other.java")" "class Other {}"
211−eq "candidate tree = snapshot + stack diff" \
212− "$(git --git-dir="$T/multigram.git" diff --stat "$S2" "$C1" | tail -n1)" \
213− "$(git --git-dir="$T/multigram.git" diff --stat "$S1" "$MG0" | tail -n1)"
214−eq "multigram not moved yet" "$(rref multigram)" "$MG0"
215−eq "the sync recorded the multigram-next it built" "$(rref refs/multigram-sync/next)" "$C1"
216−cand=$(o candidate) prev=$(o multigram) tv=$(o tag_version)
217−ci_clone
218−run "$T/ci" promote --remote origin --candidate "$cand" --previous "$prev" --version "$tv" --shallow
219−eq "promote exit code" "$RC" 0
220−eq "promote status" "$(o status)" promoted
221−eq "multigram moved to the candidate" "$(rref multigram)" "$C1"
222−eq "old multigram tagged" "$(rref refs/tags/multigram-before-1.1.0)" "$MG0"
223−eq "multigram-next deleted" "$(rref multigram-next)" ""
224−
225−# ------------------------------------------------------------------ D: conflicting release
226−title "D. New Forkgram release 1.2.0 that conflicts"
227−cd "$T/fg"
228−git reset -q --hard "$U2"
229−upstream 1.2.0 120 "Fork 2" "end, 1.1.0" 3333333333333333333333333333333333333333
230−fork_commits 1.2.0
231−R3=$(git rev-parse HEAD)
232−git push -q -f "$T/forkgram.git" HEAD:refs/heads/dev
233−MG1=$(rref multigram)
234−ci_clone
235−run "$T/ci" --remote origin --push
236−eq "exit code" "$RC" 2
237−eq "status" "$(o status)" conflict
238−S3=$(rref forkgram)
239−eq "snapshot pushed anyway" "$S3" "$(o snapshot)"
240−eq "forkgram_version" "$(o forkgram_version)" 1.2.0
241−eq "conflict commit is the rename" "$(o conflict_commit)" "$(git --git-dir="$T/multigram.git" rev-parse "$MG1~1")"
242−eq "conflict subject" "$(o conflict_subject)" "Rename the app"
243−eq "conflict position" "$(o conflict_position)" "2 of 3"
244−eq "conflict files" "$(o conflict_files)" app/Main.java
245−eq "conflict details" "$(o conflict_details)" "changed on both sides: app/Main.java"
246−eq "multigram untouched" "$(rref multigram)" "$MG1"
247−eq "no multigram-next" "$(rref multigram-next)" ""
248−eq "no candidate" "$(o candidate)" ""
249−eq "pushed" "$(o pushed)" forkgram
250−
251−# ------------------------------------------------------------------ E: behind
252−title "E. Next run, no new release: multigram is behind"
253−ci_clone
254−run "$T/ci" --remote origin --push
255−eq "exit code" "$RC" 0
256−eq "status" "$(o status)" behind
257−eq "multigram_base is the old snapshot" "$(o multigram_base)" "$S2"
258−eq "nothing pushed" "$(o pushed)" ""
259−
260−# ------------------------------------------------------------------ F: hand fix with rerere
261−title "F. Hand fix in a local clone; rerere replays it on the next local run"
262−git clone -q "file://$T/multigram.git" "$T/owner"
263−cd "$T/owner"
264−git config rerere.enabled true
265−git switch -q -C multigram-next origin/multigram
266−if git rebase -q --onto "$S3" "$S2" >/dev/null 2>&1; then bad "hand rebase should conflict"; fi
267−main_java "MultiGram 2" "end, 1.1.0" >app/Main.java
268−git add app/Main.java
269−GIT_EDITOR=true git rebase --continue >/dev/null 2>&1
270−FIX=$(git rev-parse HEAD)
271−eq "hand fix has 3 commits on the new snapshot" "$(git rev-list --count "$S3..$FIX")" 3
272−git switch -q --detach
273−git branch -q -D multigram-next
274−# The owner's own branches, with rebase.updateRefs on: the replay must not move them.
275−git config rebase.updateRefs true
276−git branch -q -f multigram origin/multigram
277−git branch -q -f wip origin/multigram~1
278−run "$T/owner" --remote origin --force
279−git config --unset rebase.updateRefs
280−eq "exit code" "$RC" 0
281−eq "status" "$(o status)" candidate
282−eq "local multigram not moved (rebase.updateRefs)" "$(git rev-parse multigram)" "$MG1"
283−eq "local wip not moved (rebase.updateRefs)" "$(git rev-parse wip)" "$(git rev-parse "$MG1~1")"
284−eq "local record = local multigram-next" "$(git rev-parse refs/multigram-sync/next)" "$(git rev-parse multigram-next)"
285−has "rerere_resolved names the file" "$(o rerere_resolved)" "app/Main.java"
286−eq "local multigram-next has the recorded resolution" "$(git show multigram-next:app/Main.java | sed -n 2p)" ' String name = "MultiGram 2";'
287−eq "same tree as the hand fix" "$(git rev-parse "multigram-next^{tree}")" "$(git rev-parse "$FIX^{tree}")"
288−eq "local forkgram = latest snapshot" "$(git rev-parse forkgram)" "$S3"
289−eq "nothing pushed without --push" "$(rref multigram-next)" ""
290−
291−# ------------------------------------------------------------------ G: --use-next
292−title "G. The hand fix on multigram-next: never replaced; --use-next checks it, then promote"
293−git push -q -f origin "$FIX:refs/heads/multigram-next"
294−ci_clone
295−run "$T/ci" --remote origin --push --force
296−eq "--force: exit code" "$RC" 0
297−eq "--force: status" "$(o status)" next-edited
298−eq "--force: existing_next" "$(o existing_next)" "$FIX"
299−eq "--force: multigram-next kept" "$(rref multigram-next)" "$FIX"
300−eq "--force: nothing pushed" "$(o pushed)" ""
301−cd "$T/fg"
302−setver 1.2.1
303−git commit -q -am "[Release] 1.2.1."
304−git push -q -f "$T/forkgram.git" HEAD:refs/heads/dev
305−ci_clone
306−run "$T/ci" --remote origin --push
307−eq "new release: status" "$(o status)" next-edited
308−eq "new release: forkgram_tip is the new commit" "$(o forkgram_tip)" "$(git -C "$T/fg" rev-parse HEAD)"
309−eq "new release: no new snapshot" "$(rref forkgram)" "$S3"
310−eq "new release: multigram-next kept" "$(rref multigram-next)" "$FIX"
311−has "new release: message says why" "$(o message)" "has commits the sync did not make"
312−git reset -q --hard "$R3"
313−git push -q -f "$T/forkgram.git" HEAD:refs/heads/dev
314−cd "$T/owner"
315−git fetch -q origin
316−git switch -q -C late origin/multigram
317−printf 'late\n' >app/Late.java
318−git add -A && GIT_AUTHOR_DATE="2030-01-01T00:00:00Z" git commit -q -m "Late MultiGram commit"
319−LATE=$(git rev-parse HEAD)
320−git push -q origin late:multigram
321−git switch -q --detach
322−git branch -q -D late
323−ci_clone
324−run "$T/ci" --remote origin --push --use-next
325−eq "late commit: exit code" "$RC" 1
326−eq "late commit: status" "$(o status)" error
327−eq "late commit: missing" "$(o missing)" "$LATE Late MultiGram commit"
328−has "late commit: error names it" "$(o error)" "Late MultiGram commit"
329−has "late commit: error offers allow_drop" "$(o error)" "allow_drop"
330−ci_clone
331−run "$T/ci" --remote origin --push --use-next --allow-drop
332−eq "--allow-drop: status" "$(o status)" candidate
333−eq "--allow-drop: dropped" "$(o dropped)" "$LATE Late MultiGram commit"
334−ci_clone
335−run "$T/ci" --remote origin --push --allow-drop
336−eq "--allow-drop without --use-next: exit code" "$RC" 1
337−git -C "$T/owner" push -q -f origin "$MG1:refs/heads/multigram"
338−ci_clone
339−run "$T/ci" --remote origin --push --use-next
340−eq "exit code" "$RC" 0
341−eq "status" "$(o status)" candidate
342−eq "candidate is the hand fix" "$(o candidate)" "$FIX"
343−eq "the hand fix carries the whole stack" "$(o missing)$(o dropped)" ""
344−eq "forkgram_version" "$(o forkgram_version)" 1.2.0
345−cand=$(o candidate) prev=$(o multigram) tv=$(o tag_version)
346−ci_clone
347−run "$T/ci" promote --remote origin --candidate "$cand" --previous "$prev" --version "$tv" --shallow
348−eq "promote status" "$(o status)" promoted
349−eq "multigram = hand fix" "$(rref multigram)" "$FIX"
350−eq "tag" "$(rref refs/tags/multigram-before-1.2.0)" "$MG1"
351−ci_clone
352−run "$T/ci" --remote origin --push
353−eq "and now up to date" "$(o status)" up-to-date
354−
355−# ------------------------------------------------------------------ H: dropped commit
356−title "H. Release 1.3.0 already has one MultiGram change: that commit is dropped"
357−cd "$T/fg"
358−git reset -q --hard "$R3"
359−mkdir -p multigram && printf 'MultiGram notes\n' >multigram/README.md
360−git add -A && git commit -q -m "Take the notes file"
361−setver 1.3.0
362−git commit -q -am "[Release] 1.3.0."
363−git push -q -f "$T/forkgram.git" HEAD:refs/heads/dev
364−ci_clone
365−run "$T/ci" --remote origin --push
366−eq "status" "$(o status)" candidate
367−eq "telegram_base still found" "$(o telegram_base)" "$(git -C "$T/fg" rev-parse "HEAD~4") (update to 1.2.0)"
368−eq "stack_count" "$(o stack_count)" 3
369−eq "candidate_count" "$(o candidate_count)" 2
370−if [ -n "$(o dropped)" ]; then ok "dropped is reported"; else bad "dropped is empty"; fi
371−cand=$(o candidate) prev=$(o multigram) tv=$(o tag_version)
372−
373−# ------------------------------------------------------------------ I: promote lease
374−title "I. Promote refuses when multigram moved after the sync"
375−cd "$T/owner"
376−git fetch -q origin
377−git commit -q --allow-empty -m "Someone else's commit" && git push -q -f origin "HEAD:refs/heads/multigram"
378−ci_clone
379−run "$T/ci" promote --remote origin --candidate "$cand" --previous "$prev" --version "$tv" --shallow
380−eq "exit code" "$RC" 1
381−eq "status" "$(o status)" error
382−has "error says why" "$(o error)" "pushed to meanwhile"
383−eq "multigram-next kept" "$(rref multigram-next)" "$cand"
384−git push -q -f origin "$prev:refs/heads/multigram"
385−ci_clone
386−run "$T/ci" promote --remote origin --candidate "$cand" --previous "$prev" --version "$tv" --shallow
387−eq "promote after restoring multigram" "$(o status)" promoted
388−
389−# ------------------------------------------------------------------ J: .github guard
390−title "J. A stack commit that adds .github is refused"
391−cd "$T/owner"
392−git fetch -q origin
393−git switch -q -C multigram origin/multigram
394−mkdir -p .github && printf 'x\n' >.github/extra.yml
395−git add -A && git commit -q -m "Add a workflow file"
396−git push -q -f origin multigram
397−BAD=$(git rev-parse HEAD)
398−F_BEFORE=$(rref forkgram)
399−ci_clone
400−run "$T/ci" --remote origin --push --force
401−eq "exit code" "$RC" 1
402−eq "status" "$(o status)" error
403−has "error names .github" "$(o error)" ".github"
404−eq "nothing pushed" "$(rref multigram-next)" ""
405−eq "forkgram unchanged" "$(rref forkgram)" "$F_BEFORE"
406−git push -q -f origin "$BAD~1:refs/heads/multigram"
407−git switch -q --detach
408−
409−# ------------------------------------------------------------------ K: guards
410−title "K. Guards: branch checked out, no multigram branch"
411−git switch -q -C forkgram origin/forkgram
412−run "$T/owner" --force
413−eq "checked-out forkgram: exit code" "$RC" 1
414−has "checked-out forkgram: error" "$(o error)" "checked out in a worktree"
415−git switch -q --detach
416−git init -q --bare "$T/empty.git"
417−git -C "$T/owner" push -q "$T/empty.git" "origin/forkgram:refs/heads/forkgram"
418−git -C "$T/owner" remote add empty "$T/empty.git"
419−run "$T/owner" --remote empty --push
420−eq "no multigram: exit code" "$RC" 1
421−has "no multigram: error" "$(o error)" "has no multigram branch yet"
422−run "$T/owner" --remote empty --use-next --force
423−eq "--force with --use-next: exit code" "$RC" 1
424−git fetch -q origin
425−UNPUSHED=$(git commit-tree -p origin/multigram -m "Unpushed fix" "origin/multigram^{tree}")
426−git branch -q -f multigram-next "$UNPUSHED"
427−F_BEFORE=$(git rev-parse forkgram)
428−run "$T/owner" --remote origin --force
429−eq "unpushed local multigram-next: exit code" "$RC" 1
430−has "unpushed local multigram-next: error" "$(o error)" "on no other branch or tag"
431−eq "unpushed local multigram-next: not moved" "$(git rev-parse multigram-next)" "$UNPUSHED"
432−eq "unpushed local multigram-next: local forkgram not moved" "$(git rev-parse forkgram)" "$F_BEFORE"
433−git branch -q -D multigram-next
434−
435−# ------------------------------------------------------------------ L: local mode
436−title "L. Local branches, no remote"
437−cd "$T/owner"
438−git fetch -q origin
439−git branch -q -f forkgram origin/forkgram
440−git branch -q -f multigram origin/multigram
441−run "$T/owner"
442−eq "status" "$(o status)" up-to-date
443−HAND=$(git commit-tree -p multigram -m "Hand fix" "multigram^{tree}")
444−git branch -q -f multigram-next "$HAND"
445−run "$T/owner" --force
446−eq "hand-made local multigram-next: exit code" "$RC" 0
447−eq "hand-made local multigram-next: status" "$(o status)" next-edited
448−eq "hand-made local multigram-next: kept" "$(git rev-parse multigram-next)" "$HAND"
449−git branch -q -D multigram-next
450−
451−# ------------------------------------------------------------------ M: export-patches
452−title "M. export-patches.sh, applied onto a plain Forkgram checkout with git am -3"
453−cd "$T/owner"
454−EXP_RC=0
455−bash "$EXPORT" --base origin/forkgram --head origin/multigram "$T/patches" >"$T/log" 2>&1 || EXP_RC=$?
456−sed 's/^/ | /' "$T/log" | head -n 8
457−eq "export exit code" "$EXP_RC" 0
458−eq "patch files" "$(cd "$T/patches" && ls ./*.patch | wc -l | tr -d ' ')" 2
459−if [ -f "$T/patches/APPLY.txt" ]; then ok "APPLY.txt written"; else bad "APPLY.txt missing"; fi
460−has "APPLY.txt names the Forkgram commit" "$(cat "$T/patches/APPLY.txt")" "$(git -C "$T/fg" rev-parse HEAD)"
461−git clone -q -b dev "$T/forkgram.git" "$T/plain"
462−cd "$T/plain"
463−git switch -q -c multigram
464−if git am -q -3 "$T/patches"/*.patch >/dev/null 2>&1; then ok "git am -3 applies"; else bad "git am -3 failed"; fi
465−eq "result = multigram + Forkgram's .github" \
466− "$(git ls-tree -r HEAD | grep -v ' .github/')" \
467− "$(git -C "$T/owner" ls-tree -r origin/multigram)"
468−EXP_RC=0
469−bash "$EXPORT" --base origin/forkgram --head origin/multigram "$T/patches" >/dev/null 2>&1 || EXP_RC=$?
470−eq "refuses a non-empty folder without --force" "$EXP_RC" 1
471−
472−# ------------------------------------------------------------------ N: Forkgram already fetched
473−title "N. Forkgram's tip already in the local clone; a history too short for Telegram-Base"
474−cd "$T/fg"
475−printf 'class Later {}\n' >app/Later.java
476−git add -A && git commit -q -m "Added a later feature."
477−setver 1.5.0
478−git commit -q -am "[Release] 1.5.0."
479−git push -q -f "$T/forkgram.git" HEAD:refs/heads/dev
480−R5=$(git rev-parse HEAD)
481−git clone -q "file://$T/multigram.git" "$T/full"
482−run "$T/full" --remote origin
483−eq "full clone: status" "$(o status)" candidate
484−has "full clone: fetched into a scratch repository" "$(cat "$T/log")" "into a scratch repository"
485−eq "full clone: still not shallow" "$(git -C "$T/full" rev-parse --is-shallow-repository)" false
486−eq "full clone: snapshot tree" "$(git -C "$T/full" ls-tree forkgram)" "$(git -C "$T/fg" ls-tree "$R5" | grep -v " .github$")"
487−eq "full clone: version from gradle.properties" "$(o forkgram_version)" 1.5.0
488−if git -C "$T/full" fsck --no-progress >"$T/fsck" 2>&1; then ok "full clone: fsck is clean"; else bad "full clone: fsck: $(head -n 3 "$T/fsck")"; fi
489−cd "$T/owner"
490−git fetch -q "$T/forkgram.git" "+refs/heads/dev:refs/forkgram-full/dev"
491−git fetch -q origin
492−git branch -q -f forkgram origin/forkgram
493−git branch -q -f multigram origin/multigram
494−FORKGRAM_HISTORY_DEPTH=1 run "$T/owner" --remote origin
495−eq "status" "$(o status)" candidate
496−has "used the local copy" "$(cat "$T/log")" "already here"
497−eq "no Telegram-Base when the history is too short" "$(o telegram_base)" ""
498−if git log -1 --format=%B forkgram | grep -q '^Telegram-Base:'; then bad "Telegram-Base line written"; else ok "no Telegram-Base line"; fi
499−eq "snapshot tree" "$(git ls-tree forkgram)" "$(git ls-tree "$R5" | grep -v " .github$")"
500−eq "local forkgram is the new snapshot, origin untouched" "$(git rev-parse forkgram^)" "$(rref forkgram)"
501−eq "not a shallow repository" "$(git rev-parse --is-shallow-repository)" false
502−
503−# ------------------------------------------------------------------ O: tag already taken
504−title "O. A second promotion for the same Forkgram version gets its own tag"
505−cd "$T/owner"
506−git fetch -q origin
507−git switch -q -C multigram-next origin/multigram
508−printf 'more\n' >>multigram/README.md 2>/dev/null || { mkdir -p multigram; printf 'more\n' >multigram/README.md; }
509−git add -A && git commit -q -m "Another MultiGram change"
510−git push -q -f origin multigram-next
511−git switch -q --detach
512−ci_clone
513−run "$T/ci" --remote origin --push --use-next
514−cand=$(o candidate) prev=$(o multigram) tv=$(o tag_version)
515−eq "same version as the last promotion" "$tv" 1.3.0
516−ci_clone
517−run "$T/ci" promote --remote origin --candidate "$cand" --previous "$prev" --version "$tv" --shallow
518−eq "status" "$(o status)" promoted
519−eq "tag with a suffix" "$(o tag)" multigram-before-1.3.0-2
520−eq "tag points at the old multigram" "$(rref refs/tags/multigram-before-1.3.0-2)" "$prev"
521−eq "first tag untouched" "$(rref refs/tags/multigram-before-1.3.0)" "$FIX"
522−
523−# ------------------------------------------------------------------ Q: merge commits
524−title "Q. Merge commits on multigram: a plain merge is flattened, one with its own changes stops the sync"
525−cd "$T/owner"
526−git fetch -q origin
527−MGQ=$(git rev-parse origin/multigram)
528−git switch -q -C side origin/forkgram
529−printf 'class Side {}\n' >app/Side.java
530−git add -A && git commit -q -m "Side feature"
531−git switch -q -C multigram "$MGQ"
532−git merge -q --no-ff --no-edit side
533−git push -q -f origin multigram
534−run "$T/owner" --remote origin --force
535−eq "plain merge: exit code" "$RC" 0
536−eq "plain merge: status" "$(o status)" candidate
537−has "plain merge: warned" "$(cat "$T/log")" "only join their parents"
538−eq "plain merge: candidate = snapshot + everything multigram changes" \
539− "$(git diff "$(o snapshot)" multigram-next -- | grep -v '^index ')" "$(git diff origin/forkgram multigram -- | grep -v '^index ')"
540−eq "plain merge: candidate is linear" "$(git rev-list --merges "origin/forkgram..multigram-next")" ""
541−git reset -q --hard "$MGQ"
542−git merge -q --no-ff --no-commit side
543−printf 'class Glue {}\n' >app/Glue.java
544−git add -A && git commit -q -m "Merge side with glue"
545−git push -q -f origin multigram
546−run "$T/owner" --remote origin --force
547−eq "merge with changes: exit code" "$RC" 1
548−has "merge with changes: error names it" "$(o error)" "Merge side with glue"
549−git push -q -f origin "$MGQ:refs/heads/multigram"
550−git switch -q --detach
551−git branch -q -D side multigram
552−
553−# ------------------------------------------------------------------ P: sync-report.py
554−title "P. sync-report.py against a fake gh"
555−mkdir -p "$T/bin"
556−cat >"$T/bin/gh" <<'PY'
557−#!/usr/bin/env python3
558−# A tiny stand-in for the gh CLI: keeps issues in a JSON file.
559−import json, os, sys
560−path = os.environ['FAKE_GH_STATE']
561−st = json.load(open(path)) if os.path.exists(path) else {'issues': [], 'labels': []}
562−a = sys.argv[1:]
563−def opt(name):
564− return a[a.index(name) + 1] if name in a else None
565−def body():
566− f = opt('--body-file')
567− return open(f).read() if f else ''
568−def issue(n):
569− return next(i for i in st['issues'] if i['number'] == int(n))
570−if a[:2] == ['label', 'create']:
571− st['labels'].append(a[2])
572−elif a[:2] == ['issue', 'list']:
573− print(json.dumps([{'number': i['number'], 'title': i['title']} for i in st['issues']
574− if i['state'] == 'open' and opt('--label') in i['labels']]))
575−elif a[:2] == ['issue', 'view']:
576− i = issue(a[2])
577− print(json.dumps({'body': i['body'], 'comments': [{'body': c} for c in i['comments']]}))
578−elif a[:2] == ['issue', 'create']:
579− if opt('--label') not in st['labels']:
580− sys.exit('label missing')
581− n = len(st['issues']) + 1
582− st['issues'].append({'number': n, 'title': opt('--title'), 'body': body(), 'labels': [opt('--label')],
583− 'state': 'open', 'comments': []})
584− print(f'https://example.invalid/issues/{n}')
585−elif a[:2] == ['issue', 'comment']:
586− issue(a[2])['comments'].append(body())
587−elif a[:2] == ['issue', 'edit']:
588− issue(a[2])['title'] = opt('--title')
589−elif a[:2] == ['issue', 'close']:
590− i = issue(a[2]); i['state'] = 'closed'; i['comments'].append(opt('--comment'))
591−else:
592− sys.exit('fake gh: unsupported ' + ' '.join(a))
593−json.dump(st, open(path, 'w'))
594−PY
595−chmod +x "$T/bin/gh"
596−export REPORT_GH=$T/bin/gh FAKE_GH_STATE=$T/gh.json RUN_URL=https://example.invalid/runs/1
597−report() { # VAR=value... : runs sync-report.py with only these job results and outputs
598− env -i PATH="$PATH" REPORT_GH="$REPORT_GH" FAKE_GH_STATE="$FAKE_GH_STATE" RUN_URL="$RUN_URL" "$@" \
599− python3 "$HERE/sync-report.py" 2>&1 | sed 's/^/ | /'
600−}
601−gh_state() { python3 -c "import json,sys; st=json.load(open('$T/gh.json')); print(eval(sys.argv[1]))" "$1"; }
602−A40=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa B40=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb C40=cccccccccccccccccccccccccccccccccccccccc
603−conflict=(SYNC_RESULT=failure COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=conflict
604− SYNC_FORKGRAM_VERSION=1.2.0 SYNC_FORKGRAM_COMMIT=$A40 SYNC_SNAPSHOT=$B40 SYNC_PREVIOUS_SNAPSHOT=$C40
605− SYNC_CONFLICT_COMMIT=$C40 "SYNC_CONFLICT_SUBJECT=Rename the \`app\`" "SYNC_CONFLICT_POSITION=2 of 3"
606− SYNC_CONFLICT_FILES=app/Main.java "SYNC_CONFLICT_DETAILS=changed on both sides: app/Main.java")
607−report "${conflict[@]}"
608−eq "conflict opens an issue" "$(gh_state "len(st['issues'])")" 1
609−eq "issue title" "$(gh_state "st['issues'][0]['title']")" "Forkgram sync needs a hand (Forkgram 1.2.0)"
610−has "issue body has the rebase command" "$(gh_state "st['issues'][0]['body']")" "git rebase --onto $B40 $C40"
611−has "issue body has the file" "$(gh_state "st['issues'][0]['body']")" "changed on both sides: app/Main.java"
612−has "backticks in the subject are neutralised" "$(gh_state "st['issues'][0]['body']")" "Rename the 'app'"
613−report "${conflict[@]}"
614−eq "the same conflict is not reported twice" "$(gh_state "len(st['issues'][0]['comments'])")" 0
615−report SYNC_RESULT=success COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=behind SYNC_FORKGRAM_VERSION=1.2.0
616−eq "behind with an open issue adds nothing" "$(gh_state "len(st['issues'][0]['comments'])")" 0
617−report SYNC_RESULT=success COMPILE_RESULT=failure PROMOTE_RESULT=skipped SYNC_STATUS=candidate \
618− SYNC_FORKGRAM_VERSION=1.2.0 SYNC_CANDIDATE=$A40 SYNC_SNAPSHOT=$B40
619−eq "compile failure comments on the open issue" "$(gh_state "len(st['issues'][0]['comments'])")" 1
620−has "comment has the autosquash fix" "$(gh_state "st['issues'][0]['comments'][0]")" "git rebase -i --autosquash $B40"
621−next_edited=(SYNC_RESULT=success COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=next-edited
622− SYNC_FORKGRAM_VERSION=1.2.0 SYNC_FORKGRAM_COMMIT=$A40 SYNC_FORKGRAM_TIP=$B40 SYNC_EXISTING_NEXT=$C40)
623−report "${next_edited[@]}"
624−eq "next-edited comments on the open issue" "$(gh_state "len(st['issues'][0]['comments'])")" 2
625−has "next-edited comment says what to do" "$(gh_state "st['issues'][0]['comments'][1]")" "use_next"
626−has "next-edited comment names the new Forkgram commit" "$(gh_state "st['issues'][0]['comments'][1]")" "${B40:0:10}"
627−report "${next_edited[@]}"
628−eq "the same next-edited is not reported twice" "$(gh_state "len(st['issues'][0]['comments'])")" 2
629−report SYNC_RESULT=success COMPILE_RESULT=success PROMOTE_RESULT=success SYNC_STATUS=candidate \
630− SYNC_FORKGRAM_VERSION=1.2.0 SYNC_CANDIDATE=$A40 PROMOTE_TAG=multigram-before-1.2.0 "SYNC_DROPPED=$C40 Late \`commit\`"
631−eq "promotion closes the issue" "$(gh_state "st['issues'][0]['state']")" closed
632−has "closing note names the tag" "$(gh_state "st['issues'][0]['comments'][-1]")" "multigram-before-1.2.0"
633−has "closing note lists the dropped commits" "$(gh_state "st['issues'][0]['comments'][-1]")" "$C40 Late 'commit'"
634−report SYNC_RESULT=success COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=up-to-date SYNC_FORKGRAM_VERSION=1.2.0
635−eq "up to date with no open issue: nothing" "$(gh_state "len(st['issues'])")" 1
636−report SYNC_RESULT=failure COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=error "SYNC_ERROR=origin has no multigram branch yet."
637−eq "a sync error opens a new issue" "$(gh_state "len(st['issues'])")" 2
638−has "error text in the issue" "$(gh_state "st['issues'][1]['body']")" "origin has no multigram branch yet."
639−report SYNC_RESULT=cancelled COMPILE_RESULT=skipped PROMOTE_RESULT=skipped
640−report SYNC_RESULT=success COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=up-to-date SYNC_FORKGRAM_VERSION=1.2.0
641−eq "in step again closes it" "$(gh_state "st['issues'][1]['state']")" closed
642−report SYNC_RESULT=success COMPILE_RESULT=skipped PROMOTE_RESULT=skipped SYNC_STATUS=behind SYNC_FORKGRAM_VERSION=1.3.0
643−eq "behind with no open issue opens one" "$(gh_state "[i['state'] for i in st['issues']]")" "['closed', 'closed', 'open']"
644−
645−printf '\n%d passed, %d failed\n' "$PASS" "$FAIL"
646−[ "$FAIL" -eq 0 ]