Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

hosted-git-info

npm158.2mISC8.0.2TypeScript support: definitely-typed

Provides metadata and conversions from repository urls for GitHub, Bitbucket and GitLab

git, github, bitbucket, gitlab

readme

hosted-git-info

This will let you identify and transform various git hosts URLs between protocols. It also can tell you what the URL is for the raw path for particular file for direct access without git.

Example

const hostedGitInfo = require("hosted-git-info")
const info = hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git", opts)
/* info looks like:
{
  type: "github",
  domain: "github.com",
  user: "npm",
  project: "hosted-git-info"
}
*/

If the URL can't be matched with a git host, null will be returned. We can match git, ssh and https urls. Additionally, we can match ssh connect strings (`git@github.com:npm/hosted-git-info) and shortcuts (eg,github:npm/hosted-git-info). GitHub specifically, is detected in the case of a third, unprefixed, form:npm/hosted-git-info`.

If it does match, the returned object has properties of:

  • info.type -- The short name of the service
  • info.domain -- The domain for git protocol use
  • info.user -- The name of the user/org on the git host
  • info.project -- The name of the project on the git host

Version Contract

The major version will be bumped any time…

  • The constructor stops accepting URLs that it previously accepted.
  • A method is removed.
  • A method can no longer accept the number and type of arguments it previously accepted.
  • A method can return a different type than it currently returns.

Implications:

  • I do not consider the specific format of the urls returned from, say .https() to be a part of the contract. The contract is that it will return a string that can be used to fetch the repo via HTTPS. But what that string looks like, specifically, can change.
  • Dropping support for a hosted git provider would constitute a breaking change.

Usage

const info = hostedGitInfo.fromUrl(gitSpecifier[, options])

  • gitSpecifer is a URL of a git repository or a SCP-style specifier of one.
  • options is an optional object. It can have the following properties:
    • noCommittish — If true then committishes won't be included in generated URLs.
    • noGitPlus — If true then git+ won't be prefixed on URLs.

Methods

All of the methods take the same options as the fromUrl factory. Options provided to a method override those provided to the constructor.

  • info.file(path, opts)

Given the path of a file relative to the repository, returns a URL for directly fetching it from the githost. If no committish was set then HEAD will be used as the default.

For example hostedGitInfo.fromUrl("git@github.com:npm/hosted-git-info.git#v1.0.0").file("package.json") would return https://raw.githubusercontent.com/npm/hosted-git-info/v1.0.0/package.json

  • info.shortcut(opts)

eg, github:npm/hosted-git-info

  • info.browse(path, fragment, opts)

eg, https://github.com/npm/hosted-git-info/tree/v1.2.0, https://github.com/npm/hosted-git-info/tree/v1.2.0/package.json, https://github.com/npm/hosted-git-info/tree/v1.2.0/README.md#supported-hosts

  • info.bugs(opts)

eg, https://github.com/npm/hosted-git-info/issues

  • info.docs(opts)

eg, https://github.com/npm/hosted-git-info/tree/v1.2.0#readme

  • info.https(opts)

eg, git+https://github.com/npm/hosted-git-info.git

  • info.sshurl(opts)

eg, git+ssh://git@github.com/npm/hosted-git-info.git

  • info.ssh(opts)

eg, `git@github.com:npm/hosted-git-info.git`

  • info.path(opts)

eg, npm/hosted-git-info

  • info.tarball(opts)

eg, https://github.com/npm/hosted-git-info/archive/v1.2.0.tar.gz

  • info.getDefaultRepresentation()

Returns the default output type. The default output type is based on the string you passed in to be parsed

  • info.toString(opts)

Uses the getDefaultRepresentation to call one of the other methods to get a URL for this resource. As such hostedGitInfo.fromUrl(url).toString() will give you a normalized version of the URL that still uses the same protocol.

Shortcuts will still be returned as shortcuts, but the special case github form of org/project will be normalized to github:org/project.

SSH connect strings will be normalized into git+ssh URLs.

Supported hosts

Currently this supports GitHub (including Gists), Bitbucket, GitLab and Sourcehut. Pull requests for additional hosts welcome.

changelog

Changelog

8.0.2 (2024-11-21)

Bug Fixes

  • cc004ba #280 even better regex for host fragment (#280) (@wraithgar)

8.0.1 (2024-11-20)

Bug Fixes

  • e47b7e4 #274 break up greedy host fragment parsing regex (#274) (@wraithgar)

    Chores

  • 3d55d13 #277 fix workflows for new backport branch (#277) (@wraithgar)
  • b3e455f #273 bump @npmcli/template-oss from 4.23.3 to 4.23.4 (#273) (@dependabot[bot], @npm-cli-bot)

8.0.0 (2024-09-03)

⚠️ BREAKING CHANGES

  • hosted-git-info now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • 967d930 #268 align to npm 10 node engine range (@hashtagchris)

    Chores

  • 20551b0 #268 run template-oss-apply (@hashtagchris)
  • 9a3c062 #265 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • 8f0fa04 #266 postinstall for dependabot template-oss PR (@hashtagchris)
  • e0fe523 #266 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])

7.0.2 (2024-05-04)

