summaryrefslogtreecommitdiff
path: root/deps/npm/lib/cache/add-remote-git.js
blob: 1ad925eec35faecb135166a063b4a4f46ad4931e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
var mkdir = require("mkdirp")
  , assert = require("assert")
  , git = require("../utils/git.js")
  , fs = require("graceful-fs")
  , log = require("npmlog")
  , path = require("path")
  , url = require("url")
  , chownr = require("chownr")
  , crypto = require("crypto")
  , npm = require("../npm.js")
  , rm = require("../utils/gently-rm.js")
  , inflight = require("inflight")
  , getCacheStat = require("./get-stat.js")
  , addLocal = require("./add-local.js")
  , realizePackageSpecifier = require("realize-package-specifier")
  , normalizeGitUrl = require("normalize-git-url")

var remotes = path.resolve(npm.config.get("cache"), "_git-remotes")
var templates = path.join(remotes, "_templates")

var VALID_VARIABLES = [
  "GIT_SSH",
  "GIT_SSL_NO_VERIFY",
  "GIT_PROXY_COMMAND",
  "GIT_SSL_CAINFO"
]

// 1. cacheDir = path.join(cache,'_git-remotes',sha1(u))
// 2. checkGitDir(cacheDir) ? 4. : 3. (rm cacheDir if necessary)
// 3. git clone --mirror u cacheDir
// 4. cd cacheDir && git fetch -a origin
// 5. git archive /tmp/random.tgz
// 6. addLocalTarball(/tmp/random.tgz) <gitref> --format=tar --prefix=package/
// silent flag is used if this should error quietly
module.exports = function addRemoteGit (u, silent, cb) {
  assert(typeof u === "string", "must have git URL")
  assert(typeof cb === "function", "must have callback")

  log.verbose("addRemoteGit", "u=%j silent=%j", u, silent)
  var normalized = normalizeGitUrl(u)
  log.silly("addRemoteGit", "normalized", normalized)

  var v = crypto.createHash("sha1").update(normalized.url).digest("hex").slice(0, 8)
  v = normalized.url.replace(/[^a-zA-Z0-9]+/g, "-")+"-"+v
  log.silly("addRemoteGit", "v", v)

  var p = path.join(remotes, v)
  cb = inflight(p, cb)
  if (!cb) return log.verbose("addRemoteGit", p, "already in flight; waiting")
  log.verbose("addRemoteGit", p, "not in flight; cloning")

  getGitDir(function (er) {
    if (er) return cb(er)
    checkGitDir(p, normalized.url, normalized.branch, u, silent, function (er, data) {
      if (er) return cb(er, data)

      addModeRecursive(p, npm.modes.file, function (er) {
        return cb(er, data)
      })
    })
  })
}

function getGitDir (cb) {
  getCacheStat(function (er, st) {
    if (er) return cb(er)

    // We don't need global templates when cloning. Use an empty directory for
    // the templates, creating it (and setting its permissions) if necessary.
    mkdir(templates, function (er) {
      if (er) return cb(er)

      // Ensure that both the template and remotes directories have the correct
      // permissions.
      fs.chown(templates, st.uid, st.gid, function (er) {
        if (er) return cb(er)

        fs.chown(remotes, st.uid, st.gid, function (er) {
          cb(er, st)
        })
      })
    })
  })
}

function checkGitDir (p, u, co, origUrl, silent, cb) {
  fs.stat(p, function (er, s) {
    if (er) return cloneGitRemote(p, u, co, origUrl, silent, cb)
    if (!s.isDirectory()) return rm(p, function (er) {
      if (er) return cb(er)
      cloneGitRemote(p, u, co, origUrl, silent, cb)
    })

    git.whichAndExec(
      [ "config", "--get", "remote.origin.url" ],
      { cwd : p, env : gitEnv },
      function (er, stdout, stderr) {
        var stdoutTrimmed = (stdout + "\n" + stderr).trim()
        if (er || u !== stdout.trim()) {
          log.warn( "`git config --get remote.origin.url` returned "
                  + "wrong result ("+u+")", stdoutTrimmed )
          return rm(p, function (er){
            if (er) return cb(er)
            cloneGitRemote(p, u, co, origUrl, silent, cb)
          })
        }
        log.verbose("git remote.origin.url", stdoutTrimmed)
        fetchRemote(p, u, co, origUrl, cb)
      }
    )
  })
}

function cloneGitRemote (p, u, co, origUrl, silent, cb) {
  mkdir(p, function (er) {
    if (er) return cb(er)

    git.whichAndExec(
      [ "clone", "--template=" + templates, "--mirror", u, p ],
      { cwd : p, env : gitEnv() },
      function (er, stdout, stderr) {
        stdout = (stdout + "\n" + stderr).trim()
        if (er) {
          if (silent) {
            log.verbose("git clone " + u, stdout)
          } else {
            log.error("git clone " + u, stdout)
          }
          return cb(er)
        }
        log.verbose("git clone " + u, stdout)
        fetchRemote(p, u, co, origUrl, cb)
      }
    )
  })
}

