蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

检查新工作树、差异与设置脚本

PYTHONWindows,CPython 3.12.14,git version 2.54.0.windows.1。desktop_review_lab.py
desktop_review_lab.py69 行
"""Observe Git review stages and setup steps in a disposable project."""
from hashlib import sha256
import json
import os
from pathlib import Path
import subprocess
import sys
from tempfile import TemporaryDirectory


def exercise(root):
    env = {key: value for key, value in os.environ.items() if not key.startswith("GIT_")}
    env.update(GIT_CONFIG_NOSYSTEM="1", GIT_CONFIG_GLOBAL=os.devnull)

    def git(cwd, *tokens):
        command = ["git", "-c", "core.autocrlf=false", *tokens]
        return subprocess.run(command, cwd=cwd, env=env, capture_output=True,
                              encoding="utf-8", check=True, timeout=15).stdout

    checkout = root / "local"
    checkout.mkdir()
    git(checkout, "init", "--quiet", "--initial-branch=main", "--template=")
    (checkout / ".gitignore").write_bytes(b".local-check\n")
    (checkout / ".local-check").write_bytes(b"local-only\n")
    (checkout / "capacity.txt").write_bytes(b"active=2\n")
    (checkout / "setup_demo.py").write_bytes(
        b'from pathlib import Path\nPath(".local-check").write_text("setup-ready", encoding="utf-8")\n'
    )
    git(checkout, "add", "--", ".gitignore", "capacity.txt", "setup_demo.py")
    git(checkout, "-c", "user.name=Fixture Author", "-c", "user.email=fixture@example.invalid",
        "commit", "--quiet", "-m", "Base project")
    worktree = root / "review"
    git(checkout, "worktree", "add", "--quiet", "--detach", str(worktree))
    initial = [(checkout / ".local-check").is_file(), (worktree / ".local-check").is_file(),
               git(worktree, "branch", "--show-current").strip()]
    assert initial == [True, False, ""]
    cases = [dict(name="desktop_worktree_baseline", input=[".local-check", "--detach"],
        operation="Create a separate checkout and inspect an ignored file and branch name",
        expected=[True, False, ""], actual=initial)]

    (worktree / "capacity.txt").write_bytes(b"active=4\n")
    unstaged = git(worktree, "diff", "--name-only").splitlines()
    git(worktree, "add", "--", "capacity.txt")
    staged = git(worktree, "diff", "--cached", "--name-only").splitlines()
    actual = [unstaged, staged, git(checkout, "status", "--porcelain").splitlines()]
    expected = [["capacity.txt"], ["capacity.txt"], []]
    assert actual == expected
    assert git(worktree, "diff", "--name-only").splitlines() == []
    cases.append(dict(name="desktop_git_review_states", input=["active=2", "active=4"],
        operation="Compare unstaged and staged file lists and check the main checkout",
        expected=expected, actual=actual))

    fixture = worktree / "setup_demo.py"
    result = subprocess.run([sys.executable, "-I", fixture.name], cwd=worktree, capture_output=True, timeout=15)
    actual = [result.returncode, (worktree / ".local-check").read_text(encoding="utf-8"),
              (checkout / ".local-check").read_text(encoding="utf-8").strip()]
    expected = [0, "setup-ready", "local-only"]
    assert actual == expected
    cases.append(dict(name="desktop_setup_fixture", input=["setup_demo.py", ".local-check"],
        operation="Run the project setup script in the isolated checkout and inspect both markers",
        expected=expected, actual=actual, setup_fixture_sha256=sha256(fixture.read_bytes()).hexdigest()))
    return dict(version={"python": sys.version.split()[0], "git": git(checkout, "--version").strip()},
                cases=cases)


if __name__ == "__main__":
    with TemporaryDirectory(prefix="mbsky-desktop-review-") as temp:
        print(json.dumps(exercise(Path(temp))))
本例需在 Windows,CPython 3.12.14,git version 2.54.0.windows.1。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows,CPython 3.12.14,git version 2.54.0.windows.1。

入口文件:projects/codex-desktop/desktop_review_lab.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 desktop_review_lab.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows,CPython 3.12.14,git version 2.54.0.windows.1。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联40章) · 含当前章节或实例 · mbskycom202612a90.zip

下载课程关联工程包:Codex 教程

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a104.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习