@adobe/helix-shared-git

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

Shared modules of the Helix Project - Git Utils

原始使用文档
文档目录21 节
  1. Installation
  2. Usage
  3. Parsing Git URLs from Strings
  4. Creating Git URLs from Objects
  5. Working with Different Git URL Formats
  6. Accessing Raw Content and API URLs
  7. Using Defaults
  8. Serialization and Deserialization
  9. Minimal JSON Representation
  10. Comparing Git URLs
  11. Working with Local Repositories
  12. API Reference
  13. Properties
  14. Methods
  15. URL Format
  16. Required Components
  17. Optional Components
  18. Special Handling
  19. GitHub URLs
  20. IP Addresses
  21. SSH Protocol Conversion

Helix Shared - Git

The GitUrl class provides utilities for parsing, manipulating, and working with Git repository URLs across various formats including HTTP/HTTPS, SSH, and SCP-style URLs.

Installation

npm install @adobe/helix-shared-git

Usage

Parsing Git URLs from Strings

The GitUrl class can parse standard Git URLs and extract repository information:

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);      // ''

Creating Git URLs from Objects

You can also create a GitUrl from an object representation:

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'

Working with Different Git URL Formats

The GitUrl class supports multiple Git URL formats:

// 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');

Accessing Raw Content and API URLs

The GitUrl class provides convenient properties for accessing raw content and API endpoints:

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'

Using Defaults

You can provide default values that will be used if the URL doesn't specify certain properties:

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'

Serialization and Deserialization

Convert GitUrl objects to and from 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'

Minimal JSON Representation

For more compact serialization, use the minimal option:

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

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

Comparing Git URLs

Compare two Git URLs while ignoring transport-level differences:

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

Working with Local Repositories

The GitUrl class has special handling for local repositories (specific to Helix):

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

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

API Reference

Properties

  • protocol - Transport protocol (e.g., 'https', 'ssh')
  • hostname - Repository provider hostname (e.g., 'github.com')
  • host - Repository provider host with port (e.g., 'github.com:443')
  • port - Repository provider port
  • owner - Repository owner
  • repo - Repository name (without .git extension)
  • ref - Repository reference, such as a branch or tag
  • path - Resource path within the repository
  • raw - Raw content URL for the repository
  • rawRoot - Root URL for raw content
  • apiRoot - Root URL for the API
  • isLocal - Whether this is a local Helix repository

Methods

  • toString() - Returns the string representation of the Git URL
  • toJSON(options) - Returns a plain object representation. Options:
    • minimal: Returns only non-default properties
    • keepFormat: Preserves the original format (string vs object)
  • toYAMLNode(doc, forceObject) - Returns a YAML node representation
  • equalsIgnoreTransport(other) - Compares URLs ignoring protocol and authentication

URL Format

The expected Git URL format is:

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

For SCP-style URLs:

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

Required Components

  • owner - The repository owner or organization
  • repo - The repository name

Optional Components

  • protocol - Defaults to 'https'
  • hostname - Defaults to 'github.com'
  • port - Defaults to standard port for the protocol
  • path - Resource path within the repository
  • ref - Branch, tag, or commit reference. Defaults to 'master' in raw URLs

Special Handling

GitHub URLs

For GitHub URLs, the raw property automatically uses raw.githubusercontent.com instead of raw.github.com.

IP Addresses

When the hostname is an IP address, the raw and API URLs are constructed differently:

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 Protocol Conversion

URLs with SSH protocol automatically convert to HTTPS for raw and API URLs:

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 是什么?

Shared modules of the Helix Project - 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 和提交记录判断。