包详细信息

@npmcli/git

npm35.7mISC6.0.3

a util for spawning git from npm CLI contexts

自述文件

@npmcli/git

A utility for spawning git from npm CLI contexts.

This is not an implementation of git itself, it's just a thing that spawns child processes to tell the system git CLI implementation to do stuff.

USAGE

const git = require('@npmcli/git')
git.clone('git://foo/bar.git', 'some-branch', 'some-path', opts) // clone a repo
  .then(() => git.spawn(['checkout', 'some-branch'], {cwd: 'bar'}))
  .then(() => git.spawn(['you get the idea']))

API

Most methods take an options object. Options are described below.

git.spawn(args, opts = {})

Launch a git subprocess with the arguments specified.

All the other functions call this one at some point.

Processes are launched using @npmcli/promise-spawn, with the stdioString: true option enabled by default, since git output is generally in readable string format.

Return value is a Promise that resolves to a result object with {cmd, args, code, signal, stdout, stderr} members, or rejects with an error with the same fields, passed back from @npmcli/promise-spawn.

git.clone(repo, ref = 'HEAD', target = null, opts = {}) -> Promise<sha String>

Clone the repository into target path (or the default path for the name of the repository), checking out ref.

Return value is the sha of the current HEAD in the locally cloned repository.

In lieu of a specific ref, you may also pass in a spec option, which is a npm-package-arg object for a git package dependency reference. In this way, you can select SemVer tags within a range, or any git committish value. For example:

const npa = require('npm-package-arg')
git.clone('git@github.com:npm/git.git', '', null, {
  spec: npa('github:npm/git#semver:1.x'),
})

// only gitRange and gitCommittish are relevant, so this works, too
git.clone('git@github.com:npm/git.git', null, null, {
  spec: { gitRange: '1.x' }
})

This will automatically do a shallow --depth=1 clone on any hosts that are known to support it. To force a shallow or deep clone, you can set the gitShallow option to true or false respectively.

git.revs(repo, opts = {}) -> Promise<rev doc Object>

Fetch a representation of all of the named references in a given repository. The resulting doc is intentionally somewhat packument-like, so that git semver ranges can be applied using the same npm-pick-manifest logic.

The resulting object looks like:

revs = {
  versions: {
    // all semver-looking tags go in here...
    // version: { sha, ref, rawRef, type }
    '1.0.0': {
      sha: '1bc5fba3353f8e1b56493b266bc459276ab23139',
      ref: 'v1.0.0',
      rawRef: 'refs/tags/v1.0.0',
      type: 'tag',
    },
  },
  'dist-tags': {
    HEAD: '1.0.0',
    latest: '1.0.0',
  },
  refs: {
    // all the advertised refs that can be cloned down remotely
    HEAD: { sha, ref, rawRef, type: 'head' },
    master: { ... },
    'v1.0.0': { ... },
    'refs/tags/v1.0.0': { ... },
  },
  shas: {
    // all named shas referenced above
    // sha: [list, of, refs]
    '6b2501f9183a1753027a9bf89a184b7d3d4602c7': [
      'HEAD',
      'master',
      'refs/heads/master',
    ],
    '1bc5fba3353f8e1b56493b266bc459276ab23139': [ 'v1.0.0', 'refs/tags/v1.0.0' ],
  },
}

git.is(opts) -> Promise<Boolean>

Resolve to true if the path argument refers to the root of a git repository.

It does this by looking for a file in ${path}/.git/index, which is not an airtight indicator, but at least avoids being fooled by an empty directory or a file named .git.

git.find(opts) -> Promise<String | null>

Given a path, walk up the file system tree until a git repo working directory is found. Since this calls stat a bunch of times, it's probably best to only call it if you're reasonably sure you're likely to be in a git project somewhere. Pass in opts.root to stop checking at that directory.

Resolves to null if not in a git project.

git.isClean(opts = {}) -> Promise<Boolean>

Return true if in a git dir, and that git dir is free of changes. This will resolve true if the git working dir is clean, or false if not, and reject if the path is not within a git directory or some other error occurs.

OPTIONS

  • retry An object to configure retry behavior for transient network errors with exponential backoff.
    • retries: Defaults to opts.fetchRetries or 2
    • factor: Defaults to opts.fetchRetryFactor or 10
    • maxTimeout: Defaults to opts.fetchRetryMaxtimeout or 60000
    • minTimeout: Defaults to opts.fetchRetryMintimeout or 1000
  • git Path to the git binary to use. Will look up the first git in the PATH if not specified.
  • spec The npm-package-arg specifier object for the thing being fetched (if relevant).
  • fakePlatform set to a fake value of process.platform to use. (Just for testing win32 behavior on Unix, and vice versa.)
  • cwd The current working dir for the git command. Particularly for find and is and isClean, it's good to know that this defaults to process.cwd(), as one might expect.
  • Any other options that can be passed to @npmcli/promise-spawn, or child_process.spawn().

更新日志

Changelog

6.0.3 (2025-02-18)

Dependencies

6.0.2 (2025-02-18)

Dependencies

  • 618c70c #223 remove promise-inflight (#223)

    Chores

  • 29dfb33 #222 ensure tests correctly mock environment variables before loading opts.js (#222) (@owlstronaut)
  • 0fc4208 #220 bump npm-package-arg from 11.0.3 to 12.0.1 (#220) (@dependabot[bot])
  • 52d8b0e #221 bump @npmcli/template-oss from 4.23.3 to 4.24.1 (#221) (@dependabot[bot], @npm-cli-bot)

