git-finalizer 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ """Git Finalizer: fail-closed Git delivery gatekeeper and toolchain manager."""
2
+
3
+ from __future__ import annotations
4
+
5
+ TOOL_NAME = "git-finalizer"
6
+ __version__ = "2.0.0"
7
+
8
+ __all__ = ["TOOL_NAME", "__version__"]
@@ -0,0 +1,3 @@
1
+ """Delivery gatekeeper engine subpackage."""
2
+
3
+ from __future__ import annotations
@@ -0,0 +1,166 @@
1
+ """Primary CLI entry point for `git-finalize`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Sequence
6
+ from contextlib import redirect_stdout
7
+ import io
8
+ import json
9
+ from pathlib import Path
10
+ import sys
11
+
12
+ from git_finalizer import __version__
13
+ from git_finalizer.delivery import engine, integration_publish, repo_bootstrap
14
+ from git_finalizer.delivery.parser import ParsedCLI, parse_cli, usage_text
15
+ from git_finalizer.delivery.runtime import (
16
+ SummaryState,
17
+ execution_lock,
18
+ render_fallback_error_summary,
19
+ )
20
+ from git_finalizer.errors import ErrorCode, FinalizerError
21
+
22
+
23
+ def _emit_summary_json(payload: dict[str, object]) -> None:
24
+ print(json.dumps(payload, ensure_ascii=False, separators=(",", ":")), flush=True)
25
+
26
+
27
+ def _apply_error_to_summary(summary: SummaryState, exc: FinalizerError) -> SummaryState:
28
+ summary.status = exc.status
29
+ summary.final_phase = exc.final_phase
30
+ summary.exit_code = exc.exit_code
31
+ summary.error_code = exc.code
32
+ summary.reason = exc.message
33
+ summary.next_action = exc.next_action
34
+ if exc.commit_created:
35
+ summary.commit_created = True
36
+ if exc.commit_sha:
37
+ summary.commit_sha = exc.commit_sha
38
+ if exc.push_executed:
39
+ summary.push_executed = True
40
+ if exc.push_result != "not_run":
41
+ summary.push_result = exc.push_result
42
+ for warning in exc.warnings:
43
+ if warning not in summary.warnings:
44
+ summary.warnings.append(warning)
45
+ if exc.resume:
46
+ summary.resume_required = True
47
+ summary.resume_oid = str(exc.resume.get("head_oid") or "") or None
48
+ summary.resume_remote = str(exc.resume.get("remote") or "") or None
49
+ if exc.scan_diagnostic:
50
+ summary.scan_diagnostic = dict(exc.scan_diagnostic)
51
+ if exc.reviewed_sensitive_sources:
52
+ summary.reviewed_sensitive_sources = [dict(x) for x in exc.reviewed_sensitive_sources]
53
+ return summary
54
+
55
+
56
+ def main(argv: Sequence[str] | None = None) -> int:
57
+ raw_argv = list(sys.argv[1:] if argv is None else argv)
58
+ entrypoint = Path(sys.argv[0]) if sys.argv and sys.argv[0] else Path(__file__).resolve()
59
+ want_summary = "--summary" in raw_argv
60
+ cli: ParsedCLI | None = None
61
+ summary: SummaryState | None = None
62
+
63
+ try:
64
+ with execution_lock(entrypoint):
65
+ cli = parse_cli(raw_argv)
66
+ if cli.dispatch == "version":
67
+ print(f"git-finalize {__version__}")
68
+ return 0
69
+ if cli.dispatch == "help":
70
+ print(usage_text())
71
+ return 0
72
+
73
+ summary = engine.create_summary(cli, entrypoint=entrypoint)
74
+ if cli.parse_error:
75
+ err = FinalizerError(ErrorCode.CLI_INVALID_ARGS, cli.parse_error, final_phase="cli")
76
+ if cli.summary:
77
+ _emit_summary_json(_apply_error_to_summary(summary, err).to_dict())
78
+ else:
79
+ print(f"ERROR [{err.code}]: {err.message}", file=sys.stderr)
80
+ return err.exit_code
81
+
82
+ if cli.dispatch == "bootstrap":
83
+ return repo_bootstrap.main(raw_argv)
84
+ if cli.dispatch == "integration_publish":
85
+ return integration_publish.main(raw_argv)
86
+
87
+ if cli.summary:
88
+ buf = io.StringIO()
89
+ with redirect_stdout(buf):
90
+ code, summary = engine.run_delivery(cli, entrypoint=entrypoint, summary=summary)
91
+ _emit_summary_json(summary.to_dict())
92
+ return code
93
+
94
+ code, _summary = engine.run_delivery(cli, entrypoint=entrypoint, summary=summary)
95
+ return code
96
+ except FinalizerError as exc:
97
+ if summary is not None and cli is not None and cli.summary:
98
+ _emit_summary_json(_apply_error_to_summary(summary, exc).to_dict())
99
+ elif want_summary or (cli is not None and cli.summary):
100
+ print(
101
+ render_fallback_error_summary(reason=exc.message, error_code=exc.code),
102
+ flush=True,
103
+ )
104
+ else:
105
+ print(f"ERROR [{exc.code}]: {exc.message}", file=sys.stderr)
106
+ if exc.commit_created and exc.commit_sha and cli is not None:
107
+ if cli.action == "initial-branch-publish":
108
+ from git_finalizer.git_io import run_git
109
+
110
+ up_res = run_git(Path(cli.repo), "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}")
111
+ obs_up = up_res.stdout.decode("utf-8", errors="replace").strip() if up_res.returncode == 0 else "(none)"
112
+ print(
113
+ f"本地提交 {exc.commit_sha} 已保留;未执行 pull、rebase、merge、reset 或 force push。",
114
+ file=sys.stderr,
115
+ )
116
+ print(f"显式目标: {cli.remote}/{cli.remote_branch}", file=sys.stderr)
117
+ print(f"当前 upstream: {obs_up}", file=sys.stderr)
118
+ elif exc.resume and exc.next_action == "resume_initial_publish":
119
+ print(
120
+ f"本地 root commit {exc.resume.get('head_oid')} 已保留;不得重跑 --initial-publish。",
121
+ file=sys.stderr,
122
+ )
123
+ print(
124
+ f"恢复入口: git-finalize --resume-initial-publish {exc.resume.get('head_oid')} "
125
+ f"--remote {exc.resume.get('remote')} --repo {cli.repo}",
126
+ file=sys.stderr,
127
+ )
128
+ else:
129
+ print(f"本地提交 {exc.commit_sha} 已保留", file=sys.stderr)
130
+ elif exc.resume and exc.next_action == "resume_existing_history_publish" and cli is not None:
131
+ print(
132
+ f"恢复入口: git-finalize --resume-existing-history-publish {exc.resume.get('head_oid')} "
133
+ f"--remote {exc.resume.get('remote')} --remote-branch {cli.remote_branch} --repo {cli.repo}",
134
+ file=sys.stderr,
135
+ )
136
+ elif exc.next_action == "retry_same_resume" and cli is not None:
137
+ if cli.action == "resume-initial-publish":
138
+ print(
139
+ f"可安全再次运行同一恢复命令: git-finalize --resume-initial-publish {exc.resume.get('head_oid') if exc.resume else cli.head_oid} "
140
+ f"--remote {exc.resume.get('remote') if exc.resume else cli.remote} --repo {cli.repo}",
141
+ file=sys.stderr,
142
+ )
143
+ else:
144
+ print(
145
+ f"可安全再次运行同一恢复命令: git-finalize --resume-publish {exc.resume.get('head_oid') if exc.resume else cli.head_oid} --repo {cli.repo}",
146
+ file=sys.stderr,
147
+ )
148
+ return exc.exit_code
149
+ except SystemExit as exc:
150
+ return int(exc.code) if isinstance(exc.code, int) else 1
151
+ except Exception as exc:
152
+ if want_summary or (cli is not None and cli.summary):
153
+ print(
154
+ render_fallback_error_summary(
155
+ reason=f"internal error: {exc}",
156
+ error_code=ErrorCode.INTERNAL_ERROR,
157
+ ),
158
+ flush=True,
159
+ )
160
+ else:
161
+ print(f"ERROR [{ErrorCode.INTERNAL_ERROR}]: {exc}", file=sys.stderr)
162
+ return 1
163
+
164
+
165
+ if __name__ == "__main__":
166
+ raise SystemExit(main())