|
| 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. |
| 13 | +const 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 |
| 22 | +const { parse } = require('url') // eslint-disable-line node/no-deprecated-api |
| 23 | +const path = require('path') |
| 24 | + |
| 25 | +const getRevs = require('./revs.js') |
| 26 | +const spawn = require('./spawn.js') |
| 27 | +const { isWindows } = require('./utils.js') |
| 28 | + |
| 29 | +const pickManifest = require('npm-pick-manifest') |
| 30 | +const fs = require('fs/promises') |
| 31 | + |
| 32 | +module.exports = (repo, ref = 'HEAD', target = null, opts = {}) => |
| 33 | + getRevs(repo, opts).then(revs => clone( |
| 34 | + repo, |
| 35 | + revs, |
| 36 | + ref, |
| 37 | + resolveRef(revs, ref, opts), |
| 38 | + target || defaultTarget(repo, opts.cwd), |
| 39 | + opts |
| 40 | + )) |
| 41 | + |
| 42 | +const maybeShallow = (repo, opts) => { |
| 43 | + if (opts.gitShallow === false || opts.gitShallow) { |
| 44 | + return opts.gitShallow |
| 45 | + } |
| 46 | + return shallowHosts.has(parse(repo).host) |
| 47 | +} |
| 48 | + |
| 49 | +const defaultTarget = (repo, /* istanbul ignore next */ cwd = process.cwd()) => |
| 50 | + path.resolve(cwd, path.basename(repo.replace(/[/\\]?\.git$/, ''))) |
| 51 | + |
| 52 | +const clone = (repo, revs, ref, revDoc, target, opts) => { |
| 53 | + if (!revDoc) { |
| 54 | + return unresolved(repo, ref, target, opts) |
| 55 | + } |
| 56 | + if (revDoc.sha === revs.refs.HEAD.sha) { |
| 57 | + return plain(repo, revDoc, target, opts) |
| 58 | + } |
| 59 | + if (revDoc.type === 'tag' || revDoc.type === 'branch') { |
| 60 | + return branch(repo, revDoc, target, opts) |
| 61 | + } |
| 62 | + return other(repo, revDoc, target, opts) |
| 63 | +} |
| 64 | + |
| 65 | +const resolveRef = (revs, ref, opts) => { |
| 66 | + const { spec = {} } = opts |
| 67 | + ref = spec.gitCommittish || ref |
| 68 | + /* istanbul ignore next - will fail anyway, can't pull */ |
| 69 | + if (!revs) { |
| 70 | + return null |
| 71 | + } |
| 72 | + if (spec.gitRange) { |
| 73 | + return pickManifest(revs, spec.gitRange, opts) |
| 74 | + } |
| 75 | + if (!ref) { |
| 76 | + return revs.refs.HEAD |
| 77 | + } |
| 78 | + if (revs.refs[ref]) { |
| 79 | + return revs.refs[ref] |
| 80 | + } |
| 81 | + if (revs.shas[ref]) { |
| 82 | + return revs.refs[revs.shas[ref][0]] |
| 83 | + } |
| 84 | + return null |
| 85 | +} |
| 86 | + |
| 87 | +// pull request or some other kind of advertised ref |
| 88 | +const other = (repo, revDoc, target, opts) => { |
| 89 | + const shallow = maybeShallow(repo, opts) |
| 90 | + |
| 91 | + const fetchOrigin = ['fetch', 'origin', revDoc.rawRef] |
| 92 | + .concat(shallow ? ['--depth=1'] : []) |
| 93 | + |
| 94 | + const git = (args) => spawn(args, { ...opts, cwd: target }) |
| 95 | + return fs.mkdir(target, { recursive: true }) |
| 96 | + .then(() => git(['init'])) |
| 97 | + .then(() => isWindows(opts) |
| 98 | + ? git(['config', '--local', '--add', 'core.longpaths', 'true']) |
| 99 | + : null) |
| 100 | + .then(() => git(['remote', 'add', 'origin', repo])) |
| 101 | + .then(() => git(fetchOrigin)) |
| 102 | + .then(() => git(['checkout', revDoc.sha])) |
| 103 | + .then(() => updateSubmodules(target, opts)) |
| 104 | + .then(() => revDoc.sha) |
| 105 | +} |
| 106 | + |
| 107 | +// tag or branches. use -b |
| 108 | +const branch = (repo, revDoc, target, opts) => { |
| 109 | + const args = [ |
| 110 | + 'clone', |
| 111 | + '-b', |
| 112 | + revDoc.ref, |
| 113 | + repo, |
| 114 | + target, |
| 115 | + '--recurse-submodules', |
| 116 | + ] |
| 117 | + if (maybeShallow(repo, opts)) { |
| 118 | + args.push('--depth=1') |
| 119 | + } |
| 120 | + if (isWindows(opts)) { |
| 121 | + args.push('--config', 'core.longpaths=true') |
| 122 | + } |
| 123 | + return spawn(args, opts).then(() => revDoc.sha) |
| 124 | +} |
| 125 | + |
| 126 | +// just the head. clone it |
| 127 | +const plain = (repo, revDoc, target, opts) => { |
| 128 | + const args = [ |
| 129 | + 'clone', |
| 130 | + repo, |
| 131 | + target, |
| 132 | + '--recurse-submodules', |
| 133 | + ] |
| 134 | + if (maybeShallow(repo, opts)) { |
| 135 | + args.push('--depth=1') |
| 136 | + } |
| 137 | + if (isWindows(opts)) { |
| 138 | + args.push('--config', 'core.longpaths=true') |
| 139 | + } |
| 140 | + return spawn(args, opts).then(() => revDoc.sha) |
| 141 | +} |
| 142 | + |
| 143 | +const updateSubmodules = async (target, opts) => { |
| 144 | + const hasSubmodules = await fs.stat(`${target}/.gitmodules`) |
| 145 | + .then(() => true) |
| 146 | + .catch(() => false) |
| 147 | + if (!hasSubmodules) { |
| 148 | + return null |
| 149 | + } |
| 150 | + return spawn([ |
| 151 | + 'submodule', |
| 152 | + 'update', |
| 153 | + '-q', |
| 154 | + '--init', |
| 155 | + '--recursive', |
| 156 | + ], { ...opts, cwd: target }) |
| 157 | +} |
| 158 | + |
| 159 | +const unresolved = (repo, ref, target, opts) => { |
| 160 | + // can't do this one shallowly, because the ref isn't advertised |
| 161 | + // but we can avoid checking out the working dir twice, at least |
| 162 | + const lp = isWindows(opts) ? ['--config', 'core.longpaths=true'] : [] |
| 163 | + const cloneArgs = ['clone', '--mirror', '-q', repo, target + '/.git'] |
| 164 | + const git = (args) => spawn(args, { ...opts, cwd: target }) |
| 165 | + return fs.mkdir(target, { recursive: true }) |
| 166 | + .then(() => git(cloneArgs.concat(lp))) |
| 167 | + .then(() => git(['init'])) |
| 168 | + .then(() => git(['checkout', ref])) |
| 169 | + .then(() => updateSubmodules(target, opts)) |
| 170 | + .then(() => git(['rev-parse', '--revs-only', 'HEAD'])) |
| 171 | + .then(({ stdout }) => stdout.trim()) |
| 172 | +} |
0 commit comments