Access and manipulate repository metadata including host type, user, project name, and commit references from GitHost instances.
Access repository metadata through GitHost instance properties.
/**
* GitHost instance properties providing repository metadata
*/
interface GitHost {
/** Host type: 'github', 'gitlab', 'bitbucket', 'gist', 'sourcehut' */
type: string;
/** Host domain (e.g., 'github.com', 'gitlab.com') */
domain: string;
/** User or organization name */
user: string;
/** Repository/project name */
project: string;
/** Commit, branch, or tag reference */
committish: string | null;
/** Authentication string (username:password or token) */
auth: string | null;
/** Default representation type ('shortcut', 'sshurl', 'https', etc.) */
default: string;
/** Options object passed during creation */
opts: object;
}Usage Examples:
const hostedGitInfo = require("hosted-git-info");
// Parse various URL formats
const info1 = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git#v1.0.0");
const info2 = hostedGitInfo.fromUrl("https://gitlab.com/user/project.git");
const info3 = hostedGitInfo.fromUrl("gist:abc123def456");
// Access repository metadata
console.log(info1.type); // "github"
console.log(info1.domain); // "github.com"
console.log(info1.user); // "npm"
console.log(info1.project); // "hosted-git-info"
console.log(info1.committish); // "v1.0.0"
console.log(info1.auth); // null
console.log(info2.type); // "gitlab"
console.log(info2.domain); // "gitlab.com"
console.log(info2.user); // "user"
console.log(info2.project); // "project"
console.log(info2.committish); // null
console.log(info3.type); // "gist"
console.log(info3.domain); // "gist.github.com"
console.log(info3.user); // null (for anonymous gists)
console.log(info3.project); // "abc123def456"Identify the hosting platform from the parsed URL.
const info = hostedGitInfo.fromUrl("https://bitbucket.org/user/repo.git");
switch (info.type) {
case 'github':
console.log('GitHub repository');
break;
case 'gitlab':
console.log('GitLab repository');
break;
case 'bitbucket':
console.log('Bitbucket repository');
break;
case 'gist':
console.log('GitHub Gist');
break;
case 'sourcehut':
console.log('Sourcehut repository');
break;
default:
console.log('Unknown host type');
}
// Output: "Bitbucket repository"Access authentication credentials embedded in URLs.
// URL with authentication
const info1 = hostedGitInfo.fromUrl("https://user:token@github.com/owner/repo.git");
console.log(info1.auth); // "user:token"
// URL with username only
const info2 = hostedGitInfo.fromUrl("https://user@github.com/owner/repo.git");
console.log(info2.auth); // "user"
// SSH URLs and shortcuts ignore auth in the URL
const info3 = hostedGitInfo.fromUrl("github:user:password@owner/repo");
console.log(info3.auth); // null (auth ignored for shortcuts)
// No authentication
const info4 = hostedGitInfo.fromUrl("https://github.com/owner/repo.git");
console.log(info4.auth); // nullWork with commit, branch, and tag references.
// URL with branch reference
const info1 = hostedGitInfo.fromUrl("github:npm/hosted-git-info#main");
console.log(info1.committish); // "main"
// URL with tag reference
const info2 = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git#v1.0.0");
console.log(info2.committish); // "v1.0.0"
// URL with commit hash
const info3 = hostedGitInfo.fromUrl("https://github.com/npm/hosted-git-info.git#abc123def456");
console.log(info3.committish); // "abc123def456"
// No committish
const info4 = hostedGitInfo.fromUrl("github:npm/hosted-git-info");
console.log(info4.committish); // null
// Get hash fragment
console.log(info1.hash()); // "#main"
console.log(info4.hash()); // ""Understand how the GitHost will represent itself by default.
// Different URL formats have different defaults
const shortcut = hostedGitInfo.fromUrl("github:npm/hosted-git-info");
console.log(shortcut.getDefaultRepresentation()); // "shortcut"
console.log(shortcut.toString()); // "github:npm/hosted-git-info"
const ssh = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git");
console.log(ssh.getDefaultRepresentation()); // "sshurl"
console.log(ssh.toString()); // "git+ssh://git@github.com/npm/hosted-git-info.git"
const https = hostedGitInfo.fromUrl("https://github.com/npm/hosted-git-info.git");
console.log(https.getDefaultRepresentation()); // "https"
console.log(https.toString()); // "git+https://github.com/npm/hosted-git-info.git"
const extreme = hostedGitInfo.fromUrl("npm/hosted-git-info");
console.log(extreme.getDefaultRepresentation()); // "shortcut"
console.log(extreme.toString()); // "github:npm/hosted-git-info"Access the options that were passed during GitHost creation.
const info = hostedGitInfo.fromUrl("github:npm/hosted-git-info#v1.0.0", {
noCommittish: true,
noGitPlus: true,
customOption: "value"
});
console.log(info.opts);
// Result: { noCommittish: true, noGitPlus: true, customOption: "value" }
// Options affect URL generation
console.log(info.https()); // Uses opts.noGitPlus: true
// Result: "https://github.com/npm/hosted-git-info.git" (no git+ prefix)github.comuser/repogitlab.comgroup/subgroupbitbucket.orggist.github.comgit.sr.ht~usernameundefinedfromUrl()nullcommittishnullauthnullusernullnullconst info1 = hostedGitInfo.fromUrl("github:npm/hosted-git-info#v1.0.0");
const info2 = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git#v2.0.0");
// Same repository, different versions
const sameRepo = info1.type === info2.type &&
info1.user === info2.user &&
info1.project === info2.project;
console.log(sameRepo); // true
const sameVersion = info1.committish === info2.committish;
console.log(sameVersion); // falseconst info = hostedGitInfo.fromUrl("https://gitlab.com/group/subgroup/project.git#main");
// Extract repository identifier
const repoId = `${info.type}:${info.user}/${info.project}`;
console.log(repoId); // "gitlab:group/subgroup/project"
// Create clone command
const cloneCmd = `git clone ${info.https()}`;
console.log(cloneCmd); // "git clone git+https://gitlab.com/group/subgroup/project.git#main"
// Get web URL for sharing
const webUrl = info.browse();
console.log(webUrl); // "https://gitlab.com/group/subgroup/project/tree/main"