Skip to content

zjs105910/workbuddy-market / bundled/github-project-manager/skills/github-project-manager

github-project-manager

Verified

管理从 GitHub 下载到本机的开源项目与技能包:列出、扫描发现、安装前预检、检查更新、一键更新、移除、回滚、数据完整性校验与环境诊断。带 O_EXCL 原子锁 + 心跳租约、事务级失败回滚、InstallPlan 状态收敛、单次遍历的磁盘预检、批量更新复用远程信息(消灭 N+1 请求)、ZIP 预扫描与压缩炸弹/设备名双重防护、skill 冲突阻断、内容指纹校验、硬链接与 rename 两种备份介质。触发方式:/ghpm、『GitHub 项目管理』『检查插件更新』『更新开源项目』『我装了哪些 GitHub 项目』『这个插件有新版吗』『卸载插件』『这个仓库能装吗』『ghpm doctor』『ghpm verify』『文件丢了/装坏了』。

scripts/ghpm/commands/scan.py

View on GitHub
# -*- coding: utf-8 -*-
"""scan:扫描本机已存在但尚未登记的 GitHub 项目,只登记不重装。"""
from __future__ import annotations

from .. import config
from ..discovery import find_skill_dirs
from ..errors import E_LOCKED, E_WRITE
from ..lock import LockSession
from ..registry import dir_ts, load, project_id, save, source_dir_for, upsert_project
from ..remote import fetch_repo_infos
from ..transaction import Transaction, report_rollback
from ..util import classify_write_error, emit, now


def _cmd_scan_locked(args):
    """扫描本机已存在但尚未登记的 GitHub 项目(调用方必须持锁)。"""
    data = load()
    known = {project_id(p) for p in data["projects"]}
    found = []

    # 1) skills 目录下 SKILL.md 里带 github source 的
    if config.SKILLS.is_dir():
        for d in sorted(config.SKILLS.iterdir()):
            f = d / "SKILL.md"
            if not f.is_file():
                continue
            try:
                txt = f.read_text(encoding="utf-8", errors="replace")[:2000]
            except Exception:
                continue
            m = config.GH_RE.search(txt)
            if m:
                found.append(("%s/%s" % (m.group(1), m.group(2)), d.name))

    # 2) ~/.workbuddy/*-source 目录
    for d in sorted(config.WB.glob("*-source")):
        if not d.is_dir():
            continue
        rf = d / "README.md"
        if not rf.is_file():
            continue
        try:
            txt = rf.read_text(encoding="utf-8", errors="replace")[:3000]
        except Exception:
            continue
        for m in config.GH_RE.finditer(txt):
            found.append(("%s/%s" % (m.group(1), m.group(2)), d.name))

    seen, uniq = set(), []
    for repo, src in found:
        if repo.lower() in known or repo.lower() in seen:
            continue
        seen.add(repo.lower())
        uniq.append((repo, src))

    if not uniq:
        print("没有发现新的未登记项目。")
        return

    print("发现 %d 个未登记项目:" % len(uniq))
    ttl = 0 if getattr(args, "fresh", False) else config.CACHE_TTL
    added = []
    # scan 也要逐个问 GitHub,串行时项目一多就很慢;同样走并发。
    fetched, fetch_errors = fetch_repo_infos(
        [repo for repo, _ in uniq], ttl=ttl,
        progress=lambda d, t, r: emit(
            "checking", "查询 %d/%d · %s" % (d, t, r), percent=int(20 * d / max(1, t))))

    tx = Transaction("registry-scan", "scan", "scan")
    tx.track([])                    # 只写清单,也走事务,写坏能还原
    try:
        tx.begin()
    except Exception as e:
        emit("failed", "清单备份失败,未登记任何项目", percent=100, code=E_WRITE,
             detail=str(e)[:160], hint=classify_write_error(e))
        raise
    try:
        for repo, src in uniq:
            info = fetched.get(repo.lower())
            if info is None:
                e = fetch_errors.get(repo.lower())
                print("  %-40s 跳过(%s)" % (repo, str(e)[:60] if e else "未取得信息"))
                continue
            name = repo.split("/")[1]
            if src.endswith("-source") and (config.WB / src).is_dir():
                src_dir = config.WB / src          # 扫到的就是源码目录本身
            else:
                src_dir = source_dir_for(repo, name, data)
            if src.endswith("-source"):
                # 从源码目录里找 skill,只算那些确实已装进 SKILLS 的
                skill_names = [n for n, _ in find_skill_dirs(config.WB / src, fallback=name)
                               if (config.SKILLS / n).is_dir()]
            else:
                # src 是某个已装 skill 的目录名,它的 SKILL.md 里写了这个仓库地址
                skill_names = [src] if (config.SKILLS / src).is_dir() else []
            rec = {
                "name": name, "repo": repo, "url": "https://github.com/%s" % repo,
                "branch": info["branch"], "sha": info["sha"], "sha_short": info["sha_short"],
                "stars": info["stars"], "desc": info["desc"], "last_msg": info["msg"],
                "source_dir": str(src_dir) if src_dir.exists() else "",
                # 之前这里硬编码成 [],前面算出来的 skill_names 全白算了:
                # remove 时不会删已装的 skill,skill_owner_map 也算不出冲突。
                "skill_names": skill_names,
                "installed_at": dir_ts(src_dir, *[config.SKILLS / n for n in skill_names]),
                "updated_at": dir_ts(src_dir, *[config.SKILLS / n for n in skill_names],
                                     mtime=True),
                "checked_at": now(),
                "previous_sha": "",
                # scan 只登记既有目录,不重写内容,所以没有可信指纹
                "content_hash": "",
                "skill_hashes": {},
            }
            upsert_project(data, rec)
            added.append(rec)
            print("  + %-40s %s  (%s)" % (repo, info["sha_short"], info["desc"][:40]))
        save(data)
    except Exception as e:
        emit("rollback", "登记失败,正在回滚清单…", percent=94, backup=str(tx.bd))
        restored = tx.rollback()
        report_rollback(tx.bd, restored, e)
        raise
    tx.commit()
    print("\n已登记 %d 个。注意:scan 只登记不重装,skill 归属请用 add 明确安装。" % len(added))


def cmd_scan(args):
    def on_busy(holder):
        emit("failed", "有另一个任务正在修改,请稍后再试", percent=100, code=E_LOCKED,
             detail="占用者:%s" % holder.get("op", "?"))

    with LockSession("scan", on_busy=on_busy):
        return _cmd_scan_locked(args)
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall