"""Offline VS Code Git Source Control workflow state machine verifier for Ch15."""
import hashlib
import json
VERSION = "CPython 3.12.14 offline VS Code Git Source Control verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = []
# Case 1: Initialize Repository -> untracked Changes -> stage single (+) -> Stage All Changes
repo = {
"initialized": False,
"changes": {"app.js": "U", "index.html": "U"},
"staged": {},
"commits": [],
}
repo["initialized"] = True
# Stage single file app.js
repo["staged"]["app.js"] = repo["changes"].pop("app.js")
after_single = (sorted(repo["changes"]), sorted(repo["staged"]))
# Stage All Changes
repo["staged"].update(repo["changes"])
repo["changes"].clear()
after_all = (sorted(repo["changes"]), sorted(repo["staged"]))
cases.append(
check(
"git_repo_init_and_staging_state",
"Verify Initialize Repository, single-file '+' staging, and Stage All Changes state transitions",
{
"initialized": True,
"after_single_changes": ["index.html"],
"after_single_staged": ["app.js"],
"after_all_changes": [],
"after_all_staged": ["app.js", "index.html"],
},
{
"initialized": repo["initialized"],
"after_single_changes": after_single[0],
"after_single_staged": after_single[1],
"after_all_changes": after_all[0],
"after_all_staged": after_all[1],
},
)
)
# Case 2: Commit staged changes with message validation & commit log verification
def commit_staged(r: dict, message: str) -> bool:
if not message.strip() or not r["staged"]:
return False
digest = hashlib.sha1(f"{message}:{sorted(r['staged'])}".encode("utf-8")).hexdigest()[:7]
r["commits"].append({"hash": digest, "message": message.strip(), "files": sorted(r["staged"])})
r["staged"].clear()
return True
empty_rejected = not commit_staged(repo, " ")
ok = commit_staged(repo, "Add hello function")
cases.append(
check(
"git_commit_and_history_log",
"Verify empty commit message rejection, successful commit recording, and clean working tree",
{
"empty_message_rejected": True,
"commit_succeeded": True,
"commit_count": 1,
"last_message": "Add hello function",
"working_tree_clean": True,
},
{
"empty_message_rejected": empty_rejected,
"commit_succeeded": ok,
"commit_count": len(repo["commits"]),
"last_message": repo["commits"][-1]["message"],
"working_tree_clean": not repo["changes"] and not repo["staged"],
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vscode-source-control-lab/explore-source-control.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-source-control.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2219.zip