@adobe/helix-shared-git

中文文档
3.0.26发布于 2026/09/28Apache-2.0ES Module构建来源已验证近 30 天下载 3.1万

Helix 项目共享模块 - Git Utils

中文使用文档
文档目录21 节
  1. 安装
  2. 使用量
  3. 从字符串分析 Git URL
  4. 从对象创建 Git URL
  5. 使用不同的 Git URL 格式工作
  6. 访问原始内容和 API URL
  7. 使用默认
  8. 串行和去串行
  9. JSON代表人数最少
  10. 比较 Git URL
  11. 与地方仓库合作
  12. API 参考
  13. 属性
  14. 方法
  15. URL 格式
  16. 所需构成部分
  17. 可选组件
  18. 特殊处理
  19. GitHub URL 软件
  20. IP 地址
  21. SSH 协议转换

螺旋共享 - Git

这个 GitUrl 类提供各种功能,用于解析,操纵,并与Git寄存器URL合作,跨越各种格式,包括HTTP/HTTPS,SSH,和SCP风格URL.

安装

npm install @adobe/helix-shared-git

使用量

从字符串分析 Git URL

这个 GitUrl 类可以解析标准 Git URL并提取寄存器信息 :

import { GitUrl } from '@adobe/helix-shared-git';

// Parse a standard Git URL
const url = new GitUrl('https://github.com/adobe/helix-shared.git#main');

console.log(url.protocol);  // 'https'
console.log(url.hostname);  // 'github.com'
console.log(url.owner);     // 'adobe'
console.log(url.repo);      // 'helix-shared'
console.log(url.ref);       // 'main'
console.log(url.path);      // ''

从对象创建 Git URL

您也可以创建 GitUrl 取自对象表示符:

const url = new GitUrl({
  owner: 'adobe',
  repo: 'helix-shared',
  ref: 'main',
  path: '/packages'
});

console.log(url.toString());
// 'https://github.com/adobe/helix-shared.git/packages#main'

使用不同的 Git URL 格式工作

这个 GitUrl 类支持多个 Git URL 格式 :

// Standard HTTPS URL
const httpsUrl = new GitUrl('https://github.com/adobe/helix-shared.git#main');

// SSH URL
const sshUrl = new GitUrl('ssh://[email protected]/adobe/helix-shared.git#main');

// SCP-style Git URL (commonly used with git clone)
const scpUrl = new GitUrl('[email protected]:adobe/helix-shared.git#main');

// Self-hosted Git server with custom port
const customUrl = new GitUrl('https://git.example.com:8080/company/repo.git#develop');

访问原始内容和 API URL

这个 GitUrl class 为访问原始内容和 API 端点提供了方便属性 :

const url = new GitUrl('https://github.com/adobe/helix-shared.git#main');

// Get the raw content URL
console.log(url.raw);
// 'https://raw.githubusercontent.com/adobe/helix-shared/main'

// Get the raw content root URL
console.log(url.rawRoot);
// 'https://raw.githubusercontent.com'

// Get the API root URL
console.log(url.apiRoot);
// 'https://api.github.com'

使用默认

您可以提供默认值, 如果 URL 不指定某些属性, 将使用 :

const url = new GitUrl('https://github.com/adobe/helix-shared.git', {
  ref: 'main',
  path: '/src'
});

console.log(url.ref);   // 'main'
console.log(url.path);  // '/src'

串行和去串行

转换 GitUrl jSON 往来对象 :

const url = new GitUrl('https://github.com/adobe/helix-shared.git#main');

// Convert to JSON
const json = url.toJSON();
console.log(json);
// {
//   protocol: 'https',
//   host: 'github.com',
//   hostname: 'github.com',
//   port: '',
//   owner: 'adobe',
//   repo: 'helix-shared',
//   ref: 'main',
//   path: ''
// }

// Create from JSON
const urlFromJson = new GitUrl(json);
console.log(urlFromJson.toString());
// 'https://github.com/adobe/helix-shared.git#main'

JSON代表人数最少

对于更紧凑的序列化,请使用 minimal 选项 :

const url = new GitUrl({
  owner: 'adobe',
  repo: 'helix-shared',
  ref: 'main'
});

