summaryrefslogtreecommitdiff
path: root/Scripts/update-parser.sh
blob: f4fab950c30eb75dc6a30b4e3ae854c228acd87b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh
#
# update-parser.sh — Bump vendored tree-sitter-zig and rebuild the dylib.
#
# Purpose:
#   Refresh the vendored snapshot under vendor/tree-sitter-zig/ to a newer
#   upstream commit, update VENDORING.md, and rebuild the parser dylib.
#
# What it does:
#   - clones tree-sitter-grammars/tree-sitter-zig into a temp dir
#   - checks out the requested ref (default: HEAD of the default branch)
#   - exits early if the upstream SHA matches the pinned SHA
#   - replaces src/, queries/, grammar.js, tree-sitter.json, LICENSE
#     with the upstream copies; leaves VENDORING.md and README.upstream.md
#     untouched
#   - rewrites the "Pinned commit:" line in VENDORING.md
#   - invokes build-parser.sh to rebuild Syntaxes/libtree-sitter-zig.dylib
#   - prints a GitHub compare link for the diff
#
# Usage:
#   ./Scripts/update-parser.sh           # bump to upstream HEAD
#   ./Scripts/update-parser.sh <ref>     # bump to a tag, branch, or SHA
#
# Caveats:
#   - any local edits inside the listed paths are overwritten — review
#     `git diff vendor/` afterwards before committing
#   - parses VENDORING.md by an exact "Pinned commit: <sha>" line prefix;
#     keep that line format intact
#
# Requirements:
#   git, plus everything build-parser.sh needs.

set -eu

ROOT="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
VENDOR_DIR="$ROOT/vendor/tree-sitter-zig"
UPSTREAM="https://github.com/tree-sitter-grammars/tree-sitter-zig.git"
REF="${1:-HEAD}"

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

git clone --quiet "$UPSTREAM" "$TMP/repo"
git -C "$TMP/repo" checkout --quiet "$REF"
SHA="$(git -C "$TMP/repo" rev-parse HEAD)"

OLD_SHA="$(awk '/^Pinned commit:/ {print $3}' "$VENDOR_DIR/VENDORING.md")"
if [ "$SHA" = "$OLD_SHA" ]; then
  echo "Already at $SHA — nothing to do."
  exit 0
fi

for path in src queries grammar.js tree-sitter.json LICENSE; do
  rm -rf "$VENDOR_DIR/$path"
  cp -R "$TMP/repo/$path" "$VENDOR_DIR/$path"
done

sed -i.bak "s/^Pinned commit: .*/Pinned commit: $SHA/" "$VENDOR_DIR/VENDORING.md"
rm "$VENDOR_DIR/VENDORING.md.bak"

"$ROOT/Scripts/build-parser.sh"

echo "Updated $OLD_SHA -> $SHA"
echo "Compare: https://github.com/tree-sitter-grammars/tree-sitter-zig/compare/$OLD_SHA...$SHA"