Composer引入GitLab私有仓库需三要素:repositories顶层数组中设"type":"vcs"、url含.git后缀;auth.json置于~/.composer/且权限600,用http-basic匹配域名;require包名必须与私有库composer.json中name字段完全一致。

Composer 不能直接“搭建” GitLab 私有仓库——GitLab 是代码托管平台,Composer 只是依赖管理工具。真正要做的,是让 Composer 能从你的 GitLab 仓库里拉取包,这靠的是 vcs 类型仓库 + 正确认证 + 包名对齐,三者缺一不可。
composer.json 的 repositories 必须写对位置和结构
repositories 是顶层数组,不是嵌套在 config、scripts 或其他字段下。写错位置,Composer 完全无视。
-
"type"必须是"vcs",不是"git"、"gitlab"或留空;写成"gitlab"会静默跳过(某些旧教程误导严重) -
"url"必须是能直接git clone的地址,含.git后缀:例如https://gitlab.example.com/acme/utils.git或git@gitlab.example.com:acme/utils.git - 不能写成网页地址:
https://gitlab.example.com/acme/utils(缺.git)或https://gitlab.example.com/acme/utils/-/tree/main(是 HTML 页面) - 如果只加了私有源,默认会关闭 packagist.org 隐式源,必须显式补一条兜底:
{"type": "composer", "url": "https://packagist.org", "packagist": false},否则其他公共包会找不到
auth.json 必须放对路径且权限严格
90% 的认证失败,不是 Token 无效,而是文件没被读到——Composer 对路径和权限极其敏感,且不报错。
生成周报:读取本机 Git 仓库最近一周的提交记录,按项目分类提炼总结,生成标准周报格式,通过微信推送并返回给用户。触发词:周报、weekly report、git总结、提交记录总结、工作周报。
- Linux/macOS 路径只能是:
~/.composer/auth.json(不是项目根目录下的auth.json) - Windows 路径只能是:
%APPDATA%\Composer\auth.json - 权限必须是
600:chmod 600 ~/.composer/auth.json,否则 Composer 直接忽略该文件 - 内容必须用
"http-basic"字段,域名 key 和仓库 URL 的 host 完全一致(不含协议、端口、路径),例如:"gitlab.example.com"对应https://gitlab.example.com/acme/utils.git - Token 类型必须是 Personal Access Token,scope 至少含
read_repository;glpat-前缀不能漏
require 的包名和版本必须与私有库 composer.json 严丝合缝
Composer 不按 Git 路径找包,只比对私有库根目录 composer.json 里的 "name" 字段。差一个字符,就 Could not find package。
- 假设私有库的
composer.json写的是{"name": "acme/utils"},那主项目require就必须写"acme/utils": "dev-main" - 不能写成
"Acme/utils"(大小写敏感)、"acme-utils"(连字符 ≠ 斜杠)、"utils"(缺 vendor 段) - 分支必须加
dev-前缀:"dev-main"可行,"main"或"*"会被当模糊约束,去 Packagist 查,查不到就失败 - 如果私有库没打任何 tag,唯一可用版本就是
dev-main(或dev-master,取决于默认分支名)
验证是否真能走通,别信“配置完了”
很多问题表面是 Composer 报错,实际是 Git 层根本连不通。最可靠验证方式是绕过 Composer,直连 Git。
- 执行:
git ls-remote -h https://gitlab.example.com/acme/utils.git,能列出分支说明 HTTPS + Token 可用 - 执行:
git ls-remote -h git@gitlab.example.com:acme/utils.git,能列出分支说明 SSH 密钥已配好 - 如果
composer install报Authentication failed,但上面命令成功,大概率是auth.json路径或权限错了 - 如果报
No valid composer.json was found,说明 Git 克隆成功了,但私有库根目录没有合法composer.json(比如放错位置、语法错误、缺name字段)
最容易被忽略的点:私有库自己的 composer.json 必须有 "name",且该值就是你在主项目 require 里写的字符串——这不是约定,是硬匹配。Git 分支、Tag、URL、Token 全对,只要这个 name 差一个字符,整个流程就卡死。

















