@vltpkg/git
A utility for spawning git from npm CLI contexts.
Overview
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.
Fork of @npmcli/git
.
Usage
API
Most methods take an options object. Options are described below.
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
@vltpkg/promise-spawn
.
Return value is a SpawnPromise
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
@vltpkg/promise-spawn
.
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:
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.
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
@vltpkg/pick-manifest
logic.
The resulting object looks like:
is(opts)
-> Promise<boolean>
Resolve to true
if the cwd
option refers to the root of a git
repository.
It does this by looking for a file or folder at ${path}/.git
,
which is not an airtight indicator, but usually pretty reliable.
git.find(opts)
-> Promise<string | undefined>
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 undefined
if not in a git project.
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 toopts.fetchRetries
or 2factor
: Defaults toopts.fetchRetryFactor
or 10maxTimeout
: Defaults toopts.fetchRetryMaxtimeout
or 60000minTimeout
: Defaults toopts.fetchRetryMintimeout
or 1000
git
Path to thegit
binary to use. Will look up the firstgit
in thePATH
if not specified.spec
The@vltpkg/spec
specifier object for the thing being fetched (if relevant).fakePlatform
set to a fake value ofprocess.platform
to use. (Just for testingwin32
behavior on Unix, and vice versa.)cwd
The current working dir for the git command. Particularly forfind
andis
andisClean
, it’s good to know that this defaults toprocess.cwd()
, as one might expect.- Any other options that can be passed to
@vltpkg/promise-spawn
, orchild_process.spawn()
.