# @adobe/helix-shared-git 中文文档

规范地址：https://npmcn.com/package/%40adobe/helix-shared-git/

版本：3\.0\.26
npm 元数据更新时间：2026-10-01T01:34:19.393Z
中文译文更新时间：2026-11-05T20:13:20.339Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40adobe/helix-shared-git
原文对照：https://npmcn.com/package/%40adobe/helix-shared-git/?lang=original
项目主页：https://github.com/adobe/helix-shared#readme
源码仓库：https://github.com/adobe/helix-shared

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：86.5 kB，6 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- 运行依赖：2 个
- 首次发布：2021-04-29T03:02:25.139Z
- 最新版本发布：2026-09-28T14:58:55.700Z
- 近 30 天下载量：30608（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：42（近 12 个月 6 个）

## 中文简介

Helix 项目共享模块 - Git Utils

## 安装

```sh
npm install @adobe/helix-shared-git
```

也可以使用：`pnpm add @adobe/helix-shared-git`、`yarn add @adobe/helix-shared-git`、`bun add @adobe/helix-shared-git`

## README

# 螺旋共享 - Git

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

## 安装

```bash
npm install @adobe/helix-shared-git
```

## 使用量

### 从字符串分析 Git URL

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

```js
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` 取自对象表示符:

```js
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 格式 :

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

// SSH URL
const sshUrl = new GitUrl('ssh://git@github.com/adobe/helix-shared.git#main');

// SCP-style Git URL (commonly used with git clone)
const scpUrl = new GitUrl('git@github.com: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 端点提供了方便属性 :

```js
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 不指定某些属性, 将使用 :

```js
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 往来对象 :

```js
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` 选项 :

```js
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 而忽略传输级别差异 :

```js
const url1 = new GitUrl('https://github.com/adobe/helix-shared.git#main');
const url2 = new GitUrl('ssh://git@github.com/adobe/helix-shared.git#main');

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

### 与地方仓库合作

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

```js
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构造不同:

```js
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 :

```js
const url = new GitUrl('ssh://git@github.com/adobe/helix-shared.git');

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

## 常见问题

### @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 和提交记录判断。

