@vltpkg/git 1.6.4 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -168,6 +168,10 @@ no value is found, it will return `undefined`.
168
168
  - `cwd` The current working dir for the git command. Particularly for
169
169
  `find` and `is` and `isClean`, it's good to know that this defaults
170
170
  to `process.cwd()`, as one might expect.
171
+ - `timeout` Milliseconds a git command gets: then it is killed, its
172
+ pipes are closed (a helper git started may still hold them, eg one
173
+ connecting to a host that doesn't answer), and it fails without a
174
+ retry.
171
175
  - Any other options that can be passed to
172
176
  [`@vltpkg/promise-spawn`](http://npm.im/@vltpkg/promise-spawn), or
173
177
  `child_process.spawn()`.
package/dist/clone.js CHANGED
@@ -4,7 +4,7 @@
4
4
  // Every method ends up with the checked out working dir
5
5
  // at the specified ref, and resolves with the git sha.
6
6
  import { gitScpURL } from '@vltpkg/git-scp-url';
7
- import { mkdir, stat } from 'node:fs/promises';
7
+ import { mkdir, rm, stat } from 'node:fs/promises';
8
8
  import { basename, resolve } from 'node:path';
9
9
  import { fileURLToPath } from 'node:url';
10
10
  import { isWindows } from "./is-windows.js";
@@ -31,8 +31,14 @@ export const clone = async (repo, ref = 'HEAD', target, opts = {}) => {
31
31
  repo = String(gitScpURL(repo) ?? repo).replace(/^git\+/, '');
32
32
  if (repo.startsWith('file://'))
33
33
  repo = fileURLToPath(repo);
34
+ target ||= defaultTarget(repo, opts.cwd);
35
+ // a commit: fetch exactly it, never the ref that is at it now
36
+ const sha = opts.spec?.gitCommittish || ref;
37
+ if (/^(?:[0-9a-f]{40}|[0-9a-f]{64})$/.test(sha)) {
38
+ return await commit(repo, sha, target, opts);
39
+ }
34
40
  const revs = await getRevs(repo, opts);
35
- return await clone_(repo, revs, ref, revs && resolveRef(revs, ref, opts), target || defaultTarget(repo, opts.cwd), opts);
41
+ return await clone_(repo, revs, ref, revs && resolveRef(revs, ref, opts), target, opts);
36
42
  };
37
43
  const maybeShallow = (repo, opts) => {
38
44
  if (opts['git-shallow'] === false || opts['git-shallow']) {
@@ -55,10 +61,29 @@ const clone_ = (repo, revs, ref, revDoc, target, opts) => {
55
61
  }
56
62
  return other(repo, revDoc, target, opts);
57
63
  };
64
+ // a commit by sha. A host that only serves advertised commits (git
65
+ // protocol v0) gets the full clone.
66
+ const commit = async (repo, sha, target, opts) => {
67
+ try {
68
+ await fetchRef(repo, sha, target, opts);
69
+ }
70
+ catch {
71
+ await rm(resolve(target, '.git'), {
72
+ recursive: true,
73
+ force: true,
74
+ });
75
+ return await unresolved(repo, sha, target, opts);
76
+ }
77
+ return await checkout(sha, target, opts);
78
+ };
58
79
  // pull request or some other kind of advertised ref
59
80
  const other = async (repo, revDoc, target, opts) => {
81
+ await fetchRef(repo, revDoc.rawRef, target, opts);
82
+ return await checkout(revDoc.sha, target, opts);
83
+ };
84
+ const fetchRef = async (repo, ref, target, opts) => {
60
85
  const shallow = maybeShallow(repo, opts);
61
- const fetchOrigin = ['fetch', 'origin', revDoc.rawRef].concat(shallow ? ['--depth=1'] : []);
86
+ const fetchOrigin = ['fetch', 'origin', ref].concat(shallow ? ['--depth=1'] : []);
62
87
  const git = (args) => spawn(args, { ...opts, cwd: target });
63
88
  await mkdir(target, { recursive: true });
64
89
  await git(['init']);
@@ -73,9 +98,11 @@ const other = async (repo, revDoc, target, opts) => {
73
98
  }
74
99
  await git(['remote', 'add', 'origin', repo]);
75
100
  await git(fetchOrigin);
76
- await git(['checkout', revDoc.sha]);
101
+ };
102
+ const checkout = async (sha, target, opts) => {
103
+ await spawn(['checkout', sha], { ...opts, cwd: target });
77
104
  await updateSubmodules(target, opts);
78
- return revDoc.sha;
105
+ return sha;
79
106
  };
80
107
  // tag or branches. use -b
81
108
  const branch = async (repo, revDoc, target, opts) => {
package/dist/index.d.ts CHANGED
@@ -46,7 +46,8 @@ export type GitOptions = PickManifestOptions & SpawnOptions & {
46
46
  */
47
47
  fakePlatform?: NodeJS.Platform;
48
48
  /**
49
- * Just to test rev lookup without continually clearing the cache
49
+ * Look the refs up afresh, and throw if that fails (the cached
50
+ * lookup resolves `undefined`). The result is still cached.
50
51
  */
51
52
  noGitRevCache?: boolean;
52
53
  };
package/dist/spawn.d.ts CHANGED
@@ -1,3 +1,5 @@
1
1
  import type { SpawnResultStderrString, SpawnResultStdoutString } from '@vltpkg/promise-spawn';
2
2
  import type { GitOptions } from './index.ts';
3
- export declare const spawn: (gitArgs: string[], opts?: GitOptions) => Promise<SpawnResultStderrString & SpawnResultStdoutString>;
3
+ type Result = SpawnResultStderrString & SpawnResultStdoutString;
4
+ export declare const spawn: (gitArgs: string[], opts?: GitOptions) => Promise<Result>;
5
+ export {};
package/dist/spawn.js CHANGED
@@ -1,8 +1,36 @@
1
+ import { error } from '@vltpkg/error-cause';
1
2
  import { promiseSpawn } from '@vltpkg/promise-spawn';
2
3
  import promiseRetry from 'promise-retry';
3
4
  import { makeError } from "./make-error.js";
4
5
  import { opts as makeOpts } from "./opts.js";
5
6
  import { which } from "./which.js";
7
+ // git, given `timeout` ms. node's own `timeout` kills git, but a helper
8
+ // git started (eg `git-remote-https` stuck connecting) keeps the pipes
9
+ // open, and the result waits for that helper to give up (minutes): so
10
+ // its pipes are closed too, and the helper ends on its own
11
+ const run = async (gitPath, args, opts, timeout) => {
12
+ const p = promiseSpawn(gitPath, args, makeOpts(opts));
13
+ if (!timeout)
14
+ return await p;
15
+ // set by the timer, read once git is gone
16
+ let timedOut = false;
17
+ const timer = setTimeout(() => {
18
+ timedOut = true;
19
+ p.process.kill();
20
+ p.process.stdout.destroy();
21
+ p.process.stderr.destroy();
22
+ }, timeout);
23
+ try {
24
+ const result = await p;
25
+ if (timedOut) {
26
+ throw error(`git did not finish within ${timeout}ms`, result);
27
+ }
28
+ return result;
29
+ }
30
+ finally {
31
+ clearTimeout(timer);
32
+ }
33
+ };
6
34
  export const spawn = async (gitArgs, opts = {}) => {
7
35
  const gitPath = which(opts);
8
36
  if (gitPath instanceof Error) {
@@ -20,8 +48,10 @@ export const spawn = async (gitArgs, opts = {}) => {
20
48
  maxTimeout: opts['fetch-retry-maxtimeout'] || 60000,
21
49
  minTimeout: opts['fetch-retry-mintimeout'] || 1000,
22
50
  };
51
+ // a run that timed out is not retried
52
+ const { timeout, ...spawnOpts } = opts;
23
53
  return promiseRetry(async (retryFn, num) => {
24
- const result = await promiseSpawn(gitPath, args, makeOpts(opts));
54
+ const result = await run(gitPath, args, spawnOpts, timeout);
25
55
  if (result.status || result.signal) {
26
56
  const [shouldRetry, gitError] = makeError(result);
27
57
  if (!shouldRetry?.(num)) {
package/dist/user.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { spawn } from "./spawn.js";
2
2
  export const getUser = async (opts = {}) => {
3
- let name = '';
4
- let email = '';
3
+ let name;
4
+ let email;
5
5
  // retrieve user.name
6
6
  const oldFlagUserNameResult = await spawn(['config', '--get', 'user.name'], opts);
7
7
  if (oldFlagUserNameResult.status || oldFlagUserNameResult.signal) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/git",
3
3
  "description": "a util for spawning git from npm CLI contexts",
4
- "version": "1.6.4",
4
+ "version": "1.8.0",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -14,28 +14,28 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "@types/promise-retry": "^1.1.6",
17
- "@vltpkg/error-cause": "1.6.4",
18
- "@vltpkg/git-scp-url": "1.6.4",
19
- "@vltpkg/pick-manifest": "1.6.4",
20
- "@vltpkg/promise-spawn": "1.6.4",
21
- "@vltpkg/semver": "1.6.4",
22
- "@vltpkg/spec": "1.6.4",
23
- "@vltpkg/which": "1.6.4",
17
+ "@vltpkg/error-cause": "1.8.0",
18
+ "@vltpkg/git-scp-url": "1.8.0",
19
+ "@vltpkg/pick-manifest": "1.8.0",
20
+ "@vltpkg/promise-spawn": "1.8.0",
21
+ "@vltpkg/semver": "1.8.0",
22
+ "@vltpkg/spec": "1.8.0",
23
+ "@vltpkg/which": "1.8.0",
24
24
  "lru-cache": "^11.2.4",
25
25
  "promise-retry": "^2.0.1",
26
26
  "retry": "^0.13.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@eslint/js": "^9.39.1",
29
+ "@eslint/js": "^10.0.1",
30
30
  "@types/node": "^22.19.2",
31
31
  "@types/retry": "^0.12.5",
32
- "@vltpkg/types": "1.6.4",
33
- "eslint": "^9.39.1",
32
+ "@vltpkg/types": "1.8.0",
33
+ "eslint": "^10.14.0",
34
34
  "prettier": "^3.7.4",
35
35
  "tap": "^21.8.0",
36
36
  "typedoc": "~0.27.9",
37
37
  "typescript": "5.7.3",
38
- "typescript-eslint": "^8.49.0"
38
+ "typescript-eslint": "^8.56.0"
39
39
  },
40
40
  "license": "ISC",
41
41
  "engines": {