File size: 3,433 Bytes
e8598c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Build the next hf-deploy commit from main, and say what /health will report.

    python make_deploy_commit.py -m "Deploy: what changed"
    git push hf hf-deploy:main        # a human runs this

WHY A SCRIPT. The Space is public, and the deploy tree used to be assembled by hand
with two .docx removed by name. On 2026-11-09 a `git add -A` added an IRB procedure, a
manuscript, its supplement and an Excel lock stub to main; the by-name list did not know
about them, and only the order of two commits kept them off the Space. Everything matching
EXCLUDE is dropped here, so a new document cannot ride along because someone forgot it.

It never pushes. It writes the commit, moves the branch and prints the build hash to
check afterwards, and the push stays a deliberate act by a person.
"""
import argparse
import hashlib
import os
import subprocess
import sys
import tempfile

#: Dropped from what the Space receives. Study documents and Office lock files.
EXCLUDE = (".docx", ".doc", ".pdf", ".numbers", ".xlsx")
EXCLUDE_PREFIX = ("~$",)

#: The files /health hashes into the build id it reports.
HEALTH_FILES = sorted(("web_app.py", "order_link.py", "experiment_structure.py",
                       "orders.py", "questionnaires.py", "gitlab_save.py"))


def git(*args, **kw):
    return subprocess.run(["git", *args], check=True, capture_output=True,
                          text=True, **kw).stdout.strip()


def excluded(path):
    base = path.rsplit("/", 1)[-1]
    return path.endswith(EXCLUDE) or base.startswith(EXCLUDE_PREFIX)


def main(argv=None):
    ap = argparse.ArgumentParser()
    ap.add_argument("-m", "--message", required=True, help="the deploy commit message")
    ap.add_argument("--source", default="main")
    ap.add_argument("--branch", default="hf-deploy")
    a = ap.parse_args(argv)

    if git("status", "--porcelain", "--untracked-files=no"):
        sys.exit("the working tree has uncommitted changes; commit them first")

    with tempfile.TemporaryDirectory() as tmp:
        env = dict(os.environ, GIT_INDEX_FILE=os.path.join(tmp, "index"))
        subprocess.run(["git", "read-tree", a.source], check=True, env=env)
        listing = subprocess.run(["git", "ls-tree", "-r", "--name-only", a.source],
                                 check=True, capture_output=True, text=True).stdout.split("\n")
        drop = [p for p in listing if p and excluded(p)]
        if drop:
            subprocess.run(["git", "update-index", "--force-remove", *drop],
                           check=True, env=env)
        tree = subprocess.run(["git", "write-tree"], check=True, capture_output=True,
                              text=True, env=env).stdout.strip()

    commit = subprocess.run(["git", "commit-tree", tree, "-p", a.branch],
                            input=a.message, check=True, capture_output=True,
                            text=True).stdout.strip()
    git("update-ref", f"refs/heads/{a.branch}", commit)

    h = hashlib.sha256()
    for name in HEALTH_FILES:
        h.update(subprocess.run(["git", "show", f"{a.branch}:{name}"],
                                check=True, capture_output=True).stdout)
    print(f"{a.branch} -> {commit[:7]}")
    print(f"held back from the Space ({len(drop)}): " + ", ".join(drop))
    print(f"\n/health should report build {h.hexdigest()[:12]} once it rebuilds")
    print(f"\n  git push hf {a.branch}:main")


if __name__ == "__main__":
    main()