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

Open
#275 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
git, python
Domain
cli, tooling

Research direction

Start in cmds/cmd_package/cmd_package_update.py:220-225 and read install_git_package, then reproduce installation with a branch name and an older commit SHA. The work is done when Git packages using branches, tags, or non-HEAD SHAs clone and checkout successfully, while the existing HEAD case continues to work.

Written by the indexing model from the issue text.

Description

问题描述

在分支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)

Dominant language
Python
Stars
79
Forks
64
Avg merge
4d 2h
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RT-Thread/env

All issues in RT-Thread/env

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.