Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for +git protocol suffix in npm paths #8657

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<!-- -->
Please add one entry in this file for each change in Yarn's behavior. Use the same format for all entries, including the third-person verb. Make sure you don't add more than one line of text to keep it clean. Thanks!

- Protocols with +git (e.g. ssh+git) will be properly handled for npm urls

[#8657](https://github.com/yarnpkg/yarn/pull/8657) - [**Patryk Musiala**](https://github.com/pmusiala)

## 1.22.10 (and prior)

- Tweak the preinstall check to not cause errors when Node is installed as root (as a downside, it won't run at all on Windows, which should be an acceptable tradeoff): https://github.com/yarnpkg/yarn/issues/8358
Expand Down Expand Up @@ -63,15 +67,15 @@ Those versions didn't contain any changes and were just triggered by our infra w
- Allows some dots in binary names again

[#7811](https://github.com/yarnpkg/yarn/pull/7811) - [**Valery Bugakov**](https://github.com/valerybugakov)

- Better error handling on `yarn set version`

[#7848](https://github.com/yarnpkg/yarn/pull/7848) - [**Nick Olinger**](https://github.com/olingern)

- Passes arguments following `--` when running a workspace script (`yarn workspace pkg run command -- arg`)

[#7776](https://github.com/yarnpkg/yarn/pull/7776) - [**Jeff Valore**](https://twitter.com/rally25rs)

- Fixes an issue where the archive paths were incorrectly sanitized

[#7831](https://github.com/yarnpkg/yarn/pull/7831) - [**Maël Nison**](https://twitter.com/arcanis)
Expand All @@ -93,7 +97,7 @@ Those versions didn't contain any changes and were just triggered by our infra w
- Implements `yarn init --install <version>`

[#7723](https://github.com/yarnpkg/yarn/pull/7723) - [**Maël Nison**](https://twitter.com/arcanis)

## 1.19.2

- Folders like `.cache` won't be pruned from the `node_modules` after each install.
Expand Down
37 changes: 36 additions & 1 deletion __tests__/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ test('npmUrlToGitUrl', () => {
hostname: 'github.com',
repository: 'https://github.com/npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('https+git://github.com/npm-opam/ocamlfind.git')).toEqual({
protocol: 'https:',
hostname: 'github.com',
repository: 'https://github.com/npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('https://github.com/npm-opam/ocamlfind.git')).toEqual({
protocol: 'https:',
hostname: 'github.com',
Expand All @@ -44,21 +49,41 @@ test('npmUrlToGitUrl', () => {
hostname: 'gitlab.mydomain.tld',
repository: 'ssh://[email protected]:10202/project-name/my-package.git',
});
expect(Git.npmUrlToGitUrl('ssh+git://[email protected]:10202/project-name/my-package.git')).toEqual({
protocol: 'ssh:',
hostname: 'gitlab.mydomain.tld',
repository: 'ssh://[email protected]:10202/project-name/my-package.git',
});
expect(Git.npmUrlToGitUrl('git+ssh://[email protected]/npm-opam/ocamlfind.git')).toEqual({
protocol: 'ssh:',
hostname: 'github.com',
repository: 'ssh://[email protected]/npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('ssh+git://[email protected]/npm-opam/ocamlfind.git')).toEqual({
protocol: 'ssh:',
hostname: 'github.com',
repository: 'ssh://[email protected]/npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('git+ssh://scp-host-nickname:npm-opam/ocamlfind.git')).toEqual({
protocol: 'ssh:',
hostname: 'scp-host-nickname',
repository: 'scp-host-nickname:npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('ssh+git://scp-host-nickname:npm-opam/ocamlfind.git')).toEqual({
protocol: 'ssh:',
hostname: 'scp-host-nickname',
repository: 'scp-host-nickname:npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('git+ssh://user@scp-host-nickname:npm-opam/ocamlfind.git')).toEqual({
protocol: 'ssh:',
hostname: 'scp-host-nickname',
repository: 'user@scp-host-nickname:npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('ssh+git://user@scp-host-nickname:npm-opam/ocamlfind.git')).toEqual({
protocol: 'ssh:',
hostname: 'scp-host-nickname',
repository: 'user@scp-host-nickname:npm-opam/ocamlfind.git',
});
expect(Git.npmUrlToGitUrl('github:npm-opam/ocamlfind.git#v1.2.3')).toEqual({
protocol: 'ssh:',
hostname: 'github.com',
Expand All @@ -84,11 +109,21 @@ test('npmUrlToGitUrl', () => {
hostname: null,
repository: '../ocalmfind.git',
});
expect(Git.npmUrlToGitUrl('file+git:../ocalmfind.git')).toEqual({
protocol: 'file:',
hostname: null,
repository: '../ocalmfind.git',
});
expect(Git.npmUrlToGitUrl('git+file:../ocalmfind')).toEqual({
protocol: 'file:',
hostname: null,
repository: '../ocalmfind',
});
expect(Git.npmUrlToGitUrl('file+git:../ocalmfind')).toEqual({
protocol: 'file:',
hostname: null,
repository: '../ocalmfind',
});
});

describe('secureGitUrl()', function() {
Expand Down Expand Up @@ -147,7 +182,7 @@ describe('secureGitUrl()', function() {
{
type: 'warning',
error: true,
data: reporter.lang(protocol == 'git:' ? 'downloadGitWithoutCommit' : 'downloadHTTPWithoutCommit', inputurl),
data: reporter.lang(protocol === 'git:' ? 'downloadGitWithoutCommit' : 'downloadHTTPWithoutCommit', inputurl),
},
]);
}
Expand Down
7 changes: 3 additions & 4 deletions src/util/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ import {resolveVersion, isCommitSha, parseRefs} from './git/git-ref-resolver.js'
import * as crypto from './crypto.js';
import * as fs from './fs.js';
import map from './map.js';
import {removePrefix} from './misc.js';

const GIT_PROTOCOL_PREFIX = 'git+';
const GIT_PROTOCOL_FRAGMENT = /(git\+)|(\+git)/;
const SSH_PROTOCOL = 'ssh:';
const SCP_PATH_PREFIX = '/:';
const FILE_PROTOCOL = 'file:';
Expand Down Expand Up @@ -87,11 +86,11 @@ export default class Git implements GitRefResolvingInterface {
gitUrl: GitUrl;

/**
* npm URLs contain a 'git+' scheme prefix, which is not understood by git.
* npm URLs contain a 'git+' scheme prefix or '+git' scheme suffix, which is not understood by git.
* git "URLs" also allow an alternative scp-like syntax, so they're not standard URLs.
*/
static npmUrlToGitUrl(npmUrl: string): GitUrl {
npmUrl = removePrefix(npmUrl, GIT_PROTOCOL_PREFIX);
npmUrl = npmUrl.replace(GIT_PROTOCOL_FRAGMENT, '');

let parsed = url.parse(npmUrl);
const expander = parsed.protocol && SHORTHAND_SERVICES[parsed.protocol];
Expand Down