螺旋共享 - 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- 存储器的原始内容 URLrawRoot- 原始内容的根 URLapiRoot- API 的根 URLisLocal- 是否是本地的 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'