Bug Fixes

Chores

  • f33287c #249 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • 7bbdfd8 #248 chore: postinstall for dependabot template-oss PR (@lukekarrys)
  • 0d4310e #249 postinstall for dependabot template-oss PR (@lukekarrys)
  • 2efc69b #248 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

7.0.1 (2023-09-13)

Bug Fixes

  • d7bac33 #213 remove sourcehut bugstemplate (#213) (@vladh)

7.0.0 (2023-08-14)

⚠️ BREAKING CHANGES

  • support for node 14 has been removed

Bug Fixes

Dependencies

6.1.1 (2022-10-27)

Bug Fixes

  • f03bfbd #176 only correct protocols when called from githost (@lukekarrys)

6.1.0 (2022-10-26)

Features

  • a44bd35 #172 add separate static method for just parsing urls (@lukekarrys)

6.0.0 (2022-10-12)

⚠️ BREAKING CHANGES

  • GitHost now has a static addHost method to use instead of manually editing the object from lib/git-host-info.js.
  • set default git ref to HEAD
  • hosted-git-info is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • 9e0ce62 #142 refactor (@lukekarrys)
  • 89155e8 set default git ref to HEAD (@darcyclarke)
  • 9ed9c38 #162 postinstall for dependabot template-oss PR (@lukekarrys)

Bug Fixes

  • 61ca7fb #152 parse branch names containing @ (@lukekarrys)
  • 3cd4a98 ignore colons after hash when correcting scp urls (@lukekarrys)

5.1.0 (2022-08-09)

Features

  • add method to get an edit link to a file (ad02952)

Bug Fixes

  • add comments to empty catch blocks for linting (70a770d)

5.0.0 (2022-03-14)

⚠ BREAKING CHANGES

  • this drops support for node 10 and non-LTS versions of node 12 and node 14

Bug Fixes

Dependencies

4.0.0 (2021-03-09)

Features

  • rewrite the entire module: all internals have been rewritten to maintain a similar contract but to remove excessive use of regular expressions, unnecessary loops, the custom string templating engine, and various other bits of complexity (c218b9)

BREAKING CHANGES

  • extending with custom providers has changed (c218b9)

3.0.8 (2021-01-28)

Bug Fixes

  • simplify the regular expression for shortcut matching (bede0dc), closes #76

3.0.7 (2020-10-15)

Bug Fixes

  • correctly filter out urls for tarballs in gitlab (eb5bd5a), closes #69

3.0.6 (2020-10-12)

Bug Fixes

  • support to github gist legacy hash length (c067102), closes #68

3.0.5 (2020-07-11)

3.0.4 (2020-02-26)

Bug Fixes

  • Do not pass scp-style URLs to the WhatWG url.URL (0835306), closes #60 #63

3.0.3 (2020-02-25)

3.0.2 (2019-10-08)

Bug Fixes

  • do not encodeURIComponent the domain (3e5fbec), closes #53

3.0.1 (2019-10-07)

Bug Fixes

  • update pathmatch for gitlab (e3e3054), closes #52
  • updated pathmatch for gitlab (fa87af7)

3.0.0 (2019-08-12)

Bug Fixes

  • cache: Switch to lru-cache to save ourselves from unlimited memory consumption (37c2891), closes #38

BREAKING CHANGES

  • cache: Drop support for node 0.x

2.8.3 (2019-08-12)

2.8.2 (2019-08-05)

Bug Fixes

  • http protocol use sshurl by default (3b1d629), closes #48

2.8.1 (2019-08-05)

Bug Fixes

  • ignore noCommittish on tarball url generation (5d4a8d7)
  • use gist tarball url that works for anonymous gists (1692435)

2.8.0 (2019-08-05)

Bug Fixes

  • Allow slashes in gitlab project section (bbcf7b2), closes #46 #43
  • git-host: disallow URI-encoded slash (%2F) in path (3776fa5), closes #44
  • gitlab: Do not URL encode slashes in project name for GitLab https URL (cbf04f9), closes #47
  • do not allow invalid gist urls (d5cf830)
  • cache: Switch to lru-cache to save ourselves from unlimited memory consumption (e518222), closes #38

Features

  • give these objects a name (60abaea)

2.7.1 (2018-07-07)

Bug Fixes

  • index: Guard against non-string types (5bc580d)
  • parse: Crash on strings that parse to having no host (c931482), closes #35

2.7.0 (2018-07-06)

Bug Fixes

  • github tarball: update github tarballtemplate (6efd582), closes #34
  • gitlab docs: switched to lowercase anchors for readmes (701bcd1)

Features

  • all: Support www. prefixes on hostnames (3349575), closes #32

2.6.1 (2018-06-25)

Bug Fixes

  • Revert: "compat: remove Object.assign fallback (#25)" (cce5a62)
  • Revert: "git-host: fix forgotten extend()" (a815ec9)

2.6.0 (2018-03-07)

Bug Fixes

  • compat: remove Object.assign fallback (#25) (627ab55)
  • git-host: fix forgotten extend() (eba1f7b)

Features

  • browse: fragment support for browse() (#28) (cd5e5bb)