Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

分支v1.5.x的Git包安装时浅克隆导致非默认分支 checkout 失败

未关闭
#275 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
git, python
领域
cli, tooling

调研方向

从 cmds/cmd_package/cmd_package_update.py:220-225 开始并阅读 install_git_package,然后使用分支名称和较旧的提交 SHA 重现安装过程。当使用分支、标签或非 HEAD SHA 的 Git 包能够成功 clone 和 checkout,同时现有的 HEAD 情况继续正常工作时,这项工作就完成了。

由索引模型根据 Issue 内容生成。

描述

问题描述

在分支v1.5.x的代码下,在安装 Git 类型包时,install_git_package 函数使用 --depth=1 浅克隆,但未指定分支。这导致非默认分支的包无法正确 checkout。

问题代码位置

cmds/cmd_package/cmd_package_update.py:220-225

clone_cmd = 'git clone ' + package_url + ' ' + repo_name_with_version + ' --depth=1'
execute_command(clone_cmd, cwd=bsp_package_path)

git_check_cmd = 'git checkout -q ' + ver_sha
execute_command(git_check_cmd, cwd=repo_path)

问题分析

场景1:VER_SHA 为分支名

当 package.json 配置如下:
{
"version": "galactic-legacy",
"VER_SHA": "galactic-legacy"
}

  • --depth=1 只克隆默认分支
  • git checkout galactic-legacy 失败,因为该分支不在浅克隆中

实验验证:
$ git clone https://github.com/wuhanstudio/micro_ros.git test --depth=1
$ git checkout galactic-legacy
error: pathspec 'galactic-legacy' did not match any file(s) known to git

场景2:VER_SHA 为 commit SHA

当仓库 HEAD 更新后,指定的旧 SHA 不在浅克隆中,checkout 同样失败。

影响范围

所有使用分支名或非 HEAD SHA 作为 VER_SHA 的包都会安装失败。

受影响的典型包:micro_ros 的多个版本配置(galactic-legacy, humble-gcc-10 等)。

建议修复方案

根据 VER_SHA 类型选择正确的克隆方式:

import re

def is_commit_sha(ver_sha):
"""检测 VER_SHA 是否为 commit SHA(40位十六进制)"""
return ver_sha and re.match(r'^[0-9a-f]{40}$', ver_sha)

def install_git_package(...):
if ver_sha and not is_commit_sha(ver_sha):
# 分支名/标签名:使用 --branch 参数
clone_cmd = 'git clone ... --depth=1 --branch ' + ver_sha
else:
# SHA:fetch 后 checkout
clone_cmd = 'git clone ... --depth=1'
execute_command('git fetch origin ' + ver_sha, cwd=repo_path)
execute_command('git checkout -q ' + ver_sha, cwd=repo_path)

主要语言
Python
星标
78
派生
64
平均合并
1 天 6 小时
30 天内合并 PR
6

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RT-Thread/env 的其他 Issue

查看 RT-Thread/env 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。