Skip to content
This repository was archived by the owner on Aug 11, 2022. It is now read-only.

Commit 39dea9c

Browse files
gagerniarna
authored andcommitted
add-remote-git: Add support for git submodules in git remotes
This is a fairly simple approach, which does not leverage the git caching mechansim to cache submodules. It also doesn't provide a means to disable automatic initialization, e.g. via a setting in the .gitmodules file. This also adds documentation for the git+file protocol, since we have a test case ensuring that this works. We have no test case for the git+rsync protocol, even though npm-package-arg seems to have support for that as well, so that wasn't added to the documentation. PR-URL: #11094 Credit: @gagern Reviewed-By: @othiym23 Fixes: #1876
1 parent 6803fed commit 39dea9c

File tree

3 files changed

+169
-3
lines changed

3 files changed

+169
-3
lines changed

doc/cli/npm-install.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,12 @@ after packing it up into a tarball (b).
169169

170170
<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish>]
171171

172-
`<protocol>` is one of `git`, `git+ssh`, `git+http`, or
173-
`git+https`. If no `<commit-ish>` is specified, then `master` is
174-
used.
172+
`<protocol>` is one of `git`, `git+ssh`, `git+http`, `git+https`,
173+
or `git+file`.
174+
If no `<commit-ish>` is specified, then `master` is used.
175+
176+
If the repository makes use of submodules, those submodules will
177+
be cloned as well.
175178

176179
The following git environment variables are recognized by npm and will be added
177180
to the environment when running git:

lib/cache/add-remote-git.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ function checkoutTreeish (from, resolvedURL, resolvedTreeish, tmpdir, cb) {
346346
}
347347
log.verbose('checkoutTreeish', from, 'checkout', stdout)
348348

349+
updateSubmodules(from, resolvedURL, tmpdir, cb)
350+
}
351+
)
352+
}
353+
354+
function updateSubmodules (from, resolvedURL, tmpdir, cb) {
355+
var args = ['submodule', '-q', 'update', '--init', '--recursive']
356+
git.whichAndExec(
357+
args,
358+
{ cwd: tmpdir, env: gitEnv() },
359+
function (er, stdout, stderr) {
360+
stdout = (stdout + '\n' + stderr).trim()
361+
if (er) {
362+
log.error('git ' + args.join(' ') + ':', stderr)
363+
return cb(er)
364+
}
365+
log.verbose('updateSubmodules', from, 'submodule update', stdout)
366+
349367
// convince addLocal that the checkout is a local dependency
350368
realizePackageSpecifier(tmpdir, function (er, spec) {
351369
if (er) {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
var fs = require('fs')
2+
var resolve = require('path').resolve
3+
4+
var osenv = require('osenv')
5+
var mkdirp = require('mkdirp')
6+
var rimraf = require('rimraf')
7+
var test = require('tap').test
8+
9+
var npm = require('../../lib/npm.js')
10+
var common = require('../common-tap.js')
11+
12+
var pkg = resolve(__dirname, 'add-remote-git-submodule')
13+
var repos = resolve(__dirname, 'add-remote-git-submodule-repos')
14+
var subwt = resolve(repos, 'subwt')
15+
var topwt = resolve(repos, 'topwt')
16+
var suburl = 'git://localhost:1234/sub.git'
17+
var topurl = 'git://localhost:1234/top.git'
18+
19+
var daemon
20+
var daemonPID
21+
var git
22+
23+
var pjParent = JSON.stringify({
24+
name: 'parent',
25+
version: '1.2.3',
26+
dependencies: {
27+
child: topurl
28+
}
29+
}, null, 2) + '\n'
30+
31+
var pjChild = JSON.stringify({
32+
name: 'child',
33+
version: '1.0.3'
34+
}, null, 2) + '\n'
35+
36+
test('setup', function (t) {
37+
setup(function (er, r) {
38+
t.ifError(er, 'git started up successfully')
39+
t.end()
40+
})
41+
})
42+
43+
test('install from repo', function (t) {
44+
bootstrap(t)
45+
npm.commands.install('.', [], function (er) {
46+
t.ifError(er, 'npm installed via git')
47+
t.end()
48+
})
49+
})
50+
51+
test('has file in submodule', function (t) {
52+
bootstrap(t)
53+
npm.commands.install('.', [], function (er) {
54+
t.ifError(er, 'npm installed via git')
55+
var fooPath = resolve('node_modules', 'child', 'subpath', 'foo.txt')
56+
fs.stat(fooPath, function (er) {
57+
t.ifError(er, 'file in submodule exists')
58+
t.end()
59+
})
60+
})
61+
})
62+
63+
test('clean', function (t) {
64+
daemon.on('close', function () {
65+
cleanup()
66+
t.end()
67+
})
68+
process.kill(daemonPID)
69+
})
70+
71+
function bootstrap (t) {
72+
mkdirp.sync(pkg)
73+
process.chdir(pkg)
74+
fs.writeFileSync('package.json', pjParent)
75+
t.tearDown(function () {
76+
process.chdir(osenv.tmpdir())
77+
rimraf.sync(pkg)
78+
})
79+
}
80+
81+
function setup (cb) {
82+
mkdirp.sync(topwt)
83+
fs.writeFileSync(resolve(topwt, 'package.json'), pjChild)
84+
mkdirp.sync(subwt)
85+
fs.writeFileSync(resolve(subwt, 'foo.txt'), 'This is provided by submodule')
86+
npm.load({ registry: common.registry, loglevel: 'silent' }, function () {
87+
git = require('../../lib/utils/git.js')
88+
89+
function startDaemon (cb) {
90+
// start git server
91+
var d = git.spawn(
92+
[
93+
'daemon',
94+
'--verbose',
95+
'--listen=localhost',
96+
'--export-all',
97+
'--base-path=.',
98+
'--reuseaddr',
99+
'--port=1234'
100+
],
101+
{
102+
cwd: repos,
103+
env: process.env,
104+
stdio: ['pipe', 'pipe', 'pipe']
105+
}
106+
)
107+
d.stderr.on('data', childFinder)
108+
109+
function childFinder (c) {
110+
var cpid = c.toString().match(/^\[(\d+)\]/)
111+
if (cpid[1]) {
112+
this.removeListener('data', childFinder)
113+
daemon = d
114+
daemonPID = cpid[1]
115+
cb(null)
116+
}
117+
}
118+
}
119+
120+
var env = { PATH: process.env.PATH }
121+
var topopt = { cwd: topwt, env: env }
122+
var reposopt = { cwd: repos, env: env }
123+
common.makeGitRepo({
124+
path: subwt,
125+
added: ['foo.txt'],
126+
commands: [
127+
git.chainableExec(['clone', '--bare', subwt, 'sub.git'], reposopt),
128+
startDaemon,
129+
[common.makeGitRepo, {
130+
path: topwt,
131+
commands: [
132+
git.chainableExec(['submodule', 'add', suburl, 'subpath'], topopt),
133+
git.chainableExec(['commit', '-m', 'added submodule'], topopt),
134+
git.chainableExec(['clone', '--bare', topwt, 'top.git'], reposopt)
135+
]
136+
}]
137+
]
138+
}, cb)
139+
})
140+
}
141+
142+
function cleanup () {
143+
process.chdir(osenv.tmpdir())
144+
rimraf.sync(repos)
145+
}

0 commit comments

Comments
 (0)