2018-10-11 admin


什么是mos-hosted-git-info,Provides metadata and conversions from repository urls for Github, Bitbucket and Gitlab



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.


var hostedGitInfo = require("hosted-git-info")
var info = hostedGitInfo.fromUrl("")
/* info looks like:
  type: "github",
  domain: "",
  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 ( 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

And methods of:

  • info.file(path)

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 master will be used as the default.

For example hostedGitInfo.fromUrl("").file("package.json") would return

  • info.shortcut()

eg, github:npm/hosted-git-info

  • info.browse()


  • info.bugs()




  • info.https()

eg, git+

  • info.sshurl()

eg, git+ssh://

  • info.ssh()


  • info.path()

eg, npm/hosted-git-info

  • info.getDefaultRepresentation()

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

  • info.toString()

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, Bitbucket and Gitlab. Pull requests for additional hosts welcome.


转载请注明:文章转载自 JavaScript中文网 []