Skip to content

Add changed-file formatting command - #303

Open
CYFS3 wants to merge 4 commits into
RT-Thread:masterfrom
CYFS3:add-format-changed
Open

CYFS3 wants to merge 4 commits into
RT-Thread:masterfrom
CYFS3:add-format-changed

Conversation

@CYFS3

@CYFS3 CYFS3 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

准备 PR 时需要统一格式化本次改动的 C/C++ 文件。命令入口和 Git 文件筛选逻辑属于 Env 的脚本功能,应由 env 仓库维护,并复用现有 Python 包安装与命令分发机制。

你的解决方案是什么 (what is your solution)

  • 在 cmds/cmd_format_changed.py 实现格式化命令,注册 format-changed 独立入口及 rt-env format-changed 子命令,补充中文使用说明。
  • 默认处理未暂存、已暂存和新增的 C/C++ 文件;--base REF 同时纳入分支自共同祖先以来、当前仍受 Git 跟踪的历史差异,避免改写已取消跟踪并忽略的本地文件;--check 只检查不修改。
  • 使用项目内的 .clang-format 或 _clang-format,对选中文件的工作区当前内容进行整文件格式化。跳过删除文件、符号链接、经过符号链接或 Windows 目录联接的路径,以及当前索引中子模块及其内部路径;缺少配置或存在冲突时停止。命令不修改暂存区,不创建提交或推送。
  • 文件筛选包含 Git 类型变更(T),保留未暂存 diff 的状态:符号链接替换为普通源码但尚未暂存时,按工作区当前类型判断;其余索引符号链接(包括 Windows core.symlinks=false 占位文件)继续跳过。
  • Git 和 clang-format 优先从 ENV_ROOT/tools/bin 查找,再检查 PATH 中明确配置的绝对目录,跳过空项和相对目录,使用绝对路径启动,避免 Windows/Python 3.11 自动优先执行仓库内同名程序。clang-format 必须为 22.1.8,Windows 程序由 Bundle clang-format 22.1.8 for Windows env-windows#43 单独集成。
  • 输出文件名及错误信息时,对当前输出编码无法表示的字符进行转义;传给 clang-format 的路径保持原样,支持 GBK 重定向输出下的 emoji 文件名。
  • tests/test_format_changed.py 包含工具查找、类型变更、Windows 占位文件、目录联接/符号链接、历史路径取消跟踪及 GBK 输出的回归测试。Windows Python 3.11 下,两个安装入口及工具查找共 21 项通过、2 项目录符号链接测试因权限限制跳过;该符号链接场景已在 WSL 中通过真实文件选择、检查和格式化验证。此前 38 项格式化测试、12 项 CLI/中文路径测试(含 13 个子测试)、Python 3.7 语法检查和 git diff --check 通过,未编译整个工程。

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@Rbb666 Rbb666 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文件筛选这里还有两处问题,合并前请修一下:

  1. cmds/cmd_format_changed.py:15 的 --diff-filter=ACMR 漏了 T。把已跟踪的符号链接换成普通 .c 文件后,Git 会把它记为类型变更。无论检查暂存改动,还是用 --base 检查已提交改动,都会漏掉这个文件,输出 No changed C/C++ files to process. 并返回 0。请纳入类型变更,再过滤当前仍为符号链接的文件,并补上这两种情况的测试。

  2. changed_files() 没有排除子模块目录下的候选路径。分支先提交新增 vendor/new.c,再把 vendor/ 暂存替换为子模块;如果子模块里也有 new.c,运行 format-changed --base <新增文件前的提交> 就会选中并改写它。--ignore-submodules=all 挡不住从 HEAD 历史差异里取出的旧路径。请根据当前索引中 mode 为 160000 的条目排除子模块目录下的文件,并补一个回归测试。

这两种情况在拆分前的 RT-Thread/env-windows#43 中都已复现,拆分后这里的筛选逻辑仍然存在同样的问题。

@Rbb666

Rbb666 commented Nov 3, 2026

Copy link
Copy Markdown
Member

看了 a90dbc7,上次提的已暂存/已提交类型变更和子模块问题都修好了,还差一个未暂存的情况。

在 core.symlinks=true 的仓库里,把已跟踪的符号链接换成普通 .c 文件,先不执行 git add。这时 Git 会报 T link.c,但索引里仍是 120000,第 58 行的 names - symlinks 会把它扣掉。已复现 format-changed --check 输出 No changed C/C++ files to process.,返回 0。

建议读取未暂存 diff 时保留 T 状态,不要按索引里的旧类型直接排除这些路径。未暂存的 T 变更继续走后面的 path.is_file() / path.is_symlink() 判断;其余索引符号链接仍按原逻辑跳过,这样也能保留 Windows core.symlinks=false 占位文件的处理。

请补一个“符号链接换成普通源码、但尚未暂存”的回归测试:未格式化时 --check 应返回非零,正常格式化应处理该文件,同时确认原有 Windows 占位文件测试仍通过。

@Rbb666 Rbb666 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这 4 处都在 Windows 上复现了,请修正后再合并。优先处理 Git 和 clang-format 的查找顺序,避免运行 --check 时执行待审查仓库里的程序。其余三处的复现步骤和修改建议写在对应代码行。

Comment thread cmds/cmd_format_changed.py Resolved
Comment thread cmds/cmd_format_changed.py Resolved
Comment thread cmds/cmd_format_changed.py Resolved
Comment thread cmds/cmd_format_changed.py Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants