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

离线校验 Git/SSH 命令规范、状态色标、三窗格冲突解决与部分提交/Stash/变基模型

PYTHONWindows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。explore-git.py
explore-git.py167 行
"""Offline Python verification lab for PyCharm Chapter 15 (git)."""
import json
import shlex

VERSION = "CPython 3.12.14 offline PyCharm workflow verifier (desktop IDE not launched)"


def case_git_config_and_ssh_keygen_specs():
    git_cmds = [
        'git config --global user.name "Your Name"',
        'git config --global user.email "your.email@example.com"',
    ]
    ssh_cmd = 'ssh-keygen -t ed25519 -C "your_email@example.com"'
    tokens = shlex.split(ssh_cmd)
    auth_modes = ["Log In via GitHub (OAuth)", "Log In with Token (PAT)"]
    expected = {
        "git_config_keys": ["user.name", "user.email"],
        "ssh_key_type": "ed25519",
        "github_auth_modes": 2,
    }
    actual = {
        "git_config_keys": [shlex.split(c)[3] for c in git_cmds],
        "ssh_key_type": tokens[tokens.index("-t") + 1],
        "github_auth_modes": len(auth_modes),
    }
    return {
        "name": "git_config_and_ssh_keygen_specs",
        "summary": "校验 Git 全局用户配置命令、GitHub OAuth/Token 认证方式与 ed25519 SSH 密钥生成参数",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_file_status_colors_and_commit_flow():
    status_colors = {
        "modified": "blue",
        "added": "green",
        "untracked": "gray",
        "conflict": "red",
    }
    shortcuts = {
        "commit": ("Ctrl+K", "⌘K"),
        "push": ("Ctrl+Shift+K", "⌘⇧K"),
        "pull": ("Ctrl+T", "⌘T"),
        "diff": ("Ctrl+D", "⌘D"),
        "history": ("Alt+9", "⌘9 / Alt+9"),
    }
    pull_strategies = ["Merge", "Rebase"]
    expected = {
        "status_count": 4,
        "conflict_color": "red",
        "commit_shortcut_win": "Ctrl+K",
        "pull_strategies": ["Merge", "Rebase"],
    }
    actual = {
        "status_count": len(status_colors),
        "conflict_color": status_colors["conflict"],
        "commit_shortcut_win": shortcuts["commit"][0],
        "pull_strategies": pull_strategies,
    }
    return {
        "name": "file_status_colors_and_commit_flow",
        "summary": "核对 PyCharm Git 文件状态颜色标识、提交/推送/拉取/差异快捷键及 Merge 与 Rebase 策略",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_three_way_merge_conflict_resolution():
    conflict_file = (
        "config = {\n"
        "<<<<<<< HEAD\n"
        '    "timeout": 30,\n'
        "=======\n"
        '    "timeout": 60,\n'
        ">>>>>>> feature/perf\n"
        "}\n"
    )
    has_markers = all(m in conflict_file for m in ("<<<<<<<", "=======", ">>>>>>>"))
    ours = '    "timeout": 30,'
    theirs = '    "timeout": 60,'

    def resolve(choice):
        if choice == "accept_left":
            body = ours
        elif choice == "accept_right":
            body = theirs
        else:
            body = '    "timeout": 45,  # manual merge'
        return f"config = {{\n{body}\n}}\n"

    merged_left = resolve("accept_left")
    merged_right = resolve("accept_right")
    merged_manual = resolve("manual")
    expected = {
        "conflict_detected": True,
        "accept_left_clean": True,
        "accept_right_clean": True,
        "manual_timeout": 45,
    }
    actual = {
        "conflict_detected": has_markers,
        "accept_left_clean": "30" in merged_left and "<<<<<<<" not in merged_left,
        "accept_right_clean": "60" in merged_right and "<<<<<<<" not in merged_right,
        "manual_timeout": 45 if "45" in merged_manual else -1,
    }
    return {
        "name": "three_way_merge_conflict_resolution",
        "summary": "检测三方合并冲突标记并模拟三窗格合并工具的接受左侧、接受右侧与手动合并输出",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_partial_commit_stash_and_interactive_rebase():
    # Partial commit: commit hunk 1 while leaving hunk 2 unstaged
    hunks = [{"id": 1, "selected": True}, {"id": 2, "selected": False}]
    committed_hunks = [h["id"] for h in hunks if h["selected"]]

    # Stash / Unstash stack
    stash_stack = []
    working_tree = {"app.py": "wip_change"}
    stash_stack.append(dict(working_tree))
    working_tree.clear()
    restored_tree = stash_stack.pop()

    # Interactive rebase: reorder + squash + reword
    commits = [("c1", "init"), ("c2", "feat: login"), ("c3", "fixup login")]
    rebased = [("c1", "init"), ("c2+c3", "feat: user login")]

    expected = {
        "committed_hunks": [1],
        "stash_roundtrip": {"app.py": "wip_change"},
        "commits_before_rebase": 3,
        "commits_after_squash": 2,
    }
    actual = {
        "committed_hunks": committed_hunks,
        "stash_roundtrip": restored_tree,
        "commits_before_rebase": len(commits),
        "commits_after_squash": len(rebased),
    }
    return {
        "name": "partial_commit_stash_and_interactive_rebase",
        "summary": "模拟按代码块部分提交、Stash 储藏与取消储藏往返以及交互式变基压缩提交",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_git_config_and_ssh_keygen_specs(),
        case_file_status_colors_and_commit_flow(),
        case_three_way_merge_conflict_resolution(),
        case_partial_commit_stash_and_interactive_rebase(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。

入口文件:projects/pycharm-git-lab/explore-git.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-git.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 PyCharm 工程语义与配置验证器(未启动桌面端 PyCharm IDE)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PyCharm

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

下载课程关联工程包:PyCharm

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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