function fetchRemote (p, u, co, origUrl, cb) {
  git.whichAndExec(
    [ "fetch", "-a", "origin" ],
    { cwd : p, env : gitEnv() },
    function (er, stdout, stderr) {
      stdout = (stdout + "\n" + stderr).trim()
      if (er) {
        log.error("git fetch -a origin ("+u+")", stdout)
        return cb(er)
      }
      log.verbose("git fetch -a origin ("+u+")", stdout)

      if (process.platform === "win32") {
        log.silly("verifyOwnership", "skipping for windows")
        resolveHead(p, u, co, origUrl, cb)
      }
      else {
        getGitDir(function (er, cs) {
          if (er) {
            log.error("Could not get cache stat")
            return cb(er)
          }

          chownr(p, cs.uid, cs.gid, function (er) {
            if (er) {
              log.error("Failed to change folder ownership under npm cache for %s", p)
              return cb(er)
            }

            resolveHead(p, u, co, origUrl, cb)
          })
        })
      }
    }
  )
}

function resolveHead (p, u, co, origUrl, cb) {
  git.whichAndExec(
    [ "rev-list", "-n1", co ],
    { cwd : p, env : gitEnv() },
    function (er, stdout, stderr) {
      stdout = (stdout + "\n" + stderr).trim()
      if (er) {
        log.error("Failed resolving git HEAD (" + u + ")", stderr)
        return cb(er)
      }
      log.verbose("git rev-list -n1 " + co, stdout)
      var parsed = url.parse(origUrl)
      parsed.hash = stdout
      var resolved = url.format(parsed)

      if (parsed.protocol !== "git:") resolved = "git+" + resolved

      // https://github.com/npm/npm/issues/3224
      // node incorrectly sticks a / at the start of the path We know that the
      // host won't change, so split and detect this
      var spo = origUrl.split(parsed.host)
      var spr = resolved.split(parsed.host)
      if (spo[1].charAt(0) === ":" && spr[1].charAt(0) === "/") {
        spr[1] = spr[1].slice(1)
      }
      resolved = spr.join(parsed.host)

      log.verbose("resolved git url", resolved)
      cache(p, u, stdout, resolved, cb)
    }
  )
}

/**
 * Make an actual clone from the bare (mirrored) cache. There is no safe way to
 * do a one-step clone to a treeish that isn't guaranteed to be a branch, so
 * this has to be two steps.
 */
function cache (p, u, treeish, resolved, cb) {
  var tmp = path.join(npm.tmp, Date.now()+"-"+Math.random(), treeish)
  git.whichAndExec(
    [ "clone", p, tmp ],
    { cwd : p, env : gitEnv() },
    function (er, stdout, stderr) {
      stdout = (stdout + "\n" + stderr).trim()
      if (er) {
        log.error("Failed to clone "+resolved+" from "+u, stderr)
        return cb(er)
      }
      log.verbose("git clone", "from", p)
      log.verbose("git clone", stdout)

      git.whichAndExec(
        [ "checkout", treeish ],
        { cwd : tmp, env : gitEnv() },
        function (er, stdout, stderr) {
          stdout = (stdout + "\n" + stderr).trim()
          if (er) {
            log.error("Failed to check out "+treeish, stderr)
            return cb(er)
          }
          log.verbose("git checkout", stdout)

          realizePackageSpecifier(tmp, function (er, spec) {
            if (er) {
              log.error("Failed to map", tmp, "to a package specifier")
              return cb(er)
            }

            // https://github.com/npm/npm/issues/6400
            // ensure pack logic is applied
            addLocal(spec, null, function (er, data) {
              if (data) data._resolved = resolved
              cb(er, data)
            })
          })
        }
      )
    }
  )
}

var gitEnv_
function gitEnv () {
  // git responds to env vars in some weird ways in post-receive hooks
  // so don't carry those along.
  if (gitEnv_) return gitEnv_
  gitEnv_ = {}
  for (var k in process.env) {
    if (!~VALID_VARIABLES.indexOf(k) && k.match(/^GIT/)) continue
    gitEnv_[k] = process.env[k]
  }
  return gitEnv_
}

// similar to chmodr except it add permissions rather than overwriting them
// adapted from https://github.com/isaacs/chmodr/blob/master/chmodr.js
function addModeRecursive(p, mode, cb) {
  fs.readdir(p, function (er, children) {
    // Any error other than ENOTDIR means it's not readable, or doesn't exist.
    // Give up.
    if (er && er.code !== "ENOTDIR") return cb(er)
    if (er || !children.length) return addMode(p, mode, cb)

    var len = children.length
    var errState = null
    children.forEach(function (child) {
      addModeRecursive(path.resolve(p, child), mode, then)
    })

    function then (er) {
      if (errState) return undefined
      if (er) return cb(errState = er)
      if (--len === 0) return addMode(p, dirMode(mode), cb)
    }
  })
}

function addMode(p, mode, cb) {
  fs.stat(p, function (er, stats) {
    if (er) return cb(er)
    mode = stats.mode | mode
    fs.chmod(p, mode, cb)
  })
}

// taken from https://github.com/isaacs/chmodr/blob/master/chmodr.js
function dirMode(mode) {
  if (mode & parseInt("0400", 8)) mode |= parseInt("0100", 8)
  if (mode & parseInt( "040", 8)) mode |= parseInt( "010", 8)
  if (mode & parseInt(  "04", 8)) mode |= parseInt(  "01", 8)
  return mode
}