source: trip-planner-front/node_modules/@npmcli/git/lib/clone.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[6a3a178]1// The goal here is to minimize both git workload and
2// the number of refs we download over the network.
3//
4// Every method ends up with the checked out working dir
5// at the specified ref, and resolves with the git sha.
6
7// Only certain whitelisted hosts get shallow cloning.
8// Many hosts (including GHE) don't always support it.
9// A failed shallow fetch takes a LOT longer than a full
10// fetch in most cases, so we skip it entirely.
11// Set opts.gitShallow = true/false to force this behavior
12// one way or the other.
13const shallowHosts = new Set([
14 'github.com',
15 'gist.github.com',
16 'gitlab.com',
17 'bitbucket.com',
18 'bitbucket.org'
19])
20// we have to use url.parse until we add the same shim that hosted-git-info has
21// to handle scp:// urls
22const { parse } = require('url') // eslint-disable-line node/no-deprecated-api
23const { basename, resolve } = require('path')
24
25const revs = require('./revs.js')
26const spawn = require('./spawn.js')
27const { isWindows } = require('./utils.js')
28
29const pickManifest = require('npm-pick-manifest')
30const fs = require('fs')
31const mkdirp = require('mkdirp')
32
33module.exports = (repo, ref = 'HEAD', target = null, opts = {}) =>
34 revs(repo, opts).then(revs => clone(
35 repo,
36 revs,
37 ref,
38 resolveRef(revs, ref, opts),
39 target || defaultTarget(repo, opts.cwd),
40 opts
41 ))
42
43const maybeShallow = (repo, opts) => {
44 if (opts.gitShallow === false || opts.gitShallow) {
45 return opts.gitShallow
46 }
47 return shallowHosts.has(parse(repo).host)
48}
49
50const defaultTarget = (repo, /* istanbul ignore next */ cwd = process.cwd()) =>
51 resolve(cwd, basename(repo.replace(/[/\\]?\.git$/, '')))
52
53const clone = (repo, revs, ref, revDoc, target, opts) => {
54 if (!revDoc) {
55 return unresolved(repo, ref, target, opts)
56 }
57 if (revDoc.sha === revs.refs.HEAD.sha) {
58 return plain(repo, revDoc, target, opts)
59 }
60 if (revDoc.type === 'tag' || revDoc.type === 'branch') {
61 return branch(repo, revDoc, target, opts)
62 }
63 return other(repo, revDoc, target, opts)
64}
65
66const resolveRef = (revs, ref, opts) => {
67 const { spec = {} } = opts
68 ref = spec.gitCommittish || ref
69 /* istanbul ignore next - will fail anyway, can't pull */
70 if (!revs) {
71 return null
72 }
73 if (spec.gitRange) {
74 return pickManifest(revs, spec.gitRange, opts)
75 }
76 if (!ref) {
77 return revs.refs.HEAD
78 }
79 if (revs.refs[ref]) {
80 return revs.refs[ref]
81 }
82 if (revs.shas[ref]) {
83 return revs.refs[revs.shas[ref][0]]
84 }
85 return null
86}
87
88// pull request or some other kind of advertised ref
89const other = (repo, revDoc, target, opts) => {
90 const shallow = maybeShallow(repo, opts)
91
92 const fetchOrigin = ['fetch', 'origin', revDoc.rawRef]
93 .concat(shallow ? ['--depth=1'] : [])
94
95 const git = (args) => spawn(args, { ...opts, cwd: target })
96 return mkdirp(target)
97 .then(() => git(['init']))
98 .then(() => isWindows(opts)
99 ? git(['config', '--local', '--add', 'core.longpaths', 'true'])
100 : null)
101 .then(() => git(['remote', 'add', 'origin', repo]))
102 .then(() => git(fetchOrigin))
103 .then(() => git(['checkout', revDoc.sha]))
104 .then(() => updateSubmodules(target, opts))
105 .then(() => revDoc.sha)
106}
107
108// tag or branches. use -b
109const branch = (repo, revDoc, target, opts) => {
110 const args = [
111 'clone',
112 '-b',
113 revDoc.ref,
114 repo,
115 target,
116 '--recurse-submodules'
117 ]
118 if (maybeShallow(repo, opts)) { args.push('--depth=1') }
119 if (isWindows(opts)) { args.push('--config', 'core.longpaths=true') }
120 return spawn(args, opts).then(() => revDoc.sha)
121}
122
123// just the head. clone it
124const plain = (repo, revDoc, target, opts) => {
125 const args = [
126 'clone',
127 repo,
128 target,
129 '--recurse-submodules'
130 ]
131 if (maybeShallow(repo, opts)) { args.push('--depth=1') }
132 if (isWindows(opts)) { args.push('--config', 'core.longpaths=true') }
133 return spawn(args, opts).then(() => revDoc.sha)
134}
135
136const updateSubmodules = (target, opts) => new Promise(resolve =>
137 fs.stat(target + '/.gitmodules', er => {
138 if (er) {
139 return resolve(null)
140 }
141 return resolve(spawn([
142 'submodule',
143 'update',
144 '-q',
145 '--init',
146 '--recursive'
147 ], { ...opts, cwd: target }))
148 }))
149
150const unresolved = (repo, ref, target, opts) => {
151 // can't do this one shallowly, because the ref isn't advertised
152 // but we can avoid checking out the working dir twice, at least
153 const lp = isWindows(opts) ? ['--config', 'core.longpaths=true'] : []
154 const cloneArgs = ['clone', '--mirror', '-q', repo, target + '/.git']
155 const git = (args) => spawn(args, { ...opts, cwd: target })
156 return mkdirp(target)
157 .then(() => git(cloneArgs.concat(lp)))
158 .then(() => git(['init']))
159 .then(() => git(['checkout', ref]))
160 .then(() => updateSubmodules(target, opts))
161 .then(() => git(['rev-parse', '--revs-only', 'HEAD']))
162 .then(({ stdout }) => stdout.trim())
163}
Note: See TracBrowser for help on using the repository browser.