console.log(url.toJSON({ minimal: true }));
// { owner: 'adobe', repo: 'helix-shared', ref: 'main' }

比较 Git URL

比较两个 Git URL 而忽略传输级别差异 :

const url1 = new GitUrl('https://github.com/adobe/helix-shared.git#main');
const url2 = new GitUrl('ssh://[email protected]/adobe/helix-shared.git#main');

console.log(url1.equalsIgnoreTransport(url2));  // true

与地方仓库合作

这个 GitUrl 类对本地寄存器有特殊处理( 专门针对 Helix ):

const localUrl = new GitUrl('https://localhost/local/default.git');

console.log(localUrl.isLocal);  // true

API 参考

属性

  • protocol - 运输协议(例如“https”、“sh”)
  • hostname - 仓库提供者主机名(例如'github.com')
  • host - 有端口的仓库提供者主机(例如'github.com:443')
  • port - 仓库提供者端口
  • owner - 仓库所有人
  • repo - 仓库名称(没有.git扩展名)
  • ref - 仓库参考资料,如分行或标签
  • path - 存储器内的资源路径
  • raw - 存储器的原始内容 URL
  • rawRoot - 原始内容的根 URL
  • apiRoot - API 的根 URL
  • isLocal - 是否是本地的 Helix 仓库

方法

  • toString() - 返回 Git URL 的字符串表达式
  • toJSON(options) - 返回平面物体表示式。 选项 :
    • minimal: 只返回非违约属性
    • keepFormat: 保留原始格式(字符串对对象)
  • toYAMLNode(doc, forceObject) - 返回YAML节点代表
  • equalsIgnoreTransport(other) - 比较忽略协议和认证的 URL

URL 格式

期望的 Git URL 格式是 :

<scheme>://<hostname>[:<port>]/<owner>/<repo>.git[/<path>][#ref]

对于 SCP 风格的 URL :

git@<hostname>:<owner>/<repo>.git[#ref]

所需构成部分

  • owner - 仓库所有人或组织
  • repo - 仓库名称

可选组件

  • protocol - 默认为“ https ”
  • hostname - 默认为"github.com"
  • port - 默认协议的标准端口
  • path - 存储器内的资源路径
  • ref - 分行,标签,或承诺参考。 在原始 URL 中默认为“ master”

特殊处理

GitHub URL 软件

对于 GitHub URL,则 raw 属性自动使用 raw.githubusercontent.com 改为 raw.github.com。 。 。 .

IP 地址

当主机名是IP地址时,原始和API URL构造不同:

const url = new GitUrl('http://127.0.0.1:8080/company/repo.git');

console.log(url.raw);
// 'http://127.0.0.1:8080/raw/company/repo/master'

SSH 协议转换

带有SSH协议的 URL 自动转换为 HTTPS 用于原始和 API URL :

const url = new GitUrl('ssh://[email protected]/adobe/helix-shared.git');

console.log(url.raw);
// 'https://raw.githubusercontent.com/adobe/helix-shared/master'

版本与发布记录

@adobe/helix-shared-git 共发布 42 个版本,首次发布于 2021/04/29;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.26latest2026/09/281 个月前
3.0.252026/05/265 个月前
3.0.242026/05/195 个月前
3.0.232026/04/017 个月前
3.0.222026/04/017 个月前
3.0.212025/12/1610 个月前

关于 @adobe/helix-shared-git 的常见问题

@adobe/helix-shared-git 是什么?

Helix 项目共享模块 - Git Utils

如何安装 @adobe/helix-shared-git?

在项目目录运行 npm install @adobe/helix-shared-git,或使用 pnpm add / yarn add / bun add。

@adobe/helix-shared-git 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/adobe__helix-shared-git 是否存在。

@adobe/helix-shared-git 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adobe/helix-shared-git 可以商用吗?

@adobe/helix-shared-git 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@adobe/helix-shared-git 的下载量有多少?

根据 npm 官方下载统计,@adobe/helix-shared-git 在 2026/10/05 – 2026/11/03 这 30 天内被下载 30,608 次,日均约 1,020 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adobe/helix-shared-git 还在维护吗?

最新版本 3.0.26 发布于 2026/09/28(1 个月前);至今共发布 42 个版本,其中过去 12 个月发布了 6 个;首次发布于 2021/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。