Skip to content

Commit 77ddd00

Browse files
committed
BREAKING CHANGE: links generated from git urls will now use `HEAD` instead of `master` as the default ref
1 parent 6c5c2a8 commit 77ddd00

File tree

18 files changed

+1565
-227
lines changed

18 files changed

+1565
-227
lines changed

node_modules/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,16 @@
157157
!/node-gyp/node_modules/nopt
158158
!/nopt
159159
!/normalize-package-data
160+
!/normalize-package-data/node_modules/
161+
/normalize-package-data/node_modules/*
162+
!/normalize-package-data/node_modules/hosted-git-info
160163
!/npm-audit-report
161164
!/npm-install-checks
162165
!/npm-normalize-package-bin
163166
!/npm-package-arg
167+
!/npm-package-arg/node_modules/
168+
/npm-package-arg/node_modules/*
169+
!/npm-package-arg/node_modules/hosted-git-info
164170
!/npm-packlist
165171
!/npm-pick-manifest
166172
!/npm-pick-manifest/node_modules/
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
'use strict'
2+
3+
const url = require('url')
4+
5+
const safeUrl = (u) => {
6+
try {
7+
return new url.URL(u)
8+
} catch {
9+
// this fn should never throw
10+
}
11+
}
12+
13+
const lastIndexOfBefore = (str, char, beforeChar) => {
14+
const startPosition = str.indexOf(beforeChar)
15+
return str.lastIndexOf(char, startPosition > -1 ? startPosition : Infinity)
16+
}
17+
18+
// accepts input like git:github.com:user/repo and inserts the // after the first :
19+
const correctProtocol = (arg, protocols) => {
20+
const firstColon = arg.indexOf(':')
21+
const proto = arg.slice(0, firstColon + 1)
22+
if (Object.prototype.hasOwnProperty.call(protocols, proto)) {
23+
return arg
24+
}
25+
26+
const firstAt = arg.indexOf('@')
27+
if (firstAt > -1) {
28+
if (firstAt > firstColon) {
29+
return `git+ssh://${arg}`
30+
} else {
31+
return arg
32+
}
33+
}
34+
35+
const doubleSlash = arg.indexOf('//')
36+
if (doubleSlash === firstColon + 1) {
37+
return arg
38+
}
39+
40+
return `${arg.slice(0, firstColon + 1)}//${arg.slice(firstColon + 1)}`
41+
}
42+
43+
// look for github shorthand inputs, such as npm/cli
44+
const isGitHubShorthand = (arg) => {
45+
// it cannot contain whitespace before the first #
46+
// it cannot start with a / because that's probably an absolute file path
47+
// but it must include a slash since repos are username/repository
48+
// it cannot start with a . because that's probably a relative file path
49+
// it cannot start with an @ because that's a scoped package if it passes the other tests
50+
// it cannot contain a : before a # because that tells us that there's a protocol
51+
// a second / may not exist before a #
52+
const firstHash = arg.indexOf('#')
53+
const firstSlash = arg.indexOf('/')
54+
const secondSlash = arg.indexOf('/', firstSlash + 1)
55+
const firstColon = arg.indexOf(':')
56+
const firstSpace = /\s/.exec(arg)
57+
const firstAt = arg.indexOf('@')
58+
59+
const spaceOnlyAfterHash = !firstSpace || (firstHash > -1 && firstSpace.index > firstHash)
60+
const atOnlyAfterHash = firstAt === -1 || (firstHash > -1 && firstAt > firstHash)
61+
const colonOnlyAfterHash = firstColon === -1 || (firstHash > -1 && firstColon > firstHash)
62+
const secondSlashOnlyAfterHash = secondSlash === -1 || (firstHash > -1 && secondSlash > firstHash)
63+
const hasSlash = firstSlash > 0
64+
// if a # is found, what we really want to know is that the character
65+
// immediately before # is not a /
66+
const doesNotEndWithSlash = firstHash > -1 ? arg[firstHash - 1] !== '/' : !arg.endsWith('/')
67+
const doesNotStartWithDot = !arg.startsWith('.')
68+
69+
return spaceOnlyAfterHash && hasSlash && doesNotEndWithSlash &&
70+
doesNotStartWithDot && atOnlyAfterHash && colonOnlyAfterHash &&
71+
secondSlashOnlyAfterHash
72+
}
73+
74+
// attempt to correct an scp style url so that it will parse with `new URL()`
75+
const correctUrl = (giturl) => {
76+
// ignore @ that come after the first hash since the denotes the start
77+
// of a committish which can contain @ characters
78+
const firstAt = lastIndexOfBefore(giturl, '@', '#')
79+
// ignore colons that come after the hash since that could include colons such as:
80+
// git@github.com:user/package-2#semver:^1.0.0
81+
const lastColonBeforeHash = lastIndexOfBefore(giturl, ':', '#')
82+
83+
if (lastColonBeforeHash > firstAt) {
84+
// the last : comes after the first @ (or there is no @)
85+
// like it would in:
86+
// proto://hostname.com:user/repo
87+
// username@hostname.com:user/repo
88+
// :password@hostname.com:user/repo
89+
// username:password@hostname.com:user/repo
90+
// proto://username@hostname.com:user/repo
91+
// proto://:password@hostname.com:user/repo
92+
// proto://username:password@hostname.com:user/repo
93+
// then we replace the last : with a / to create a valid path
94+
giturl = giturl.slice(0, lastColonBeforeHash) + '/' + giturl.slice(lastColonBeforeHash + 1)
95+
}
96+
97+
if (lastIndexOfBefore(giturl, ':', '#') === -1 && giturl.indexOf('//') === -1) {
98+
// we have no : at all
99+
// as it would be in:
100+
// username@hostname.com/user/repo
101+
// then we prepend a protocol
102+
giturl = `git+ssh://${giturl}`
103+
}
104+
105+
return giturl
106+
}
107+
108+
module.exports = (giturl, opts, { gitHosts, protocols }) => {
109+
if (!giturl) {
110+
return
111+
}
112+
113+
const correctedUrl = isGitHubShorthand(giturl)
114+
? `github:${giturl}`
115+
: correctProtocol(giturl, protocols)
116+
const parsed = safeUrl(correctedUrl) || safeUrl(correctUrl(correctedUrl))
117+
if (!parsed) {
118+
return
119+
}
120+
121+
const gitHostShortcut = gitHosts.byShortcut[parsed.protocol]
122+
const gitHostDomain = gitHosts.byDomain[parsed.hostname.startsWith('www.')
123+
? parsed.hostname.slice(4)
124+
: parsed.hostname]
125+
const gitHostName = gitHostShortcut || gitHostDomain
126+
if (!gitHostName) {
127+
return
128+
}
129+
130+
const gitHostInfo = gitHosts[gitHostShortcut || gitHostDomain]
131+
let auth = null
132+
if (protocols[parsed.protocol]?.auth && (parsed.username || parsed.password)) {
133+
auth = `${parsed.username}${parsed.password ? ':' + parsed.password : ''}`
134+
}
135+
136+
let committish = null
137+
let user = null
138+
let project = null
139+
let defaultRepresentation = null
140+
141+
try {
142+
if (gitHostShortcut) {
143+
let pathname = parsed.pathname.startsWith('/') ? parsed.pathname.slice(1) : parsed.pathname
144+
const firstAt = pathname.indexOf('@')
145+
// we ignore auth for shortcuts, so just trim it out
146+
if (firstAt > -1) {
147+
pathname = pathname.slice(firstAt + 1)
148+
}
149+
150+
const lastSlash = pathname.lastIndexOf('/')
151+
if (lastSlash > -1) {
152+
user = decodeURIComponent(pathname.slice(0, lastSlash))
153+
// we want nulls only, never empty strings
154+
if (!user) {
155+
user = null
156+
}
157+
project = decodeURIComponent(pathname.slice(lastSlash + 1))
158+
} else {
159+
project = decodeURIComponent(pathname)
160+
}
161+
162+
if (project.endsWith('.git')) {
163+
project = project.slice(0, -4)
164+
}
165+
166+
if (parsed.hash) {
167+
committish = decodeURIComponent(parsed.hash.slice(1))
168+
}
169+
170+
defaultRepresentation = 'shortcut'
171+
} else {
172+
if (!gitHostInfo.protocols.includes(parsed.protocol)) {
173+
return
174+
}
175+
176+
const segments = gitHostInfo.extract(parsed)
177+
if (!segments) {
178+
return
179+
}
180+
181+
user = segments.user && decodeURIComponent(segments.user)
182+
project = decodeURIComponent(segments.project)
183+
committish = decodeURIComponent(segments.committish)
184+
defaultRepresentation = protocols[parsed.protocol]?.name || parsed.protocol.slice(0, -1)
185+
}
186+
} catch (err) {
187+
/* istanbul ignore else */
188+
if (err instanceof URIError) {
189+
return
190+
} else {
191+
throw err
192+
}
193+
}
194+
195+
return [gitHostName, user, auth, project, committish, defaultRepresentation, opts]
196+
}

0 commit comments

Comments
 (0)