6.0.1 (2024-10-02)

Dependencies

6.0.0 (2024-09-26)

⚠️ BREAKING CHANGES

  • @npmcli/git now supports node ^18.17.0 || >=20.5.0

    Bug Fixes

  • a405d65 #211 align to npm 10 node engine range (@reggi)

    Dependencies

  • 68c31f0 #211 proc-log@5.0.0
  • 917a535 #211 ini@5.0.0
  • e192bea #211 @npmcli/promise-spawn@8.0.0

    Chores

  • e989fa1 #211 run template-oss-apply (@reggi)
  • dbfdd90 #205 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])
  • 4d33150 #206 postinstall for dependabot template-oss PR (@hashtagchris)
  • 8c699c1 #206 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])

5.0.8 (2024-07-09)

Bug Fixes

  • 2135513 #194 smarter git ssh override (#194) (@dennishenry, pacotedev)

5.0.7 (2024-05-04)

Bug Fixes

Chores

  • 3a54aa9 #186 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)
  • ea26e3a #186 postinstall for dependabot template-oss PR (@lukekarrys)
  • b7ab170 #184 bump @npmcli/template-oss from 4.21.3 to 4.21.4 (@dependabot[bot])

5.0.6 (2024-04-12)

Dependencies

5.0.5 (2024-04-08)

Bug Fixes

  • d9bf355 #180 use fs/promises instead of util.promisify (#180) (@lukekarrys)

5.0.4 (2023-12-19)

Bug Fixes

  • c398872 #176 lazy load of which module (#176) (@thecodrr)

Chores

  • 46192f5 #175 postinstall for dependabot template-oss PR (@lukekarrys)
  • 51011f8 #175 bump @npmcli/template-oss from 4.21.1 to 4.21.3 (@dependabot[bot])
  • 8b1d897 #173 postinstall for dependabot template-oss PR (@lukekarrys)
  • 157ed48 #173 bump @npmcli/template-oss from 4.19.0 to 4.21.1 (@dependabot[bot])
  • 0d06a9f #154 postinstall for dependabot template-oss PR (@lukekarrys)
  • b425d28 #154 bump @npmcli/template-oss from 4.18.1 to 4.19.0 (@dependabot[bot])
  • ecc938a #152 postinstall for dependabot template-oss PR (@lukekarrys)
  • 72430ad #152 bump @npmcli/template-oss from 4.18.0 to 4.18.1 (@dependabot[bot])

5.0.3 (2023-08-30)

Dependencies

  • 5d5a6ed #150 bump @npmcli/promise-spawn from 6.0.2 to 7.0.0

5.0.2 (2023-08-30)

Dependencies

5.0.1 (2023-08-15)

Dependencies

  • 25fb1d1 #146 bump npm-pick-manifest from 8.0.2 to 9.0.0

5.0.0 (2023-08-14)

⚠️ BREAKING CHANGES

  • support for node 14 has been removed

Bug Fixes

Dependencies

4.1.0 (2023-06-06)

Features

  • f73ed8d #135 add option to stop git.find at a directory (#135) (@lukekarrys)

Bug Fixes

  • d5a99e3 #137 dont default find root option to anything (@lukekarrys)

4.0.4 (2023-03-08)

Dependencies

4.0.3 (2022-11-01)

Dependencies

  • 1d9ca2a #105 bump @npmcli/promise-spawn from 5.0.0 to 6.0.0 (#105)
  • 1103697 #104 bump which from 2.0.2 to 3.0.0 (#104)

4.0.2 (2022-10-26)

Dependencies

  • 96c168e #100 bump @npmcli/promise-spawn from 4.0.0 to 5.0.0 (#100)

4.0.1 (2022-10-17)

Dependencies

  • 4eff9fe #94 bump @npmcli/promise-spawn from 3.0.0 to 4.0.0 (#94)
  • 496d1cb #96 bump npm-pick-manifest from 7.0.2 to 8.0.0 (#96)

4.0.0 (2022-10-14)

⚠️ BREAKING CHANGES

  • @npmcli/git is now compatible with the following semver range for node: ^14.17.0 || ^16.13.0 || >=18.0.0

Features

  • 84d1268 #85 postinstall for dependabot template-oss PR (@lukekarrys)

Dependencies

3.0.2 (2022-08-15)

Bug Fixes

3.0.1 (2022-04-05)

Bug Fixes

  • replace deprecated String.prototype.substr() (#68) (b146f20)

Dependencies

  • bump @npmcli/promise-spawn from 1.3.2 to 3.0.0 (#69) (d8f4488)
  • update lru-cache requirement from ^7.3.1 to ^7.4.0 (#50) (3beb9a2)
  • update lru-cache requirement from ^7.4.0 to ^7.4.1 (#55) (17c1e8a)
  • update lru-cache requirement from ^7.4.1 to ^7.4.2 (#56) (eeb67f4)
  • update lru-cache requirement from ^7.4.2 to ^7.4.4 (#57) (24dff2f)

3.0.0 (2022-02-16)

⚠ BREAKING CHANGES

  • this drops support for the log property and all logs are now emitted on the process object via proc-log.
  • this drops support for node10 and non-LTS versions of node12 and node14.

Features

  • use proc-log instead of process.emit and drop support for log property (#41) (70fc3c4)

Dependencies

  • @npmcli/template-oss@2.7.1 (#39) (2f4154a)
  • bump lru-cache from 6.0.0 to 7.3.1 (#45) (ca9ccde)
  • bump npm-pick-manifest from 6.1.1 to 7.0.0 (#46) (294da31)