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

比较主工作树和实验工作树

PYTHONCPython 3.12.14 与 Git 2.54.0.windows.1,独立临时仓库。worktree_lab.py
worktree_lab.py51 行
"""Observe what stays separate when the same repository has two worktrees."""
import json
import os
from pathlib import Path
import subprocess
import sys
from tempfile import TemporaryDirectory


def exercise(root):
    env = {**os.environ, "GIT_CONFIG_NOSYSTEM": "1", "GIT_CONFIG_GLOBAL": os.devnull}
    for name in tuple(env):
        if name.startswith(("GIT_CONFIG_KEY_", "GIT_CONFIG_VALUE_")) or name in {
            "GIT_CONFIG", "GIT_CONFIG_COUNT", "GIT_CONFIG_PARAMETERS", "GIT_DIR", "GIT_WORK_TREE",
            "GIT_INDEX_FILE", "GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES",
            "GIT_NAMESPACE", "GIT_TEMPLATE_DIR",
        }:
            del env[name]

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

    main = root / "main"
    branch = root / "experiment"
    main.mkdir()
    git(main, "init", "--quiet", "--initial-branch=main", "--template=")
    (main / "policy.txt").write_bytes(b"limit=2\n")
    git(main, "add", "--", "policy.txt")
    git(main, "-c", "user.name=Fixture Author", "-c", "user.email=fixture@example.invalid",
        "commit", "--quiet", "-m", "Baseline")
    git(main, "worktree", "add", "--quiet", "-b", "experiment", str(branch))
    (branch / "policy.txt").write_bytes(b"limit=1\n")
    observation = [(main / "policy.txt").read_text().strip(),
        (branch / "policy.txt").read_text().strip(), git(main, "status", "--porcelain").splitlines(),
        git(branch, "status", "--porcelain").splitlines(), git(main, "branch", "--show-current").strip(),
        git(branch, "branch", "--show-current").strip(),
        git(main, "rev-parse", "HEAD").strip() == git(branch, "rev-parse", "HEAD").strip()]
    expected = ["limit=2", "limit=1", [], [" M policy.txt"], "main", "experiment", True]
    assert observation == expected
    assert git(branch, "diff", "--cached", "--name-only") == ""
    return dict(version={"python":sys.version.split()[0],"git":git(main,"--version").strip()}, cases=[
        dict(name="codex_local_worktree_isolation", input={"main":"limit=2","experiment":"limit=1"},
             operation="Create an additional Git worktree and change its tracked file, then compare both checkouts",
             expected=expected, actual=observation)])
    

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

运行环境:CPython 3.12.14 与 Git 2.54.0.windows.1,独立临时仓库。

入口文件:projects/codex-worktree/worktree_lab.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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