Skip to content

Bug: x repo 多个子命令存在参数传递和错误检查问题 #2

Description

@lunrenyi

Bug 1:x repo <id> 在本地仓库不存在时会 clone 两次

复现

rm -rf ~/.x-repo/github.com/x-cmd/x-cmd-version
x repo x-cmd/x-cmd-version

预期

clone 一次,然后 cd 进入目录。

实际

Cloning into '/Users/liaoxuanbin/.x-repo/github.com/x-cmd/x-cmd-version'...
...
fatal: destination path 'x-cmd-version' already exists and is not an empty directory.

根因

___x_cmd_repo_cd(以及 edit/code)在目录不存在时调用 ___x_cmd_repo_update "$id" "$x_",把 $x_(本地路径)当作第二个 repo ID 传入。___x_cmd_repo_update 的 while 循环将所有位置参数视为 repo ID:

  1. 第一轮:$id = x-cmd/x-cmd-version → 目录不存在 → clone 成功
  2. 第二轮:$id = /Users/.../.x-repo/.../x-cmd-version → resolve 到刚 clone 的目录 → update 内部尝试 clone,报 already exists

修复

位置 函数 改动
lib/main:88 ___x_cmd_repo_edit "$id" "$x_" → "$id"
lib/main:109 ___x_cmd_repo_cd "$id" "$x_" → "$id"
lib/main:123 ___x_cmd_repo_code "$id" "$x_" → "$id"

Bug 2a:x repo fresh <不存在的id> 缺少错误检查

复现

x repo fresh nonexistent/repo-12345

预期

报错提示仓库不存在。

实际

___x_cmd_repo_resolve_ resolve 失败后未检查返回值,$x_ 为空,继续执行了 ___x_cmd fresh ""。

根因

lib/main:51 — ___x_cmd_repo_resolve_ "$id" 后缺少 || return $?。

修复

位置 函数 改动
lib/main:51 ___x_cmd_repo_fresh 加 || return $?

Bug 2b:x repo cd <不存在的id> 缺少错误检查

复现

x repo cd nonexistent/repo-12345

预期

报错提示仓库不存在。

实际

resolve 失败后 $x_ 为空,[ -d "" ] 为 false,触发 ___x_cmd_repo_update "",行为不可预期。

根因

lib/main:107 — ___x_cmd_repo_resolve_ "$id" 后缺少 || return $?。

修复

位置 函数 改动
lib/main:107 ___x_cmd_repo_cd 加 || return $?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions