File size: 1,804 Bytes
8bf9ce1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Classify a Hugging Face-to-GitHub synchronization safely."""

from __future__ import annotations

import argparse
from collections.abc import Sequence


SyncAction = str


def classify_sync(
    github_sha: str,
    hf_sha: str,
    github_is_ancestor: bool,
    hf_is_ancestor: bool,
) -> SyncAction:
    """Return the only permitted synchronization action for two revisions."""
    if not github_sha.strip() or not hf_sha.strip():
        raise ValueError("both repositories must provide a commit SHA")
    if github_sha == hf_sha:
        return "noop_equal"
    if hf_is_ancestor:
        return "noop_hf_behind"
    if github_is_ancestor:
        return "fast_forward"
    return "pull_request"


def _boolean(value: str) -> bool:
    normalized = value.strip().lower()
    if normalized == "true":
        return True
    if normalized == "false":
        return False
    raise argparse.ArgumentTypeError("expected true or false")


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="Classify GitHub and Hugging Face main-branch ancestry."
    )
    parser.add_argument("--github-sha", required=True)
    parser.add_argument("--hf-sha", required=True)
    parser.add_argument("--github-is-ancestor", required=True, type=_boolean)
    parser.add_argument("--hf-is-ancestor", required=True, type=_boolean)
    return parser


def main(argv: Sequence[str] | None = None) -> int:
    args = build_parser().parse_args(argv)
    print(
        classify_sync(
            github_sha=args.github_sha,
            hf_sha=args.hf_sha,
            github_is_ancestor=args.github_is_ancestor,
            hf_is_ancestor=args.hf_is_ancestor,
        )
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())